Skip to content

Commit

Permalink
Adding cluster rm by name or tag and dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
jdewinne committed Dec 11, 2023
1 parent 4212b3a commit a469767
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 21 deletions.
25 changes: 22 additions & 3 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"strings"

"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/spf13/cobra"
)

Expand All @@ -11,11 +14,27 @@ var (

func (r *runners) InitClusterCommand(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "cluster",
Short: "Manage test clusters",
Long: ``,
Use: "cluster",
Short: "Manage test clusters",
Long: ``,
}
parent.AddCommand(cmd)

return cmd
}

func parseTags(tags []string) ([]kotsclient.ClusterTag, error) {
clusterTags := []kotsclient.ClusterTag{}
for _, tag := range tags {
tagParts := strings.SplitN(tag, "=", 2)
if len(tagParts) != 2 {
return nil, errors.Errorf("invalid tag format: %s", tag)
}

clusterTags = append(clusterTags, kotsclient.ClusterTag{
Key: tagParts[0],
Value: tagParts[1],
})
}
return clusterTags, nil
}
15 changes: 3 additions & 12 deletions cli/cmd/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"strings"
"time"

"github.com/moby/moby/pkg/namesgenerator"
Expand Down Expand Up @@ -51,17 +50,9 @@ func (r *runners) createCluster(_ *cobra.Command, args []string) error {
r.args.createClusterName = generateClusterName()
}

tags := []kotsclient.ClusterTag{}
for _, tag := range r.args.createClusterTags {
tagParts := strings.SplitN(tag, "=", 2)
if len(tagParts) != 2 {
return errors.Errorf("invalid tag format: %s", tag)
}

tags = append(tags, kotsclient.ClusterTag{
Key: tagParts[0],
Value: tagParts[1],
})
tags, err := parseTags(r.args.createClusterTags)
if err != nil {
return errors.Wrap(err, "parse tags")
}

opts := kotsclient.CreateClusterOpts{
Expand Down
65 changes: 60 additions & 5 deletions cli/cmd/cluster_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,75 @@ You can specify the --all flag to terminate all clusters.`,
}
parent.AddCommand(cmd)

cmd.Flags().StringArrayVar(&r.args.removeClusterNames, "name", []string{}, "Name of the cluster to remove (can be specified multiple times)")
cmd.Flags().StringArrayVar(&r.args.removeClusterTags, "tag", []string{}, "Tag of the cluster to remove (key=value format, can be specified multiple times)")

cmd.Flags().BoolVar(&r.args.removeClusterAll, "all", false, "remove all clusters")

cmd.Flags().BoolVar(&r.args.removeClusterDryRun, "dry-run", false, "Dry run")

return cmd
}

func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
if len(args) == 0 && !r.args.removeClusterAll {
return errors.New("ID or --all flag required")
} else if len(args) > 0 && r.args.removeClusterAll {
return errors.New("cannot specify ID and --all flag")
if len(args) == 0 && !r.args.removeClusterAll && len(r.args.removeClusterNames) == 0 && len(r.args.removeClusterTags) == 0 {
return errors.New("One of ID, --all, --name or --tag flag required")
} else if len(args) > 0 && (r.args.removeClusterAll || len(r.args.removeClusterNames) > 0 || len(r.args.removeClusterTags) > 0) {
return errors.New("cannot specify ID and --all, --name or --tag flag")
} else if len(args) == 0 && r.args.removeClusterAll && (len(r.args.removeClusterNames) > 0 || len(r.args.removeClusterTags) > 0) {
return errors.New("cannot specify --all and --name or --tag flag")
} else if len(args) == 0 && !r.args.removeClusterAll && len(r.args.removeClusterNames) > 0 && len(r.args.removeClusterTags) > 0 {
return errors.New("cannot specify --name and --tag flag")
}

if r.args.removeClusterAll {
if len(r.args.removeClusterNames) > 0 {
clusters, err := r.kotsAPI.ListClusters(false, nil, nil)
if err != nil {
return errors.Wrap(err, "list clusters")
}
for _, cluster := range clusters {
for _, name := range r.args.removeClusterNames {
if cluster.Name == name {
err := remove(r, cluster.ID)
if err != nil {
return errors.Wrap(err, "remove cluster")
}
}
}
}
}

if len(r.args.removeClusterTags) > 0 {
clusters, err := r.kotsAPI.ListClusters(false, nil, nil)
if err != nil {
return errors.Wrap(err, "list clusters")
}
tags, err := parseTags(r.args.removeClusterTags)
if err != nil {
return errors.Wrap(err, "parse tags")
}

for _, cluster := range clusters {
if cluster.Tags != nil && len(cluster.Tags) > 0 {
for _, tag := range tags {
for _, clusterTag := range cluster.Tags {
if clusterTag.Key == tag.Key && clusterTag.Value == tag.Value {
err := remove(r, cluster.ID)
if err != nil {
return errors.Wrap(err, "remove cluster")
}
}
}
}
}
}
}

if r.args.removeClusterAll {
clusters, err := r.kotsAPI.ListClusters(false, nil, nil)
if err != nil {
return errors.Wrap(err, "list clusters")
}
for _, cluster := range clusters {
err := remove(r, cluster.ID)
if err != nil {
Expand All @@ -56,6 +107,10 @@ func (r *runners) removeCluster(_ *cobra.Command, args []string) error {
}

func remove(r *runners, clusterID string) error {
if r.args.removeClusterDryRun {
fmt.Printf("would remove cluster %s\n", clusterID)
return nil
}
err := r.kotsAPI.RemoveCluster(clusterID)
if errors.Cause(err) == platformclient.ErrForbidden {
return ErrCompatibilityMatrixTermsNotAccepted
Expand Down
5 changes: 4 additions & 1 deletion cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ type runnerArgs struct {
prepareClusterKotsSharedPassword string
prepareClusterAppReadyTimeout time.Duration

removeClusterAll bool
removeClusterAll bool
removeClusterTags []string
removeClusterNames []string
removeClusterDryRun bool

lsAppVersion string
lsVersionsClusterKubernetesDistribution string
Expand Down

0 comments on commit a469767

Please sign in to comment.