Skip to content

Commit 715b932

Browse files
ybelMekksindrerh2Reasonable-Solutions
committed
feat(debug): use same proomts as for migrate
* metrics to be able to sort by version we remove the commit hash Co-authored-by: Sindre Rødseth Hansen <[email protected]> Co-authored-by: Carl Hedgren <[email protected]>
1 parent 1e264b7 commit 715b932

File tree

6 files changed

+35
-66
lines changed

6 files changed

+35
-66
lines changed

.goreleaser.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ builds:
1818
binary: nais
1919
ldflags:
2020
- -s -w -X github.com/nais/cli/cmd.version={{.Version}} -X github.com/nais/cli/cmd.commit={{.Commit}}
21-
- -s -w -X github.com/nais/cli/pkg/metrics.version={{.Version}} -X github.com/nais/cli/pkg/metrics.commit={{.Commit}}
21+
- -s -w -X github.com/nais/cli/pkg/metrics.version={{.Version}}
2222
- env: [ CGO_ENABLED=0 ]
2323
targets:
2424
- windows_amd64
@@ -28,7 +28,7 @@ builds:
2828
binary: nais
2929
ldflags:
3030
- -s -w -X github.com/nais/cli/cmd.version={{.Version}} -X github.com/nais/cli/cmd.commit={{.Commit}}
31-
- -s -w -X github.com/nais/cli/pkg/metrics.version={{.Version}} -X github.com/nais/cli/pkg/metrics.commit={{.Commit}}
31+
- -s -w -X github.com/nais/cli/pkg/metrics.version={{.Version}}
3232
hooks:
3333
post:
3434
- osslsigncode sign -certs nais.crt -key nais.key -n "nais-cli" -i "https://doc.nais.io/cli" -verbose -in "{{ .Path }}" -out "{{ .Path }}-signed"

