1
0
Fork 0
mirror of https://github.com/external-secrets/external-secrets.git synced 2024-12-14 11:57:59 +00:00

Demonstrate new slices/maps packages (#3839)

This commit is contained in:
Tom Godkin 2024-08-27 21:20:41 +01:00 committed by GitHub
parent 34a1a50609
commit bc97ae06f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 23 additions and 48 deletions

View file

@ -29,6 +29,7 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"sync" "sync"
"time" "time"
@ -107,18 +108,9 @@ type CertInfo struct {
CAName string CAName string
} }
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("CustomResourceDefinition", req.NamespacedName) log := r.Log.WithValues("CustomResourceDefinition", req.NamespacedName)
if contains(r.CrdResources, req.NamespacedName.Name) { if slices.Contains(r.CrdResources, req.NamespacedName.Name) {
err := r.updateCRD(ctx, req) err := r.updateCRD(ctx, req)
if err != nil { if err != nil {
log.Error(err, "failed to inject conversion webhook") log.Error(err, "failed to inject conversion webhook")

View file

@ -19,6 +19,8 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"maps"
"slices"
"strings" "strings"
"time" "time"
@ -474,11 +476,8 @@ func getManagedDataKeys(secret *v1.Secret, fieldOwner string) ([]string, error)
if !ok { if !ok {
return nil return nil
} }
var keys []string
for k := range df { return slices.Collect(maps.Keys(df))
keys = append(keys, k)
}
return keys
}) })
} }
@ -639,12 +638,9 @@ func isSecretValid(existingSecret v1.Secret) bool {
// computeDataHashAnnotation generate a hash of the secret data combining the old key with the new keys to add or override. // computeDataHashAnnotation generate a hash of the secret data combining the old key with the new keys to add or override.
func (r *Reconciler) computeDataHashAnnotation(existing, secret *v1.Secret) string { func (r *Reconciler) computeDataHashAnnotation(existing, secret *v1.Secret) string {
data := make(map[string][]byte) data := make(map[string][]byte)
for k, v := range existing.Data { maps.Insert(data, maps.All(existing.Data))
data[k] = v maps.Insert(data, maps.All(secret.Data))
}
for k, v := range secret.Data {
data[k] = v
}
return utils.ObjectHash(data) return utils.ObjectHash(data)
} }

View file

@ -17,6 +17,7 @@ package externalsecret
import ( import (
"context" "context"
"fmt" "fmt"
"maps"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
@ -44,9 +45,7 @@ func (r *Reconciler) applyTemplate(ctx context.Context, es *esv1beta1.ExternalSe
} }
// Merge Policy should merge secrets // Merge Policy should merge secrets
if es.Spec.Target.Template.MergePolicy == esv1beta1.MergePolicyMerge { if es.Spec.Target.Template.MergePolicy == esv1beta1.MergePolicyMerge {
for k, v := range dataMap { maps.Insert(secret.Data, maps.All(dataMap))
secret.Data[k] = v
}
} }
execute, err := template.EngineForVersion(es.Spec.Target.Template.EngineVersion) execute, err := template.EngineForVersion(es.Spec.Target.Template.EngineVersion)
if err != nil { if err != nil {

View file

@ -18,6 +18,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"maps"
"strings" "strings"
"time" "time"
@ -235,9 +236,7 @@ func mergeSecretState(newMap, old esapi.SyncedPushSecretsMap) esapi.SyncedPushSe
if !ok { if !ok {
out[k] = make(map[string]esapi.PushSecretData) out[k] = make(map[string]esapi.PushSecretData)
} }
for kk, vv := range v { maps.Insert(out[k], maps.All(v))
out[k][kk] = vv
}
} }
return out return out
} }

View file

@ -19,6 +19,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"slices"
"strings" "strings"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
@ -239,12 +240,9 @@ func (pm *ParameterStore) PushSecret(ctx context.Context, secret *corev1.Secret,
} }
func isManagedByESO(tags []*ssm.Tag) bool { func isManagedByESO(tags []*ssm.Tag) bool {
for _, tag := range tags { return slices.ContainsFunc(tags, func(tag *ssm.Tag) bool {
if *tag.Key == managedBy && *tag.Value == externalSecrets { return *tag.Key == managedBy && *tag.Value == externalSecrets
return true })
}
}
return false
} }
func (pm *ParameterStore) setManagedRemoteParameter(ctx context.Context, secretRequest ssm.PutParameterInput, createManagedByTags bool) error { func (pm *ParameterStore) setManagedRemoteParameter(ctx context.Context, secretRequest ssm.PutParameterInput, createManagedByTags bool) error {

View file

@ -18,6 +18,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"slices"
authv1 "k8s.io/api/authorization/v1" authv1 "k8s.io/api/authorization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -89,20 +90,11 @@ func (c *Client) Validate() (esv1beta1.ValidationResult, error) {
return esv1beta1.ValidationResultUnknown, fmt.Errorf("could not verify if client is valid: %w", err) return esv1beta1.ValidationResultUnknown, fmt.Errorf("could not verify if client is valid: %w", err)
} }
for _, rev := range authReview.Status.ResourceRules { for _, rev := range authReview.Status.ResourceRules {
if (contains("secrets", rev.Resources) || contains("*", rev.Resources)) && if (slices.Contains(rev.Resources, "secrets") || slices.Contains(rev.Resources, "*")) &&
(contains("get", rev.Verbs) || contains("*", rev.Verbs)) && (slices.Contains(rev.Verbs, "get") || slices.Contains(rev.Verbs, "*")) &&
(len(rev.APIGroups) == 0 || (contains("", rev.APIGroups) || contains("*", rev.APIGroups))) { (len(rev.APIGroups) == 0 || (slices.Contains(rev.APIGroups, "") || slices.Contains(rev.APIGroups, "*"))) {
return esv1beta1.ValidationResultReady, nil return esv1beta1.ValidationResultReady, nil
} }
} }
return esv1beta1.ValidationResultError, errors.New("client is not allowed to get secrets") return esv1beta1.ValidationResultError, errors.New("client is not allowed to get secrets")
} }
func contains(sub string, args []string) bool {
for _, k := range args {
if k == sub {
return true
}
}
return false
}

View file

@ -20,6 +20,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"maps"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
@ -107,9 +108,7 @@ func (c *client) PushSecret(ctx context.Context, secret *corev1.Secret, data esv
return nil return nil
} }
} }
for k, v := range vaultSecret { maps.Insert(secretVal, maps.All(vaultSecret))
secretVal[k] = v
}
// Secret got from vault is already on map[string]string format // Secret got from vault is already on map[string]string format
secretVal[data.GetProperty()] = string(value) secretVal[data.GetProperty()] = string(value)
} else { } else {