From 50a34110d05a62a1f47a130b4a83602b843ad8a6 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 | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/internal/testing/controlplane/plane.go b/pkg/internal/testing/controlplane/plane.go index 36fd3c6306..19fdb8ac86 100644 --- a/pkg/internal/testing/controlplane/plane.go +++ b/pkg/internal/testing/controlplane/plane.go @@ -47,13 +47,18 @@ type ControlPlane struct { } // Start will start your control plane processes. To stop them, call Stop(). -func (f *ControlPlane) Start() error { +func (f *ControlPlane) Start() (retErr error) { if f.Etcd == nil { f.Etcd = &Etcd{} } if err := f.Etcd.Start(); err != nil { return err } + defer func() { + if retErr != nil { + _ = f.Etcd.Stop() + } + }() if f.APIServer == nil { f.APIServer = &APIServer{} @@ -62,6 +67,11 @@ func (f *ControlPlane) Start() error { if err := f.APIServer.Start(); err != nil { return err } + defer func() { + if retErr != nil { + _ = f.APIServer.Stop() + } + }() // provision the default user -- can be removed when the related // methods are removed. The default user has admin permissions to