Skip to content

Commit

Permalink
Refactor addon args into runnerArgs
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Squizzato <[email protected]>
  • Loading branch information
squizzi committed Dec 30, 2024
1 parent f34298c commit 67b0efc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
38 changes: 14 additions & 24 deletions cli/cmd/cluster_addon_create_objectstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ import (
"github.com/spf13/cobra"
)

type clusterAddonCreateObjectStoreArgs struct {
objectStoreBucket string
clusterID string
waitDuration time.Duration
dryRun bool
outputFormat string
}

func (r *runners) InitClusterAddonCreateObjectStore(parent *cobra.Command) *cobra.Command {
args := clusterAddonCreateObjectStoreArgs{}

cmd := &cobra.Command{
Use: "object-store CLUSTER_ID --bucket-prefix BUCKET_PREFIX",
Short: "Create an object store bucket for a cluster.",
Expand All @@ -48,40 +38,40 @@ Examples:
05929b24 Object Store pending {"bucket_prefix":"mybucket"}`,
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, cmdArgs []string) error {
args.clusterID = cmdArgs[0]
return r.clusterAddonCreateObjectStoreCreateRun(args)
r.args.clusterAddonCreateObjectStoreClusterID = cmdArgs[0]
return r.clusterAddonCreateObjectStoreCreateRun()
},
}
parent.AddCommand(cmd)

err := clusterAddonCreateObjectStoreFlags(cmd, &args)
err := r.clusterAddonCreateObjectStoreFlags(cmd)
if err != nil {
panic(err)
}

return cmd
}

func clusterAddonCreateObjectStoreFlags(cmd *cobra.Command, args *clusterAddonCreateObjectStoreArgs) error {
cmd.Flags().StringVar(&args.objectStoreBucket, "bucket-prefix", "", "A prefix for the bucket name to be created (required)")
func (r *runners) clusterAddonCreateObjectStoreFlags(cmd *cobra.Command) error {
cmd.Flags().StringVar(&r.args.clusterAddonCreateObjectStoreBucket, "bucket-prefix", "", "A prefix for the bucket name to be created (required)")
err := cmd.MarkFlagRequired("bucket-prefix")
if err != nil {
return err
}
cmd.Flags().DurationVar(&args.waitDuration, "wait", 0, "Wait duration for add-on to be ready before exiting (leave empty to not wait)")
cmd.Flags().BoolVar(&args.dryRun, "dry-run", false, "Simulate creation to verify that your inputs are valid without actually creating an add-on")
cmd.Flags().StringVar(&args.outputFormat, "output", "table", "The output format to use. One of: json|table|wide (default: table)")
cmd.Flags().DurationVar(&r.args.clusterAddonCreateObjectStoreDuration, "wait", 0, "Wait duration for add-on to be ready before exiting (leave empty to not wait)")
cmd.Flags().BoolVar(&r.args.clusterAddonCreateObjectStoreDryRun, "dry-run", false, "Simulate creation to verify that your inputs are valid without actually creating an add-on")
cmd.Flags().StringVar(&r.args.clusterAddonCreateObjectStoreOutput, "output", "table", "The output format to use. One of: json|table|wide (default: table)")
return nil
}

func (r *runners) clusterAddonCreateObjectStoreCreateRun(args clusterAddonCreateObjectStoreArgs) error {
func (r *runners) clusterAddonCreateObjectStoreCreateRun() error {
opts := kotsclient.CreateClusterAddonObjectStoreOpts{
ClusterID: args.clusterID,
Bucket: args.objectStoreBucket,
DryRun: args.dryRun,
ClusterID: r.args.clusterAddonCreateObjectStoreClusterID,
Bucket: r.args.clusterAddonCreateObjectStoreBucket,
DryRun: r.args.clusterAddonCreateObjectStoreDryRun,
}

addon, err := r.createAndWaitForClusterAddonCreateObjectStore(opts, args.waitDuration)
addon, err := r.createAndWaitForClusterAddonCreateObjectStore(opts, r.args.clusterAddonCreateObjectStoreDuration)
if err != nil {
if errors.Cause(err) == ErrWaitDurationExceeded {
defer func() {
Expand All @@ -97,7 +87,7 @@ func (r *runners) clusterAddonCreateObjectStoreCreateRun(args clusterAddonCreate
return err
}

return print.Addon(args.outputFormat, r.w, addon)
return print.Addon(r.args.clusterAddonCreateObjectStoreOutput, r.w, addon)
}

func (r *runners) createAndWaitForClusterAddonCreateObjectStore(opts kotsclient.CreateClusterAddonObjectStoreOpts, waitDuration time.Duration) (*types.ClusterAddon, error) {
Expand Down
24 changes: 23 additions & 1 deletion cli/cmd/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ Use the '--dry-run' flag to simulate the creation process and get an estimated c
--ttl 24h
# Create a cluster with custom tags
replicated cluster create --distribution eks --version 1.21 --nodes 3 --tag env=test --tag project=demo --ttl 24h`,
replicated cluster create --distribution eks --version 1.21 --nodes 3 --tag env=test --tag project=demo --ttl 24h
# Create a cluster with addons
replicated cluster create --distribution eks --version 1.21 --nodes 3 --addon object-store --ttl 24h`,
SilenceUsage: true,
RunE: r.createCluster,
Args: cobra.NoArgs,
Expand All @@ -68,6 +71,8 @@ Use the '--dry-run' flag to simulate the creation process and get an estimated c

cmd.Flags().StringArrayVar(&r.args.createClusterTags, "tag", []string{}, "Tag to apply to the cluster (key=value format, can be specified multiple times)")

cmd.Flags().StringArrayVar(&r.args.createClusterAddons, "addon", []string{}, "Addons to install on the cluster (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|wide (default: table)")
Expand Down Expand Up @@ -137,6 +142,23 @@ func (r *runners) createCluster(_ *cobra.Command, args []string) error {
}
}

if r.args.createClusterAddons != nil {
for _, addon := range r.args.createClusterAddons {
if addon == "object-store" {
err := r.clusterAddonCreateObjectStoreCreateRun(clusterAddonCreateObjectStoreArgs{

Check failure on line 148 in cli/cmd/cluster_create.go

View workflow job for this annotation

GitHub Actions / make-tests

undefined: clusterAddonCreateObjectStoreArgs

Check failure on line 148 in cli/cmd/cluster_create.go

View workflow job for this annotation

GitHub Actions / make-tests

too many arguments in call to r.clusterAddonCreateObjectStoreCreateRun

Check failure on line 148 in cli/cmd/cluster_create.go

View workflow job for this annotation

GitHub Actions / make-build

undefined: clusterAddonCreateObjectStoreArgs

Check failure on line 148 in cli/cmd/cluster_create.go

View workflow job for this annotation

GitHub Actions / make-build

too many arguments in call to r.clusterAddonCreateObjectStoreCreateRun
clusterID: cl.ID,
dryRun: r.args.createClusterDryRun,
waitDuration: r.args.createClusterWaitDuration,
})
if err != nil {
return err
}
}
}
}

r.createAndWaitForClusterAddonCreateObjectStore()

Check failure on line 160 in cli/cmd/cluster_create.go

View workflow job for this annotation

GitHub Actions / make-tests

not enough arguments in call to r.createAndWaitForClusterAddonCreateObjectStore

Check failure on line 160 in cli/cmd/cluster_create.go

View workflow job for this annotation

GitHub Actions / make-build

not enough arguments in call to r.createAndWaitForClusterAddonCreateObjectStore

if opts.DryRun {
estimatedCostMessage := fmt.Sprintf("Estimated cost: %s (if run to TTL of %s)", print.CreditsToDollarsDisplay(cl.EstimatedCost), cl.TTL)
_, err := fmt.Fprintln(r.w, estimatedCostMessage)
Expand Down
7 changes: 7 additions & 0 deletions cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type runnerArgs struct {
createClusterInstanceType string
createClusterNodeGroups []string
createClusterTags []string
createClusterAddons []string

upgradeClusterKubernetesVersion string
upgradeClusterDryRun bool
Expand Down Expand Up @@ -247,4 +248,10 @@ type runnerArgs struct {
lsNetworkStartTime string
lsNetworkEndTime string
lsNetworkWatch bool

clusterAddonCreateObjectStoreBucket string
clusterAddonCreateObjectStoreClusterID string
clusterAddonCreateObjectStoreDuration time.Duration
clusterAddonCreateObjectStoreDryRun bool
clusterAddonCreateObjectStoreOutput string
}

0 comments on commit 67b0efc

Please sign in to comment.