|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package manifest |
| 5 | + |
| 6 | +import ( |
| 7 | + "github.com/aws/aws-sdk-go/aws" |
| 8 | + "github.com/aws/copilot-cli/internal/pkg/template" |
| 9 | + "github.com/imdario/mergo" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + workerSvcManifestPath = "workloads/services/worker/manifest.yml" |
| 14 | +) |
| 15 | + |
| 16 | +// WorkerService holds the configuration to create a worker service. |
| 17 | +type WorkerService struct { |
| 18 | + Workload `yaml:",inline"` |
| 19 | + WorkerServiceConfig `yaml:",inline"` |
| 20 | + // Use *WorkerServiceConfig because of https://github.com/imdario/mergo/issues/146 |
| 21 | + Environments map[string]*WorkerServiceConfig `yaml:",flow"` |
| 22 | + |
| 23 | + parser template.Parser |
| 24 | +} |
| 25 | + |
| 26 | +// WorkerServiceConfig holds the configuration that can be overridden per environments. |
| 27 | +type WorkerServiceConfig struct { |
| 28 | + ImageConfig ImageWithHealthcheck `yaml:"image,flow"` |
| 29 | + ImageOverride `yaml:",inline"` |
| 30 | + TaskConfig `yaml:",inline"` |
| 31 | + *Logging `yaml:"logging,flow"` |
| 32 | + Sidecars map[string]*SidecarConfig `yaml:"sidecars"` |
| 33 | + Subscribe *SubscribeConfig `yaml:"subscribe"` |
| 34 | + Network *NetworkConfig `yaml:"network"` |
| 35 | +} |
| 36 | + |
| 37 | +// WorkerServiceProps represents the configuration needed to create a worker service. |
| 38 | +type WorkerServiceProps struct { |
| 39 | + WorkloadProps |
| 40 | + HealthCheck *ContainerHealthCheck // Optional healthcheck configuration. |
| 41 | + Topics *[]TopicSubscription // Optional topics for subscriptions |
| 42 | +} |
| 43 | + |
| 44 | +// SubscribeConfig represents the configurable options for setting up subscriptions. |
| 45 | +type SubscribeConfig struct { |
| 46 | + Topics *[]TopicSubscription `yaml:"topics"` |
| 47 | +} |
| 48 | + |
| 49 | +// TopicSubscription represents the configurable options for setting up a SNS Topic Subscription. |
| 50 | +type TopicSubscription struct { |
| 51 | + Name string `yaml:"name"` |
| 52 | + Service string `yaml:"service"` |
| 53 | +} |
| 54 | + |
| 55 | +// NewWorkerService applies the props to a default Worker service configuration with |
| 56 | +// minimal cpu/memory thresholds, single replica, no healthcheck, and then returns it. |
| 57 | +func NewWorkerService(props WorkerServiceProps) *WorkerService { |
| 58 | + svc := newDefaultWorkerService() |
| 59 | + // Apply overrides. |
| 60 | + svc.Name = stringP(props.Name) |
| 61 | + svc.WorkerServiceConfig.ImageConfig.Image.Location = stringP(props.Image) |
| 62 | + svc.WorkerServiceConfig.ImageConfig.Build.BuildArgs.Dockerfile = stringP(props.Dockerfile) |
| 63 | + svc.WorkerServiceConfig.ImageConfig.HealthCheck = props.HealthCheck |
| 64 | + svc.WorkerServiceConfig.Subscribe.Topics = props.Topics |
| 65 | + svc.parser = template.New() |
| 66 | + return svc |
| 67 | +} |
| 68 | + |
| 69 | +// newDefaultWorkerService returns a Worker service with minimal task sizes and a single replica. |
| 70 | +func newDefaultWorkerService() *WorkerService { |
| 71 | + return &WorkerService{ |
| 72 | + Workload: Workload{ |
| 73 | + Type: aws.String(WorkerServiceType), |
| 74 | + }, |
| 75 | + WorkerServiceConfig: WorkerServiceConfig{ |
| 76 | + ImageConfig: ImageWithHealthcheck{}, |
| 77 | + Subscribe: &SubscribeConfig{}, |
| 78 | + TaskConfig: TaskConfig{ |
| 79 | + CPU: aws.Int(256), |
| 80 | + Memory: aws.Int(512), |
| 81 | + Count: Count{ |
| 82 | + Value: aws.Int(1), |
| 83 | + }, |
| 84 | + ExecuteCommand: ExecuteCommand{ |
| 85 | + Enable: aws.Bool(false), |
| 86 | + }, |
| 87 | + }, |
| 88 | + Network: &NetworkConfig{ |
| 89 | + VPC: &vpcConfig{ |
| 90 | + Placement: aws.String(PublicSubnetPlacement), |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// MarshalBinary serializes the manifest object into a binary YAML document. |
| 98 | +// Implements the encoding.BinaryMarshaler interface. |
| 99 | +func (s *WorkerService) MarshalBinary() ([]byte, error) { |
| 100 | + content, err := s.parser.Parse(workerSvcManifestPath, *s, template.WithFuncs(map[string]interface{}{ |
| 101 | + "fmtSlice": template.FmtSliceFunc, |
| 102 | + "quoteSlice": template.QuoteSliceFunc, |
| 103 | + })) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + return content.Bytes(), nil |
| 108 | +} |
| 109 | + |
| 110 | +// BuildRequired returns if the service requires building from the local Dockerfile. |
| 111 | +func (s *WorkerService) BuildRequired() (bool, error) { |
| 112 | + return requiresBuild(s.ImageConfig.Image) |
| 113 | +} |
| 114 | + |
| 115 | +// BuildArgs returns a docker.BuildArguments object for the service given a workspace root directory |
| 116 | +func (s *WorkerService) BuildArgs(wsRoot string) *DockerBuildArgs { |
| 117 | + return s.ImageConfig.BuildConfig(wsRoot) |
| 118 | +} |
| 119 | + |
| 120 | +// ApplyEnv returns the service manifest with environment overrides. |
| 121 | +// If the environment passed in does not have any overrides then it returns itself. |
| 122 | +func (s WorkerService) ApplyEnv(envName string) (WorkloadManifest, error) { |
| 123 | + overrideConfig, ok := s.Environments[envName] |
| 124 | + if !ok { |
| 125 | + return &s, nil |
| 126 | + } |
| 127 | + |
| 128 | + if overrideConfig == nil { |
| 129 | + return &s, nil |
| 130 | + } |
| 131 | + |
| 132 | + envCount := overrideConfig.TaskConfig.Count |
| 133 | + if !envCount.IsEmpty() { |
| 134 | + s.TaskConfig.Count = envCount |
| 135 | + } |
| 136 | + |
| 137 | + // Apply overrides to the original service s. |
| 138 | + err := mergo.Merge(&s, WorkerService{ |
| 139 | + WorkerServiceConfig: *overrideConfig, |
| 140 | + }, mergo.WithOverride, mergo.WithOverwriteWithEmptyValue, mergo.WithTransformers(workloadTransformer{})) |
| 141 | + |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + s.Environments = nil |
| 146 | + return &s, nil |
| 147 | +} |
0 commit comments