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

Add delete and list command #101

Merged
merged 3 commits into from
Jul 31, 2024
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
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions pkg/cmd/delete/root.go
Original file line number Diff line number Diff line change
@@ -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
}
43 changes: 43 additions & 0 deletions pkg/cmd/get/clusters/clusters.go
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not to be too picky here but looks like the other commands are verbs and this one is a noun. shall we keep it as list like it used to be? or get maybe?

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
}
2 changes: 2 additions & 0 deletions pkg/cmd/get/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package get
import (
"fmt"

"github.com/cnoe-io/idpbuilder/pkg/cmd/get/clusters"
"github.com/spf13/cobra"
)

Expand All @@ -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.")
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}

Expand Down
Loading