Remove Cluster from context

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-07-16 08:28:17 +02:00
parent 37b9e74308
commit efeded2670
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
11 changed files with 90 additions and 95 deletions

View File

@ -45,7 +45,7 @@ func (o upOptions) LoadBalancerArn() *string {
func ConvertCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.Command { func ConvertCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "convert", Use: "convert",
RunE: docker.WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error { RunE: WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error {
opts := options.WithOsEnv() opts := options.WithOsEnv()
project, err := cli.ProjectFromOptions(&opts) project, err := cli.ProjectFromOptions(&opts)
if err != nil { if err != nil {
@ -72,7 +72,7 @@ func UpCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.Comman
opts := upOptions{} opts := upOptions{}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "up", Use: "up",
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error { RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
return backend.Up(context.Background(), *options) return backend.Up(context.Background(), *options)
}), }),
} }
@ -84,7 +84,7 @@ func PsCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.Comman
opts := upOptions{} opts := upOptions{}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "ps", Use: "ps",
RunE: docker.WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error { RunE: WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error {
status, err := backend.Ps(context.Background(), *options) status, err := backend.Ps(context.Background(), *options)
if err != nil { if err != nil {
return err return err
@ -109,7 +109,7 @@ func DownCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.
opts := downOptions{} opts := downOptions{}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "down", Use: "down",
RunE: docker.WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error { RunE: WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error {
return backend.Down(context.Background(), *projectOpts) return backend.Down(context.Background(), *projectOpts)
}), }),
} }
@ -120,7 +120,7 @@ func DownCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.
func LogsCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.Command { func LogsCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "logs [PROJECT NAME]", Use: "logs [PROJECT NAME]",
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error { RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
return backend.Logs(context.Background(), *projectOpts) return backend.Logs(context.Background(), *projectOpts)
}), }),
} }

View File

@ -0,0 +1,31 @@
package commands
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/docker/cli/cli/command"
amazon "github.com/docker/ecs-plugin/pkg/amazon/backend"
"github.com/docker/ecs-plugin/pkg/docker"
"github.com/spf13/cobra"
)
type ContextFunc func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error
func WithAwsContext(dockerCli command.Cli, f ContextFunc) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
ctx, err := docker.GetAwsContext(dockerCli)
if err != nil {
return err
}
backend, err := amazon.NewBackend(ctx.Profile, ctx.Region)
if err != nil {
return err
}
err = f(*ctx, backend, args)
if e, ok := err.(awserr.Error); ok {
return fmt.Errorf(e.Message())
}
return err
}
}

View File

