Skip to content

Commit

Permalink
Merge pull request #332 from replicatedhq/cmx-tags
Browse files Browse the repository at this point in the history
Support for setting tags when creating a cluster
  • Loading branch information
marccampbell authored Oct 13, 2023
2 parents eb78b27 + ccf0e20 commit 5581cbc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
23 changes: 20 additions & 3 deletions cli/cmd/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"strings"
"time"

"github.com/moby/moby/pkg/namesgenerator"
Expand All @@ -18,10 +19,11 @@ func (r *runners) InitClusterCreate(parent *cobra.Command) *cobra.Command {
Use: "create",
Short: "Create test clusters",
Long: `Create test clusters.
This is a beta feature, with some known limitations:
https://docs.replicated.com/vendor/testing-how-to#limitations`,
RunE: r.createCluster,
SilenceUsage: true,
RunE: r.createCluster,
}
parent.AddCommand(cmd)

Expand All @@ -32,8 +34,9 @@ https://docs.replicated.com/vendor/testing-how-to#limitations`,
cmd.Flags().Int64Var(&r.args.createClusterDiskGiB, "disk", int64(50), "Disk Size (GiB) to request per node")
cmd.Flags().StringVar(&r.args.createClusterTTL, "ttl", "", "Cluster TTL (duration, max 48h)")
cmd.Flags().DurationVar(&r.args.createClusterWaitDuration, "wait", time.Second*0, "Wait duration for cluster to be ready (leave empty to not wait)")

cmd.Flags().StringVar(&r.args.createClusterInstanceType, "instance-type", "", "The type of instance to use (e.g. m6i.large)")
cmd.Flags().StringArrayVar(&r.args.createClusterTags, "tag", []string{}, "Tag to apply to the cluster (key=value format, can be specified multiple times)")

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

cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")
Expand All @@ -48,6 +51,19 @@ 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],
})
}

opts := kotsclient.CreateClusterOpts{
Name: r.args.createClusterName,
KubernetesDistribution: r.args.createClusterKubernetesDistribution,
Expand All @@ -56,6 +72,7 @@ func (r *runners) createCluster(_ *cobra.Command, args []string) error {
DiskGiB: r.args.createClusterDiskGiB,
TTL: r.args.createClusterTTL,
InstanceType: r.args.createClusterInstanceType,
Tags: tags,
DryRun: r.args.createClusterDryRun,
}
cl, err := r.createAndWaitForCluster(opts)
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ type runnerArgs struct {
createClusterTTL string
createClusterWaitDuration time.Duration
createClusterInstanceType string
createClusterTags []string

upgradeClusterKubernetesVersion string
upgradeClusterDryRun bool
Expand Down
4 changes: 2 additions & 2 deletions cli/print/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

// TODO: implement a -o wide, and expose nodecount also?
var clustersTmplHeaderSrc = `ID NAME DISTRIBUTION VERSION STATUS CREATED EXPIRES`
var clustersTmplHeaderSrc = `ID NAME DISTRIBUTION VERSION STATUS CREATED EXPIRES TAGS`
var clustersTmplRowSrc = `{{ range . -}}
{{ .ID }} {{ padding .Name 27 }} {{ padding .KubernetesDistribution 12 }} {{ padding .KubernetesVersion 10 }} {{ padding (printf "%s" .Status) 12 }} {{ .CreatedAt}} {{if .ExpiresAt.IsZero}}-{{else}}{{ .ExpiresAt }}{{end}}
{{ .ID }} {{ padding .Name 27 }} {{ padding .KubernetesDistribution 12 }} {{ padding .KubernetesVersion 10 }} {{ padding (printf "%s" .Status) 12 }} {{ .CreatedAt}} {{if .ExpiresAt.IsZero}}-{{else}}{{ .ExpiresAt }}{{end}} {{ range $index, $tag := .Tags }}{{if $index}}, {{end}}{{ $tag.Key }}={{ $tag.Value }}{{ end }}
{{ end }}`
var clustersTmplSrc = fmt.Sprintln(clustersTmplHeaderSrc) + clustersTmplRowSrc

Expand Down
22 changes: 15 additions & 7 deletions pkg/kotsclient/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
)

type CreateClusterRequest struct {
Name string `json:"name"`
KubernetesDistribution string `json:"kubernetes_distribution"`
KubernetesVersion string `json:"kubernetes_version"`
NodeCount int `json:"node_count"`
DiskGiB int64 `json:"disk_gib"`
TTL string `json:"ttl"`
InstanceType string `json:"instance_type"`
Name string `json:"name"`
KubernetesDistribution string `json:"kubernetes_distribution"`
KubernetesVersion string `json:"kubernetes_version"`
NodeCount int `json:"node_count"`
DiskGiB int64 `json:"disk_gib"`
TTL string `json:"ttl"`
InstanceType string `json:"instance_type"`
Tags []ClusterTag `json:"tags"`
}

type CreateClusterResponse struct {
Expand All @@ -26,6 +27,11 @@ type CreateClusterResponse struct {
SupportedDistributions map[string]string `json:"supported_distributions"`
}

type ClusterTag struct {
Key string `json:"key"`
Value string `json:"value"`
}

type CreateClusterOpts struct {
Name string
KubernetesDistribution string
Expand All @@ -34,6 +40,7 @@ type CreateClusterOpts struct {
DiskGiB int64
TTL string
InstanceType string
Tags []ClusterTag
DryRun bool
}

Expand Down Expand Up @@ -64,6 +71,7 @@ func (c *VendorV3Client) CreateCluster(opts CreateClusterOpts) (*types.Cluster,
DiskGiB: opts.DiskGiB,
TTL: opts.TTL,
InstanceType: opts.InstanceType,
Tags: opts.Tags,
}

if opts.DryRun {
Expand Down
7 changes: 7 additions & 0 deletions pkg/types/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const (
ClusterStatusDeleted ClusterStatus = "deleted"
)

type ClusterTag struct {
Key string `json:"key"`
Value string `json:"value"`
}

type Cluster struct {
ID string `json:"id"`
Name string `json:"name"`
Expand All @@ -28,6 +33,8 @@ type Cluster struct {
Status ClusterStatus `json:"status"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`

Tags []ClusterTag `json:"tags"`
}

type ClusterVersion struct {
Expand Down

0 comments on commit 5581cbc

Please sign in to comment.