Skip to content

Commit

Permalink
feat: add new GetConfig command and inject ImpersonateAs variable
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Cox <mail@logan-cox.com>
  • Loading branch information
logan-bobo committed Aug 21, 2024
1 parent cf7894e commit 09252ed
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 61 deletions.
51 changes: 0 additions & 51 deletions pkg/impersonation/main.go

This file was deleted.

22 changes: 22 additions & 0 deletions pkg/k8s/main.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions pkg/kuttlctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

)

Expand Down Expand Up @@ -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())
Expand Down
46 changes: 41 additions & 5 deletions pkg/test/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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}
}
Expand Down

0 comments on commit 09252ed

Please sign in to comment.