ca: report state transition error from setSTate

This commit is contained in:
Daniel Nephin 2021-07-22 11:54:21 -04:00
parent 9052975c96
commit 584e4c5e45

View File

@ -154,16 +154,24 @@ func (c *CAManager) setState(newState caState, validateState bool) (caState, err
state := c.state state := c.state
if !validateState || if !validateState ||
state == caStateInitialized || (state == caStateInitialized && newState != caStateInitializing) ||
(state == caStateUninitialized && newState == caStateInitializing) || (state == caStateUninitialized && newState == caStateInitializing) ||
(state == caStateUninitialized && newState == caStateReconfig) { (state == caStateUninitialized && newState == caStateReconfig) {
c.state = newState c.state = newState
} else { } else {
return state, fmt.Errorf("CA is already in state %q", state) return state, &caStateError{Current: state}
} }
return state, nil return state, nil
} }
type caStateError struct {
Current caState
}
func (e *caStateError) Error() string {
return fmt.Sprintf("CA is already in state %q", e.Current)
}
// setPrimaryRoots updates the most recently seen roots from the primary. // setPrimaryRoots updates the most recently seen roots from the primary.
func (c *CAManager) setPrimaryRoots(newRoots structs.IndexedCARoots) error { func (c *CAManager) setPrimaryRoots(newRoots structs.IndexedCARoots) error {
c.stateLock.Lock() c.stateLock.Lock()
@ -360,8 +368,12 @@ func (c *CAManager) InitializeCA() (reterr error) {
} }
// Update the state before doing anything else. // Update the state before doing anything else.
oldState, err := c.setState(caStateInitializing, true) _, err := c.setState(caStateInitializing, true)
if err != nil { var errCaState *caStateError
switch {
case errors.As(err, &errCaState) && errCaState.Current == caStateInitialized:
return nil
case err != nil:
return err return err
} }
@ -377,11 +389,6 @@ func (c *CAManager) InitializeCA() (reterr error) {
} }
}() }()
// if we were already in the initialized state then there is nothing to be done.
if oldState == caStateInitialized {
return nil
}
// Initialize the provider based on the current config. // Initialize the provider based on the current config.
conf, err := c.initializeCAConfig() conf, err := c.initializeCAConfig()
if err != nil { if err != nil {