|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package stack |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/aws/aws-sdk-go/aws" |
| 12 | + "github.com/aws/copilot-cli/internal/pkg/manifest" |
| 13 | + "github.com/aws/copilot-cli/internal/pkg/template" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + defaultSidecarPort = "80" |
| 18 | +) |
| 19 | + |
| 20 | +// convertSidecar converts the manifest sidecar configuration into a format parsable by the templates pkg. |
| 21 | +func convertSidecar(s map[string]*manifest.SidecarConfig) ([]*template.SidecarOpts, error) { |
| 22 | + if s == nil { |
| 23 | + return nil, nil |
| 24 | + } |
| 25 | + var sidecars []*template.SidecarOpts |
| 26 | + for name, config := range s { |
| 27 | + port, protocol, err := parsePortMapping(config.Port) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + sidecars = append(sidecars, &template.SidecarOpts{ |
| 32 | + Name: aws.String(name), |
| 33 | + Image: config.Image, |
| 34 | + Port: port, |
| 35 | + Protocol: protocol, |
| 36 | + CredsParam: config.CredsParam, |
| 37 | + Secrets: config.Secrets, |
| 38 | + Variables: config.Variables, |
| 39 | + }) |
| 40 | + } |
| 41 | + return sidecars, nil |
| 42 | +} |
| 43 | + |
| 44 | +// Valid sidecar portMapping example: 2000/udp, or 2000 (default to be tcp). |
| 45 | +func parsePortMapping(s *string) (port *string, protocol *string, err error) { |
| 46 | + if s == nil { |
| 47 | + // default port for sidecar container to be 80. |
| 48 | + return aws.String(defaultSidecarPort), nil, nil |
| 49 | + } |
| 50 | + portProtocol := strings.Split(*s, "/") |
| 51 | + switch len(portProtocol) { |
| 52 | + case 1: |
| 53 | + return aws.String(portProtocol[0]), nil, nil |
| 54 | + case 2: |
| 55 | + return aws.String(portProtocol[0]), aws.String(portProtocol[1]), nil |
| 56 | + default: |
| 57 | + return nil, nil, fmt.Errorf("cannot parse port mapping from %s", *s) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// convertAutoscaling converts the service's Auto Scaling configuration into a format parsable |
| 62 | +// by the templates pkg. |
| 63 | +func convertAutoscaling(a *manifest.Autoscaling) (*template.AutoscalingOpts, error) { |
| 64 | + if a.IsEmpty() { |
| 65 | + return nil, nil |
| 66 | + } |
| 67 | + min, max, err := a.Range.Parse() |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + autoscalingOpts := template.AutoscalingOpts{ |
| 72 | + MinCapacity: &min, |
| 73 | + MaxCapacity: &max, |
| 74 | + } |
| 75 | + if a.CPU != nil { |
| 76 | + autoscalingOpts.CPU = aws.Float64(float64(*a.CPU)) |
| 77 | + } |
| 78 | + if a.Memory != nil { |
| 79 | + autoscalingOpts.Memory = aws.Float64(float64(*a.Memory)) |
| 80 | + } |
| 81 | + if a.Requests != nil { |
| 82 | + autoscalingOpts.Requests = aws.Float64(float64(*a.Requests)) |
| 83 | + } |
| 84 | + if a.ResponseTime != nil { |
| 85 | + responseTime := float64(*a.ResponseTime) / float64(time.Second) |
| 86 | + autoscalingOpts.ResponseTime = aws.Float64(responseTime) |
| 87 | + } |
| 88 | + return &autoscalingOpts, nil |
| 89 | +} |
| 90 | + |
| 91 | +// convertHTTPHealthCheck converts the ALB health check configuration into a format parsable by the templates pkg. |
| 92 | +func convertHTTPHealthCheck(hc *manifest.HealthCheckArgsOrString) template.HTTPHealthCheckOpts { |
| 93 | + opts := template.HTTPHealthCheckOpts{ |
| 94 | + HealthCheckPath: manifest.DefaultHealthCheckPath, |
| 95 | + HealthyThreshold: hc.HealthCheckArgs.HealthyThreshold, |
| 96 | + UnhealthyThreshold: hc.HealthCheckArgs.UnhealthyThreshold, |
| 97 | + } |
| 98 | + if hc.HealthCheckArgs.Path != nil { |
| 99 | + opts.HealthCheckPath = *hc.HealthCheckArgs.Path |
| 100 | + } else if hc.HealthCheckPath != nil { |
| 101 | + opts.HealthCheckPath = *hc.HealthCheckPath |
| 102 | + } |
| 103 | + if hc.HealthCheckArgs.Interval != nil { |
| 104 | + opts.Interval = aws.Int64(int64(hc.HealthCheckArgs.Interval.Seconds())) |
| 105 | + } |
| 106 | + if hc.HealthCheckArgs.Timeout != nil { |
| 107 | + opts.Timeout = aws.Int64(int64(hc.HealthCheckArgs.Timeout.Seconds())) |
| 108 | + } |
| 109 | + return opts |
| 110 | +} |
| 111 | + |
| 112 | +func convertLogging(lc *manifest.Logging) *template.LogConfigOpts { |
| 113 | + if lc == nil { |
| 114 | + return nil |
| 115 | + } |
| 116 | + return logConfigOpts(lc) |
| 117 | +} |
| 118 | +func logConfigOpts(lc *manifest.Logging) *template.LogConfigOpts { |
| 119 | + return &template.LogConfigOpts{ |
| 120 | + Image: lc.LogImage(), |
| 121 | + ConfigFile: lc.ConfigFile, |
| 122 | + EnableMetadata: lc.GetEnableMetadata(), |
| 123 | + Destination: lc.Destination, |
| 124 | + SecretOptions: lc.SecretOptions, |
| 125 | + } |
| 126 | +} |
0 commit comments