@ -47,7 +47,7 @@ func CreateSecret(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "create NAME", Use: "create NAME",
Short: "Creates a secret.", Short: "Creates a secret.",
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error { RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
if len(args) == 0 { if len(args) == 0 {
return errors.New("Missing mandatory parameter: NAME") return errors.New("Missing mandatory parameter: NAME")
} }
@ -69,7 +69,7 @@ func InspectSecret(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "inspect ID", Use: "inspect ID",
Short: "Displays secret details", Short: "Displays secret details",
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error { RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
if len(args) == 0 { if len(args) == 0 {
return errors.New("Missing mandatory parameter: ID") return errors.New("Missing mandatory parameter: ID")
} }
@ -94,7 +94,7 @@ func ListSecrets(dockerCli command.Cli) *cobra.Command {
Use: "list", Use: "list",
Aliases: []string{"ls"}, Aliases: []string{"ls"},
Short: "List secrets stored for the existing account.", Short: "List secrets stored for the existing account.",
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error { RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
secrets, err := backend.ListSecrets(context.Background()) secrets, err := backend.ListSecrets(context.Background())
if err != nil { if err != nil {
return err return err
@ -113,7 +113,7 @@ func DeleteSecret(dockerCli command.Cli) *cobra.Command {
Use: "delete NAME", Use: "delete NAME",
Aliases: []string{"rm", "remove"}, Aliases: []string{"rm", "remove"},
Short: "Removes a secret.", Short: "Removes a secret.",
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error { RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
if len(args) == 0 { if len(args) == 0 {
return errors.New("Missing mandatory parameter: [NAME]") return errors.New("Missing mandatory parameter: [NAME]")
} }

View File

@ -22,17 +22,18 @@ const enterLabelPrefix = "Enter "
type setupOptions struct { type setupOptions struct {
name string name string
context contextStore.AwsContext profile string
region string
accessKeyID string accessKeyID string
secretAccessKey string secretAccessKey string
} }
func (s setupOptions) unsetRequiredArgs() []string { func (s setupOptions) unsetRequiredArgs() []string {
unset := []string{} unset := []string{}
if s.context.Profile == "" { if s.profile == "" {
unset = append(unset, "profile") unset = append(unset, "profile")
} }
if s.context.Region == "" { if s.region == "" {
unset = append(unset, "region") unset = append(unset, "region")
} }
return unset return unset
@ -51,25 +52,28 @@ func SetupCommand() *cobra.Command {
} }
} }
if opts.accessKeyID != "" && opts.secretAccessKey != "" { if opts.accessKeyID != "" && opts.secretAccessKey != "" {
if err := saveCredentials(opts.context.Profile, opts.accessKeyID, opts.secretAccessKey); err != nil { if err := saveCredentials(opts.profile, opts.accessKeyID, opts.secretAccessKey); err != nil {
return err return err
} }
} }
backend, err := amazon.NewBackend(opts.context.Profile, opts.context.Cluster, opts.context.Region) backend, err := amazon.NewBackend(opts.profile, opts.region)
if err != nil { if err != nil {
return err return err
} }
_, _, err = backend.CreateContextData(context.Background(), nil)
context, _, err := backend.CreateContextData(context.Background(), map[string]string{
amazon.ContextParamProfile: opts.profile,
amazon.ContextParamRegion: opts.region,
})
if err != nil { if err != nil {
return err return err
} }
return contextStore.NewContext(opts.name, &opts.context) return contextStore.NewContext(opts.name, context)
}, },
} }
cmd.Flags().StringVarP(&opts.name, "name", "n", "ecs", "Context Name") cmd.Flags().StringVarP(&opts.name, "name", "n", "ecs", "Context Name")
cmd.Flags().StringVarP(&opts.context.Profile, "profile", "p", "", "AWS Profile") cmd.Flags().StringVarP(&opts.profile, "profile", "p", "", "AWS Profile")
cmd.Flags().StringVarP(&opts.context.Cluster, "cluster", "c", "", "ECS cluster") cmd.Flags().StringVarP(&opts.region, "region", "r", "", "AWS region")
cmd.Flags().StringVarP(&opts.context.Region, "region", "r", "", "AWS region")
cmd.Flags().StringVarP(&opts.accessKeyID, "aws-key-id", "k", "", "AWS Access Key ID") cmd.Flags().StringVarP(&opts.accessKeyID, "aws-key-id", "k", "", "AWS Access Key ID")
cmd.Flags().StringVarP(&opts.secretAccessKey, "aws-secret-key", "s", "", "AWS Secret Access Key") cmd.Flags().StringVarP(&opts.secretAccessKey, "aws-secret-key", "s", "", "AWS Secret Access Key")
@ -88,10 +92,6 @@ func interactiveCli(opts *setupOptions) error {
return err return err
} }
if err := setCluster(opts, err); err != nil {
return err
}
if err := setRegion(opts, section); err != nil { if err := setRegion(opts, section); err != nil {
return err return err
} }
@ -156,7 +156,7 @@ func awsProfiles(filename string) (map[string]ini.Section, error) {
} }
func setContextName(opts *setupOptions) error { func setContextName(opts *setupOptions) error {
if opts.name == "aws" { if opts.name == "ecs" {
result, err := promptString(opts.name, "context name", enterLabelPrefix, 2) result, err := promptString(opts.name, "context name", enterLabelPrefix, 2)
if err != nil { if err != nil {
return err return err
@ -171,7 +171,7 @@ func setProfile(opts *setupOptions, section ini.Section) (ini.Section, error) {
if err != nil { if err != nil {
return ini.Section{}, err return ini.Section{}, err
} }
section, ok := profilesList[opts.context.Profile] section, ok := profilesList[opts.profile]
if !ok { if !ok {
prompt := promptui.Select{ prompt := promptui.Select{
Label: "Select AWS Profile", Label: "Select AWS Profile",
@ -179,14 +179,14 @@ func setProfile(opts *setupOptions, section ini.Section) (ini.Section, error) {
} }
_, result, err := prompt.Run() _, result, err := prompt.Run()
if result == "new profile" { if result == "new profile" {
result, err := promptString(opts.context.Profile, "profile name", enterLabelPrefix, 2) result, err := promptString(opts.profile, "profile name", enterLabelPrefix, 2)
if err != nil { if err != nil {
return ini.Section{}, err return ini.Section{}, err
} }
opts.context.Profile = result opts.profile = result
} else { } else {
section = profilesList[result] section = profilesList[result]
opts.context.Profile = result opts.profile = result
} }
if err != nil { if err != nil {
return ini.Section{}, err return ini.Section{}, err
@ -196,7 +196,7 @@ func setProfile(opts *setupOptions, section ini.Section) (ini.Section, error) {
} }
func setRegion(opts *setupOptions, section ini.Section) error { func setRegion(opts *setupOptions, section ini.Section) error {
defaultRegion := opts.context.Region defaultRegion := opts.region
if defaultRegion == "" && section.Name() != "" { if defaultRegion == "" && section.Name() != "" {
region, err := section.GetKey("region") region, err := section.GetKey("region")
if err == nil { if err == nil {
@ -207,17 +207,7 @@ func setRegion(opts *setupOptions, section ini.Section) error {
if err != nil { if err != nil {
return err return err
} }
opts.context.Region = result opts.region = result
return nil
}
func setCluster(opts *setupOptions, err error) error {
result, err := promptString(opts.context.Cluster, "cluster name", enterLabelPrefix, 0)
if err != nil {
return err
}
opts.context.Cluster = result
return nil return nil
} }

View File

@ -6,7 +6,7 @@ import (
"github.com/docker/ecs-plugin/pkg/amazon/sdk" "github.com/docker/ecs-plugin/pkg/amazon/sdk"
) )
func NewBackend(profile string, cluster string, region string) (*Backend, error) { func NewBackend(profile string, region string) (*Backend, error) {
sess, err := session.NewSessionWithOptions(session.Options{ sess, err := session.NewSessionWithOptions(session.Options{
Profile: profile, Profile: profile,
Config: aws.Config{ Config: aws.Config{
@ -17,14 +17,12 @@ func NewBackend(profile string, cluster string, region string) (*Backend, error)
return nil, err return nil, err
} }
return &Backend{ return &Backend{
Cluster: cluster,
Region: region, Region: region,
api: sdk.NewAPI(sess), api: sdk.NewAPI(sess),
}, nil }, nil
} }
type Backend struct { type Backend struct {
Cluster string
Region string Region string
api sdk.API api sdk.API
} }

View File

@ -21,7 +21,7 @@ import (
func TestSimpleConvert(t *testing.T) { func TestSimpleConvert(t *testing.T) {
project := load(t, "testdata/input/simple-single-service.yaml") project := load(t, "testdata/input/simple-single-service.yaml")
result := convertResultAsString(t, project, "TestCluster") result := convertResultAsString(t, project)
expected := "simple/simple-cloudformation-conversion.golden" expected := "simple/simple-cloudformation-conversion.golden"
golden.Assert(t, result, expected) golden.Assert(t, result, expected)
} }
@ -197,10 +197,10 @@ services:
} }
} }
func convertResultAsString(t *testing.T, project *types.Project, clusterName string) string { func convertResultAsString(t *testing.T, project *types.Project) string {
client, err := NewBackend("", clusterName, "") backend, err := NewBackend("", "")
assert.NilError(t, err) assert.NilError(t, err)
result, err := client.Convert(project) result, err := backend.Convert(project)
assert.NilError(t, err) assert.NilError(t, err)
resultAsJSON, err := result.JSON() resultAsJSON, err := result.JSON()
assert.NilError(t, err) assert.NilError(t, err)

View File

@ -2,7 +2,13 @@ package backend
import ( import (
"context" "context"
"fmt"
"github.com/docker/ecs-plugin/pkg/docker"
)
const (
ContextParamRegion = "region"
ContextParamProfile = "profile"
) )
func (b *Backend) CreateContextData(ctx context.Context, params map[string]string) (contextData interface{}, description string, err error) { func (b *Backend) CreateContextData(ctx context.Context, params map[string]string) (contextData interface{}, description string, err error) {
@ -11,14 +17,8 @@ func (b *Backend) CreateContextData(ctx context.Context, params map[string]strin
return "", "", err return "", "", err
} }
if b.Cluster != "" { return docker.AwsContext{
exists, err := b.api.ClusterExists(ctx, b.Cluster) Profile: params[ContextParamProfile],
if err != nil { Region: params[ContextParamRegion],
return "", "", err }, "Amazon ECS context", nil
}
if !exists {
return "", "", fmt.Errorf("cluster %s does not exists", b.Cluster)
}
}
return "", "", nil
} }

View File

@ -9,12 +9,12 @@ import (
) )
func (b *Backend) Down(ctx context.Context, options cli.ProjectOptions) error { func (b *Backend) Down(ctx context.Context, options cli.ProjectOptions) error {
name, err2 := b.projectName(options) name, err := b.projectName(options)
if err2 != nil { if err != nil {
return err2 return err
} }
err := b.api.DeleteStack(ctx, name) err = b.api.DeleteStack(ctx, name)
if err != nil { if err != nil {
return err return err
} }

View File

@ -123,5 +123,5 @@ func (b Backend) GetCluster(ctx context.Context, project *types.Project) (string
} }
return cluster, nil return cluster, nil
} }
return b.Cluster, nil return "", nil
} }

View File

@ -3,13 +3,10 @@ package docker
import ( import (
"fmt" "fmt"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
cliconfig "github.com/docker/cli/cli/config" cliconfig "github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/context/store" "github.com/docker/cli/cli/context/store"
amazon "github.com/docker/ecs-plugin/pkg/amazon/backend"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
) )
const contextType = "aws" const contextType = "aws"
@ -24,16 +21,15 @@ func getter() interface{} {
type AwsContext struct { type AwsContext struct {
Profile string Profile string
Cluster string
Region string Region string
} }
func NewContext(name string, awsContext *AwsContext) error { func NewContext(name string, awsContext interface{}) error {
_, err := NewContextWithStore(name, awsContext, cliconfig.ContextStoreDir()) _, err := NewContextWithStore(name, awsContext.(AwsContext), cliconfig.ContextStoreDir())
return err return err
} }
func NewContextWithStore(name string, awsContext *AwsContext, contextDirectory string) (store.Store, error) { func NewContextWithStore(name string, awsContext AwsContext, contextDirectory string) (store.Store, error) {
contextStore := initContextStore(contextDirectory) contextStore := initContextStore(contextDirectory)
endpoints := map[string]interface{}{ endpoints := map[string]interface{}{
"aws": awsContext, "aws": awsContext,
@ -74,26 +70,6 @@ func checkAwsContextExists(contextName string) (*AwsContext, error) {
return &awsContext, nil return &awsContext, nil
} }
type ContextFunc func(ctx AwsContext, backend *amazon.Backend, args []string) error
func WithAwsContext(dockerCli command.Cli, f ContextFunc) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
ctx, err := GetAwsContext(dockerCli)
if err != nil {
return err
}
backend, err := amazon.NewBackend(ctx.Profile, ctx.Cluster, ctx.Region)
if err != nil {
return err
}
err = f(*ctx, backend, args)
if e, ok := err.(awserr.Error); ok {
return fmt.Errorf(e.Message())
}
return err
}
}
func GetAwsContext(dockerCli command.Cli) (*AwsContext, error) { func GetAwsContext(dockerCli command.Cli) (*AwsContext, error) {
contextName := dockerCli.CurrentContext() contextName := dockerCli.CurrentContext()
return checkAwsContextExists(contextName) return checkAwsContextExists(contextName)

View File

@ -58,7 +58,7 @@ func (d dockerCliCommand) createTestCmd(ops ...ConfigFileOperator) (icmd.Cmd, fu
Profile: "sandbox.devtools.developer", Profile: "sandbox.devtools.developer",
Region: "eu-west-3", Region: "eu-west-3",
} }
testStore, err := docker.NewContextWithStore(testContextName, &awsContext, filepath.Join(configDir, "contexts")) testStore, err := docker.NewContextWithStore(testContextName, awsContext, filepath.Join(configDir, "contexts"))
if err != nil { if err != nil {
panic(err) panic(err)
} }