@@ -14,35 +14,46 @@ import (
14
14
)
15
15
16
16
type Kubectl struct {
17
- exec exec.ProcessExecutor
17
+ exec exec.ProcessExecutor
18
+ timeout time.Duration
18
19
}
19
20
20
- func NewKubectl (exec exec.ProcessExecutor ) Kubectl {
21
+ func NewKubectl (exec exec.ProcessExecutor , timeout time. Duration ) Kubectl {
21
22
return Kubectl {
22
- exec : exec ,
23
+ exec : exec ,
24
+ timeout : timeout ,
23
25
}
24
26
}
25
27
26
28
// CreateNamespace creates a new namespace with the given name.
27
29
func (k Kubectl ) CreateNamespace (namespace string ) error {
28
30
fmt .Printf ("Creating namespace '%s'...\n " , namespace )
29
- return k .exec .RunProcess ("kubectl" , "create" , "namespace" , namespace )
31
+ return k .exec .RunProcess ("kubectl" ,
32
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
33
+ "create" , "namespace" , namespace )
30
34
}
31
35
32
36
// DeleteNamespace deletes the specified namespace. If the namespace does not terminate within 120s, pods running in the
33
37
// namespace and, eventually, the namespace itself are force-deleted.
34
38
func (k Kubectl ) DeleteNamespace (namespace string ) {
35
39
fmt .Printf ("Deleting namespace '%s'...\n " , namespace )
36
40
timeoutSec := "180s"
37
- if err := k .exec .RunProcess ("kubectl" , "delete" , "namespace" , namespace , "--timeout" , timeoutSec ); err != nil {
41
+ err := k .exec .RunProcess ("kubectl" ,
42
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
43
+ "delete" , "namespace" , namespace , "--timeout" , timeoutSec )
44
+ if err != nil {
38
45
fmt .Printf ("Namespace '%s' did not terminate after %s.\n " , namespace , timeoutSec )
39
46
}
40
47
41
48
if k .getNamespace (namespace ) {
42
49
fmt .Printf ("Namespace '%s' did not terminate after %s.\n " , namespace , timeoutSec )
43
50
44
51
fmt .Println ("Force-deleting everything..." )
45
- if err := k .exec .RunProcess ("kubectl" , "delete" , "all" , "--namespace" , namespace , "--all" , "--force" , "--grace-period=0" ); err != nil {
52
+ err = k .exec .RunProcess ("kubectl" ,
53
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
54
+ "delete" , "all" , "--namespace" , namespace , "--all" , "--force" ,
55
+ "--grace-period=0" )
56
+ if err != nil {
46
57
fmt .Printf ("Error deleting everything in the namespace %v: %v" , namespace , err )
47
58
}
48
59
@@ -59,7 +70,9 @@ func (k Kubectl) DeleteNamespace(namespace string) {
59
70
60
71
func (k Kubectl ) forceNamespaceDeletion (namespace string ) error {
61
72
// Getting the namespace json to remove the finalizer
62
- cmdOutput , err := k .exec .RunProcessAndCaptureOutput ("kubectl" , "get" , "namespace" , namespace , "--output=json" )
73
+ cmdOutput , err := k .exec .RunProcessAndCaptureOutput ("kubectl" ,
74
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
75
+ "get" , "namespace" , namespace , "--output=json" )
63
76
if err != nil {
64
77
fmt .Println ("Error getting namespace json:" , err )
65
78
return err
@@ -111,13 +124,20 @@ func (k Kubectl) forceNamespaceDeletion(namespace string) error {
111
124
time .Sleep (5 * time .Second )
112
125
113
126
// Check again
114
- if _ , err := k .exec .RunProcessAndCaptureOutput ("kubectl" , "get" , "namespace" , namespace ); err != nil {
127
+ _ , err = k .exec .RunProcessAndCaptureOutput ("kubectl" ,
128
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
129
+ "get" , "namespace" , namespace )
130
+ if err != nil {
115
131
fmt .Printf ("Namespace '%s' terminated.\n " , namespace )
116
132
return nil
117
133
}
118
134
119
135
fmt .Printf ("Force-deleting namespace '%s'...\n " , namespace )
120
- if err := k .exec .RunProcess ("kubectl" , "delete" , "namespace" , namespace , "--force" , "--grace-period=0" , "--ignore-not-found=true" ); err != nil {
136
+ err = k .exec .RunProcess ("kubectl" ,
137
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
138
+ "delete" , "namespace" , namespace , "--force" , "--grace-period=0" ,
139
+ "--ignore-not-found=true" )
140
+ if err != nil {
121
141
fmt .Println ("Error deleting namespace:" , err )
122
142
return err
123
143
}
@@ -126,16 +146,20 @@ func (k Kubectl) forceNamespaceDeletion(namespace string) error {
126
146
}
127
147
128
148
func (k Kubectl ) WaitForDeployments (namespace string , selector string ) error {
129
- output , err := k .exec .RunProcessAndCaptureOutput (
130
- "kubectl" , "get" , "deployments" , "--namespace" , namespace , "--selector" , selector , "--output" , "jsonpath={.items[*].metadata.name}" )
149
+ output , err := k .exec .RunProcessAndCaptureOutput ("kubectl" ,
150
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
151
+ "get" , "deployments" , "--namespace" , namespace , "--selector" , selector ,
152
+ "--output" , "jsonpath={.items[*].metadata.name}" )
131
153
if err != nil {
132
154
return err
133
155
}
134
156
135
157
deployments := strings .Fields (output )
136
158
for _ , deployment := range deployments {
137
159
deployment = strings .Trim (deployment , "'" )
138
- err := k .exec .RunProcess ("kubectl" , "rollout" , "status" , "deployment" , deployment , "--namespace" , namespace )
160
+ err = k .exec .RunProcess ("kubectl" ,
161
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
162
+ "rollout" , "status" , "deployment" , deployment , "--namespace" , namespace )
139
163
if err != nil {
140
164
return err
141
165
}
@@ -145,7 +169,9 @@ func (k Kubectl) WaitForDeployments(namespace string, selector string) error {
145
169
//
146
170
// Just after rollout, pods from the previous deployment revision may still be in a
147
171
// terminating state.
148
- unavailable , err := k .exec .RunProcessAndCaptureOutput ("kubectl" , "get" , "deployment" , deployment , "--namespace" , namespace , "--output" ,
172
+ unavailable , err := k .exec .RunProcessAndCaptureOutput ("kubectl" ,
173
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
174
+ "get" , "deployment" , deployment , "--namespace" , namespace , "--output" ,
149
175
`jsonpath={.status.unavailableReplicas}` )
150
176
if err != nil {
151
177
return err
@@ -159,7 +185,9 @@ func (k Kubectl) WaitForDeployments(namespace string, selector string) error {
159
185
}
160
186
161
187
func (k Kubectl ) GetPodsforDeployment (namespace string , deployment string ) ([]string , error ) {
162
- jsonString , _ := k .exec .RunProcessAndCaptureOutput ("kubectl" , "get" , "deployment" , deployment , "--namespace" , namespace , "--output=json" )
188
+ jsonString , _ := k .exec .RunProcessAndCaptureOutput ("kubectl" ,
189
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
190
+ "get" , "deployment" , deployment , "--namespace" , namespace , "--output=json" )
163
191
var deploymentMap map [string ]interface {}
164
192
err := json .Unmarshal ([]byte (jsonString ), & deploymentMap )
165
193
if err != nil {
@@ -183,23 +211,30 @@ func (k Kubectl) GetPodsforDeployment(namespace string, deployment string) ([]st
183
211
func (k Kubectl ) GetPods (args ... string ) ([]string , error ) {
184
212
kubectlArgs := []string {"get" , "pods" }
185
213
kubectlArgs = append (kubectlArgs , args ... )
186
- pods , err := k .exec .RunProcessAndCaptureOutput ("kubectl" , kubectlArgs )
214
+ pods , err := k .exec .RunProcessAndCaptureOutput ("kubectl" ,
215
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ), kubectlArgs )
187
216
if err != nil {
188
217
return nil , err
189
218
}
190
219
return strings .Fields (pods ), nil
191
220
}
192
221
193
222
func (k Kubectl ) GetEvents (namespace string ) error {
194
- return k .exec .RunProcess ("kubectl" , "get" , "events" , "--output" , "wide" , "--namespace" , namespace )
223
+ return k .exec .RunProcess ("kubectl" ,
224
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
225
+ "get" , "events" , "--output" , "wide" , "--namespace" , namespace )
195
226
}
196
227
197
228
func (k Kubectl ) DescribePod (namespace string , pod string ) error {
198
- return k .exec .RunProcess ("kubectl" , "describe" , "pod" , pod , "--namespace" , namespace )
229
+ return k .exec .RunProcess ("kubectl" ,
230
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
231
+ "describe" , "pod" , pod , "--namespace" , namespace )
199
232
}
200
233
201
234
func (k Kubectl ) Logs (namespace string , pod string , container string ) error {
202
- return k .exec .RunProcess ("kubectl" , "logs" , pod , "--namespace" , namespace , "--container" , container )
235
+ return k .exec .RunProcess ("kubectl" ,
236
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
237
+ "logs" , pod , "--namespace" , namespace , "--container" , container )
203
238
}
204
239
205
240
func (k Kubectl ) GetInitContainers (namespace string , pod string ) ([]string , error ) {
@@ -211,7 +246,10 @@ func (k Kubectl) GetContainers(namespace string, pod string) ([]string, error) {
211
246
}
212
247
213
248
func (k Kubectl ) getNamespace (namespace string ) bool {
214
- if _ , err := k .exec .RunProcessAndCaptureOutput ("kubectl" , "get" , "namespace" , namespace ); err != nil {
249
+ _ , err := k .exec .RunProcessAndCaptureOutput ("kubectl" ,
250
+ fmt .Sprintf ("--request-timeout=%s" , k .timeout ),
251
+ "get" , "namespace" , namespace )
252
+ if err != nil {
215
253
fmt .Printf ("Namespace '%s' terminated.\n " , namespace )
216
254
return false
217
255
}
0 commit comments