Skip to content

Commit 2a4d5bb

Browse files
authored
Merge pull request #169 from alvaroaleman/configurable-control-plane-start-timeout
Make start and stop timeouts for test controlplane configurable
2 parents b6fdbe1 + 88bfd77 commit 2a4d5bb

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

pkg/envtest/server.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ limitations under the License.
1717
package envtest
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"path/filepath"
23+
"time"
2224

2325
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
2426
"k8s.io/client-go/rest"
@@ -32,6 +34,8 @@ const (
3234
envEtcdBin = "TEST_ASSET_ETCD"
3335
envKubectlBin = "TEST_ASSET_KUBECTL"
3436
envKubebuilderPath = "KUBEBUILDER_ASSETS"
37+
envStartTimeout = "KUBEBUILDER_CONTROLPLANE_START_TIMEOUT"
38+
envStopTimeout = "KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT"
3539
defaultKubebuilderPath = "/usr/local/kubebuilder/bin"
3640
StartTimeout = 60
3741
StopTimeout = 60
@@ -76,6 +80,16 @@ type Environment struct {
7680
// existing kubeconfig, instead of trying to stand up a new control plane.
7781
// This is useful in cases that need aggregated API servers and the like.
7882
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
7993
}
8094

8195
// Stop stops a running server
@@ -102,6 +116,7 @@ func (te *Environment) Start() (*rest.Config, error) {
102116
} else {
103117
te.ControlPlane = integration.ControlPlane{}
104118
te.ControlPlane.APIServer = &integration.APIServer{Args: defaultKubeAPIServerFlags}
119+
105120
if os.Getenv(envKubeAPIServerBin) == "" {
106121
te.ControlPlane.APIServer.Path = defaultAssetPath("kube-apiserver")
107122
}
@@ -115,6 +130,14 @@ func (te *Environment) Start() (*rest.Config, error) {
115130
}
116131
}
117132

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+
118141
// Start the control plane - retry if it fails
119142
if err := te.ControlPlane.Start(); err != nil {
120143
return nil, err
@@ -132,3 +155,25 @@ func (te *Environment) Start() (*rest.Config, error) {
132155
})
133156
return te.Config, err
134157
}
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

Comments
 (0)