From b09cefa181dc5b391c511b829078a8d650adb2f3 Mon Sep 17 00:00:00 2001 From: Logan Cox Date: Wed, 21 Aug 2024 07:26:54 +0100 Subject: [PATCH] feat: add new GetConfig command and inject ImpersonateAs variable Signed-off-by: Logan Cox Signed-off-by: Logan Cox --- pkg/impersonation/main.go | 51 --------------------------------------- pkg/k8s/main.go | 22 +++++++++++++++++ pkg/kuttlctl/cmd/root.go | 4 +-- pkg/test/assert.go | 46 +++++++++++++++++++++++++++++++---- pkg/test/step.go | 6 ++--- 5 files changed, 68 insertions(+), 61 deletions(-) delete mode 100644 pkg/impersonation/main.go create mode 100644 pkg/k8s/main.go diff --git a/pkg/impersonation/main.go b/pkg/impersonation/main.go deleted file mode 100644 index 1c150047..00000000 --- a/pkg/impersonation/main.go +++ /dev/null @@ -1,51 +0,0 @@ -package impersonation - -import ( - "fmt" - - "k8s.io/client-go/discovery" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/config" - - testutils "github.com/kudobuilder/kuttl/pkg/test/utils" -) - -var ImpersonateAs = "" - -func Client(_ bool) (client.Client, error) { - cfg, err := config.GetConfig() - if err != nil { - return nil, err - } - - if ImpersonateAs != "" { - cfg.Impersonate.UserName = ImpersonateAs - } - - client, err := testutils.NewRetryClient(cfg, client.Options{ - Scheme: testutils.Scheme(), - }) - if err != nil { - return nil, fmt.Errorf("fatal error getting client: %v", err) - } - return client, nil -} - -func DiscoveryClient() (discovery.DiscoveryInterface, error) { - cfg, err := config.GetConfig() - - if ImpersonateAs != "" { - cfg.Impersonate.UserName = ImpersonateAs - } - - if err != nil { - return nil, err - } - dclient, err := discovery.NewDiscoveryClientForConfig(cfg) - if err != nil { - return nil, fmt.Errorf("fatal error getting discovery client: %v", err) - } - return dclient, nil -} - - diff --git a/pkg/k8s/main.go b/pkg/k8s/main.go new file mode 100644 index 00000000..4b0e215b --- /dev/null +++ b/pkg/k8s/main.go @@ -0,0 +1,22 @@ +package k8s + +import ( + "k8s.io/client-go/rest" + "sigs.k8s.io/controller-runtime/pkg/client/config" +) + +var ImpersonateAs = "" + +func GetConfig() (*rest.Config, error) { + cfg, err := config.GetConfig() + + if err != nil { + return &rest.Config{}, err + } + + if ImpersonateAs != "" { + cfg.Impersonate.UserName = ImpersonateAs + } + + return cfg, nil +} diff --git a/pkg/kuttlctl/cmd/root.go b/pkg/kuttlctl/cmd/root.go index 3a3c47f4..7c6eec4a 100644 --- a/pkg/kuttlctl/cmd/root.go +++ b/pkg/kuttlctl/cmd/root.go @@ -4,7 +4,7 @@ import ( "github.com/spf13/cobra" "github.com/kudobuilder/kuttl/pkg/version" - "github.com/kudobuilder/kuttl/pkg/impersonation" + "github.com/kudobuilder/kuttl/pkg/k8s" ) @@ -32,7 +32,7 @@ and serves as an API aggregation layer. Version: version.Get().GitVersion, } - cmd.PersistentFlags().StringVar(&impersonation.ImpersonateAs, "as", "", "the username that you wish to impersonate") + cmd.PersistentFlags().StringVar(&k8s.ImpersonateAs, "as", "", "the username that you wish to impersonate") cmd.AddCommand(newAssertCmd()) cmd.AddCommand(newErrorsCmd()) cmd.AddCommand(newTestCmd()) diff --git a/pkg/test/assert.go b/pkg/test/assert.go index b300f5df..75564483 100644 --- a/pkg/test/assert.go +++ b/pkg/test/assert.go @@ -5,9 +5,11 @@ import ( "fmt" "time" + "k8s.io/client-go/discovery" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/kudobuilder/kuttl/pkg/impersonation" + testutils "github.com/kudobuilder/kuttl/pkg/test/utils" + "github.com/kudobuilder/kuttl/pkg/k8s" ) // Assert checks all provided assert files against a namespace. Upon assert failure, it prints the failures and returns an error @@ -25,8 +27,8 @@ func Assert(namespace string, timeout int, assertFiles ...string) error { // feels like the wrong abstraction, need to do some refactoring s := &Step{ Timeout: 0, - Client: impersonation.Client, - DiscoveryClient: impersonation.DiscoveryClient, + Client: Client, + DiscoveryClient: DiscoveryClient, } var testErrors []error @@ -70,8 +72,8 @@ func Errors(namespace string, timeout int, errorFiles ...string) error { // feels like the wrong abstraction, need to do some refactoring s := &Step{ Timeout: 0, - Client: impersonation.Client, - DiscoveryClient: impersonation.DiscoveryClient, + Client: Client, + DiscoveryClient: DiscoveryClient, } var testErrors []error @@ -102,3 +104,37 @@ func Errors(namespace string, timeout int, errorFiles ...string) error { return errors.New("error asserts not valid") } + +func Client(_ bool) (client.Client, error) { + cfg, err := k8s.GetConfig() + + if err != nil { + return nil, err + } + + client, err := testutils.NewRetryClient(cfg, client.Options{ + Scheme: testutils.Scheme(), + }) + + if err != nil { + return nil, fmt.Errorf("fatal error getting client: %v", err) + } + + return client, nil +} + +func DiscoveryClient() (discovery.DiscoveryInterface, error) { + cfg, err := k8s.GetConfig() + + if err != nil { + return nil, err + } + + dclient, err := discovery.NewDiscoveryClientForConfig(cfg) + + if err != nil { + return nil, fmt.Errorf("fatal error getting discovery client: %v", err) + } + + return dclient, nil +} diff --git a/pkg/test/step.go b/pkg/test/step.go index 5238c234..522df6c5 100644 --- a/pkg/test/step.go +++ b/pkg/test/step.go @@ -61,7 +61,7 @@ type Step struct { // Clean deletes all resources defined in the Apply list. func (s *Step) Clean(namespace string) error { - cl, err := s.Client(false, ) + cl, err := s.Client(false) if err != nil { return err } @@ -87,7 +87,7 @@ func (s *Step) Clean(namespace string) error { // DeleteExisting deletes any resources in the TestStep.Delete list prior to running the tests. func (s *Step) DeleteExisting(namespace string) error { - cl, err := s.Client(false, ) + cl, err := s.Client(false) if err != nil { return err } @@ -175,7 +175,7 @@ func (s *Step) DeleteExisting(namespace string) error { // Create applies all resources defined in the Apply list. func (s *Step) Create(test *testing.T, namespace string) []error { - cl, err := s.Client(true ) + cl, err := s.Client(true) if err != nil { return []error{err} }