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

feat(cli): add --cluster flag to task run #2164

Merged
merged 8 commits into from
Apr 15, 2021
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
3 changes: 3 additions & 0 deletions internal/pkg/cli/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const (
imageFlag = "image"
taskRoleFlag = "task-role"
executionRoleFlag = "execution-role"
clusterFlag = "cluster"
subnetsFlag = "subnets"
securityGroupsFlag = "security-groups"
envVarsFlag = "env-vars"
Expand Down Expand Up @@ -133,6 +134,8 @@ Mutually exclusive with -%s, --%s`, imageFlagShort, imageFlag)
wkldTypeFlagDescription = fmt.Sprintf(`Type of job or svc to create. Must be one of:
%s`, strings.Join(template.QuoteSliceFunc(manifest.WorkloadTypes), ", "))

clusterFlagDescription = fmt.Sprintf(`Optional. The short name or full ARN of the cluster to run the task in.
Cannot be specified with '%s', '%s' or '%s'.`, appFlag, envFlag, taskDefaultFlag)
subnetsFlagDescription = fmt.Sprintf(`Optional. The subnet IDs for the task to use. Can be specified multiple times.
Cannot be specified with '%s', '%s' or '%s'.`, appFlag, envFlag, taskDefaultFlag)
securityGroupsFlagDescription = fmt.Sprintf(`Optional. The security group IDs for the task to use. Can be specified multiple times.
Expand Down
49 changes: 38 additions & 11 deletions internal/pkg/cli/task_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ type runTaskVars struct {
taskRole string
executionRole string

subnets []string
securityGroups []string
env string
appName string
useDefaultSubnets bool
cluster string
subnets []string
securityGroups []string
env string
appName string
useDefaultSubnetsAndCluster bool

envVars map[string]string
secrets map[string]string
Expand Down Expand Up @@ -200,10 +201,11 @@ func (o *runTaskOpts) configureRunner() (taskRunner, error) {
}, nil
}

return &task.NetworkConfigRunner{
return &task.ConfigRunner{
Count: o.count,
GroupName: o.groupName,

Cluster: o.cluster,
Subnets: o.subnets,
SecurityGroups: o.securityGroups,

Expand Down Expand Up @@ -273,6 +275,10 @@ func (o *runTaskOpts) Validate() error {
}
}

if err := o.validateFlagsWithCluster(); err != nil {
return err
}

if err := o.validateFlagsWithDefaultCluster(); err != nil {
return err
}
Expand Down Expand Up @@ -300,8 +306,28 @@ func (o *runTaskOpts) Validate() error {
return nil
}

func (o *runTaskOpts) validateFlagsWithCluster() error {
if o.cluster == "" {
return nil
}

if o.appName != "" {
return fmt.Errorf("cannot specify both `--app` and `--cluster`")
}

if o.env != "" {
return fmt.Errorf("cannot specify both `--env` and `--cluster`")
}

if o.useDefaultSubnetsAndCluster {
return fmt.Errorf("cannot specify both `--default` and `--cluster`")
}

return nil
}

func (o *runTaskOpts) validateFlagsWithDefaultCluster() error {
if !o.useDefaultSubnets {
if !o.useDefaultSubnetsAndCluster {
return nil
}

Expand All @@ -325,7 +351,7 @@ func (o *runTaskOpts) validateFlagsWithSubnets() error {
return nil
}

if o.useDefaultSubnets {
if o.useDefaultSubnetsAndCluster {
return fmt.Errorf("cannot specify both `--subnets` and `--default`")
}

Expand Down Expand Up @@ -371,7 +397,7 @@ func (o *runTaskOpts) Ask() error {
func (o *runTaskOpts) shouldPromptForAppEnv() bool {
// NOTE: if security groups are specified but subnets are not, then we use the default subnets with the
// specified security groups.
useDefault := o.useDefaultSubnets || (o.securityGroups != nil && o.subnets == nil)
useDefault := o.useDefaultSubnetsAndCluster || (o.securityGroups != nil && o.subnets == nil && o.cluster == "")
useConfig := o.subnets != nil

// if user hasn't specified that they want to use the default subnets, and that they didn't provide specific subnets
Expand Down Expand Up @@ -399,7 +425,7 @@ func (o *runTaskOpts) Execute() error {
return err
}

if o.env == "" {
if o.env == "" && o.cluster == "" {
hasDefaultCluster, err := o.defaultClusterGetter.HasDefaultCluster()
if err != nil {
return fmt.Errorf(`find "default" cluster to deploy the task to: %v`, err)
Expand Down Expand Up @@ -700,9 +726,10 @@ Run a task with a command.

cmd.Flags().StringVar(&vars.appName, appFlag, "", taskAppFlagDescription)
cmd.Flags().StringVar(&vars.env, envFlag, "", taskEnvFlagDescription)
cmd.Flags().StringVar(&vars.cluster, clusterFlag, "", clusterFlagDescription)
cmd.Flags().StringSliceVar(&vars.subnets, subnetsFlag, nil, subnetsFlagDescription)
cmd.Flags().StringSliceVar(&vars.securityGroups, securityGroupsFlag, nil, securityGroupsFlagDescription)
cmd.Flags().BoolVar(&vars.useDefaultSubnets, taskDefaultFlag, false, taskRunDefaultFlagDescription)
cmd.Flags().BoolVar(&vars.useDefaultSubnetsAndCluster, taskDefaultFlag, false, taskRunDefaultFlagDescription)

cmd.Flags().StringToStringVar(&vars.envVars, envVarsFlag, nil, envVarsFlagDescription)
cmd.Flags().StringToStringVar(&vars.secrets, secretsFlag, nil, secretsFlagDescription)
Expand Down
93 changes: 70 additions & 23 deletions internal/pkg/cli/task_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestTaskRunOpts_Validate(t *testing.T) {
inTaskRole string

inEnv string
inCluster string
inSubnets []string
inSecurityGroups []string

Expand Down Expand Up @@ -277,6 +278,30 @@ func TestTaskRunOpts_Validate(t *testing.T) {

wantedError: errors.New("cannot specify both `--subnets` and `--default`"),
},
"both cluster and default specified": {
basicOpts: defaultOpts,

inDefault: true,
inCluster: "special-cluster",

wantedError: errors.New("cannot specify both `--default` and `--cluster`"),
},
"both cluster and application specified": {
basicOpts: defaultOpts,

inCluster: "special-cluster",
appName: "my-app",

wantedError: errors.New("cannot specify both `--app` and `--cluster`"),
},
"both cluster and environment specified": {
basicOpts: defaultOpts,

inCluster: "special-cluster",
inEnv: "my-env",

wantedError: errors.New("cannot specify both `--env` and `--cluster`"),
},
}

for name, tc := range testCases {
Expand All @@ -288,22 +313,23 @@ func TestTaskRunOpts_Validate(t *testing.T) {

opts := runTaskOpts{
runTaskVars: runTaskVars{
appName: tc.appName,
count: tc.inCount,
cpu: tc.inCPU,
memory: tc.inMemory,
groupName: tc.inName,
image: tc.inImage,
env: tc.inEnv,
taskRole: tc.inTaskRole,
subnets: tc.inSubnets,
securityGroups: tc.inSecurityGroups,
dockerfilePath: tc.inDockerfilePath,
envVars: tc.inEnvVars,
secrets: tc.inSecrets,
command: tc.inCommand,
entrypoint: tc.inEntryPoint,
useDefaultSubnets: tc.inDefault,
appName: tc.appName,
count: tc.inCount,
cpu: tc.inCPU,
memory: tc.inMemory,
groupName: tc.inName,
image: tc.inImage,
env: tc.inEnv,
taskRole: tc.inTaskRole,
cluster: tc.inCluster,
subnets: tc.inSubnets,
securityGroups: tc.inSecurityGroups,
dockerfilePath: tc.inDockerfilePath,
envVars: tc.inEnvVars,
secrets: tc.inSecrets,
command: tc.inCommand,
entrypoint: tc.inEntryPoint,
useDefaultSubnetsAndCluster: tc.inDefault,
},
isDockerfileSet: tc.isDockerfileSet,

Expand All @@ -319,7 +345,6 @@ func TestTaskRunOpts_Validate(t *testing.T) {
}

err := opts.Validate()

if tc.wantedError != nil {
require.EqualError(t, tc.wantedError, err.Error())
} else {
Expand All @@ -333,6 +358,7 @@ func TestTaskRunOpts_Ask(t *testing.T) {
testCases := map[string]struct {
inName string

inCluster string
inSubnets []string
inSecurityGroups []string

Expand Down Expand Up @@ -437,6 +463,16 @@ func TestTaskRunOpts_Ask(t *testing.T) {
m.EXPECT().Environment(taskRunEnvPrompt, gomock.Any(), gomock.Any(), appEnvOptionNone).AnyTimes()
},
},
"don't prompt for app if cluster is specified": {
inCluster: "cluster-1",
mockPrompt: func(m *mocks.Mockprompter) {
m.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
},
mockSel: func(m *mocks.MockappEnvSelector) {
m.EXPECT().Application(taskRunAppPrompt, gomock.Any(), gomock.Any()).AnyTimes()
m.EXPECT().Environment(taskRunEnvPrompt, gomock.Any(), gomock.Any(), appEnvOptionNone).Times(0)
},
},
"don't prompt for env if subnets are specified": {
inSubnets: []string{"subnet-1"},
mockPrompt: func(m *mocks.Mockprompter) {
Expand All @@ -447,6 +483,16 @@ func TestTaskRunOpts_Ask(t *testing.T) {
m.EXPECT().Environment(taskRunEnvPrompt, gomock.Any(), gomock.Any(), appEnvOptionNone).Times(0)
},
},
"don't prompt for env if cluster is specified": {
inCluster: "cluster-1",
mockPrompt: func(m *mocks.Mockprompter) {
m.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
},
mockSel: func(m *mocks.MockappEnvSelector) {
m.EXPECT().Application(taskRunAppPrompt, gomock.Any(), gomock.Any()).AnyTimes()
m.EXPECT().Environment(taskRunEnvPrompt, gomock.Any(), gomock.Any(), appEnvOptionNone).Times(0)
},
},
"don't prompt for app if security groups are specified": {
inSecurityGroups: []string{"sg-1"},
mockPrompt: func(m *mocks.Mockprompter) {
Expand Down Expand Up @@ -528,12 +574,13 @@ func TestTaskRunOpts_Ask(t *testing.T) {

opts := runTaskOpts{
runTaskVars: runTaskVars{
appName: tc.appName,
groupName: tc.inName,
env: tc.inEnv,
useDefaultSubnets: tc.inDefault,
subnets: tc.inSubnets,
securityGroups: tc.inSecurityGroups,
appName: tc.appName,
groupName: tc.inName,
env: tc.inEnv,
useDefaultSubnetsAndCluster: tc.inDefault,
subnets: tc.inSubnets,
securityGroups: tc.inSecurityGroups,
cluster: tc.inCluster,
},
sel: mockSel,
}
Expand Down
29 changes: 18 additions & 11 deletions internal/pkg/task/config_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ const (
fmtErrDefaultSubnets = "get default subnet IDs: %w"
)

// NetworkConfigRunner runs an Amazon ECS task in the subnets, security groups, and the default cluster.
type NetworkConfigRunner struct {
// ConfigRunner runs an Amazon ECS task in the subnets, security groups, and cluster.
// It uses the default subnets and the default cluster if the corresponding field is empty.
type ConfigRunner struct {
// Count of the tasks to be launched.
Count int
// Group Name of the tasks that use the same task definition.
GroupName string

// The ARN of the cluster to run the task.
Cluster string

// Network configuration
Subnets []string
SecurityGroups []string
Expand All @@ -33,18 +37,22 @@ type NetworkConfigRunner struct {
VPCGetter VPCGetter
}

// Run runs tasks in the subnets and the security groups, and returns the tasks.
// Run runs tasks given subnets, security groups and the cluster, and returns the tasks.
// If subnets are not provided, it uses the default subnets.
func (r *NetworkConfigRunner) Run() ([]*Task, error) {
// If cluster is not provided, it uses the default cluster.
func (r *ConfigRunner) Run() ([]*Task, error) {
if err := r.validateDependencies(); err != nil {
return nil, err
}

cluster, err := r.ClusterGetter.DefaultCluster()
if err != nil {
return nil, &errGetDefaultCluster{
parentErr: err,
if r.Cluster == "" {
cluster, err := r.ClusterGetter.DefaultCluster()
if err != nil {
return nil, &errGetDefaultCluster{
parentErr: err,
}
}
r.Cluster = cluster
}

if r.Subnets == nil {
Expand All @@ -55,12 +63,11 @@ func (r *NetworkConfigRunner) Run() ([]*Task, error) {
if len(subnets) == 0 {
return nil, errNoSubnetFound
}

r.Subnets = subnets
}

ecsTasks, err := r.Starter.RunTask(ecs.RunTaskInput{
Cluster: cluster,
Cluster: r.Cluster,
Count: r.Count,
Subnets: r.Subnets,
SecurityGroups: r.SecurityGroups,
Expand All @@ -77,7 +84,7 @@ func (r *NetworkConfigRunner) Run() ([]*Task, error) {
return convertECSTasks(ecsTasks), nil
}

func (r *NetworkConfigRunner) validateDependencies() error {
func (r *ConfigRunner) validateDependencies() error {
if r.ClusterGetter == nil {
return errClusterGetterNil
}
Expand Down
Loading