From f17f7c0c6a53f50e6933a796b0804eb119a671cf Mon Sep 17 00:00:00 2001 From: Ryan Richard Date: Mon, 26 Jul 2021 17:46:06 -0700 Subject: [PATCH] Small refactors in impersonator_config.go suggested by @mattmoyer --- .../impersonatorconfig/impersonator_config.go | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/internal/controller/impersonatorconfig/impersonator_config.go b/internal/controller/impersonatorconfig/impersonator_config.go index e7ca5962..d9d567dc 100644 --- a/internal/controller/impersonatorconfig/impersonator_config.go +++ b/internal/controller/impersonatorconfig/impersonator_config.go @@ -532,7 +532,7 @@ func (c *impersonatorConfigController) createOrUpdateService(ctx context.Context // to be able to detect that the missing key means that we should remove the key. This is needed to // differentiate it from a key that was added by another actor, which we should not remove. // But don't bother recording the requested annotations if there were no annotations requested. - desiredAnnotationKeys := []string{} + desiredAnnotationKeys := make([]string, 0, len(desiredService.Annotations)) for k := range desiredService.Annotations { desiredAnnotationKeys = append(desiredAnnotationKeys, k) } @@ -543,9 +543,6 @@ func (c *impersonatorConfigController) createOrUpdateService(ctx context.Context if err != nil { return err // This shouldn't really happen. We should always be able to marshal an array of strings. } - if desiredService.Annotations == nil { - desiredService.Annotations = map[string]string{} - } // Save the desired annotations to a bookkeeping annotation. desiredService.Annotations[annotationKeysKey] = string(keysJSONArray) } @@ -593,9 +590,8 @@ func (c *impersonatorConfigController) createOrUpdateService(ctx context.Context // Check if any annotations which were previously in the CredentialIssuer spec are now gone from the spec, // which means that those now-missing annotations should get deleted. - // Nested loops are not efficient here, but these lists of annotations should be small. for _, oldKey := range oldDesiredAnnotationKeys { - if !stringSliceContains(desiredAnnotationKeys, oldKey) { + if _, existsInDesired := desiredService.Annotations[oldKey]; !existsInDesired { delete(updatedService.Annotations, oldKey) } } @@ -618,15 +614,6 @@ func (c *impersonatorConfigController) createOrUpdateService(ctx context.Context return err } -func stringSliceContains(haystack []string, needle string) bool { - for _, s := range haystack { - if s == needle { - return true - } - } - return false -} - func (c *impersonatorConfigController) ensureTLSSecret(ctx context.Context, nameInfo *certNameInfo, ca *certauthority.CA) error { secretFromInformer, err := c.secretsInformer.Lister().Secrets(c.namespace).Get(c.tlsSecretName) notFound := k8serrors.IsNotFound(err)