Skip to content

Commit f2e2c75

Browse files
committed
Ensure testing ControlPlane is restartable
This makes sure that you can start a ControlPlane after stopping it. This preserves existing functionality. Note that the ability to re-use users or keep data around is *not* preserved -- use a fixed CertDir if you want this.
1 parent 0abb95a commit f2e2c75

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

pkg/internal/testing/controlplane/apiserver.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,8 @@ func (s *APIServer) Start() error {
165165
}
166166

167167
func (s *APIServer) prepare() error {
168-
if s.processState == nil {
169-
if err := s.setProcessState(); err != nil {
170-
return err
171-
}
168+
if err := s.setProcessState(); err != nil {
169+
return err
172170
}
173171
if err := s.Authn.Start(); err != nil {
174172
return err
@@ -222,6 +220,10 @@ func (s *APIServer) setProcessState() error {
222220

223221
var err error
224222

223+
// unconditionally re-set this so we can successfully restart
224+
// TODO(directxman12): we supported this in the past, but do we actually
225+
// want to support re-using an API server object to restart? The loss
226+
// of provisioned users is surprising to say the least.
225227
s.processState = &process.State{
226228
Dir: s.CertDir,
227229
Path: s.Path,
@@ -400,6 +402,9 @@ func (s *APIServer) populateAPIServerCerts() error {
400402
// Stop stops this process gracefully, waits for its termination, and cleans up
401403
// the CertDir if necessary.
402404
func (s *APIServer) Stop() error {
405+
if s.processState.DirNeedsCleaning {
406+
s.CertDir = "" // reset the directory if it was randomly allocated, so that we can safely restart
407+
}
403408
if s.processState != nil {
404409
if err := s.processState.Stop(); err != nil {
405410
return err

pkg/internal/testing/controlplane/etcd.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ type Etcd struct {
8989
// Start starts the etcd, waits for it to come up, and returns an error, if one
9090
// occoured.
9191
func (e *Etcd) Start() error {
92-
if e.processState == nil {
93-
if err := e.setProcessState(); err != nil {
94-
return err
95-
}
92+
if err := e.setProcessState(); err != nil {
93+
return err
9694
}
9795
return e.processState.Start(e.Out, e.Err)
9896
}
@@ -105,6 +103,10 @@ func (e *Etcd) setProcessState() error {
105103
StopTimeout: e.StopTimeout,
106104
}
107105

106+
// unconditionally re-set this so we can successfully restart
107+
// TODO(directxman12): we supported this in the past, but do we actually
108+
// want to support re-using an API server object to restart? The loss
109+
// of provisioned users is surprising to say the least.
108110
if err := e.processState.Init("etcd"); err != nil {
109111
return err
110112
}
@@ -140,6 +142,9 @@ func (e *Etcd) setProcessState() error {
140142
// Stop stops this process gracefully, waits for its termination, and cleans up
141143
// the DataDir if necessary.
142144
func (e *Etcd) Stop() error {
145+
if e.processState.DirNeedsCleaning {
146+
e.DataDir = "" // reset the directory if it was randomly allocated, so that we can safely restart
147+
}
143148
return e.processState.Stop()
144149
}
145150

pkg/internal/testing/controlplane/plane_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ var _ = Describe("Control Plane", func() {
4747
Expect(plane.Etcd).To(BeIdenticalTo(etcd))
4848
})
4949

50+
It("should be able to restart", func() {
51+
// NB(directxman12): currently restarting invalidates all current users
52+
// when using CertAuthn. We need to support restarting as per our previous
53+
// contract, but it's not clear how much else we actually need to handle, or
54+
// whether or not this is a safe operation.
55+
plane := &ControlPlane{}
56+
Expect(plane.Start()).To(Succeed())
57+
Expect(plane.Stop()).To(Succeed())
58+
Expect(plane.Start()).To(Succeed())
59+
Expect(plane.Stop()).To(Succeed())
60+
})
61+
5062
Context("after having started", func() {
5163
var plane *ControlPlane
5264
BeforeEach(func() {

0 commit comments

Comments
 (0)