-
Notifications
You must be signed in to change notification settings - Fork 314
Added labels support for ci-secret-bootstrap #5189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ import ( | |
| "k8s.io/apimachinery/pkg/types" | ||
| utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| "k8s.io/apimachinery/pkg/util/validation" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| coreclientset "k8s.io/client-go/kubernetes/typed/core/v1" | ||
| "k8s.io/client-go/rest" | ||
|
|
@@ -671,19 +672,34 @@ func fetchUserSecrets(secretsMap map[string]map[types.NamespacedName]coreapi.Sec | |
| secretsMap[cluster] = map[types.NamespacedName]coreapi.Secret{} | ||
| } | ||
| entry, alreadyExists := secretsMap[cluster][secretName] | ||
| labels := map[string]string{api.DPTPRequesterLabel: "ci-secret-bootstrap"} | ||
| if vaultValue, ok := secretKeys[vaultapi.SecretSyncTargetLabelsKey]; ok { | ||
| for _, label := range strings.Split(vaultValue, ",") { | ||
| label := strings.Split(label, ":") | ||
| if validation.IsQualifiedName(label[0]) == nil && validation.IsValidLabelValue(label[1]) == nil { | ||
| labels[strings.Trim(label[0], " ")] = strings.Trim(label[1], " ") | ||
| } else { | ||
| errs = append(errs, fmt.Errorf("label for secret %s, it is invalid: key '%s' value '%s'", secretName, label[0], label[1])) | ||
| } | ||
|
hector-vido marked this conversation as resolved.
hector-vido marked this conversation as resolved.
|
||
| } | ||
| } | ||
| if !alreadyExists { | ||
| entry = coreapi.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{Namespace: secretName.Namespace, Name: secretName.Name, Labels: map[string]string{api.DPTPRequesterLabel: "ci-secret-bootstrap"}}, | ||
| Data: map[string][]byte{}, | ||
| Type: coreapi.SecretTypeOpaque, | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Namespace: secretName.Namespace, | ||
| Name: secretName.Name, | ||
| }, | ||
| Data: map[string][]byte{}, | ||
| Type: coreapi.SecretTypeOpaque, | ||
| } | ||
| } | ||
| entry.ObjectMeta.Labels = labels | ||
| if entry.Type != coreapi.SecretTypeOpaque { | ||
| errs = append(errs, fmt.Errorf("secret %s in cluster %s has ci-secret-bootstrap config as non-opaque type and is targeted by user sync from key %s", secretName.String(), cluster, secretKeys[vaultapi.VaultSourceKey])) | ||
| continue | ||
| } | ||
| for vaultKey, vaultValue := range secretKeys { | ||
| if vaultKey == vaultapi.SecretSyncTargetClusterKey { | ||
| if vaultKey == vaultapi.SecretSyncTargetClusterKey || vaultKey == vaultapi.SecretSyncTargetLabelsKey { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why you changed that. Can you elaborate?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this key is not used to create an entry inside the |
||
| continue | ||
| } | ||
| if _, alreadyExists := entry.Data[vaultKey]; alreadyExists { | ||
|
|
@@ -802,6 +818,7 @@ func updateSecrets(getters map[string]Getter, secretsMap map[string][]*coreapi.S | |
|
|
||
| if !shouldCreate { | ||
| differentData := !equality.Semantic.DeepEqual(secret.Data, existingSecret.Data) | ||
| differentLabels := !equality.Semantic.DeepEqual(secret.ObjectMeta.Labels, existingSecret.ObjectMeta.Labels) | ||
| var addedKeys, changedKeys, removedKeys []string | ||
| for k, value := range secret.Data { | ||
| if existingValue, ok := existingSecret.Data[k]; !ok { | ||
|
|
@@ -821,7 +838,7 @@ func updateSecrets(getters map[string]Getter, secretsMap map[string][]*coreapi.S | |
| errs = append(errs, fmt.Errorf("secret %s:%s/%s needs updating in place (%s), use --force to do so", cluster, secret.Namespace, secret.Name, change)) | ||
| continue | ||
| } | ||
| if existingSecret.Labels == nil || existingSecret.Labels[api.DPTPRequesterLabel] != "ci-secret-bootstrap" || differentData { | ||
| if existingSecret.Labels == nil || existingSecret.Labels[api.DPTPRequesterLabel] != "ci-secret-bootstrap" || differentData || differentLabels { | ||
| if _, err := secretClient.Update(context.TODO(), secret, metav1.UpdateOptions{DryRun: dryRunOptions}); err != nil { | ||
| errs = append(errs, fmt.Errorf("error updating secret %s:%s/%s: %w", cluster, secret.Namespace, secret.Name, err)) | ||
| continue | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -122,6 +122,14 @@ func (k *kvUpdateTransport) RoundTrip(r *http.Request) (*http.Response, error) { | |
| } | ||
| continue | ||
| } | ||
| if key == vault.SecretSyncTargetLabelsKey { | ||
| if len(strings.TrimSpace(value)) == 0 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are mutating the real value here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This happens during the put/patch, I am mutating, indeed, but this is the only way to check if this value is not empty or some blank string. |
||
| errs = append(errs, fmt.Sprintf("value of key %s cannot be empty", key)) | ||
| } else if valueErrs := validation.IsValidLabelValue(value); len(valueErrs) > 0 { | ||
| errs = append(errs, fmt.Sprintf("value of key %s is invalid: %v", key, valueErrs)) | ||
| } | ||
| continue | ||
| } | ||
| if key == vault.SecretSyncTargetClusterKey { | ||
| continue | ||
| } | ||
|
|
@@ -231,7 +239,7 @@ func (k *kvUpdateTransport) validateKeysDontConflict(ctx context.Context, path s | |
|
|
||
| name := types.NamespacedName{Namespace: namespace, Name: data[vault.SecretSyncTargetNameKey]} | ||
| for key := range data { | ||
| if key == vault.SecretSyncTargetNamepaceKey || key == vault.SecretSyncTargetNameKey || key == vault.SecretSyncTargetClusterKey { | ||
| if key == vault.SecretSyncTargetNamepaceKey || key == vault.SecretSyncTargetNameKey || key == vault.SecretSyncTargetClusterKey || key == vault.SecretSyncTargetLabelsKey { | ||
| continue | ||
| } | ||
| if k.existingSecretKeysByNamespaceName[name].Has(key) && !namespacedNameKeySliceContains(k.existingSecretKeysByVaultSecretName[path], namespacedNameKey{name: name, key: key}) { | ||
|
|
@@ -357,7 +365,7 @@ func (k *kvUpdateTransport) syncSecret(data map[string]string) { | |
| secret.Data = map[string][]byte{} | ||
| } | ||
| for k, v := range data { | ||
| if k == vault.SecretSyncTargetNamepaceKey || k == vault.SecretSyncTargetNameKey || k == vault.SecretSyncTargetClusterKey { | ||
| if k == vault.SecretSyncTargetNamepaceKey || k == vault.SecretSyncTargetNameKey || k == vault.SecretSyncTargetClusterKey || k == vault.SecretSyncTargetLabelsKey { | ||
| continue | ||
| } | ||
| secret.Data[k] = []byte(v) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.