Skip to content

Commit

Permalink
[release-1.10] Function CopySecretWithName for creating secrets with …
Browse files Browse the repository at this point in the history
…specific name (knative#7094) (#302)

This is an automated cherry-pick of knative#7002

```release-note

```

Co-authored-by: Knative Prow Robot <knative-prow-robot@google.com>
Co-authored-by: Martin Gencur <mgencur@redhat.com>
  • Loading branch information
3 people authored Aug 4, 2023
1 parent 6ecb162 commit 45e12e1
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/utils/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ import (
// It'll either return a pointer to the new Secret or and error indicating
// why it couldn't do it.
func CopySecret(corev1Input clientcorev1.CoreV1Interface, srcNS string, srcSecretName string, tgtNS string, svcAccount string) (*corev1.Secret, error) {
return CopySecretWithName(corev1Input,
srcNS,
srcSecretName,
tgtNS,
srcSecretName, /* Use same target name as source by default */
svcAccount)
}

// CopySecretWithName will copy a secret from one namespace into another.
// Allows for specifying target secret name.
func CopySecretWithName(corev1Input clientcorev1.CoreV1Interface, srcNS, srcSecretName, tgtNS, tgtSecretName, svcAccount string) (*corev1.Secret, error) {
tgtNamespaceSvcAcct := corev1Input.ServiceAccounts(tgtNS)
srcSecrets := corev1Input.Secrets(srcNS)
tgtNamespaceSecrets := corev1Input.Secrets(tgtNS)
Expand All @@ -54,7 +65,7 @@ func CopySecret(corev1Input clientcorev1.CoreV1Interface, srcNS string, srcSecre
context.Background(),
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: srcSecretName,
Name: tgtSecretName,
},
Data: srcSecret.Data,
Type: srcSecret.Type,
Expand All @@ -72,14 +83,14 @@ func CopySecret(corev1Input clientcorev1.CoreV1Interface, srcNS string, srcSecre
}

for _, secret := range tgtSvcAccount.ImagePullSecrets {
if secret.Name == srcSecretName {
if secret.Name == tgtSecretName {
return newSecret, nil
}
}
// Prevent overwriting existing imagePullSecrets
patch := `[{"op":"add","path":"/imagePullSecrets/-","value":{"name":"` + srcSecretName + `"}}]`
patch := `[{"op":"add","path":"/imagePullSecrets/-","value":{"name":"` + tgtSecretName + `"}}]`
if len(tgtSvcAccount.ImagePullSecrets) == 0 {
patch = `[{"op":"add","path":"/imagePullSecrets","value":[{"name":"` + srcSecretName + `"}]}]`
patch = `[{"op":"add","path":"/imagePullSecrets","value":[{"name":"` + tgtSecretName + `"}]}]`
}
_, err = tgtNamespaceSvcAcct.Patch(context.Background(), svcAccount, types.JSONPatchType,
[]byte(patch), metav1.PatchOptions{})
Expand Down

0 comments on commit 45e12e1

Please sign in to comment.