Skip to content
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

feat: add user impersonation to all commands #549

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Flags are:

Directory to output kind logs to (if not specified, the current working directory).

* **`--as (string)`**

The kubernetes user to impersonate for the operation. User could be a regular user or a service account in a namespace.

* **`--config (string)`**

Path to file to load test settings from. This is usually the `kuttl-test.yaml` file.
Expand Down
21 changes: 21 additions & 0 deletions pkg/k8s/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 nil, err
}

if ImpersonateAs != "" {
cfg.Impersonate.UserName = ImpersonateAs
}

return cfg, nil
}
2 changes: 2 additions & 0 deletions pkg/kuttlctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/spf13/cobra"

"github.com/kudobuilder/kuttl/pkg/k8s"
"github.com/kudobuilder/kuttl/pkg/version"
)

Expand Down Expand Up @@ -30,6 +31,7 @@ and serves as an API aggregation layer.
Version: version.Get().GitVersion,
}

cmd.PersistentFlags().StringVar(&k8s.ImpersonateAs, "as", "", "Username to impersonate for the operation. User could be a regular user or a service account in a namespace.")
cmd.AddCommand(newAssertCmd())
cmd.AddCommand(newErrorsCmd())
cmd.AddCommand(newTestCmd())
Expand Down
10 changes: 7 additions & 3 deletions pkg/test/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"

"github.com/kudobuilder/kuttl/pkg/k8s"
testutils "github.com/kudobuilder/kuttl/pkg/test/utils"
)

Expand Down Expand Up @@ -105,27 +105,31 @@ func Errors(namespace string, timeout int, errorFiles ...string) error {
}

func Client(_ bool) (client.Client, error) {
cfg, err := config.GetConfig()
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 := config.GetConfig()
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
}
4 changes: 2 additions & 2 deletions pkg/test/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/envtest"
kindConfig "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"

harness "github.com/kudobuilder/kuttl/pkg/apis/testharness/v1beta1"
"github.com/kudobuilder/kuttl/pkg/file"
"github.com/kudobuilder/kuttl/pkg/http"
"github.com/kudobuilder/kuttl/pkg/k8s"
"github.com/kudobuilder/kuttl/pkg/report"
testutils "github.com/kudobuilder/kuttl/pkg/test/utils"
)
Expand Down Expand Up @@ -253,7 +253,7 @@ func (h *Harness) Config() (*rest.Config, error) {
h.config, err = h.RunKIND()
default:
h.T.Log("running tests using configured kubeconfig.")
h.config, err = config.GetConfig()
h.config, err = k8s.GetConfig()
if err != nil {
return nil, err
}
Expand Down
Loading