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

Extend GetKubeconfig methods with args #471

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
26 changes: 25 additions & 1 deletion support/kind/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type Cluster struct {
version string
image string
rc *rest.Config

getKubeconfigArgs []string
}

// Enforce Type check always to avoid future breaks
Expand Down Expand Up @@ -78,6 +80,15 @@ func WithPath(path string) support.ClusterOpts {
}
}

func WithGetKubeConfigArgs(args ...string) support.ClusterOpts {
return func(c support.E2EClusterProvider) {
k, ok := c.(*Cluster)
if ok {
k.getKubeconfigArgs = args
}
}
}

func (k *Cluster) SetDefaults() support.E2EClusterProvider {
if k.path == "" {
k.path = "kind"
Expand Down Expand Up @@ -111,7 +122,11 @@ func (k *Cluster) getKubeconfig() (string, error) {
kubecfg := fmt.Sprintf("%s-kubecfg", k.name)

var stdout, stderr bytes.Buffer
err := utils.RunCommandWithSeperatedOutput(fmt.Sprintf(`%s get kubeconfig --name %s`, k.path, k.name), &stdout, &stderr)
cmd := fmt.Sprintf(`%s get kubeconfig --name %s`, k.path, k.name)
if len(k.getKubeconfigArgs) > 0 {
cmd = fmt.Sprintf("%s %s", cmd, strings.Join(k.getKubeconfigArgs, " "))
}
err := utils.RunCommandWithSeperatedOutput(cmd, &stdout, &stderr)
if err != nil {
return "", fmt.Errorf("kind get kubeconfig: stderr: %s: %w", stderr.String(), err)
}
Expand Down Expand Up @@ -208,6 +223,10 @@ func (k *Cluster) GetKubeconfig() string {
return k.kubecfgFile
}

func (c *Cluster) GetLiveKubeconfig() (string, error) {
return c.getKubeconfig()
}

func (k *Cluster) GetKubectlContext() string {
return fmt.Sprintf("kind-%s", k.name)
}
Expand Down Expand Up @@ -304,6 +323,11 @@ func (k *Cluster) WaitForControlPlane(ctx context.Context, client klient.Client)
return nil
}

func (k *Cluster) WithGetKubeConfigArgs(args ...string) support.E2EClusterProvider {
k.getKubeconfigArgs = args
return k
}

func (k *Cluster) KubernetesRestConfig() *rest.Config {
return k.rc
}
27 changes: 25 additions & 2 deletions support/kwok/kwok.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Cluster struct {
version string
waitDuration time.Duration
rc *rest.Config

getKubeconfigArgs []string
}

var _ support.E2EClusterProvider = &Cluster{}
Expand All @@ -64,6 +66,15 @@ func WithPath(path string) support.ClusterOpts {
}
}

func WithGetKubeConfigArgs(args ...string) support.ClusterOpts {
return func(c support.E2EClusterProvider) {
k, ok := c.(*Cluster)
if ok {
k.getKubeconfigArgs = args
}
}
}

func (k *Cluster) findOrInstallKwokCtl() error {
if k.version != "" {
kwokVersion = k.version
Expand All @@ -89,8 +100,11 @@ func (k *Cluster) getKubeconfig() (string, error) {
kubecfg := fmt.Sprintf("%s-kubecfg", k.name)

var stdout, stderr bytes.Buffer
err := utils.RunCommandWithSeperatedOutput(fmt.Sprintf(`%s get kubeconfig --name %s`,
k.path, k.name), &stdout, &stderr)
cmd := fmt.Sprintf(`%s get kubeconfig --name %s`, k.path, k.name)
if len(k.getKubeconfigArgs) > 0 {
cmd = fmt.Sprintf("%s %s", cmd, strings.Join(k.getKubeconfigArgs, " "))
}
err := utils.RunCommandWithSeperatedOutput(cmd, &stdout, &stderr)
if err != nil {
return "", fmt.Errorf("kwokctl get kubeconfig: stderr: %s: %w", stderr.String(), err)
}
Expand Down Expand Up @@ -234,6 +248,10 @@ func (k *Cluster) GetKubeconfig() string {
return k.kubecfgFile
}

func (c *Cluster) GetLiveKubeconfig() (string, error) {
return c.getKubeconfig()
}

func (k *Cluster) SetDefaults() support.E2EClusterProvider {
if k.path == "" {
k.path = "kwokctl"
Expand Down Expand Up @@ -268,6 +286,11 @@ func (k *Cluster) WithVersion(version string) support.E2EClusterProvider {
return k
}

func (k *Cluster) WithGetKubeConfigArgs(args ...string) support.E2EClusterProvider {
k.getKubeconfigArgs = args
return k
}

func (k *Cluster) KubernetesRestConfig() *rest.Config {
return k.rc
}
11 changes: 11 additions & 0 deletions support/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type E2EClusterProvider interface {
// PATH variable and you want to use that instead of framework trying to install one on it's own.
WithPath(path string) E2EClusterProvider

// WithGetKubeConfigArgs is used to provide a mechanism where the cluster provider can be configured
// to provide additonal flags to the get kubeconfig command that is used to extract the kubeconfig
// file from the cluster. This can be used to provide additional flags such as --internal for kind.
// It is not validated by the framework and is passed as is to the cluster provider.
WithGetKubeConfigArgs(args ...string) E2EClusterProvider

// WithOpts provides a way to customize the options that can be used while setting up the
// cluster using the providers such as kind or kwok or anything else. These helpers can be
// leveraged to setup arguments or configuration values that can be provided while performing
Expand All @@ -58,6 +64,11 @@ type E2EClusterProvider interface {
// using the cluster provider native way
GetKubeconfig() string

// GetLiveKubeconfig provides a way to extract the kubeconfig file associated with the cluster in question.
// Different from GetKubeconfig, this method is used to extract the kubeconfig live, where the GetKubeconfig is cached.
// It is useful in cases where the kubeconfig file is required to be extracted multiple times during the test run with different arguments.
GetLiveKubeconfig() (string, error)

// GetKubectlContext is used to extract the kubectl context to be used while performing the operation
GetKubectlContext() string

Expand Down
17 changes: 16 additions & 1 deletion third_party/vcluster/vcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type Cluster struct {
hostKubeCfg string // kubeconfig file for the host cluster
hostKubeContext string // kubeconfig context for the host cluster
rc *rest.Config

getKubeconfigArgs []string
}

// Enforce Type check always to avoid future breaks
Expand Down Expand Up @@ -118,6 +120,11 @@ func (c *Cluster) WithPath(path string) support.E2EClusterProvider {
return c
}

func (c *Cluster) WithGetKubeConfigArgs(args ...string) support.E2EClusterProvider {
c.getKubeconfigArgs = args
return c
}

func (c *Cluster) WithOpts(opts ...support.ClusterOpts) support.E2EClusterProvider {
for _, opt := range opts {
opt(c)
Expand Down Expand Up @@ -199,6 +206,10 @@ func (c *Cluster) GetKubeconfig() string {
return c.kubecfgFile
}

func (c *Cluster) GetLiveKubeconfig() (string, error) {
return c.getKubeconfig()
}

type kubeconfig struct {
CurrentContext string `json:"current-context"`
}
Expand Down Expand Up @@ -308,7 +319,11 @@ func (c *Cluster) getKubeconfig() (string, error) {
kubecfg := fmt.Sprintf("%s-kubecfg", c.name)

var stdout, stderr bytes.Buffer
err := utils.RunCommandWithSeperatedOutput(fmt.Sprintf(`%s connect %s --print`, c.path, c.name), &stdout, &stderr)
cmd := fmt.Sprintf(`%s connect %s --print`, c.path, c.name)
if len(c.getKubeconfigArgs) > 0 {
cmd = fmt.Sprintf("%s %s", cmd, strings.Join(c.getKubeconfigArgs, " "))
}
err := utils.RunCommandWithSeperatedOutput(cmd, &stdout, &stderr)
if err != nil {
return "", fmt.Errorf("vcluster connect: stderr: %s: %w", stderr.String(), err)
}
Expand Down