diff --git a/.github/workflows/test-mock.yml b/.github/workflows/test-mock.yml index fe47dcc..a7f2c20 100644 --- a/.github/workflows/test-mock.yml +++ b/.github/workflows/test-mock.yml @@ -42,77 +42,77 @@ jobs: - name: civo create working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe create civo -n demo -r LON1 --nodeSizeMP "g4s.kube.small" --verbose -1 --approve - .\ksctl.exe create ha-civo -n ha-demo --nodeSizeDS "fake.small" --nodeSizeLB "fake.small" --nodeSizeCP "fake.small" --nodeSizeWP "fake.small" -r LON1 --verbose -1 --approve + .\ksctl.exe create civo -n demo -r LON1 --nodeSizeMP "g4s.kube.small" --verbose -1 -s local --approve + .\ksctl.exe create ha-civo -n ha-demo --nodeSizeDS "fake.small" --nodeSizeLB "fake.small" --nodeSizeCP "fake.small" --nodeSizeWP "fake.small" -s local -r LON1 --verbose -1 --approve - name: azure create working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe create azure -n demo -r fake --nodeSizeMP "fake" --verbose -1 --approve - .\ksctl.exe create ha-azure -n ha-demo --nodeSizeDS "fake" --nodeSizeLB "fake" --nodeSizeCP "fake" --nodeSizeWP "fake" -r fake --verbose -1 --approve + .\ksctl.exe create azure -n demo -r fake --nodeSizeMP "fake" -s local --verbose -1 --approve + .\ksctl.exe create ha-azure -n ha-demo --nodeSizeDS "fake" --nodeSizeLB "fake" --nodeSizeCP "fake" --nodeSizeWP "fake" -r fake --verbose -1 -s local --approve - name: local create working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe create local -n demolocal --verbose -1 --approve + .\ksctl.exe create local -n demolocal --verbose -1 -s local --approve - name: civo get working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe get -p civo + .\ksctl.exe get -s local -p civo - name: azure get working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe get -p azure + .\ksctl.exe get -s local -p azure - name: get all working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe get + .\ksctl.exe get -s local - name: civo switch working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe switch -p civo -n demo -r LON1 - .\ksctl.exe switch -p ha-civo -n ha-demo -r LON1 - + .\ksctl.exe switch -p civo -n demo -s local -r LON1 + .\ksctl.exe switch -p ha-civo -n ha-demo -s local -r LON1 + - name: azure switch working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe switch -p azure -n demo -r fake - .\ksctl.exe switch -p ha-azure -n ha-demo -r fake + .\ksctl.exe switch -p azure -n demo -s local -r fake + .\ksctl.exe switch -p ha-azure -n ha-demo -s local -r fake - name: civo scale up and down working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe delete ha-civo delete-nodes -n ha-demo --noWP 0 -r LON1 --approve -v -1 - .\ksctl.exe create ha-civo add-nodes -n ha-demo --noWP 1 --nodeSizeWP "fake.small" -r LON1 -v -1 --approve + .\ksctl.exe delete ha-civo delete-nodes -n ha-demo --noWP 0 -r LON1 -s local --approve -v -1 + .\ksctl.exe create ha-civo add-nodes -n ha-demo --noWP 1 --nodeSizeWP "fake.small" -s local -r LON1 -v -1 --approve - name: azure scale up and down working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe delete ha-azure delete-nodes -n ha-demo --noWP 0 -r fake --approve -v -1 - .\ksctl.exe create ha-azure add-nodes -n ha-demo --noWP 1 --nodeSizeWP "fake" -r fake -v -1 --approve + .\ksctl.exe delete ha-azure delete-nodes -n ha-demo --noWP 0 -r fake -s local --approve -v -1 + .\ksctl.exe create ha-azure add-nodes -n ha-demo --noWP 1 --nodeSizeWP "fake" -s local -r fake -v -1 --approve - name: civo delete working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe delete civo -n demo -r LON1 -v -1 --approve - .\ksctl.exe delete ha-civo -n ha-demo -r LON1 -v -1 --approve + .\ksctl.exe delete civo -n demo -r LON1 -s local -v -1 --approve + .\ksctl.exe delete ha-civo -n ha-demo -r LON1 -s local -v -1 --approve - name: azure delete working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe delete azure -n demo -r fake -v -1 --approve - .\ksctl.exe delete ha-azure -n ha-demo -r fake -v -1 --approve + .\ksctl.exe delete azure -n demo -r fake -s local -v -1 --approve + .\ksctl.exe delete ha-azure -n ha-demo -r fake -s local -v -1 --approve - name: local delete working-directory: 'C:\Users\runneradmin\AppData\Local\ksctl' run: | - .\ksctl.exe delete local -n demolocal -v -1 --approve + .\ksctl.exe delete local -n demolocal -s local -v -1 --approve ksctl-cli-unix: strategy: @@ -146,59 +146,65 @@ jobs: - name: civo create run: | - ksctl create civo -n demo -r LON1 --nodeSizeMP "g4s.kube.small" --verbose -1 --approve - ksctl create ha-civo -n ha-demo --nodeSizeDS "fake.small" --nodeSizeLB "fake.small" --nodeSizeCP "fake.small" --nodeSizeWP "fake.small" -r LON1 --verbose -1 --approve + ksctl create civo -n demo -r LON1 --nodeSizeMP "g4s.kube.small" -s local --verbose -1 --approve + ksctl create ha-civo -n ha-demo --nodeSizeDS "fake.small" -s local --nodeSizeLB "fake.small" --nodeSizeCP "fake.small" --nodeSizeWP "fake.small" -r LON1 --verbose -1 --approve - name: azure create run: | - ksctl create azure -n demo -r fake --nodeSizeMP "fake" --verbose -1 --approve - ksctl create ha-azure -n ha-demo --nodeSizeDS "fake" --nodeSizeLB "fake" --nodeSizeCP "fake" --nodeSizeWP "fake" -r fake --verbose -1 --approve + ksctl create azure -n demo -r fake --nodeSizeMP "fake" -s local --verbose -1 --approve + ksctl create ha-azure -n ha-demo --nodeSizeDS "fake" -s local --nodeSizeLB "fake" --nodeSizeCP "fake" --nodeSizeWP "fake" -r fake --verbose -1 --approve - name: local create run: | - ksctl create local -n demolocal --verbose -1 --approve + ksctl create local -n demolocal -s local --verbose -1 --approve - name: civo get run: | - ksctl get -p civo + ksctl get -p civo -s local - name: azure get run: | - ksctl get -p azure + ksctl get -p azure -s local - name: all run: | - ksctl get + ksctl get -s local - name: civo switch run: | - ksctl switch -p civo -n demo -r LON1 - ksctl switch -p ha-civo -n ha-demo -r LON1 - + ksctl switch -p civo -n demo -s local -r LON1 + ksctl switch -p ha-civo -n ha-demo -s local -r LON1 + - name: azure switch run: | - ksctl switch -p azure -n demo -r fake - ksctl switch -p ha-azure -n ha-demo -r fake + ksctl switch -p azure -n demo -s local -r fake + ksctl switch -p ha-azure -n ha-demo -s local -r fake - name: civo scale up and down run: | - ksctl delete ha-civo delete-nodes -n ha-demo --noWP 0 -r LON1 --approve -v -1 - ksctl create ha-civo add-nodes -n ha-demo --noWP 1 --nodeSizeWP "fake.small" -r LON1 -v -1 --approve + ksctl delete ha-civo delete-nodes -n ha-demo --noWP 0 -r LON1 -s local --approve -v -1 + ksctl create ha-civo add-nodes -n ha-demo --noWP 1 --nodeSizeWP "fake.small" -s local -r LON1 -v -1 --approve - name: azure scale up and down run: | - ksctl delete ha-azure delete-nodes -n ha-demo --noWP 0 -r fake --approve -v -1 - ksctl create ha-azure add-nodes -n ha-demo --noWP 1 --nodeSizeWP "fake" -r fake -v -1 --approve + ksctl delete ha-azure delete-nodes -n ha-demo --noWP 0 -s local -r fake --approve -v -1 + ksctl create ha-azure add-nodes -n ha-demo --noWP 1 --nodeSizeWP "fake" -s local -r fake -v -1 --approve - name: civo delete run: | - ksctl delete civo -n demo -r LON1 -v -1 --approve - ksctl delete ha-civo -n ha-demo -r LON1 -v -1 --approve + ksctl delete civo -n demo -r LON1 -s local -v -1 --approve + ksctl delete ha-civo -n ha-demo -r LON1 -s local -v -1 --approve - name: azure delete run: | - ksctl delete azure -n demo -r fake -v -1 --approve - ksctl delete ha-azure -n ha-demo -r fake -v -1 --approve + ksctl delete azure -n demo -r fake -s local -v -1 --approve + ksctl delete ha-azure -n ha-demo -r fake -s local -v -1 --approve - name: local delete run: | - ksctl delete local -n demolocal -v -1 --approve + ksctl delete local -n demolocal -v -1 -s local --approve + + - name: storage provider + run: | + ksctl create local -n demolocal -s local -v -1 --approve + ksctl create local -n demolocal -s external-mongo -v -1 --approve || echo "Expected to fail :) due to missing env vars" + ksctl create local -n demolocal -s local123 -v -1 --approve || echo "Expected to fail :) due to invalid provider name" \ No newline at end of file diff --git a/cli/cmd/addNodesHAAzure.go b/cli/cmd/addNodesHAAzure.go index eb1cf20..83ef183 100644 --- a/cli/cmd/addNodesHAAzure.go +++ b/cli/cmd/addNodesHAAzure.go @@ -4,10 +4,10 @@ package cmd import ( "context" - "github.com/ksctl/ksctl/pkg/helpers" "os" - control_pkg "github.com/ksctl/ksctl/pkg/controllers" + "github.com/ksctl/ksctl/pkg/helpers" + "github.com/spf13/cobra" "github.com/ksctl/ksctl/pkg/helpers/consts" @@ -22,15 +22,14 @@ ksctl create-cluster ha-azure add-nodes `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } - SetRequiredFeatureFlags(cmd) cli.Client.Metadata.Provider = consts.CloudAzure + SetDefaults(consts.CloudAzure, consts.ClusterTypeHa) + cli.Client.Metadata.NoWP = noWP cli.Client.Metadata.WorkerPlaneNodeType = nodeSizeWP cli.Client.Metadata.ClusterName = clusterName @@ -39,6 +38,11 @@ ksctl create-cluster ha-azure add-nodes cli.Client.Metadata.K8sDistro = consts.KsctlKubernetes(distro) cli.Client.Metadata.K8sVersion = k8sVer + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } + if err := createApproval(cmd.Flags().Lookup("approve").Changed); err != nil { log.Error(err.Error()) os.Exit(1) @@ -61,6 +65,7 @@ func init() { regionFlag(addMoreWorkerNodesHAAzure) k8sVerFlag(addMoreWorkerNodesHAAzure) distroFlag(addMoreWorkerNodesHAAzure) + storageFlag(addMoreWorkerNodesHAAzure) addMoreWorkerNodesHAAzure.MarkFlagRequired("name") addMoreWorkerNodesHAAzure.MarkFlagRequired("region") diff --git a/cli/cmd/addNodesHACivo.go b/cli/cmd/addNodesHACivo.go index 5cb9afa..62f412c 100644 --- a/cli/cmd/addNodesHACivo.go +++ b/cli/cmd/addNodesHACivo.go @@ -4,10 +4,9 @@ package cmd import ( "context" - "github.com/ksctl/ksctl/pkg/helpers" "os" - control_pkg "github.com/ksctl/ksctl/pkg/controllers" + "github.com/ksctl/ksctl/pkg/helpers" "github.com/ksctl/ksctl/pkg/helpers/consts" "github.com/spf13/cobra" @@ -22,24 +21,27 @@ ksctl create-cluster ha-civo add-nodes `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } - SetRequiredFeatureFlags(cmd) cli.Client.Metadata.Provider = consts.CloudCivo + SetDefaults(consts.CloudCivo, consts.ClusterTypeHa) + cli.Client.Metadata.NoWP = noWP cli.Client.Metadata.WorkerPlaneNodeType = nodeSizeWP cli.Client.Metadata.ClusterName = clusterName cli.Client.Metadata.Region = region cli.Client.Metadata.K8sDistro = consts.KsctlKubernetes(distro) cli.Client.Metadata.K8sVersion = k8sVer - cli.Client.Metadata.IsHA = true + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } + if err := createApproval(cmd.Flags().Lookup("approve").Changed); err != nil { log.Error(err.Error()) os.Exit(1) @@ -62,6 +64,7 @@ func init() { regionFlag(addMoreWorkerNodesHACivo) k8sVerFlag(addMoreWorkerNodesHACivo) distroFlag(addMoreWorkerNodesHACivo) + storageFlag(addMoreWorkerNodesHACivo) addMoreWorkerNodesHACivo.MarkFlagRequired("name") addMoreWorkerNodesHACivo.MarkFlagRequired("region") diff --git a/cli/cmd/createCluster.go b/cli/cmd/createCluster.go index 60dc28f..434b609 100644 --- a/cli/cmd/createCluster.go +++ b/cli/cmd/createCluster.go @@ -4,10 +4,10 @@ package cmd import ( "context" - control_pkg "github.com/ksctl/ksctl/pkg/controllers" - "github.com/ksctl/ksctl/pkg/helpers" "os" + "github.com/ksctl/ksctl/pkg/helpers" + "github.com/ksctl/ksctl/pkg/helpers/consts" "github.com/spf13/cobra" ) @@ -42,16 +42,19 @@ var createClusterAzure = &cobra.Command{ `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout + cli.Client.Metadata.Provider = consts.CloudAzure - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) + SetDefaults(consts.CloudAzure, consts.ClusterTypeMang) + + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) } - SetRequiredFeatureFlags(cmd) - cli.Client.Metadata.Provider = consts.CloudAzure - SetDefaults(consts.CloudAzure, consts.ClusterTypeMang) createManaged(cmd.Flags().Lookup("approve").Changed) }, } @@ -65,16 +68,19 @@ ksctl create-cluster civo `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout + cli.Client.Metadata.Provider = consts.CloudCivo - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) + SetDefaults(consts.CloudCivo, consts.ClusterTypeMang) + + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) } - SetRequiredFeatureFlags(cmd) - cli.Client.Metadata.Provider = consts.CloudCivo - SetDefaults(consts.CloudCivo, consts.ClusterTypeMang) createManaged(cmd.Flags().Lookup("approve").Changed) }, } @@ -88,16 +94,19 @@ ksctl create-cluster local `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout + cli.Client.Metadata.Provider = consts.CloudLocal - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) + SetDefaults(consts.CloudLocal, consts.ClusterTypeMang) + + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) } - SetRequiredFeatureFlags(cmd) - cli.Client.Metadata.Provider = consts.CloudLocal - SetDefaults(consts.CloudLocal, consts.ClusterTypeMang) createManaged(cmd.Flags().Lookup("approve").Changed) }, } @@ -111,16 +120,18 @@ ksctl create-cluster ha-civo `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") - cli.Client.Metadata.LogVerbosity = verbosity - cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity + cli.Client.Metadata.LogWritter = os.Stdout cli.Client.Metadata.Provider = consts.CloudCivo + SetDefaults(consts.CloudCivo, consts.ClusterTypeHa) + + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } createHA(cmd.Flags().Lookup("approve").Changed) }, } @@ -134,15 +145,18 @@ var createClusterHAAzure = &cobra.Command{ `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } - SetRequiredFeatureFlags(cmd) cli.Client.Metadata.Provider = consts.CloudAzure + SetDefaults(consts.CloudAzure, consts.ClusterTypeHa) + + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } createHA(cmd.Flags().Lookup("approve").Changed) }, } diff --git a/cli/cmd/credentials.go b/cli/cmd/credentials.go index 897d27d..bce8f07 100644 --- a/cli/cmd/credentials.go +++ b/cli/cmd/credentials.go @@ -4,10 +4,10 @@ package cmd import ( "context" "fmt" - "github.com/ksctl/ksctl/pkg/helpers" "os" - control_pkg "github.com/ksctl/ksctl/pkg/controllers" + "github.com/ksctl/ksctl/pkg/helpers" + "github.com/ksctl/ksctl/pkg/helpers/consts" "github.com/spf13/cobra" ) @@ -22,9 +22,13 @@ var credCmd = &cobra.Command{ verbosity, _ := cmd.Flags().GetInt("verbose") cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout + if len(storage) == 0 { + storage = string(consts.StoreLocal) + } - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) } SetRequiredFeatureFlags(cmd) @@ -56,6 +60,8 @@ var credCmd = &cobra.Command{ func init() { rootCmd.AddCommand(credCmd) + storageFlag(credCmd) + credCmd.Flags().BoolP("verbose", "v", true, "for verbose output") } diff --git a/cli/cmd/deleteCluster.go b/cli/cmd/deleteCluster.go index 69cff5d..d245790 100644 --- a/cli/cmd/deleteCluster.go +++ b/cli/cmd/deleteCluster.go @@ -4,11 +4,11 @@ package cmd import ( "context" - control_pkg "github.com/ksctl/ksctl/pkg/controllers" + "os" + "github.com/ksctl/ksctl/pkg/helpers" "github.com/ksctl/ksctl/pkg/helpers/consts" "github.com/spf13/cobra" - "os" ) // deleteClusterCmd represents the deleteCluster command @@ -31,17 +31,19 @@ ksctl create-cluster azure `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } - - SetRequiredFeatureFlags(cmd) cli.Client.Metadata.Provider = consts.CloudAzure + SetDefaults(consts.CloudAzure, consts.ClusterTypeMang) + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } + deleteManaged(cmd.Flags().Lookup("approve").Changed) }, } @@ -55,17 +57,19 @@ ksctl delete-cluster civo `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } - - SetRequiredFeatureFlags(cmd) cli.Client.Metadata.Provider = consts.CloudCivo + SetDefaults(consts.CloudCivo, consts.ClusterTypeMang) + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } + deleteManaged(cmd.Flags().Lookup("approve").Changed) }, @@ -80,17 +84,19 @@ var deleteClusterHAAzure = &cobra.Command{ `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } - - SetRequiredFeatureFlags(cmd) cli.Client.Metadata.Provider = consts.CloudAzure + SetDefaults(consts.CloudAzure, consts.ClusterTypeHa) + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } + deleteHA(cmd.Flags().Lookup("approve").Changed) }, } @@ -104,17 +110,19 @@ ksctl delete-cluster ha-civo `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } - - SetRequiredFeatureFlags(cmd) cli.Client.Metadata.Provider = consts.CloudCivo + SetDefaults(consts.CloudCivo, consts.ClusterTypeHa) + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } + deleteHA(cmd.Flags().Lookup("approve").Changed) }, } @@ -128,17 +136,19 @@ ksctl delete-cluster local `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } - - SetRequiredFeatureFlags(cmd) cli.Client.Metadata.Provider = consts.CloudLocal + SetDefaults(consts.CloudLocal, consts.ClusterTypeMang) + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } + deleteManaged(cmd.Flags().Lookup("approve").Changed) }, } diff --git a/cli/cmd/deleteNodesHAAzure.go b/cli/cmd/deleteNodesHAAzure.go index 60aefb6..fe2a8bf 100644 --- a/cli/cmd/deleteNodesHAAzure.go +++ b/cli/cmd/deleteNodesHAAzure.go @@ -4,10 +4,10 @@ package cmd import ( "context" - "github.com/ksctl/ksctl/pkg/helpers" "os" - control_pkg "github.com/ksctl/ksctl/pkg/controllers" + "github.com/ksctl/ksctl/pkg/helpers" + "github.com/ksctl/ksctl/pkg/helpers/consts" "github.com/spf13/cobra" ) @@ -21,22 +21,25 @@ ksctl delete-cluster ha-azure delete-nodes `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } - SetRequiredFeatureFlags(cmd) cli.Client.Metadata.Provider = consts.CloudAzure cli.Client.Metadata.IsHA = true SetDefaults(consts.CloudAzure, consts.ClusterTypeHa) + cli.Client.Metadata.NoWP = noWP cli.Client.Metadata.ClusterName = clusterName cli.Client.Metadata.Region = region cli.Client.Metadata.K8sDistro = consts.KsctlKubernetes(distro) + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } + if err := deleteApproval(cmd.Flags().Lookup("approve").Changed); err != nil { log.Error(err.Error()) os.Exit(1) @@ -58,6 +61,7 @@ func init() { noOfWPFlag(deleteNodesHAAzure) regionFlag(deleteNodesHAAzure) distroFlag(deleteNodesHAAzure) + storageFlag(deleteNodesHAAzure) deleteNodesHAAzure.MarkFlagRequired("name") deleteNodesHAAzure.MarkFlagRequired("region") diff --git a/cli/cmd/deleteNodesHACivo.go b/cli/cmd/deleteNodesHACivo.go index b93ad88..7ace6fb 100644 --- a/cli/cmd/deleteNodesHACivo.go +++ b/cli/cmd/deleteNodesHACivo.go @@ -4,10 +4,10 @@ package cmd import ( "context" - "github.com/ksctl/ksctl/pkg/helpers" "os" - control_pkg "github.com/ksctl/ksctl/pkg/controllers" + "github.com/ksctl/ksctl/pkg/helpers" + "github.com/ksctl/ksctl/pkg/helpers/consts" "github.com/spf13/cobra" ) @@ -21,22 +21,23 @@ ksctl delete-cluster ha-civo delete-nodes `, Run: func(cmd *cobra.Command, args []string) { verbosity, _ := cmd.Flags().GetInt("verbose") + SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout - - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) - } - SetRequiredFeatureFlags(cmd) cli.Client.Metadata.Provider = consts.CloudCivo cli.Client.Metadata.IsHA = true - SetDefaults(consts.CloudCivo, consts.ClusterTypeHa) cli.Client.Metadata.NoWP = noWP cli.Client.Metadata.ClusterName = clusterName cli.Client.Metadata.Region = region cli.Client.Metadata.K8sDistro = consts.KsctlKubernetes(distro) + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) + } + if err := deleteApproval(cmd.Flags().Lookup("approve").Changed); err != nil { log.Error(err.Error()) os.Exit(1) @@ -58,6 +59,7 @@ func init() { regionFlag(deleteNodesHACivo) //k8sVerFlag(deleteNodesHACivo) distroFlag(deleteNodesHACivo) + storageFlag(deleteNodesHACivo) deleteNodesHACivo.MarkFlagRequired("name") deleteNodesHACivo.MarkFlagRequired("region") diff --git a/cli/cmd/featureFlags.go b/cli/cmd/featureFlags.go index 513315d..dbb5ceb 100644 --- a/cli/cmd/featureFlags.go +++ b/cli/cmd/featureFlags.go @@ -15,7 +15,6 @@ const ( func featureFlag(f *cobra.Command) { f.Flags().StringP("feature-flags", "", "", `Experimental Features: Supported values with comma seperated: [autoscale]`) - // f.Flags().StringArrayP("feature-flags", "", nil, `Supported values: [autoscale]`) } func SetRequiredFeatureFlags(cmd *cobra.Command) { diff --git a/cli/cmd/flags.go b/cli/cmd/flags.go index 67513cc..46f2d69 100644 --- a/cli/cmd/flags.go +++ b/cli/cmd/flags.go @@ -45,6 +45,10 @@ func verboseFlags() { deleteNodesHACivo.Flags().BoolP("approve", "", true, msgApproval) } +func storageFlag(f *cobra.Command) { + f.Flags().StringVarP(&storage, "storage", "s", "", "storage provider") +} + func clusterNameFlag(f *cobra.Command) { f.Flags().StringVarP(&clusterName, "name", "n", "demo", "Cluster Name") // keep it same for all } diff --git a/cli/cmd/getCluster.go b/cli/cmd/getCluster.go index e2e66f1..2b63663 100644 --- a/cli/cmd/getCluster.go +++ b/cli/cmd/getCluster.go @@ -8,12 +8,12 @@ authors Dipankar import ( "context" - "github.com/ksctl/ksctl/pkg/helpers" "os" + "github.com/ksctl/ksctl/pkg/helpers" + "github.com/ksctl/ksctl/pkg/helpers/consts" - control_pkg "github.com/ksctl/ksctl/pkg/controllers" "github.com/spf13/cobra" ) @@ -35,9 +35,13 @@ ksctl get-clusters `, verbosity, _ := cmd.Flags().GetInt("verbose") cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout + if len(storage) == 0 { + storage = string(consts.StoreLocal) + } - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) } if len(provider) == 0 { @@ -57,5 +61,7 @@ ksctl get-clusters `, func init() { rootCmd.AddCommand(getClusterCmd) + storageFlag(getClusterCmd) + getClusterCmd.Flags().StringVarP(&provider, "provider", "p", "", "Provider") } diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 3f5dc44..1c74a74 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -8,10 +8,11 @@ authors Dipankar package cmd import ( - "github.com/ksctl/ksctl/pkg/resources/controllers" "os" "time" + "github.com/ksctl/ksctl/pkg/resources/controllers" + controlPkg "github.com/ksctl/ksctl/pkg/controllers" "github.com/ksctl/ksctl/pkg/helpers/consts" "github.com/ksctl/ksctl/pkg/logger" @@ -35,10 +36,10 @@ var ( apps string cni string provider string - //storage string // Currently only local storage is present - distro string - k8sVer string - cloud map[int]string + storage string + distro string + k8sVer string + cloud map[int]string ) type CobraCmd struct { @@ -80,7 +81,6 @@ func Execute() { 3: string(consts.CloudCivo), 4: string(consts.CloudLocal), } - cli.Client.Metadata.StateLocation = consts.StoreLocal timer := time.Now() err := rootCmd.Execute() diff --git a/cli/cmd/switchCluster.go b/cli/cmd/switchCluster.go index 3c04e79..0545118 100644 --- a/cli/cmd/switchCluster.go +++ b/cli/cmd/switchCluster.go @@ -4,10 +4,10 @@ package cmd import ( "context" - "github.com/ksctl/ksctl/pkg/helpers" "os" - control_pkg "github.com/ksctl/ksctl/pkg/controllers" + "github.com/ksctl/ksctl/pkg/helpers" + "github.com/ksctl/ksctl/pkg/helpers/consts" "github.com/spf13/cobra" ) @@ -24,11 +24,16 @@ ksctl switch-context -p -n -r verbosity, _ := cmd.Flags().GetInt("verbose") cli.Client.Metadata.LogVerbosity = verbosity cli.Client.Metadata.LogWritter = os.Stdout + if len(storage) == 0 { + storage = string(consts.StoreLocal) + } - if err := control_pkg.InitializeStorageFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName()), &cli.Client); err != nil { - log.Error("Inialize Storage Driver", "Reason", err) + if err := safeInitializeStorageLoggerFactory(context.WithValue(context.Background(), "USERID", helpers.GetUserName())); err != nil { + log.Error("Failed Inialize Storage Driver", "Reason", err) + os.Exit(1) } SetRequiredFeatureFlags(cmd) + cli.Client.Metadata.ClusterName = clusterName cli.Client.Metadata.Region = region @@ -65,6 +70,7 @@ func init() { rootCmd.AddCommand(switchCluster) clusterNameFlag(switchCluster) regionFlag(switchCluster) + storageFlag(switchCluster) switchCluster.Flags().StringVarP(&provider, "provider", "p", "", "Provider") switchCluster.MarkFlagRequired("name") diff --git a/cli/cmd/utils.go b/cli/cmd/utils.go index 57f04a7..9c9a661 100644 --- a/cli/cmd/utils.go +++ b/cli/cmd/utils.go @@ -2,11 +2,13 @@ package cmd // authors Dipankar import ( + "context" "encoding/json" "fmt" "os" "strings" + control_pkg "github.com/ksctl/ksctl/pkg/controllers" "github.com/ksctl/ksctl/pkg/helpers/consts" ) @@ -177,6 +179,9 @@ func SetDefaults(provider consts.KsctlCloud, clusterType consts.KsctlClusterType if len(k8sVer) == 0 { k8sVer = "1.27.1" } + if len(storage) == 0 { + storage = string(consts.StoreLocal) + } case string(consts.CloudAzure) + string(consts.ClusterTypeMang): if len(nodeSizeMP) == 0 { @@ -191,6 +196,9 @@ func SetDefaults(provider consts.KsctlCloud, clusterType consts.KsctlClusterType if len(k8sVer) == 0 { k8sVer = "1.27" } + if len(storage) == 0 { + storage = string(consts.StoreLocal) + } case string(consts.CloudCivo) + string(consts.ClusterTypeMang): if len(nodeSizeMP) == 0 { @@ -205,6 +213,9 @@ func SetDefaults(provider consts.KsctlCloud, clusterType consts.KsctlClusterType if len(k8sVer) == 0 { k8sVer = "1.27.1" } + if len(storage) == 0 { + storage = string(consts.StoreLocal) + } case string(consts.CloudAzure) + string(consts.ClusterTypeHa): if len(nodeSizeCP) == 0 { @@ -237,6 +248,9 @@ func SetDefaults(provider consts.KsctlCloud, clusterType consts.KsctlClusterType if len(distro) == 0 { distro = string(consts.K8sK3s) } + if len(storage) == 0 { + storage = string(consts.StoreLocal) + } case string(consts.CloudCivo) + string(consts.ClusterTypeHa): if len(nodeSizeCP) == 0 { @@ -266,12 +280,32 @@ func SetDefaults(provider consts.KsctlCloud, clusterType consts.KsctlClusterType if len(k8sVer) == 0 { k8sVer = "1.27.1" } + if len(storage) == 0 { + storage = string(consts.StoreLocal) + } if len(distro) == 0 { distro = string(consts.K8sK3s) } } } +func safeInitializeStorageLoggerFactory(ctx context.Context) error { + defer func() { + if r := recover(); r != nil { + log.Error("Recovered in safeInitializeStorageLoggerFactory", r) + } + }() + + switch storage { + case string(consts.StoreExtMongo), string(consts.StoreLocal): + cli.Client.Metadata.StateLocation = consts.KsctlStore(storage) + default: + return log.NewError("not a valid storage option. Valid Options %v\n", [...]string{"external-mongo", "local"}) + } + + return control_pkg.InitializeStorageFactory(ctx, &cli.Client) +} + func argsFlags() { // Managed Azure clusterNameFlag(createClusterAzure) @@ -282,6 +316,7 @@ func argsFlags() { distroFlag(createClusterAzure) appsFlag(createClusterAzure) cniFlag(createClusterAzure) + storageFlag(createClusterAzure) // Managed Civo clusterNameFlag(createClusterCivo) @@ -292,6 +327,7 @@ func argsFlags() { noOfMPFlag(createClusterCivo) distroFlag(createClusterCivo) k8sVerFlag(createClusterCivo) + storageFlag(createClusterCivo) // Managed Local clusterNameFlag(createClusterLocal) @@ -300,6 +336,7 @@ func argsFlags() { noOfMPFlag(createClusterLocal) distroFlag(createClusterLocal) k8sVerFlag(createClusterLocal) + storageFlag(createClusterLocal) // HA Civo clusterNameFlag(createClusterHACivo) @@ -315,6 +352,7 @@ func argsFlags() { noOfDSFlag(createClusterHACivo) distroFlag(createClusterHACivo) k8sVerFlag(createClusterHACivo) + storageFlag(createClusterHACivo) // HA Azure clusterNameFlag(createClusterHAAzure) @@ -330,26 +368,32 @@ func argsFlags() { noOfDSFlag(createClusterHAAzure) distroFlag(createClusterHAAzure) k8sVerFlag(createClusterHAAzure) + storageFlag(createClusterHAAzure) // Delete commands // Managed Local clusterNameFlag(deleteClusterLocal) + storageFlag(deleteClusterLocal) // managed Azure clusterNameFlag(deleteClusterAzure) regionFlag(deleteClusterAzure) + storageFlag(deleteClusterAzure) // Managed Civo clusterNameFlag(deleteClusterCivo) regionFlag(deleteClusterCivo) + storageFlag(deleteClusterCivo) // HA Civo clusterNameFlag(deleteClusterHACivo) regionFlag(deleteClusterHACivo) + storageFlag(deleteClusterHACivo) // HA Azure clusterNameFlag(deleteClusterHAAzure) regionFlag(deleteClusterHAAzure) + storageFlag(deleteClusterHAAzure) AllFeatures() }