@@ -7,10 +7,9 @@ import (
7
7
"os/exec"
8
8
"time"
9
9
10
+ "github.com/pterm/pterm"
10
11
k8serrors "k8s.io/apimachinery/pkg/api/errors"
11
12
12
- "github.com/manifoldco/promptui"
13
-
14
13
core_v1 "k8s.io/api/core/v1"
15
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
15
"k8s.io/client-go/kubernetes"
@@ -45,6 +44,7 @@ func Setup(client kubernetes.Interface, cfg *Config) *Debug {
45
44
}
46
45
47
46
func (d * Debug ) getPodsForWorkload () (* core_v1.PodList , error ) {
47
+ pterm .Info .Println ("Fetching workload..." )
48
48
var podList * core_v1.PodList
49
49
var err error
50
50
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 {
73
73
pN := debuggerContainerName (podName )
74
74
_ , err := d .client .CoreV1 ().Pods (d .cfg .Namespace ).Get (d .ctx , pN , metav1.GetOptions {})
75
75
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 )
77
77
78
78
// Polling loop to check if the debugger container is running
79
79
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 )
81
81
pod , err := d .client .CoreV1 ().Pods (d .cfg .Namespace ).Get (d .ctx , pN , metav1.GetOptions {})
82
82
if err != nil {
83
83
return fmt .Errorf ("failed to get debug pod copy %s: %v" , pN , err )
84
84
}
85
85
86
86
for _ , c := range pod .Status .ContainerStatuses {
87
87
if c .Name == debuggerContainerDefaultName && c .State .Running != nil {
88
+ pterm .Success .Println ("Container is running. Attaching..." )
88
89
return d .attachToExistingDebugContainer (pN )
89
90
}
90
91
}
@@ -103,8 +104,8 @@ func (d *Debug) debugPod(podName string) error {
103
104
}
104
105
105
106
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 )
108
109
}
109
110
}
110
111
@@ -130,7 +131,7 @@ func (d *Debug) attachToExistingDebugContainer(podName string) error {
130
131
if err := cmd .Start (); err != nil {
131
132
return fmt .Errorf ("failed to start attach command: %v" , err )
132
133
}
133
- fmt . Printf ("attached to pod %s\n " , podName )
134
+ pterm . Success . Printf ("Attached to pod %s\n " , podName )
134
135
135
136
if err := cmd .Wait (); err != nil {
136
137
return fmt .Errorf ("attach command failed: %v" , err )
@@ -170,18 +171,18 @@ func (d *Debug) createDebugPod(podName string) error {
170
171
}
171
172
172
173
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 ))
174
175
} else {
175
- fmt . Printf ( "debugging container created...\n " )
176
+ pterm . Info . Println ( "Debugging container created..." )
176
177
}
177
- fmt . Printf ("using debugger image %s\n " , d .cfg .DebugImage )
178
+ pterm . Info . Printf ("Using debugger image %s\n " , d .cfg .DebugImage )
178
179
179
180
if err := cmd .Wait (); err != nil {
180
181
return fmt .Errorf ("debug command failed: %v" , err )
181
182
}
182
183
183
184
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 )
185
186
}
186
187
187
188
return nil
@@ -199,26 +200,22 @@ func (d *Debug) Debug() error {
199
200
}
200
201
201
202
if len (podNames ) == 0 {
202
- fmt .Println ("No pods found." )
203
+ pterm . Info .Println ("No pods found." )
203
204
return nil
204
205
}
205
206
206
207
podName := podNames [0 ]
207
208
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 ()
214
210
if err != nil {
215
- fmt . Printf ("prompt failed %v\n " , err )
211
+ pterm . Error . Printf ("Prompt failed: %v\n " , err )
216
212
return err
217
213
}
214
+ podName = result
218
215
}
219
216
220
217
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 )
222
219
}
223
220
224
221
return nil
0 commit comments