go.mod

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/go-logr/logr v1.4.2
99
github.com/goccy/go-yaml v1.13.2
1010
github.com/mailgun/raymond/v2 v2.0.48
11-
github.com/manifoldco/promptui v0.9.0
1211
github.com/mitchellh/go-ps v1.0.0
1312
github.com/nais/device v0.0.0-20231030073017-240e8a0234d9
1413
github.com/nais/liberator v0.0.0-20241101125801-cc4bc469b422
@@ -39,7 +38,6 @@ require (
3938

4039
require (
4140
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
42-
github.com/chzyer/readline v1.5.1 // indirect
4341
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
4442
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
4543
github.com/klauspost/cpuid/v2 v2.2.7 // indirect

go.sum

-13
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyY
3939
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
4040
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
4141
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
42-
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
43-
github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM=
44-
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
45-
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
46-
github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=
47-
github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk=
48-
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
49-
github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04=
50-
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
5142
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
5243
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
5344
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
@@ -216,8 +207,6 @@ github.com/mailgun/raymond/v2 v2.0.48 h1:5dmlB680ZkFG2RN/0lvTAghrSxIESeu9/2aeDqA
216207
github.com/mailgun/raymond/v2 v2.0.48/go.mod h1:lsgvL50kgt1ylcFJYZiULi5fjPBkkhNfj4KA0W54Z18=
217208
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
218209
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
219-
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
220-
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
221210
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
222211
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
223212
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
@@ -391,7 +380,6 @@ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
391380
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
392381
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
393382
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
394-
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
395383
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
396384
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
397385
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -401,7 +389,6 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
401389
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
402390
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
403391
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
404-
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
405392
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
406393
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
407394
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

pkg/debug/debug.go

+17-20
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import (
77
"os/exec"
88
"time"
99

10+
"github.com/pterm/pterm"
1011
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1112

12-
"github.com/manifoldco/promptui"
13-
1413
core_v1 "k8s.io/api/core/v1"
1514
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1615
"k8s.io/client-go/kubernetes"
@@ -45,6 +44,7 @@ func Setup(client kubernetes.Interface, cfg *Config) *Debug {
4544
}
4645

4746
func (d *Debug) getPodsForWorkload() (*core_v1.PodList, error) {
47+
pterm.Info.Println("Fetching workload...")
4848
var podList *core_v1.PodList
4949
var err error
5050
podList, err = d.client.CoreV1().Pods(d.cfg.Namespace).List(d.ctx, metav1.ListOptions{
@@ -73,18 +73,19 @@ func (d *Debug) debugPod(podName string) error {
7373
pN := debuggerContainerName(podName)
7474
_, err := d.client.CoreV1().Pods(d.cfg.Namespace).Get(d.ctx, pN, metav1.GetOptions{})
7575
if err == nil {
76-
fmt.Printf("%s already exists, trying to attach...\n", pN)
76+
pterm.Info.Printf("%s already exists, trying to attach...\n", pN)
7777

7878
// Polling loop to check if the debugger container is running
7979
for i := 0; i < maxRetries; i++ {
80-
fmt.Printf("attempt %d/%d: Time remaining: %d seconds\n", i+1, maxRetries, (maxRetries-i)*pollInterval)
80+
pterm.Info.Printf("Attempt %d/%d: Time remaining: %d seconds\n", i+1, maxRetries, (maxRetries-i)*pollInterval)
8181
pod, err := d.client.CoreV1().Pods(d.cfg.Namespace).Get(d.ctx, pN, metav1.GetOptions{})
8282
if err != nil {
8383
return fmt.Errorf("failed to get debug pod copy %s: %v", pN, err)
8484
}
8585

8686
for _, c := range pod.Status.ContainerStatuses {
8787
if c.Name == debuggerContainerDefaultName && c.State.Running != nil {
88+
pterm.Success.Println("Container is running. Attaching...")
8889
return d.attachToExistingDebugContainer(pN)
8990
}
9091
}
@@ -103,8 +104,8 @@ func (d *Debug) debugPod(podName string) error {
103104
}
104105

105106
if len(pod.Spec.EphemeralContainers) > 0 {
106-
fmt.Printf("the container %s already has %d terminated debug containers.\n", podName, len(pod.Spec.EphemeralContainers))
107-
fmt.Printf("please consider using 'nais debug tidy %s' to clean up\n", d.cfg.WorkloadName)
107+
pterm.Warning.Printf("The container %s already has %d terminated debug containers.\n", podName, len(pod.Spec.EphemeralContainers))
108+
pterm.Info.Printf("Please consider using 'nais debug tidy %s' to clean up\n", d.cfg.WorkloadName)
108109
}
109110
}
110111

@@ -130,7 +131,7 @@ func (d *Debug) attachToExistingDebugContainer(podName string) error {
130131
if err := cmd.Start(); err != nil {
131132
return fmt.Errorf("failed to start attach command: %v", err)
132133
}
133-
fmt.Printf("attached to pod %s\n", podName)
134+
pterm.Success.Printf("Attached to pod %s\n", podName)
134135

135136
if err := cmd.Wait(); err != nil {
136137
return fmt.Errorf("attach command failed: %v", err)
@@ -170,18 +171,18 @@ func (d *Debug) createDebugPod(podName string) error {
170171
}
171172

172173
if d.cfg.CopyPod {
173-
fmt.Printf("debugging pod copy created, enable process namespace sharing in %s\n", debuggerContainerName(podName))
174+
pterm.Info.Printf("Debugging pod copy created, enable process namespace sharing in %s\n", debuggerContainerName(podName))
174175
} else {
175-
fmt.Printf("debugging container created...\n")
176+
pterm.Info.Println("Debugging container created...")
176177
}
177-
fmt.Printf("using debugger image %s\n", d.cfg.DebugImage)
178+
pterm.Info.Printf("Using debugger image %s\n", d.cfg.DebugImage)
178179

179180
if err := cmd.Wait(); err != nil {
180181
return fmt.Errorf("debug command failed: %v", err)
181182
}
182183

183184
if d.cfg.CopyPod {
184-
fmt.Printf("Run 'nais debug -cp %s' command to attach to the debug pod\n", d.cfg.WorkloadName)
185+
pterm.Info.Printf("Run 'nais debug -cp %s' command to attach to the debug pod\n", d.cfg.WorkloadName)
185186
}
186187

187188
return nil
@@ -199,26 +200,22 @@ func (d *Debug) Debug() error {
199200
}
200201

201202
if len(podNames) == 0 {
202-
fmt.Println("No pods found.")
203+
pterm.Info.Println("No pods found.")
203204
return nil
204205
}
205206

206207
podName := podNames[0]
207208
if d.cfg.ByPod {
208-
prompt := promptui.Select{
209-
Label: "Select pod to Debug",
210-
Items: podNames,
211-
}
212-
213-
_, podName, err = prompt.Run()
209+
result, err := pterm.DefaultInteractiveSelect.WithOptions(podNames).Show()
214210
if err != nil {
215-
fmt.Printf("prompt failed %v\n", err)
211+
pterm.Error.Printf("Prompt failed: %v\n", err)
216212
return err
217213
}
214+
podName = result
218215
}
219216

220217
if err := d.debugPod(podName); err != nil {
221-
fmt.Printf("failed to debug pod %s: %v\n", podName, err)
218+
pterm.Error.Printf("Failed to debug pod %s: %v\n", podName, err)
222219
}
223220

224221
return nil

pkg/debug/tidy.go

+15-27
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package debug
22

33
import (
4-
"errors"
54
"fmt"
6-
"strings"
75

86
k8serrors "k8s.io/apimachinery/pkg/api/errors"
97

10-
"github.com/manifoldco/promptui"
8+
"github.com/pterm/pterm"
119
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1210
)
1311

@@ -23,7 +21,7 @@ func (d *Debug) Tidy() error {
2321
}
2422

2523
if len(podNames) == 0 {
26-
fmt.Println("No pods found")
24+
pterm.Info.Println("No pods found")
2725
return nil
2826
}
2927

@@ -34,44 +32,34 @@ func (d *Debug) Tidy() error {
3432
}
3533

3634
if !d.cfg.CopyPod && len(pod.Spec.EphemeralContainers) == 0 {
37-
fmt.Printf("no debug container found for: %s\n", pod.Name)
35+
pterm.Info.Printf("No debug container found for: %s\n", pod.Name)
3836
continue
3937
}
4038

4139
_, err := d.client.CoreV1().Pods(d.cfg.Namespace).Get(d.ctx, podName, metav1.GetOptions{})
4240
if err != nil {
4341
if k8serrors.IsNotFound(err) {
44-
fmt.Printf("no debug pod found for: %s\n", pod.Name)
42+
pterm.Info.Printf("No debug pod found for: %s\n", pod.Name)
4543
continue
4644
}
47-
fmt.Printf("failed to get pod %s: %v\n", podName, err)
45+
pterm.Error.Printf("Failed to get pod %s: %v\n", podName, err)
4846
return err
4947
}
5048

51-
prompt := promptui.Prompt{
52-
Label: fmt.Sprintf("Pod '%s' with debug container, do you want to clean up", podName),
53-
IsConfirm: true,
54-
}
49+
confirm, _ := pterm.DefaultInteractiveConfirm.
50+
WithDefaultText(fmt.Sprintf("Pod '%s' with debug container, do you want to clean up?", podName)).
51+
Show()
5552

56-
answer, err := prompt.Run()
57-
if err != nil {
58-
if errors.Is(err, promptui.ErrAbort) {
59-
fmt.Printf("skipping deletion for pod: %s\n", podName)
60-
continue
61-
}
62-
fmt.Printf("error reading input for pod %s: %v\n", podName, err)
63-
return err
53+
if !confirm {
54+
pterm.Info.Printf("Skipping deletion for pod: %s\n", podName)
55+
continue
6456
}
6557

66-
// Delete pod if user confirms with "y" or "yes"
67-
if strings.ToLower(answer) == "y" || strings.ToLower(answer) == "yes" {
68-
if err := d.client.CoreV1().Pods(d.cfg.Namespace).Delete(d.ctx, podName, metav1.DeleteOptions{}); err != nil {
69-
fmt.Printf("Failed to delete pod %s: %v\n", podName, err)
70-
} else {
71-
fmt.Println("Deleted pod:", podName)
72-
}
58+
// Delete pod if user confirms
59+
if err := d.client.CoreV1().Pods(d.cfg.Namespace).Delete(d.ctx, podName, metav1.DeleteOptions{}); err != nil {
60+
pterm.Error.Printf("Failed to delete pod %s: %v\n", podName, err)
7361
} else {
74-
fmt.Println("Skipped pod:", podName)
62+
pterm.Success.Printf("Deleted pod: %s\n", podName)
7563
}
7664
}
7765
return nil

pkg/metrics/otel.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
var (
2323
version = "local"
24-
commit = "uncommited"
2524
naisCliPrefixName = "nais_cli"
2625
collectorURL = "https://collector-internet.nav.cloud.nais.io"
2726
)
@@ -30,7 +29,7 @@ func newResource() (*resource.Resource, error) {
3029
return resource.Merge(resource.Default(),
3130
resource.NewWithAttributes(semconv.SchemaURL,
3231
semconv.ServiceName("nais_cli"),
33-
semconv.ServiceVersion(version+"-"+commit),
32+
semconv.ServiceVersion(version),
3433
))
3534
}
3635

0 commit comments

Comments
 (0)