Skip to content

Commit

Permalink
chore: Interpolate slice of strings in the manifest (#4993)
Browse files Browse the repository at this point in the history
closes #4928


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.
  • Loading branch information
KollaAdithya committed Jun 29, 2023
1 parent f111d1b commit d6b741a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
17 changes: 16 additions & 1 deletion internal/pkg/manifest/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package manifest

import (
"bytes"
"encoding/json"
"fmt"
"os"
"regexp"
Expand Down Expand Up @@ -72,7 +73,21 @@ func (i *Interpolator) applyInterpolation(node *yaml.Node) error {
if err != nil {
return err
}
node.Value = interpolated
var s []string
if err = json.Unmarshal([]byte(interpolated), &s); err == nil && len(s) != 0 {
seqNode := &yaml.Node{
Kind: yaml.SequenceNode,
}
for _, value := range s {
seqNode.Content = append(seqNode.Content, &yaml.Node{
Kind: yaml.ScalarNode,
Value: value,
})
}
*node = *seqNode
} else {
node.Value = interpolated
}
default:
for _, content := range node.Content {
if err := i.applyInterpolation(content); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/pkg/manifest/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ cpu: 256#${CPU}
memory: 512 # ${Memory}
variables:
${foo}: ${bar}
network:
vpc:
security_groups: ${SECURITY_GROUPS}
`,
inputEnvVar: map[string]string{
"0accountID": "1234567890",
Expand All @@ -69,6 +72,7 @@ variables:
"CPU": "512",
"bar": "bar",
"ip": "10.24.34.0/23",
"SECURITY_GROUPS": `["sg-1","sg-2","sg-3"]`,
},

wanted: `# The manifest for the ${name} service.
Expand All @@ -89,6 +93,12 @@ cpu: 256#512
memory: 512 # ${Memory}
variables:
${foo}: bar
network:
vpc:
security_groups:
- sg-1
- sg-2
- sg-3
`,
},
}
Expand Down
21 changes: 20 additions & 1 deletion site/content/docs/developing/manifest-env-var.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@ image:
```
When Copilot defines the container, it will use the image located at `id.dkr.ecr.zone.amazonaws.com/project-name` and with tag `version01`.

It is also possible to interpolate `Array of Strings`, from environment variables in your manifest files:

```yaml
network:
vpc:
security_groups: ${SECURITY_GROUPS}
```

Suppose the shell has `SECURITY_GROUPS=["sg-06b511534b8fa8bbb","sg-06b511534b8fa8bbb","sg-0e921ad50faae7777"]`, the manifest example will be resolved as

```yaml
network:
vpc:
security_groups:
- sg-06b511534b8fa8bbb
- sg-06b511534b8fa8bbb
- sg-0e921ad50faae7777
```

!!! Info
At this moment, you can only substitute shell environment variables for fields that accept strings, including `String` (e.g., `image.location`), `Array of Strings` (e.g., `entrypoint`), or `Map` where the value type is `String` (e.g., `secrets`).
At this moment, you can only substitute shell environment variables for fields that accept strings, including `String` (e.g., `image.location`), `Array of Strings` (e.g., `entrypoint`), or `Map` where the value type is `String` or `Array of Strings` (e.g., `secrets`).

## Predefined variables
Predefined variables are reserved variables that will be resolved by Copilot when interpreting the manifest. Currently, available predefined environment variables include:
Expand Down

0 comments on commit d6b741a

Please sign in to comment.