From 4b4059a16e8e43eb6009190053f126409ef99885 Mon Sep 17 00:00:00 2001 From: Vaibhav Kamra Date: Tue, 20 Aug 2019 22:23:16 -0700 Subject: [PATCH] Remove unnecessary "kind" check in SecretRef The ActionSet schema allows specifying secret refs under the "secrets" key. This commit removes the requirement that the user specify a `Kind` in the reference because that information is not required. --- pkg/param/param.go | 16 ++++++++++++---- pkg/param/param_test.go | 12 ++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/pkg/param/param.go b/pkg/param/param.go index d68409afd2..58514ce70c 100644 --- a/pkg/param/param.go +++ b/pkg/param/param.go @@ -214,12 +214,20 @@ func fetchKeyPairCredential(ctx context.Context, cli kubernetes.Interface, c *cr }, nil } -func fetchSecrets(ctx context.Context, cli kubernetes.Interface, refs map[string]crv1alpha1.ObjectReference) (map[string]v1.Secret, error) { - secrets := make(map[string]v1.Secret, len(refs)) +func filterByKind(refs map[string]crv1alpha1.ObjectReference, kind string) map[string]crv1alpha1.ObjectReference { + filtered := make(map[string]crv1alpha1.ObjectReference, len(refs)) for name, ref := range refs { - if strings.ToLower(ref.Kind) != SecretKind { + if strings.ToLower(ref.Kind) != strings.ToLower(kind) { continue } + filtered[name] = ref + } + return filtered +} + +func fetchSecrets(ctx context.Context, cli kubernetes.Interface, refs map[string]crv1alpha1.ObjectReference) (map[string]v1.Secret, error) { + secrets := make(map[string]v1.Secret, len(refs)) + for name, ref := range refs { s, err := cli.CoreV1().Secrets(ref.Namespace).Get(ref.Name, metav1.GetOptions{}) if err != nil { return nil, errors.WithStack(err) @@ -343,7 +351,7 @@ func InitPhaseParams(ctx context.Context, cli kubernetes.Interface, tp *Template if tp.Phases == nil { tp.Phases = make(map[string]*Phase) } - secrets, err := fetchSecrets(ctx, cli, objects) + secrets, err := fetchSecrets(ctx, cli, filterByKind(objects, SecretKind)) if err != nil { return err } diff --git a/pkg/param/param_test.go b/pkg/param/param_test.go index e823ae67c8..54ee4c3bf2 100644 --- a/pkg/param/param_test.go +++ b/pkg/param/param_test.go @@ -12,7 +12,7 @@ import ( "github.com/Masterminds/sprig" . "gopkg.in/check.v1" appsv1 "k8s.io/api/apps/v1" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -521,7 +521,7 @@ func (s *ParamsSuite) TestPhaseParams(c *C) { }, }, } - _, err := s.cli.CoreV1().Secrets(s.namespace).Create(secret) + secret, err := s.cli.CoreV1().Secrets(s.namespace).Create(secret) c.Assert(err, IsNil) defer s.cli.CoreV1().Secrets(s.namespace).Delete("secret-name", &metav1.DeleteOptions{}) @@ -543,6 +543,12 @@ func (s *ParamsSuite) TestPhaseParams(c *C) { Name: "profName", Namespace: s.namespace, }, + Secrets: map[string]crv1alpha1.ObjectReference{ + "actionSetSecret": crv1alpha1.ObjectReference{ + Name: secret.Name, + Namespace: secret.Namespace, + }, + }, } tp, err := New(ctx, s.cli, crCli, as) c.Assert(err, IsNil) @@ -552,6 +558,8 @@ func (s *ParamsSuite) TestPhaseParams(c *C) { UpdatePhaseParams(ctx, tp, "backup", map[string]interface{}{"version": "0.20.0"}) c.Assert(tp.Phases, HasLen, 1) c.Assert(tp.Phases["backup"], NotNil) + c.Assert(tp.Secrets, HasLen, 1) + c.Assert(tp.Secrets["actionSetSecret"], DeepEquals, *secret) } func (s *ParamsSuite) TestRenderingPhaseParams(c *C) {