Skip to content

Commit

Permalink
Make the --kubeconfig frag persistent (#2229)
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel authored Oct 21, 2024
1 parent 05dd6f8 commit fc4fd73
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 204 deletions.
9 changes: 1 addition & 8 deletions internal/cmd/alpha/access/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

type accessConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

name string
clusterrole string
Expand All @@ -26,24 +25,18 @@ type accessConfig struct {

func NewAccessCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
cfg := accessConfig{
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
KymaConfig: kymaConfig,
}

cmd := &cobra.Command{
Use: "access",
Short: "Produce a kubeconfig with Service Account based token and certificate",
Long: "Produce a kubeconfig with Service Account based token and certificate that is valid for a specified time or indefinitely",
PreRun: func(_ *cobra.Command, _ []string) {
clierror.Check(cfg.KubeClientConfig.Complete())
},
Run: func(_ *cobra.Command, _ []string) {
clierror.Check(runAccess(&cfg))
},
}

cfg.KubeClientConfig.AddFlag(cmd)

cmd.Flags().StringVar(&cfg.name, "name", "", "Name of the Service Account to be created")
cmd.Flags().StringVar(&cfg.clusterrole, "clusterrole", "", "Name of the cluster role to bind the Service Account to")
cmd.Flags().StringVar(&cfg.output, "output", "", "Path to output the kubeconfig file, if not provided the kubeconfig will be printed")
Expand Down
8 changes: 1 addition & 7 deletions internal/cmd/alpha/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,27 @@ import (

type addConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

modules []string
crs []string
}

func NewAddCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
cfg := addConfig{
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
KymaConfig: kymaConfig,
}

cmd := &cobra.Command{
Use: "add",
Short: "Adds Kyma modules.",
Long: `Use this command to add Kyma modules`,
PreRun: func(_ *cobra.Command, _ []string) {
clierror.Check(cfg.KubeClientConfig.Complete())
},
Run: func(_ *cobra.Command, _ []string) {
clierror.Check(runAdd(&cfg))
},
}

cmd.AddCommand(managed.NewManagedCMD(kymaConfig))

cfg.KubeClientConfig.AddFlag(cmd)
cmd.Flags().StringSliceVar(&cfg.modules, "module", []string{}, "Name and version of the modules to add. Example: --module serverless,keda:1.1.1,etc...")
cmd.Flags().StringSliceVar(&cfg.crs, "cr", []string{}, "Path to the custom CR file")

Expand Down
9 changes: 1 addition & 8 deletions internal/cmd/alpha/add/managed/managed.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
package managed

import (
"github.com/kyma-project/cli.v3/internal/clierror"
"github.com/kyma-project/cli.v3/internal/cmdcommon"
"github.com/spf13/cobra"
)

type managedConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

module string
channel string
}

func NewManagedCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
config := &managedConfig{
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
KymaConfig: kymaConfig,
}

cmd := &cobra.Command{
Use: "managed",
Short: "Add managed Kyma module in a managed Kyma instance",
PreRun: func(_ *cobra.Command, _ []string) {
clierror.Check(config.KubeClientConfig.Complete())
},
RunE: func(_ *cobra.Command, _ []string) error {
return runAddManaged(config)
},
}

config.KubeClientConfig.AddFlag(cmd)
cmd.Flags().StringVar(&config.module, "module", "", "Name of the module to add")
cmd.Flags().StringVar(&config.channel, "channel", "", "Name of the Kyma channel to use for the module")

Expand Down
18 changes: 8 additions & 10 deletions internal/cmd/alpha/alpha.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package alpha

import (
"context"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/registry"

"github.com/kyma-project/cli.v3/internal/clierror"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/access"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/add"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/hana"
Expand All @@ -12,23 +10,23 @@ import (
"github.com/kyma-project/cli.v3/internal/cmd/alpha/oidc"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/provision"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/referenceinstance"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/registry"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/remove"
"github.com/kyma-project/cli.v3/internal/cmdcommon"
"github.com/spf13/cobra"
)

func NewAlphaCMD() *cobra.Command {

config := &cmdcommon.KymaConfig{
Ctx: context.Background(),
}

func NewAlphaCMD() (*cobra.Command, clierror.Error) {
cmd := &cobra.Command{
Use: "alpha",
Short: "Groups command prototypes the API for which may still change.",
Long: `A set of alpha prototypes that may still change. Use in automations on your own risk.`,
DisableFlagsInUseLine: true,
}
config, err := cmdcommon.NewKymaConfig(cmd)
if err != nil {
return nil, err
}

cmd.AddCommand(hana.NewHanaCMD(config))
cmd.AddCommand(imageimport.NewImportCMD(config))
Expand All @@ -41,5 +39,5 @@ func NewAlphaCMD() *cobra.Command {
cmd.AddCommand(remove.NewRemoveCMD(config))
cmd.AddCommand(registry.NewRegistryCMD(config))

return cmd
return cmd, nil
}
13 changes: 3 additions & 10 deletions internal/cmd/alpha/hana/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

type hanaCheckConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

name string
namespace string
Expand All @@ -27,26 +26,20 @@ type hanaCheckConfig struct {

func NewHanaCheckCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
config := hanaCheckConfig{
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
stdout: os.Stdout,
checkCommands: checkCommands,
KymaConfig: kymaConfig,
stdout: os.Stdout,
checkCommands: checkCommands,
}

cmd := &cobra.Command{
Use: "check",
Short: "Check if the Hana instance is provisioned.",
Long: "Use this command to check if the Hana instance is provisioned on the SAP Kyma platform.",
PreRun: func(_ *cobra.Command, _ []string) {
clierror.Check(config.KubeClientConfig.Complete())
},
Run: func(_ *cobra.Command, _ []string) {
clierror.Check(runCheck(&config))
},
}

config.KubeClientConfig.AddFlag(cmd)

cmd.Flags().StringVar(&config.name, "name", "", "Name of Hana instance.")
cmd.Flags().StringVar(&config.namespace, "namespace", "default", "Namespace for Hana instance.")

Expand Down
12 changes: 7 additions & 5 deletions internal/cmd/alpha/hana/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ func fixCheckConfig(objects ...runtime.Object) hanaCheckConfig {
scheme.AddKnownTypes(btp.GVRServiceInstance.GroupVersion())
dynamic := dynamic_fake.NewSimpleDynamicClient(scheme, objects...)
config := hanaCheckConfig{
stdout: io.Discard,
KymaConfig: &cmdcommon.KymaConfig{Ctx: context.Background()},
KubeClientConfig: cmdcommon.KubeClientConfig{
KubeClient: &kube_fake.FakeKubeClient{
TestBtpInterface: btp.NewClient(dynamic),
stdout: io.Discard,
KymaConfig: &cmdcommon.KymaConfig{
Ctx: context.Background(),
KubeClientConfig: &cmdcommon.KubeClientConfig{
KubeClient: &kube_fake.FakeKubeClient{
TestBtpInterface: btp.NewClient(dynamic),
},
},
},
name: testName,
Expand Down
9 changes: 1 addition & 8 deletions internal/cmd/alpha/hana/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

type hanaCredentialsConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

name string
namespace string
Expand All @@ -26,24 +25,18 @@ type credentials struct {

func NewHanaCredentialsCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
config := hanaCredentialsConfig{
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
KymaConfig: kymaConfig,
}

cmd := &cobra.Command{
Use: "credentials",
Short: "Print credentials of the Hana instance.",
Long: "Use this command to print credentials of the Hana instance on the SAP Kyma platform.",
PreRun: func(_ *cobra.Command, _ []string) {
clierror.Check(config.KubeClientConfig.Complete())
},
Run: func(_ *cobra.Command, _ []string) {
clierror.Check(runCredentials(&config))
},
}

config.KubeClientConfig.AddFlag(cmd)

cmd.Flags().StringVar(&config.name, "name", "", "Name of Hana instance.")
cmd.Flags().StringVar(&config.namespace, "namespace", "default", "Namespace for Hana instance.")

Expand Down
9 changes: 1 addition & 8 deletions internal/cmd/alpha/hana/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,25 @@ import (

type hanaDeleteConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

name string
namespace string
}

func NewHanaDeleteCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
config := hanaDeleteConfig{
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
KymaConfig: kymaConfig,
}

cmd := &cobra.Command{
Use: "delete",
Short: "Delete a Hana instance on the Kyma.",
Long: "Use this command to delete a Hana instance on the SAP Kyma platform.",
PreRun: func(_ *cobra.Command, _ []string) {
clierror.Check(config.KubeClientConfig.Complete())
},
Run: func(_ *cobra.Command, _ []string) {
clierror.Check(runDelete(&config))
},
}

config.KubeClientConfig.AddFlag(cmd)

cmd.Flags().StringVar(&config.name, "name", "", "Name of Hana instance.")
cmd.Flags().StringVar(&config.namespace, "namespace", "default", "Namespace for Hana instance.")

Expand Down
8 changes: 1 addition & 7 deletions internal/cmd/alpha/hana/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,18 @@ import (
// this command uses the same config as check command
func NewMapHanaCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
config := hanaCheckConfig{
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
KymaConfig: kymaConfig,
}

cmd := &cobra.Command{
Use: "map",
Short: "Map the Hana instance to the Kyma cluster.",
Long: "Use this command to map the Hana instance to the Kyma cluster.",
PreRun: func(_ *cobra.Command, _ []string) {
clierror.Check(config.KubeClientConfig.Complete())
},
Run: func(_ *cobra.Command, _ []string) {
clierror.Check(runMap(&config))
},
}

config.KubeClientConfig.AddFlag(cmd)

cmd.Flags().StringVar(&config.name, "name", "", "Name of Hana instance.")
cmd.Flags().StringVar(&config.namespace, "namespace", "default", "Namespace for Hana instance.")
cmd.Flags().DurationVar(&config.timeout, "timeout", 7*time.Minute, "Timeout for the command")
Expand Down
9 changes: 1 addition & 8 deletions internal/cmd/alpha/hana/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

type hanaProvisionConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

name string
namespace string
Expand All @@ -24,24 +23,18 @@ type hanaProvisionConfig struct {

func NewHanaProvisionCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
config := hanaProvisionConfig{
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
KymaConfig: kymaConfig,
}

cmd := &cobra.Command{
Use: "provision",
Short: "Provisions a Hana instance on the Kyma.",
Long: "Use this command to provision a Hana instance on the SAP Kyma platform.",
PreRun: func(_ *cobra.Command, _ []string) {
clierror.Check(config.KubeClientConfig.Complete())
},
Run: func(_ *cobra.Command, _ []string) {
clierror.Check(runProvision(&config))
},
}

config.KubeClientConfig.AddFlag(cmd)

cmd.Flags().StringVar(&config.name, "name", "", "Name of Hana instance.")
cmd.Flags().StringVar(&config.namespace, "namespace", "default", "Namespace for Hana instance.")
cmd.Flags().StringVar(&config.planName, "plan", "hana", "Name of the service plan.")
Expand Down
12 changes: 3 additions & 9 deletions internal/cmd/alpha/imageimport/imageimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import (

type provisionConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

image string
}

func NewImportCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
config := provisionConfig{
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
KymaConfig: kymaConfig,
}

cmd := &cobra.Command{
Expand All @@ -30,16 +28,14 @@ func NewImportCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
Args: cobra.ExactArgs(1),

PreRun: func(_ *cobra.Command, args []string) {
clierror.Check(config.complete(args))
config.complete(args)
clierror.Check(config.validate())
},
Run: func(_ *cobra.Command, _ []string) {
clierror.Check(runImageImport(&config))
},
}

config.KubeClientConfig.AddFlag(cmd)

return cmd
}

Expand All @@ -52,10 +48,8 @@ func (pc *provisionConfig) validate() clierror.Error {
return nil
}

func (pc *provisionConfig) complete(args []string) clierror.Error {
func (pc *provisionConfig) complete(args []string) {
pc.image = args[0]

return pc.KubeClientConfig.Complete()
}

func runImageImport(config *provisionConfig) clierror.Error {
Expand Down
Loading

0 comments on commit fc4fd73

Please sign in to comment.