From f241d7423d0c5952953469d3562d5397a5f2fdd4 Mon Sep 17 00:00:00 2001 From: mcmarkj Date: Thu, 19 Aug 2021 16:11:29 +0100 Subject: [PATCH] Use deepequal --- pkg/controller/deployment/deployment_controller.go | 5 ++--- .../deployment/deployment_controller_test.go | 11 +++++++---- .../onepassworditem/onepassworditem_test.go | 9 +++++++++ .../kubernetes_secrets_builder.go | 14 ++------------ 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/pkg/controller/deployment/deployment_controller.go b/pkg/controller/deployment/deployment_controller.go index e4fd363..3b81fdf 100644 --- a/pkg/controller/deployment/deployment_controller.go +++ b/pkg/controller/deployment/deployment_controller.go @@ -191,8 +191,7 @@ func (r *ReconcileDeployment) HandleApplyingDeployment(namespace string, annotat reqLog := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) secretName := annotations[op.NameAnnotation] - secretLabels := map[string]string{} - secretAnnotations := map[string]string{} + secretLabels := map[string]string(nil) if len(secretName) == 0 { reqLog.Info("No 'item-name' annotation set. 'item-path' and 'item-name' must be set as annotations to add new secret.") return nil @@ -203,5 +202,5 @@ func (r *ReconcileDeployment) HandleApplyingDeployment(namespace string, annotat return fmt.Errorf("Failed to retrieve item: %v", err) } - return kubeSecrets.CreateKubernetesSecretFromItem(r.kubeClient, secretName, namespace, item, annotations[op.RestartDeploymentsAnnotation], secretLabels, secretAnnotations) + return kubeSecrets.CreateKubernetesSecretFromItem(r.kubeClient, secretName, namespace, item, annotations[op.RestartDeploymentsAnnotation], secretLabels, annotations) } diff --git a/pkg/controller/deployment/deployment_controller_test.go b/pkg/controller/deployment/deployment_controller_test.go index 9ab7a55..25ae956 100644 --- a/pkg/controller/deployment/deployment_controller_test.go +++ b/pkg/controller/deployment/deployment_controller_test.go @@ -258,7 +258,7 @@ var tests = []testReconcileItem{ }, }, { - testName: "Test Do not update if OnePassword Item Version has not changed", + testName: "Test Do not update if Annotations have not changed", deploymentResource: &appsv1.Deployment{ TypeMeta: metav1.TypeMeta{ Kind: deploymentKind, @@ -268,10 +268,10 @@ var tests = []testReconcileItem{ Name: name, Namespace: namespace, Annotations: map[string]string{ - op.VersionAnnotation: fmt.Sprint(version), op.ItemPathAnnotation: itemPath, op.NameAnnotation: name, }, + Labels: map[string]string{}, }, }, existingSecret: &corev1.Secret{ @@ -279,8 +279,9 @@ var tests = []testReconcileItem{ Name: name, Namespace: namespace, Annotations: map[string]string{ - op.ItemPathAnnotation: itemPath, op.VersionAnnotation: fmt.Sprint(version), + op.ItemPathAnnotation: itemPath, + op.NameAnnotation: name, }, }, Data: expectedSecretData, @@ -291,9 +292,11 @@ var tests = []testReconcileItem{ Name: name, Namespace: namespace, Annotations: map[string]string{ - op.ItemPathAnnotation: itemPath, op.VersionAnnotation: fmt.Sprint(version), + op.ItemPathAnnotation: itemPath, + op.NameAnnotation: name, }, + Labels: map[string]string(nil), }, Data: expectedSecretData, }, diff --git a/pkg/controller/onepassworditem/onepassworditem_test.go b/pkg/controller/onepassworditem/onepassworditem_test.go index 26eb5df..296ef27 100644 --- a/pkg/controller/onepassworditem/onepassworditem_test.go +++ b/pkg/controller/onepassworditem/onepassworditem_test.go @@ -149,6 +149,11 @@ var tests = []testReconcileItem{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: namespace, + Annotations: map[string]string{ + op.VersionAnnotation: fmt.Sprint(version), + op.ItemPathAnnotation: itemPath, + }, + Labels: map[string]string{}, }, Spec: onepasswordv1.OnePasswordItemSpec{ ItemPath: itemPath, @@ -160,7 +165,9 @@ var tests = []testReconcileItem{ Namespace: namespace, Annotations: map[string]string{ op.VersionAnnotation: "456", + op.ItemPathAnnotation: itemPath, }, + Labels: map[string]string{}, }, Data: expectedSecretData, }, @@ -171,7 +178,9 @@ var tests = []testReconcileItem{ Namespace: namespace, Annotations: map[string]string{ op.VersionAnnotation: fmt.Sprint(version), + op.ItemPathAnnotation: itemPath, }, + Labels: map[string]string{}, }, Data: expectedSecretData, }, diff --git a/pkg/kubernetessecrets/kubernetes_secrets_builder.go b/pkg/kubernetessecrets/kubernetes_secrets_builder.go index 093ede1..40f411f 100644 --- a/pkg/kubernetessecrets/kubernetes_secrets_builder.go +++ b/pkg/kubernetessecrets/kubernetes_secrets_builder.go @@ -3,13 +3,13 @@ package kubernetessecrets import ( "context" "fmt" - "github.com/1Password/connect-sdk-go/onepassword" "github.com/1Password/onepassword-operator/pkg/utils" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" + "reflect" kubernetesClient "sigs.k8s.io/controller-runtime/pkg/client" logf "sigs.k8s.io/controller-runtime/pkg/log" ) @@ -54,7 +54,7 @@ func CreateKubernetesSecretFromItem(kubeClient kubernetesClient.Client, secretNa return err } - if CompareSecretFieldsWithOnePasswordItem(currentSecret.Annotations, secretAnnotations) || CompareSecretFieldsWithOnePasswordItem(currentSecret.Labels, labels) { + if ! reflect.DeepEqual(currentSecret.Annotations, secretAnnotations) || ! reflect.DeepEqual(currentSecret.Labels, labels) { log.Info(fmt.Sprintf("Updating Secret %v at namespace '%v'", secret.Name, secret.Namespace)) currentSecret.ObjectMeta.Annotations = secretAnnotations currentSecret.ObjectMeta.Labels = labels @@ -87,13 +87,3 @@ func BuildKubernetesSecretData(fields []*onepassword.ItemField) map[string][]byt } return secretData } - -func CompareSecretFieldsWithOnePasswordItem(currentSecretsFields map[string]string, expectedFieldsOnSecret map[string]string) bool{ - for key, value := range expectedFieldsOnSecret { - currentValue, exists := currentSecretsFields[key] - if !exists || currentValue != value { - return true - } - } - return false -} \ No newline at end of file