@@ -17,8 +17,10 @@ limitations under the License.
17
17
package envtest
18
18
19
19
import (
20
+ "fmt"
20
21
"os"
21
22
"path/filepath"
23
+ "time"
22
24
23
25
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
24
26
"k8s.io/client-go/rest"
@@ -32,6 +34,8 @@ const (
32
34
envEtcdBin = "TEST_ASSET_ETCD"
33
35
envKubectlBin = "TEST_ASSET_KUBECTL"
34
36
envKubebuilderPath = "KUBEBUILDER_ASSETS"
37
+ envStartTimeout = "KUBEBUILDER_CONTROLPLANE_START_TIMEOUT"
38
+ envStopTimeout = "KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT"
35
39
defaultKubebuilderPath = "/usr/local/kubebuilder/bin"
36
40
StartTimeout = 60
37
41
StopTimeout = 60
@@ -76,6 +80,16 @@ type Environment struct {
76
80
// existing kubeconfig, instead of trying to stand up a new control plane.
77
81
// This is useful in cases that need aggregated API servers and the like.
78
82
UseExistingCluster bool
83
+
84
+ // ControlPlaneStartTimeout is the the maximum duration each controlplane component
85
+ // may take to start. It defaults to the KUBEBUILDER_CONTROLPLANE_START_TIMEOUT
86
+ // environment variable or 20 seconds if unspecified
87
+ ControlPlaneStartTimeout time.Duration
88
+
89
+ // ControlPlaneStopTimeout is the the maximum duration each controlplane component
90
+ // may take to stop. It defaults to the KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT
91
+ // environment variable or 20 seconds if unspecified
92
+ ControlPlaneStopTimeout time.Duration
79
93
}
80
94
81
95
// Stop stops a running server
@@ -102,6 +116,7 @@ func (te *Environment) Start() (*rest.Config, error) {
102
116
} else {
103
117
te .ControlPlane = integration.ControlPlane {}
104
118
te .ControlPlane .APIServer = & integration.APIServer {Args : defaultKubeAPIServerFlags }
119
+
105
120
if os .Getenv (envKubeAPIServerBin ) == "" {
106
121
te .ControlPlane .APIServer .Path = defaultAssetPath ("kube-apiserver" )
107
122
}
@@ -115,6 +130,14 @@ func (te *Environment) Start() (*rest.Config, error) {
115
130
}
116
131
}
117
132
133
+ if err := te .defaultTimeouts (); err != nil {
134
+ return nil , fmt .Errorf ("failed to default controlplane timeouts: %v" , err )
135
+ }
136
+ te .ControlPlane .Etcd .StartTimeout = te .ControlPlaneStartTimeout
137
+ te .ControlPlane .Etcd .StopTimeout = te .ControlPlaneStopTimeout
138
+ te .ControlPlane .APIServer .StartTimeout = te .ControlPlaneStartTimeout
139
+ te .ControlPlane .APIServer .StopTimeout = te .ControlPlaneStopTimeout
140
+
118
141
// Start the control plane - retry if it fails
119
142
if err := te .ControlPlane .Start (); err != nil {
120
143
return nil , err
@@ -132,3 +155,25 @@ func (te *Environment) Start() (*rest.Config, error) {
132
155
})
133
156
return te .Config , err
134
157
}
158
+
159
+ func (te * Environment ) defaultTimeouts () error {
160
+ var err error
161
+ if te .ControlPlaneStartTimeout == 0 {
162
+ if envVal := os .Getenv (envStartTimeout ); envVal != "" {
163
+ te .ControlPlaneStartTimeout , err = time .ParseDuration (envVal )
164
+ if err != nil {
165
+ return err
166
+ }
167
+ }
168
+ }
169
+
170
+ if te .ControlPlaneStopTimeout == 0 {
171
+ if envVal := os .Getenv (envStopTimeout ); envVal != "" {
172
+ te .ControlPlaneStopTimeout , err = time .ParseDuration (envVal )
173
+ if err != nil {
174
+ return err
175
+ }
176
+ }
177
+ }
178
+ return nil
179
+ }
0 commit comments