5
5
"fmt"
6
6
"os"
7
7
"os/exec"
8
+ "time"
8
9
9
10
k8serrors "k8s.io/apimachinery/pkg/api/errors"
10
11
@@ -16,7 +17,8 @@ import (
16
17
)
17
18
18
19
const (
19
- debuggerSuffix = "nais-debugger"
20
+ debuggerSuffix = "nais-debugger"
21
+ debuggerContainerDefaultName = "debugger"
20
22
)
21
23
22
24
type Debug struct {
@@ -64,13 +66,33 @@ func debuggerContainerName(podName string) string {
64
66
}
65
67
66
68
func (d * Debug ) debugPod (podName string ) error {
69
+ const maxRetries = 6
70
+ const pollInterval = 5
71
+
67
72
if d .cfg .CopyPod {
68
73
pN := debuggerContainerName (podName )
69
74
_ , err := d .client .CoreV1 ().Pods (d .cfg .Namespace ).Get (d .ctx , pN , metav1.GetOptions {})
70
75
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" )
74
96
} else if ! k8serrors .IsNotFound (err ) {
75
97
return fmt .Errorf ("failed to check for existing debug pod copy %s: %v" , pN , err )
76
98
}
@@ -81,22 +103,21 @@ func (d *Debug) debugPod(podName string) error {
81
103
}
82
104
83
105
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 )
86
108
}
87
109
}
88
110
89
111
return d .createDebugPod (podName )
90
112
}
91
113
92
114
func (d * Debug ) attachToExistingDebugContainer (podName string ) error {
93
- defaultDebuggerName := "debugger"
94
115
cmd := exec .Command (
95
116
"kubectl" ,
96
117
"attach" ,
97
118
"-n" , d .cfg .Namespace ,
98
119
fmt .Sprintf ("pod/%s" , podName ),
99
- "-c" , defaultDebuggerName ,
120
+ "-c" , debuggerContainerDefaultName ,
100
121
"-i" ,
101
122
"-t" ,
102
123
"--context" , d .cfg .Context ,
@@ -109,7 +130,7 @@ func (d *Debug) attachToExistingDebugContainer(podName string) error {
109
130
if err := cmd .Start (); err != nil {
110
131
return fmt .Errorf ("failed to start attach command: %v" , err )
111
132
}
112
- fmt .Printf ("Attaching to existing debug container %s in pod %s\n " , defaultDebuggerName , podName )
133
+ fmt .Printf ("attached to pod %s\n " , podName )
113
134
114
135
if err := cmd .Wait (); err != nil {
115
136
return fmt .Errorf ("attach command failed: %v" , err )
@@ -149,18 +170,18 @@ func (d *Debug) createDebugPod(podName string) error {
149
170
}
150
171
151
172
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 ))
153
174
} else {
154
- fmt .Printf ("Debugging container created...\n " )
175
+ fmt .Printf ("debugging container created...\n " )
155
176
}
156
- fmt .Printf ("Using debugger image %s\n " , d .cfg .DebugImage )
177
+ fmt .Printf ("using debugger image %s\n " , d .cfg .DebugImage )
157
178
158
179
if err := cmd .Wait (); err != nil {
159
180
return fmt .Errorf ("debug command failed: %v" , err )
160
181
}
161
182
162
183
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 )
164
185
}
165
186
166
187
return nil
0 commit comments