diff --git a/go.mod b/go.mod index 71565cc0..e95c4ec1 100644 --- a/go.mod +++ b/go.mod @@ -11,8 +11,10 @@ require ( github.com/go-logr/logr v1.4.1 github.com/google/go-cmp v0.6.0 github.com/google/go-github/v61 v61.0.0 + github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.9.0 + gotest.tools/v3 v3.5.1 k8s.io/api v0.29.1 k8s.io/apiextensions-apiserver v0.29.1 k8s.io/apimachinery v0.29.1 @@ -47,6 +49,7 @@ require ( github.com/go-errors/errors v1.4.2 // indirect github.com/go-fed/httpsig v1.1.0 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect + github.com/go-logr/zapr v1.3.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/jsonreference v0.21.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect @@ -81,7 +84,6 @@ require ( github.com/opencontainers/image-spec v1.1.0 // indirect github.com/pelletier/go-toml v1.9.5 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.18.0 // indirect github.com/prometheus/client_model v0.5.0 // indirect @@ -94,6 +96,8 @@ require ( github.com/stretchr/objx v0.5.2 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/xlab/treeprint v1.2.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.26.0 // indirect golang.org/x/crypto v0.21.0 // indirect golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect golang.org/x/mod v0.14.0 // indirect @@ -113,7 +117,6 @@ require ( gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - gotest.tools/v3 v3.5.1 // indirect k8s.io/component-base v0.29.1 // indirect k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect diff --git a/pkg/build/build.go b/pkg/build/build.go index 3bf0564c..9789aea6 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -225,11 +225,6 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error { return fmt.Errorf("creating localbuild resource: %w", err) } - if err != nil { - setupLog.Error(err, "Error creating localbuild resource") - return err - } - err = <-managerExit close(managerExit) return err diff --git a/pkg/cmd/delete/root.go b/pkg/cmd/delete/root.go new file mode 100644 index 00000000..4db1a20f --- /dev/null +++ b/pkg/cmd/delete/root.go @@ -0,0 +1,51 @@ +package delete + +import ( + "flag" + + "github.com/cnoe-io/idpbuilder/pkg/cmd/helpers" + "github.com/pkg/errors" + "github.com/spf13/cobra" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + "sigs.k8s.io/kind/pkg/cluster" +) + +var ( + // Flags + buildName string +) + +var DeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete an IDP cluster", + Long: ``, + RunE: delete, + PreRunE: preDeleteE, +} + +func init() { + DeleteCmd.PersistentFlags().StringVar(&buildName, "build-name", "localdev", "Name of the kind cluster to be deleted.") + + zapfs := flag.NewFlagSet("zap", flag.ExitOnError) + opts := zap.Options{ + Development: true, + } + opts.BindFlags(zapfs) + DeleteCmd.Flags().AddGoFlagSet(zapfs) +} + +func preDeleteE(cmd *cobra.Command, args []string) error { + return helpers.SetLogger() +} + +func delete(cmd *cobra.Command, args []string) error { + detectOpt, err := cluster.DetectNodeProvider() + if err != nil { + return err + } + provider := cluster.NewProvider(detectOpt) + if err := provider.Delete(buildName, ""); err != nil { + return errors.Wrapf(err, "failed to delete cluster %q", buildName) + } + return nil +} diff --git a/pkg/cmd/get/clusters/clusters.go b/pkg/cmd/get/clusters/clusters.go new file mode 100644 index 00000000..b1f1ebd5 --- /dev/null +++ b/pkg/cmd/get/clusters/clusters.go @@ -0,0 +1,43 @@ +package clusters + +import ( + "flag" + "fmt" + + "github.com/pkg/errors" + "github.com/spf13/cobra" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + "sigs.k8s.io/kind/pkg/cluster" +) + +var ClustersCmd = &cobra.Command{ + Use: "clusters", + Short: "Get idp clusters", + Long: ``, + RunE: list, +} + +func init() { + zapfs := flag.NewFlagSet("zap", flag.ExitOnError) + opts := zap.Options{ + Development: true, + } + opts.BindFlags(zapfs) + ClustersCmd.Flags().AddGoFlagSet(zapfs) + + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) +} + +func list(cmd *cobra.Command, args []string) error { + provider := cluster.NewProvider(cluster.ProviderWithDocker()) + clusters, err := provider.List() + if err != nil { + return errors.Wrapf(err, "failed to list clusters") + } + + for _, cluster := range clusters { + fmt.Println(cluster) + } + return nil +} diff --git a/pkg/cmd/get/root.go b/pkg/cmd/get/root.go index 1d475878..0b24bf0e 100644 --- a/pkg/cmd/get/root.go +++ b/pkg/cmd/get/root.go @@ -3,6 +3,7 @@ package get import ( "fmt" + "github.com/cnoe-io/idpbuilder/pkg/cmd/get/clusters" "github.com/spf13/cobra" ) @@ -19,6 +20,7 @@ var ( ) func init() { + GetCmd.AddCommand(clusters.ClustersCmd) GetCmd.AddCommand(SecretsCmd) GetCmd.PersistentFlags().StringSliceVarP(&packages, "packages", "p", []string{}, "names of packages.") GetCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", "", "Output format. json or yaml.") diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 0e582dff..ce1aca38 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -5,6 +5,7 @@ import ( "os" "github.com/cnoe-io/idpbuilder/pkg/cmd/create" + "github.com/cnoe-io/idpbuilder/pkg/cmd/delete" "github.com/cnoe-io/idpbuilder/pkg/cmd/get" "github.com/cnoe-io/idpbuilder/pkg/cmd/helpers" "github.com/cnoe-io/idpbuilder/pkg/cmd/version" @@ -21,6 +22,7 @@ func init() { rootCmd.PersistentFlags().StringVarP(&helpers.LogLevel, "log-level", "l", "info", helpers.LogLevelMsg) rootCmd.AddCommand(create.CreateCmd) rootCmd.AddCommand(get.GetCmd) + rootCmd.AddCommand(delete.DeleteCmd) rootCmd.AddCommand(version.VersionCmd) }