@@ -14,35 +14,46 @@ import (
1414)
1515
1616type Kubectl struct {
17- exec exec.ProcessExecutor
17+ exec exec.ProcessExecutor
18+ timeout time.Duration
1819}
1920
20- func NewKubectl (exec exec.ProcessExecutor ) Kubectl {
21+ func NewKubectl (exec exec.ProcessExecutor , timeout time. Duration ) Kubectl {
2122 return Kubectl {
22- exec : exec ,
23+ exec : exec ,
24+ timeout : timeout ,
2325 }
2426}
2527
2628// CreateNamespace creates a new namespace with the given name.
2729func (k Kubectl ) CreateNamespace (namespace string ) error {
2830 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 )
3034}
3135
3236// DeleteNamespace deletes the specified namespace. If the namespace does not terminate within 120s, pods running in the
3337// namespace and, eventually, the namespace itself are force-deleted.
3438func (k Kubectl ) DeleteNamespace (namespace string ) {
3539 fmt .Printf ("Deleting namespace '%s'...\n " , namespace )
3640 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 {
3845 fmt .Printf ("Namespace '%s' did not terminate after %s.\n " , namespace , timeoutSec )
3946 }
4047
4148 if k .getNamespace (namespace ) {
4249 fmt .Printf ("Namespace '%s' did not terminate after %s.\n " , namespace , timeoutSec )
4350
4451 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 {
4657 fmt .Printf ("Error deleting everything in the namespace %v: %v" , namespace , err )
4758 }
4859
@@ -59,7 +70,9 @@ func (k Kubectl) DeleteNamespace(namespace string) {
5970
6071func (k Kubectl ) forceNamespaceDeletion (namespace string ) error {
6172 // 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" )
6376 if err != nil {
6477 fmt .Println ("Error getting namespace json:" , err )
6578 return err
@@ -111,13 +124,20 @@ func (k Kubectl) forceNamespaceDeletion(namespace string) error {
111124 time .Sleep (5 * time .Second )
112125
113126 // 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 {
115131 fmt .Printf ("Namespace '%s' terminated.\n " , namespace )
116132 return nil
117133 }
118134
119135 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 {
121141 fmt .Println ("Error deleting namespace:" , err )
122142 return err
123143 }
@@ -126,16 +146,20 @@ func (k Kubectl) forceNamespaceDeletion(namespace string) error {
126146}
127147
128148func (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}" )
131153 if err != nil {
132154 return err
133155 }
134156
135157 deployments := strings .Fields (output )
136158 for _ , deployment := range deployments {
137159 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 )
139163 if err != nil {
140164 return err
141165 }
@@ -145,7 +169,9 @@ func (k Kubectl) WaitForDeployments(namespace string, selector string) error {
145169 //
146170 // Just after rollout, pods from the previous deployment revision may still be in a
147171 // 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" ,
149175 `jsonpath={.status.unavailableReplicas}` )
150176 if err != nil {
151177 return err
@@ -159,7 +185,9 @@ func (k Kubectl) WaitForDeployments(namespace string, selector string) error {
159185}
160186
161187func (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" )
163191 var deploymentMap map [string ]interface {}
164192 err := json .Unmarshal ([]byte (jsonString ), & deploymentMap )
165193 if err != nil {
@@ -183,23 +211,30 @@ func (k Kubectl) GetPodsforDeployment(namespace string, deployment string) ([]st
183211func (k Kubectl ) GetPods (args ... string ) ([]string , error ) {
184212 kubectlArgs := []string {"get" , "pods" }
185213 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 )
187216 if err != nil {
188217 return nil , err
189218 }
190219 return strings .Fields (pods ), nil
191220}
192221
193222func (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 )
195226}
196227
197228func (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 )
199232}
200233
201234func (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 )
203238}
204239
205240func (k Kubectl ) GetInitContainers (namespace string , pod string ) ([]string , error ) {
@@ -211,7 +246,10 @@ func (k Kubectl) GetContainers(namespace string, pod string) ([]string, error) {
211246}
212247
213248func (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 {
215253 fmt .Printf ("Namespace '%s' terminated.\n " , namespace )
216254 return false
217255 }
0 commit comments