Repository: docker/compose-cli
Pull request #817 (2020-10-20 08:29:33)

Configure autoscaling parameters
**What I did**
Introduce a struct to support autoscaling parameters on x-aws-autoscaling

**Related issue**
https://github.com/docker/compose-cli/issues/767

**(not mandatory) A picture of a cute animal, if possible in relation with what you did**
![image](https://user-images.githubusercontent.com/132757/96560862-21335d80-12bf-11eb-90a6-c74cbc5b3cc3.png)



Patch:
diff --git a/api/secrets/api.go b/api/secrets/api.go
index bf3315e1..681bed62 100644
--- a/api/secrets/api.go
+++ b/api/secrets/api.go
@@ -31,17 +31,21 @@ type Service interface {
 
 // Secret hold sensitive data
 type Secret struct {
-	ID      string            `json:"ID"`
-	Name    string            `json:"Name"`
-	Labels  map[string]string `json:"Tags"`
-	content []byte
+	ID          string            `json:"ID"`
+	Name        string            `json:"Name"`
+	Labels      map[string]string `json:"Labels"`
+	Description string            `json:"Description"`
+	username    string
+	password    string
 }
 
 // NewSecret builds a secret
-func NewSecret(name string, content []byte) Secret {
+func NewSecret(name, username, password, description string) Secret {
 	return Secret{
-		Name:    name,
-		content: content,
+		Name:        name,
+		username:    username,
+		password:    password,
+		Description: description,
 	}
 }
 
@@ -54,7 +58,15 @@ func (s Secret) ToJSON() (string, error) {
 	return string(b), nil
 }
 
-// GetContent returns a Secret's sensitive data
-func (s Secret) GetContent() []byte {
-	return s.content
+// GetCredString marshall a Secret's sensitive data into JSON string
+func (s Secret) GetCredString() (string, error) {
+	creds := map[string]string{
+		"username": s.username,
+		"password": s.password,
+	}
+	b, err := json.Marshal(&creds)
+	if err != nil {
+		return "", err
+	}
+	return string(b), nil
 }
diff --git a/cli/cmd/secrets.go b/cli/cmd/secrets.go
index 0fc14c58..3106e3dc 100644
--- a/cli/cmd/secrets.go
+++ b/cli/cmd/secrets.go
@@ -19,7 +19,6 @@ package cmd
 import (
 	"fmt"
 	"io"
-	"io/ioutil"
 	"os"
 
 	"github.com/spf13/cobra"
@@ -29,6 +28,13 @@ import (
 	"github.com/docker/compose-cli/formatter"
 )
 
+type createSecretOptions struct {
+	Label       string
+	Username    string
+	Password    string
+	Description string
+}
+
 // SecretCommand manage secrets
 func SecretCommand() *cobra.Command {
 	cmd := &cobra.Command{
@@ -46,39 +52,18 @@ func SecretCommand() *cobra.Command {
 }
 
 func createSecret() *cobra.Command {
+	opts := createSecretOptions{}
 	cmd := &cobra.Command{
-		Use:   "create [OPTIONS] SECRET [file|-]",
+		Use:   "create NAME",
 		Short: "Creates a secret.",
-		Args:  cobra.RangeArgs(1, 2),
+		Args:  cobra.ExactArgs(1),
 		RunE: func(cmd *cobra.Command, args []string) error {
 			c, err := client.New(cmd.Context())
 			if err != nil {
 				return err
 			}
-			file := "-"
-			if len(args) == 2 {
-				file = args[1]
-			}
-			if len(file) == 0 {
-				return fmt.Errorf("secret data source empty: %q", file)
-			}
-			var in io.ReadCloser
-			switch file {
-			case "-":
-				in = os.Stdin
-			default:
-				in, err = os.Open(file)
-				if err != nil {
-					return err
-				}
-				defer func() { _ = in.Close() }()
-			}
-			content, err := ioutil.ReadAll(in)
-			if err != nil {
-				return fmt.Errorf("failed to read content from %q: %v", file, err)
-			}
 			name := args[0]
-			secret := secrets.NewSecret(name, content)
+			secret := secrets.NewSecret(name, opts.Username, opts.Password, opts.Description)
 			id, err := c.SecretsService().CreateSecret(cmd.Context(), secret)
 			if err != nil {
 				return err
@@ -87,6 +72,10 @@ func createSecret() *cobra.Command {
 			return nil
 		},
 	}
+
+	cmd.Flags().StringVarP(&opts.Username, "username", "u", "", "username")
+	cmd.Flags().StringVarP(&opts.Password, "password", "p", "", "password")
+	cmd.Flags().StringVarP(&opts.Description, "description", "d", "", "Secret description")
 	return cmd
 }
 
@@ -146,7 +135,7 @@ func listSecrets() *cobra.Command {
 				for _, secret := range view {
 					_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", secret.ID, secret.Name, secret.Description)
 				}
-			}, "ID", "NAME")
+			}, "ID", "NAME", "DESCRIPTION")
 		},
 	}
 	cmd.Flags().StringVar(&opts.format, "format", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
@@ -164,8 +153,9 @@ func viewFromSecretList(secretList []secrets.Secret) []secretView {
 	retList := make([]secretView, len(secretList))
 	for i, s := range secretList {
 		retList[i] = secretView{
-			ID:   s.ID,
-			Name: s.Name,
+			ID:          s.ID,
+			Name:        s.Name,
+			Description: s.Description,
 		}
 	}
 	return retList
diff --git a/ecs/architecture.md b/ecs/architecture.md
index c0f2e0c5..113c4350 100644
--- a/ecs/architecture.md
+++ b/ecs/architecture.md
@@ -22,7 +22,7 @@ This diagram shows compose model and on same line AWS components that get create
   |       |  +---------+
   |       +--+ Deploy  |
   |       |  +---------+                    +-------------------+
-  |       |  x-aws-autoscaling  . . . . . . | ScalableTarget    |
+  |       |  x-aws-autoscale  . . . . . .   | ScalableTarget    |
   |       |                                 +-------------------+---+
   |       |                                     | ScalingPolicy     |
   |       |                                     +-------------------+-+
diff --git a/ecs/autoscaling.go b/ecs/autoscaling.go
index 7a40938d..927d614c 100644
--- a/ecs/autoscaling.go
+++ b/ecs/autoscaling.go
@@ -17,6 +17,7 @@
 package ecs
 
 import (
+	"encoding/json"
 	"fmt"
 
 	applicationautoscaling2 "github.com/aws/aws-sdk-go/service/applicationautoscaling"
@@ -26,13 +27,37 @@ import (
 	"github.com/compose-spec/compose-go/types"
 )
 
-func (b *ecsAPIService) createAutoscalingPolicy(project *types.Project, resources awsResources, template *cloudformation.Template, service types.ServiceConfig) {
+type autoscalingConfig struct {
+	Memory int `json:"memory,omitempty"`
+	CPU    int `json:"cpu,omitempty"`
+	Min    int `json:"min,omitempty"`
+	Max    int `json:"max,omitempty"`
+}
+
+func (b *ecsAPIService) createAutoscalingPolicy(project *types.Project, resources awsResources, template *cloudformation.Template, service types.ServiceConfig) error {
 	if service.Deploy == nil {
-		return
+		return nil
 	}
 	v, ok := service.Deploy.Extensions[extensionAutoScaling]
 	if !ok {
-		return
+		return nil
+	}
+
+	marshalled, err := json.Marshal(v)
+	if err != nil {
+		return err
+	}
+	var config autoscalingConfig
+	err = json.Unmarshal(marshalled, &config)
+	if err != nil {
+		return err
+	}
+
+	if config.Memory != 0 && config.CPU != 0 {
+		return fmt.Errorf("%s can't be set with both cpu and memory targets", extensionAutoScaling)
+	}
+	if config.Max == 0 {
+		return fmt.Errorf("%s MUST define max replicas", extensionAutoScaling)
 	}
 
 	role := fmt.Sprintf("%sAutoScalingRole", normalizeResourceName(service.Name))
@@ -66,8 +91,8 @@ func (b *ecsAPIService) createAutoscalingPolicy(project *types.Project, resource
 
 	target := fmt.Sprintf("%sScalableTarget", normalizeResourceName(service.Name))
 	template.Resources[target] = &applicationautoscaling.ScalableTarget{
-		MaxCapacity:                10,
-		MinCapacity:                0,
+		MaxCapacity:                config.Max,
+		MinCapacity:                config.Min,
 		ResourceId:                 resourceID,
 		RoleARN:                    cloudformation.GetAtt(role, "Arn"),
 		ScalableDimension:          applicationautoscaling2.ScalableDimensionEcsServiceDesiredCount,
@@ -75,6 +100,15 @@ func (b *ecsAPIService) createAutoscalingPolicy(project *types.Project, resource
 		AWSCloudFormationDependsOn: []string{serviceResourceName(service.Name)},
 	}
 
+	var (
+		metric        = applicationautoscaling2.MetricTypeEcsserviceAverageCpuutilization
+		targetPercent = config.CPU
+	)
+	if config.Memory != 0 {
+		metric = applicationautoscaling2.MetricTypeEcsserviceAverageMemoryUtilization
+		targetPercent = config.Memory
+	}
+
 	policy := fmt.Sprintf("%sScalingPolicy", normalizeResourceName(service.Name))
 	template.Resources[policy] = &applicationautoscaling.ScalingPolicy{
 		PolicyType:                     "TargetTrackingScaling",
@@ -83,11 +117,12 @@ func (b *ecsAPIService) createAutoscalingPolicy(project *types.Project, resource
 		StepScalingPolicyConfiguration: nil,
 		TargetTrackingScalingPolicyConfiguration: &applicationautoscaling.ScalingPolicy_TargetTrackingScalingPolicyConfiguration{
 			PredefinedMetricSpecification: &applicationautoscaling.ScalingPolicy_PredefinedMetricSpecification{
-				PredefinedMetricType: applicationautoscaling2.MetricTypeEcsserviceAverageCpuutilization,
+				PredefinedMetricType: metric,
 			},
 			ScaleOutCooldown: 60,
 			ScaleInCooldown:  60,
-			TargetValue:      float64(v.(int)),
+			TargetValue:      float64(targetPercent),
 		},
 	}
+	return nil
 }
diff --git a/ecs/cloudformation.go b/ecs/cloudformation.go
index e284dd51..f098d99d 100644
--- a/ecs/cloudformation.go
+++ b/ecs/cloudformation.go
@@ -83,7 +83,10 @@ func (b *ecsAPIService) convert(ctx context.Context, project *types.Project) (*c
 			return nil, err
 		}
 
-		b.createAutoscalingPolicy(project, resources, template, service)
+		err = b.createAutoscalingPolicy(project, resources, template, service)
+		if err != nil {
+			return nil, err
+		}
 	}
 
 	err = b.createCapacityProvider(ctx, project, template, resources)
diff --git a/ecs/sdk.go b/ecs/sdk.go
index 6fd8bb18..77ee60d4 100644
--- a/ecs/sdk.go
+++ b/ecs/sdk.go
@@ -551,21 +551,15 @@ func (s sdk) DeleteStack(ctx context.Context, name string) error {
 
 func (s sdk) CreateSecret(ctx context.Context, secret secrets.Secret) (string, error) {
 	logrus.Debug("Create secret " + secret.Name)
-	var tags []*secretsmanager.Tag
-	for k, v := range secret.Labels {
-		tags = []*secretsmanager.Tag{
-			{
-				Key:   aws.String(k),
-				Value: aws.String(v),
-			},
-		}
+	secretStr, err := secret.GetCredString()
+	if err != nil {
+		return "", err
 	}
-	// store the secret content as string
-	content := string(secret.GetContent())
+
 	response, err := s.SM.CreateSecret(&secretsmanager.CreateSecretInput{
 		Name:         &secret.Name,
-		SecretString: &content,
-		Tags:         tags,
+		SecretString: &secretStr,
+		Description:  &secret.Description,
 	})
 	if err != nil {
 		return "", err
@@ -579,15 +573,17 @@ func (s sdk) InspectSecret(ctx context.Context, id string) (secrets.Secret, erro
 	if err != nil {
 		return secrets.Secret{}, err
 	}
-	tags := map[string]string{}
+	labels := map[string]string{}
 	for _, tag := range response.Tags {
-		tags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
+		labels[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
 	}
-
 	secret := secrets.Secret{
 		ID:     aws.StringValue(response.ARN),
 		Name:   aws.StringValue(response.Name),
-		Labels: tags,
+		Labels: labels,
+	}
+	if response.Description != nil {
+		secret.Description = *response.Description
 	}
 	return secret, nil
 }
@@ -602,14 +598,19 @@ func (s sdk) ListSecrets(ctx context.Context) ([]secrets.Secret, error) {
 	var ls []secrets.Secret
 	for _, sec := range response.SecretList {
 
-		tags := map[string]string{}
+		labels := map[string]string{}
 		for _, tag := range sec.Tags {
-			tags[*tag.Key] = *tag.Value
+			labels[*tag.Key] = *tag.Value
+		}
+		description := ""
+		if sec.Description != nil {
+			description = *sec.Description
 		}
 		ls = append(ls, secrets.Secret{
-			ID:     *sec.ARN,
-			Name:   *sec.Name,
-			Labels: tags,
+			ID:          *sec.ARN,
+			Name:        *sec.Name,
+			Labels:      labels,
+			Description: description,
 		})
 	}
 	return ls, nil


Test patch:
diff --git a/ecs/autoscaling_test.go b/ecs/autoscaling_test.go
index d91b768c..27032316 100644
--- a/ecs/autoscaling_test.go
+++ b/ecs/autoscaling_test.go
@@ -29,13 +29,15 @@ services:
   foo:
     image: hello_world
     deploy:
-      x-aws-autoscaling: 75
+      x-aws-autoscaling: 
+        cpu: 75
+        max: 10
 `, useDefaultVPC)
 	target := template.Resources["FooScalableTarget"].(*autoscaling.ScalableTarget)
 	assert.Check(t, target != nil)
+	assert.Check(t, target.MaxCapacity == 10) //nolint:staticcheck
+
 	policy := template.Resources["FooScalingPolicy"].(*autoscaling.ScalingPolicy)
-	if policy == nil || policy.TargetTrackingScalingPolicyConfiguration == nil {
-		t.Fail()
-	}
-	assert.Check(t, policy.TargetTrackingScalingPolicyConfiguration.TargetValue == float64(75))
+	assert.Check(t, policy != nil)
+	assert.Check(t, policy.TargetTrackingScalingPolicyConfiguration.TargetValue == float64(75)) //nolint:staticcheck
 }
diff --git a/tests/ecs-e2e/e2e-ecs_test.go b/tests/ecs-e2e/e2e-ecs_test.go
index 9acd30be..18460a92 100644
--- a/tests/ecs-e2e/e2e-ecs_test.go
+++ b/tests/ecs-e2e/e2e-ecs_test.go
@@ -21,7 +21,6 @@ import (
 	"io/ioutil"
 	"net/http"
 	"os"
-	"path/filepath"
 	"strconv"
 	"strings"
 	"testing"
@@ -51,23 +50,23 @@ func TestMain(m *testing.M) {
 func TestSecrets(t *testing.T) {
 	cmd, testID := setupTest(t)
 	secretName := "secret" + testID
+	description := "description " + testID
 
 	t.Run("create secret", func(t *testing.T) {
-		secretFile := filepath.Join(cmd.BinDir, "secret.txt")
-		err := ioutil.WriteFile(secretFile, []byte("pass1"), 0644)
-		assert.Check(t, err == nil)
-		res := cmd.RunDockerCmd("secret", "create", secretName, secretFile)
-		assert.Check(t, strings.Contains(res.Stdout(), secretName))
+		res := cmd.RunDockerCmd("secret", "create", secretName, "-u", "user1", "-p", "pass1", "-d", description)
+		assert.Check(t, strings.Contains(res.Stdout(), "secret:"+secretName))
 	})
 
 	t.Run("list secrets", func(t *testing.T) {
 		res := cmd.RunDockerCmd("secret", "list")
 		assert.Check(t, strings.Contains(res.Stdout(), secretName))
+		assert.Check(t, strings.Contains(res.Stdout(), description))
 	})
 
 	t.Run("inspect secret", func(t *testing.T) {
 		res := cmd.RunDockerCmd("secret", "inspect", secretName)
 		assert.Check(t, strings.Contains(res.Stdout(), `"Name": "`+secretName+`"`))
+		assert.Check(t, strings.Contains(res.Stdout(), `"Description": "`+description+`"`))
 	})
 
 	t.Run("rm secret", func(t *testing.T) {