Skip to content

Commit 1e264b7

Browse files
ybelMekksindrerh2Reasonable-Solutions
committed
chore(debug): clean up code
* add retry for attach to copy container debugger container not yet started * fix output Co-authored-by: Sindre Rødseth Hansen <[email protected]> Co-authored-by: Carl Hedgren <[email protected]>
1 parent 851b8a0 commit 1e264b7

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

pkg/debug/debug.go

+34-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"os/exec"
8+
"time"
89

910
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1011

@@ -16,7 +17,8 @@ import (
1617
)
1718

1819
const (
19-
debuggerSuffix = "nais-debugger"
20+
debuggerSuffix = "nais-debugger"
21+
debuggerContainerDefaultName = "debugger"
2022
)
2123

2224
type Debug struct {
@@ -64,13 +66,33 @@ func debuggerContainerName(podName string) string {
6466
}
6567

6668
func (d *Debug) debugPod(podName string) error {
69+
const maxRetries = 6
70+
const pollInterval = 5
71+
6772
if d.cfg.CopyPod {
6873
pN := debuggerContainerName(podName)
6974
_, err := d.client.CoreV1().Pods(d.cfg.Namespace).Get(d.ctx, pN, metav1.GetOptions{})
7075
if err == nil {
71-
fmt.Printf("Debug pod copy %s already exists. Attaching...\n", pN)
72-
// Debug pod copy already exists, attach to it
73-
return d.attachToExistingDebugContainer(pN)
76+
fmt.Printf("%s already exists, trying to attach...\n", pN)
77+
78+
// Polling loop to check if the debugger container is running
79+
for i := 0; i < maxRetries; i++ {
80+
fmt.Printf("attempt %d/%d: Time remaining: %d seconds\n", i+1, maxRetries, (maxRetries-i)*pollInterval)
81+
pod, err := d.client.CoreV1().Pods(d.cfg.Namespace).Get(d.ctx, pN, metav1.GetOptions{})
82+
if err != nil {
83+
return fmt.Errorf("failed to get debug pod copy %s: %v", pN, err)
84+
}
85+
86+
for _, c := range pod.Status.ContainerStatuses {
87+
if c.Name == debuggerContainerDefaultName && c.State.Running != nil {
88+
return d.attachToExistingDebugContainer(pN)
89+
}
90+
}
91+
time.Sleep(time.Duration(pollInterval) * time.Second)
92+
}
93+
94+
// If the loop finishes without finding the running container
95+
return fmt.Errorf("container did not start within the expected time")
7496
} else if !k8serrors.IsNotFound(err) {
7597
return fmt.Errorf("failed to check for existing debug pod copy %s: %v", pN, err)
7698
}
@@ -81,22 +103,21 @@ func (d *Debug) debugPod(podName string) error {
81103
}
82104

83105
if len(pod.Spec.EphemeralContainers) > 0 {
84-
fmt.Printf("The container %s already has %d terminated debug containers. \n", podName, len(pod.Spec.EphemeralContainers))
85-
fmt.Printf("Please consider using 'nais debug tidy %s' to clean up\n", d.cfg.WorkloadName)
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)
86108
}
87109
}
88110

89111
return d.createDebugPod(podName)
90112
}
91113

92114
func (d *Debug) attachToExistingDebugContainer(podName string) error {
93-
defaultDebuggerName := "debugger"
94115
cmd := exec.Command(
95116
"kubectl",
96117
"attach",
97118
"-n", d.cfg.Namespace,
98119
fmt.Sprintf("pod/%s", podName),
99-
"-c", defaultDebuggerName,
120+
"-c", debuggerContainerDefaultName,
100121
"-i",
101122
"-t",
102123
"--context", d.cfg.Context,
@@ -109,7 +130,7 @@ func (d *Debug) attachToExistingDebugContainer(podName string) error {
109130
if err := cmd.Start(); err != nil {
110131
return fmt.Errorf("failed to start attach command: %v", err)
111132
}
112-
fmt.Printf("Attaching to existing debug container %s in pod %s\n", defaultDebuggerName, podName)
133+
fmt.Printf("attached to pod %s\n", podName)
113134

114135
if err := cmd.Wait(); err != nil {
115136
return fmt.Errorf("attach command failed: %v", err)
@@ -149,18 +170,18 @@ func (d *Debug) createDebugPod(podName string) error {
149170
}
150171

151172
if d.cfg.CopyPod {
152-
fmt.Printf("Debugging pod copy created, enable process namespace sharing in %s\n", debuggerContainerName(podName))
173+
fmt.Printf("debugging pod copy created, enable process namespace sharing in %s\n", debuggerContainerName(podName))
153174
} else {
154-
fmt.Printf("Debugging container created...\n")
175+
fmt.Printf("debugging container created...\n")
155176
}
156-
fmt.Printf("Using debugger image %s\n", d.cfg.DebugImage)
177+
fmt.Printf("using debugger image %s\n", d.cfg.DebugImage)
157178

158179
if err := cmd.Wait(); err != nil {
159180
return fmt.Errorf("debug command failed: %v", err)
160181
}
161182

162183
if d.cfg.CopyPod {
163-
fmt.Printf("Run 'nais debug -cp %s' command to attach to the debug pod\n", podName)
184+
fmt.Printf("Run 'nais debug -cp %s' command to attach to the debug pod\n", d.cfg.WorkloadName)
164185
}
165186

166187
return nil

pkg/debug/tidy.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ func (d *Debug) Tidy() error {
3434
}
3535

3636
if !d.cfg.CopyPod && len(pod.Spec.EphemeralContainers) == 0 {
37-
fmt.Printf("No debug container found for: %s\n", pod.Name)
37+
fmt.Printf("no debug container found for: %s\n", pod.Name)
3838
continue
3939
}
4040

4141
_, err := d.client.CoreV1().Pods(d.cfg.Namespace).Get(d.ctx, podName, metav1.GetOptions{})
4242
if err != nil {
4343
if k8serrors.IsNotFound(err) {
44-
fmt.Printf("No debug pod found for: %s\n", pod.Name)
44+
fmt.Printf("no debug pod found for: %s\n", pod.Name)
4545
continue
4646
}
47-
fmt.Printf("Failed to get pod %s: %v\n", podName, err)
47+
fmt.Printf("failed to get pod %s: %v\n", podName, err)
4848
return err
4949
}
5050

@@ -56,10 +56,10 @@ func (d *Debug) Tidy() error {
5656
answer, err := prompt.Run()
5757
if err != nil {
5858
if errors.Is(err, promptui.ErrAbort) {
59-
fmt.Printf("Skipping deletion for pod: %s\n", podName)
59+
fmt.Printf("skipping deletion for pod: %s\n", podName)
6060
continue
6161
}
62-
fmt.Printf("Error reading input for pod %s: %v\n", podName, err)
62+
fmt.Printf("error reading input for pod %s: %v\n", podName, err)
6363
return err
6464
}
6565

0 commit comments

Comments
 (0)