Refactor some utilitiy methods for sharing.

This commit is contained in:
aram price 2020-12-11 20:48:45 -08:00 committed by Andrew Keesler
parent 9e2213cbae
commit 3e31668eb0
No known key found for this signature in database
GPG Key ID: 27CE0444346F9413

View File

@ -87,14 +87,14 @@ 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 || !c.isValid(secret) secretNeedsUpdate := isNotFound || !isValid(secret)
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.setCache(secret.Data[symmetricKeySecretDataKey]) c.setCache(secret.Data[symmetricKeySecretDataKey])
return nil return nil
} }
newSecret, err := c.generateSecret(ctx.Key.Namespace, ctx.Key.Name) newSecret, err := generateSecret(ctx.Key.Namespace, ctx.Key.Name, secretDataFunc, c.owner)
if err != nil { if err != nil {
return fmt.Errorf("failed to generate secret: %w", err) return fmt.Errorf("failed to generate secret: %w", err)
} }
@ -113,7 +113,7 @@ func (c *supervisorSecretsController) Sync(ctx controllerlib.Context) error {
return nil return nil
} }
func (c *supervisorSecretsController) isValid(secret *corev1.Secret) bool { func isValid(secret *corev1.Secret) bool {
if secret.Type != symmetricKeySecretType { if secret.Type != symmetricKeySecretType {
return false return false
} }
@ -129,12 +129,23 @@ func (c *supervisorSecretsController) isValid(secret *corev1.Secret) bool {
return true return true
} }
func (c *supervisorSecretsController) generateSecret(namespace, name string) (*corev1.Secret, error) { func secretDataFunc() (map[string][]byte, error) {
symmetricKey, err := generateKey() symmetricKey, err := generateKey()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return map[string][]byte{
symmetricKeySecretDataKey: symmetricKey,
}, nil
}
func generateSecret(namespace, name string, secretDataFunc func() (map[string][]byte, error), owner metav1.Object) (*corev1.Secret, error) {
secretData, err := secretDataFunc()
if err != nil {
return nil, err
}
deploymentGVK := schema.GroupVersionKind{ deploymentGVK := schema.GroupVersionKind{
Group: appsv1.SchemeGroupVersion.Group, Group: appsv1.SchemeGroupVersion.Group,
Version: appsv1.SchemeGroupVersion.Version, Version: appsv1.SchemeGroupVersion.Version,
@ -145,13 +156,11 @@ func (c *supervisorSecretsController) generateSecret(namespace, name string) (*c
Name: name, Name: name,
Namespace: namespace, Namespace: namespace,
OwnerReferences: []metav1.OwnerReference{ OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(c.owner, deploymentGVK), *metav1.NewControllerRef(owner, deploymentGVK),
}, },
}, },
Type: symmetricKeySecretType, Type: symmetricKeySecretType,
Data: map[string][]byte{ Data: secretData,
symmetricKeySecretDataKey: symmetricKey,
},
}, nil }, nil
} }
@ -176,7 +185,7 @@ func (c *supervisorSecretsController) updateSecret(ctx context.Context, newSecre
return nil return nil
} }
if c.isValid(currentSecret) { if isValid(currentSecret) {
*newSecret = currentSecret *newSecret = currentSecret
return nil return nil
} }