Skip to content

Commit d4c8079

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 8238b6f commit d4c8079

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
@@ -151,10 +151,8 @@ func (s *APIServer) Start() error {
151151
}
152152

153153
func (s *APIServer) prepare() error {
154-
if s.processState == nil {
155-
if err := s.setProcessState(); err != nil {
156-
return err
157-
}
154+
if err := s.setProcessState(); err != nil {
155+
return err
158156
}
159157
if err := s.Authn.Start(); err != nil {
160158
return err
@@ -219,6 +217,10 @@ func (s *APIServer) setProcessState() error {
219217

220218
var err error
221219

220+
// unconditionally re-set this so we can successfully restart
221+
// TODO(directxman12): we supported this in the past, but do we actually
222+
// want to support re-using an API server object to restart? The loss
223+
// of provisioned users is surprising to say the least.
222224
s.processState = &process.State{
223225
Dir: s.CertDir,
224226
Path: s.Path,
@@ -395,6 +397,9 @@ func (s *APIServer) populateAPIServerCerts() error {
395397
// Stop stops this process gracefully, waits for its termination, and cleans up
396398
// the CertDir if necessary.
397399
func (s *APIServer) Stop() error {
400+
if s.processState.DirNeedsCleaning {
401+
s.CertDir = "" // reset the directory if it was randomly allocated, so that we can safely restart
402+
}
398403
if s.processState != nil {
399404
if err := s.processState.Stop(); err != nil {
400405
return err

pkg/internal/testing/controlplane/etcd.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ type Etcd struct {
7373
// Start starts the etcd, waits for it to come up, and returns an error, if one
7474
// occoured.
7575
func (e *Etcd) Start() error {
76-
if e.processState == nil {
77-
if err := e.setProcessState(); err != nil {
78-
return err
79-
}
76+
if err := e.setProcessState(); err != nil {
77+
return err
8078
}
8179
return e.processState.Start(e.Out, e.Err)
8280
}
@@ -89,6 +87,10 @@ func (e *Etcd) setProcessState() error {
8987
StopTimeout: e.StopTimeout,
9088
}
9189

90+
// unconditionally re-set this so we can successfully restart
91+
// TODO(directxman12): we supported this in the past, but do we actually
92+
// want to support re-using an API server object to restart? The loss
93+
// of provisioned users is surprising to say the least.
9294
if err := e.processState.Init("etcd"); err != nil {
9395
return err
9496
}
@@ -124,6 +126,9 @@ func (e *Etcd) setProcessState() error {
124126
// Stop stops this process gracefully, waits for its termination, and cleans up
125127
// the DataDir if necessary.
126128
func (e *Etcd) Stop() error {
129+
if e.processState.DirNeedsCleaning {
130+
e.DataDir = "" // reset the directory if it was randomly allocated, so that we can safely restart
131+
}
127132
return e.processState.Stop()
128133
}
129134

pkg/internal/testing/controlplane/plane_test.go

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

34+
It("should be able to restart", func() {
35+
// NB(directxman12): currently restarting invalidates all current users
36+
// when using CertAuthn. We need to support restarting as per our previous
37+
// contract, but it's not clear how much else we actually need to handle, or
38+
// whether or not this is a safe operation.
39+
plane := &ControlPlane{}
40+
Expect(plane.Start()).To(Succeed())
41+
Expect(plane.Stop()).To(Succeed())
42+
Expect(plane.Start()).To(Succeed())
43+
Expect(plane.Stop()).To(Succeed())
44+
})
45+
3446
Context("after having started", func() {
3547
var plane *ControlPlane
3648
BeforeEach(func() {

0 commit comments

Comments
 (0)