Ensure labels are set correct on generated Supervisor secret

Signed-off-by: Andrew Keesler <akeesler@vmware.com>
This commit is contained in:
Andrew Keesler 2020-12-15 15:55:14 -05:00
parent 9d9040944a
commit 35bb76ea82
No known key found for this signature in database
GPG Key ID: 27CE0444346F9413
3 changed files with 37 additions and 4 deletions

View File

@ -26,7 +26,7 @@ func generateSymmetricKey() ([]byte, error) {
return b, nil return b, nil
} }
func isValid(secret *corev1.Secret) bool { func isValid(secret *corev1.Secret, labels map[string]string) bool {
if secret.Type != symmetricSecretType { if secret.Type != symmetricSecretType {
return false return false
} }
@ -39,6 +39,12 @@ func isValid(secret *corev1.Secret) bool {
return false return false
} }
for key, value := range labels {
if secret.Labels[key] != value {
return false
}
}
return true return true
} }

View File

@ -81,7 +81,7 @@ func (c *supervisorSecretsController) Sync(ctx controllerlib.Context) error {
return fmt.Errorf("failed to list secret %s/%s: %w", ctx.Key.Namespace, ctx.Key.Name, err) return fmt.Errorf("failed to list secret %s/%s: %w", ctx.Key.Namespace, ctx.Key.Name, err)
} }
secretNeedsUpdate := isNotFound || !isValid(secret) secretNeedsUpdate := isNotFound || !isValid(secret, c.labels)
if !secretNeedsUpdate { if !secretNeedsUpdate {
plog.Debug("secret is up to date", "secret", klog.KObj(secret)) plog.Debug("secret is up to date", "secret", klog.KObj(secret))
c.setCacheFunc(secret.Data[symmetricSecretDataKey]) c.setCacheFunc(secret.Data[symmetricSecretDataKey])
@ -128,13 +128,16 @@ func (c *supervisorSecretsController) updateSecret(ctx context.Context, newSecre
return nil return nil
} }
if isValid(currentSecret) { if isValid(currentSecret, c.labels) {
*newSecret = currentSecret *newSecret = currentSecret
return nil return nil
} }
currentSecret.Type = (*newSecret).Type currentSecret.Type = (*newSecret).Type
currentSecret.Data = (*newSecret).Data currentSecret.Data = (*newSecret).Data
for key, value := range c.labels {
currentSecret.Labels[key] = value
}
_, err = secrets.Update(ctx, currentSecret, metav1.UpdateOptions{}) _, err = secrets.Update(ctx, currentSecret, metav1.UpdateOptions{})
return err return err

View File

@ -46,7 +46,6 @@ var (
} }
) )
// TODO want what??
func TestSupervisorSecretsControllerFilterSecret(t *testing.T) { func TestSupervisorSecretsControllerFilterSecret(t *testing.T) {
t.Parallel() t.Parallel()
@ -288,6 +287,9 @@ func TestSupervisorSecretsControllerSync(t *testing.T) {
} }
) )
// Add an extra label to make sure we don't overwrite existing labels on a Secret.
generatedSecret.Labels["extra-label-key"] = "extra-label-value"
once := sync.Once{} once := sync.Once{}
tests := []struct { tests := []struct {
@ -429,6 +431,28 @@ func TestSupervisorSecretsControllerSync(t *testing.T) {
}, },
wantCallbackSecret: otherGeneratedSymmetricKey, wantCallbackSecret: otherGeneratedSymmetricKey,
}, },
{
name: "upon updating we discover that a secret with missing labels exists",
storedSecret: func(secret **corev1.Secret) {
delete((*secret).Labels, "some-label-key-1")
},
wantActions: []kubetesting.Action{
kubetesting.NewGetAction(secretsGVR, generatedSecretNamespace, generatedSecretName),
kubetesting.NewUpdateAction(secretsGVR, generatedSecretNamespace, generatedSecret),
},
wantCallbackSecret: generatedSymmetricKey,
},
{
name: "upon updating we discover that a secret with incorrect labels exists",
storedSecret: func(secret **corev1.Secret) {
(*secret).Labels["some-label-key-1"] = "incorrect"
},
wantActions: []kubetesting.Action{
kubetesting.NewGetAction(secretsGVR, generatedSecretNamespace, generatedSecretName),
kubetesting.NewUpdateAction(secretsGVR, generatedSecretNamespace, generatedSecret),
},
wantCallbackSecret: generatedSymmetricKey,
},
{ {
name: "upon updating we discover that the secret has been deleted", name: "upon updating we discover that the secret has been deleted",
storedSecret: func(secret **corev1.Secret) { storedSecret: func(secret **corev1.Secret) {