Impersonator server should take in a cancellable context instead of a stop channel

This commit is contained in:
Joshua Casey
2024-08-27 13:26:39 -05:00
parent 504f0dc26f
commit 8bd9b94d0a
4 changed files with 22 additions and 19 deletions
@@ -81,7 +81,7 @@ type impersonatorConfigController struct {
impersonatorFunc impersonator.FactoryFunc
hasControlPlaneNodes *bool
serverStopCh chan struct{}
serverCancelFunc context.CancelFunc
errorCh chan error
tlsServingCertDynamicCertProvider dynamiccert.Private
log plog.Logger
@@ -461,7 +461,7 @@ func (c *impersonatorConfigController) tlsSecretExists() (bool, *corev1.Secret,
}
func (c *impersonatorConfigController) ensureImpersonatorIsStarted(syncCtx controllerlib.Context) error {
if c.serverStopCh != nil {
if c.serverCancelFunc != nil {
// The server was already started, but it could have died in the background, so make a non-blocking
// check to see if it has sent any errors on the errorCh.
select {
@@ -495,7 +495,8 @@ func (c *impersonatorConfigController) ensureImpersonatorIsStarted(syncCtx contr
return err
}
c.serverStopCh = make(chan struct{})
var serverCtx context.Context
serverCtx, c.serverCancelFunc = context.WithCancel(context.Background())
// use a buffered channel so that startImpersonatorFunc can send
// on it without coordinating with the main controller go routine
c.errorCh = make(chan error, 1)
@@ -509,26 +510,26 @@ func (c *impersonatorConfigController) ensureImpersonatorIsStarted(syncCtx contr
defer syncCtx.Queue.AddRateLimited(syncCtx.Key)
// Forward any errors returned by startImpersonatorFunc on the errorCh.
c.errorCh <- startImpersonatorFunc(c.serverStopCh)
c.errorCh <- startImpersonatorFunc(serverCtx)
}()
return nil
}
func (c *impersonatorConfigController) ensureImpersonatorIsStopped(shouldCloseErrChan bool) error {
if c.serverStopCh == nil {
if c.serverCancelFunc == nil {
return nil
}
c.log.Info("stopping impersonation proxy", "port", c.impersonationProxyPort)
close(c.serverStopCh)
c.serverCancelFunc()
stopErr := <-c.errorCh
if shouldCloseErrChan {
close(c.errorCh)
}
c.serverStopCh = nil
c.serverCancelFunc = nil
c.errorCh = nil
return stopErr
@@ -316,7 +316,7 @@ func TestImpersonatorConfigControllerSync(t *testing.T) {
dynamicCertProvider dynamiccert.Private,
impersonationProxySignerCAProvider dynamiccert.Public,
expiringSingletonTokenCacheGet tokenclient.ExpiringSingletonTokenCacheGet,
) (func(stopCh <-chan struct{}) error, error) {
) (func(ctx context.Context) error, error) {
impersonatorFuncWasCalled++
r.Equal(8444, port)
r.NotNil(dynamicCertProvider)
@@ -376,7 +376,7 @@ func TestImpersonatorConfigControllerSync(t *testing.T) {
// This fake server is enough like the real impersonation proxy server for this unit test because it
// uses the supplied providers to serve TLS. The goal of this unit test is to make sure that the server
// was started/stopped/configured correctly, not to test the actual impersonation behavior.
return func(stopCh <-chan struct{}) error {
return func(ctx context.Context) error {
if impersonatorFuncReturnedFuncError != nil {
return impersonatorFuncReturnedFuncError
}
@@ -406,7 +406,7 @@ func TestImpersonatorConfigControllerSync(t *testing.T) {
if testHTTPServerInterruptCh == nil {
// Wait in the foreground for the stopCh to be closed, and kill the server when that happens.
// This is similar to the behavior of the real impersonation server.
<-stopCh
<-ctx.Done()
} else {
// The test supplied an interrupt channel because it wants to test unexpected termination
// of the server, so wait for that channel to close instead of waiting for the one that