From 3a1a3dede9ffdc440f691252e79bf4bfea38e6d8 Mon Sep 17 00:00:00 2001 From: Saggi Mizrahi Date: Tue, 14 Dec 2021 13:33:23 +0000 Subject: [PATCH] Cleanup if envtest controlplane fails to start Currently if the api server fails to start etcd process will be up the next time ControlPlane.Start() is called, they will both have the same listening address which will make the new etcd instance fail to start. The controlplane object will also be in an incomplete state so calling ControlPlane.Close() will panic. --- pkg/internal/testing/controlplane/plane.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/internal/testing/controlplane/plane.go b/pkg/internal/testing/controlplane/plane.go index 36fd3c6306..733ea11b54 100644 --- a/pkg/internal/testing/controlplane/plane.go +++ b/pkg/internal/testing/controlplane/plane.go @@ -48,6 +48,7 @@ type ControlPlane struct { // Start will start your control plane processes. To stop them, call Stop(). func (f *ControlPlane) Start() error { + success := false if f.Etcd == nil { f.Etcd = &Etcd{} } @@ -55,6 +56,12 @@ func (f *ControlPlane) Start() error { return err } + defer func() { + if !success { + f.Etcd.Stop() + } + }() + if f.APIServer == nil { f.APIServer = &APIServer{} } @@ -63,6 +70,12 @@ func (f *ControlPlane) Start() error { return err } + defer func() { + if !success { + f.APIServer.Stop() + } + }() + // provision the default user -- can be removed when the related // methods are removed. The default user has admin permissions to // mimic legacy no-authz setups. @@ -76,6 +89,8 @@ func (f *ControlPlane) Start() error { } f.defaultUserCfg = user.Config() f.defaultUserKubectl = kubectl + + success = true return nil }