Skip to content

Commit

Permalink
fix: prevent duplicate owner references
Browse files Browse the repository at this point in the history
Signed-off-by: Anish Ramasekar <anish.ramasekar@gmail.com>
  • Loading branch information
aramase committed Mar 25, 2021
1 parent 191786d commit bba5984
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 3 additions & 1 deletion controllers/secretproviderclasspodstatus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,10 @@ func (r *SecretProviderClassPodStatusReconciler) patchSecretWithOwnerRef(ctx con
if _, exists := secretOwnerMap[ownerRefs[i].Name]; exists {
continue
}
// add to map for tracking
secretOwnerMap[ownerRefs[i].Name] = ownerRefs[i].UID
needsPatch = true
klog.Infof("Adding %s/%s as owner ref for %s/%s", ownerRefs[i].APIVersion, ownerRefs[i].Name, namespace, name)
klog.V(5).Infof("Adding %s/%s as owner ref for %s/%s", ownerRefs[i].APIVersion, ownerRefs[i].Name, namespace, name)
secretOwnerRefs = append(secretOwnerRefs, ownerRefs[i])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func TestPatchSecretWithOwnerRef(t *testing.T) {
client := fake.NewFakeClientWithScheme(scheme, initObjects...)
reconciler := newReconciler(client, scheme, "node1")

err = reconciler.patchSecretWithOwnerRef(context.TODO(), "my-secret", "default", ref)
err = reconciler.patchSecretWithOwnerRef(context.TODO(), "my-secret", "default", ref, ref)
g.Expect(err).NotTo(HaveOccurred())

secret := &v1.Secret{}
Expand Down
6 changes: 3 additions & 3 deletions test/bats/azure.bats
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ setup() {
result=$(kubectl get secret foosecret -o jsonpath="{.metadata.labels.secrets-store\.csi\.k8s\.io/managed}")
[[ "${result//$'\r'}" == "true" ]]

run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret default 4"
run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret default 2"
assert_success
}

@test "Sync with K8s secrets - delete deployment, check owner ref updated, check secret deleted" {
run kubectl delete -f $BATS_TESTS_DIR/nginx-deployment-synck8s-azure.yaml
assert_success

run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret default 2"
run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret default 1"
assert_success

run kubectl delete -f $BATS_TESTS_DIR/nginx-deployment-two-synck8s-azure.yaml
Expand Down Expand Up @@ -197,7 +197,7 @@ setup() {
result=$(kubectl exec -n test-ns $POD -- printenv | grep SECRET_USERNAME) | awk -F"=" '{ print $2}'
[[ "${result//$'\r'}" == "${SECRET_VALUE}" ]]

run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret test-ns 2"
run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret test-ns 1"
assert_success
}

Expand Down
6 changes: 3 additions & 3 deletions test/bats/vault.bats
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ EOF
result=$(kubectl get secret foosecret -o jsonpath="{.metadata.labels.secrets-store\.csi\.k8s\.io/managed}")
[[ "${result//$'\r'}" == "true" ]]

run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret default 4"
run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret default 2"
assert_success
}

@test "Sync with K8s secrets - delete deployment, check secret is deleted" {
run kubectl delete -f $BATS_TESTS_DIR/nginx-deployment-synck8s.yaml
assert_success

run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret default 2"
run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret default 1"
assert_success

run kubectl delete -f $BATS_TESTS_DIR/nginx-deployment-two-synck8s.yaml
Expand Down Expand Up @@ -254,7 +254,7 @@ EOF
result=$(kubectl exec -n test-ns $POD -- printenv | grep SECRET_USERNAME | awk -F"=" '{ print $2 }' | tr -d '\r\n')
[[ "$result" == "hello1" ]]

run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret test-ns 2"
run wait_for_process $WAIT_TIME $SLEEP_TIME "compare_owner_count foosecret test-ns 1"
assert_success
}

Expand Down

0 comments on commit bba5984

Please sign in to comment.