Skip to content

Commit

Permalink
Always template vendor source and targets (#712)
Browse files Browse the repository at this point in the history
This change improves the templating within vendor manifests slightly: It
officially adds support for the `Component` field to both `source` and
`targets`.

These features were already supported but mostly undocumented and hidden
behind an implicit switch: The templating was only triggered if the `Version`
field was set. Which was also the only officially supported field.

In reality though all fields from the current source definition were
available but in the state they were currently in, depending on the
order of the templates.

With this change
 * It is clearly documented which fields are supported in which YAML
   values.
 * Only the two static fields are supported.
 * The values are always templated.

Theoretically this could be a breaking change if somebody used no
`version` field but curly braces in their paths. Or relied on the
half-populated source data structure to refer to unsupported fields.
If xkcd 1172 applies it should be possible to amend this logic to add
more officially supported fields.

Co-authored-by: Andriy Knysh <[email protected]>
  • Loading branch information
mss and aknysh authored Oct 14, 2024
1 parent fecba15 commit 94e0226
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
25 changes: 11 additions & 14 deletions internal/exec/vendor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,15 @@ func ExecuteAtmosVendorInternal(
)
}

tmplData := struct {
Component string
Version string
}{s.Component, s.Version}

// Parse 'source' template
if s.Version != "" {
uri, err = ProcessTmpl(fmt.Sprintf("source-%d-%s", indexSource, s.Version), s.Source, s, false)
if err != nil {
return err
}
} else {
uri = s.Source
uri, err = ProcessTmpl(fmt.Sprintf("source-%d", indexSource), s.Source, tmplData, false)
if err != nil {
return err
}

useOciScheme := false
Expand Down Expand Up @@ -317,13 +318,9 @@ func ExecuteAtmosVendorInternal(
for indexTarget, tgt := range s.Targets {
var target string
// Parse 'target' template
if s.Version != "" {
target, err = ProcessTmpl(fmt.Sprintf("target-%d-%d-%s", indexSource, indexTarget, s.Version), tgt, s, false)
if err != nil {
return err
}
} else {
target = tgt
target, err = ProcessTmpl(fmt.Sprintf("target-%d-%d", indexSource, indexTarget), tgt, tmplData, false)
if err != nil {
return err
}

targetPath := path.Join(vendorConfigFilePath, target)
Expand Down
8 changes: 4 additions & 4 deletions website/docs/core-concepts/vendor/vendor-manifest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ spec:
# `source` supports the following protocols: local paths (absolute and relative), OCI (https://opencontainers.org),
# Git, Mercurial, HTTP, HTTPS, Amazon S3, Google GCP,
# and all URL and archive formats as described in https://github.com/hashicorp/go-getter.
# In 'source', Golang templates are supported https://pkg.go.dev/text/template.
# If 'version' is provided, '{{.Version}}' will be replaced with the 'version' value before pulling the files from 'source'.
# In 'source' and 'targets', Golang templates are supported https://pkg.go.dev/text/template.
# Currently the fields '{{.Component}}' and '{{.Version}}' are supported.
# Download the component from the AWS public ECR registry (https://docs.aws.amazon.com/AmazonECR/latest/public/public-registries.html).
- component: "vpc"
source: "oci://public.ecr.aws/cloudposse/components/terraform/stable/aws/vpc:{{.Version}}"
Expand All @@ -93,7 +93,7 @@ spec:
source: "github.com/cloudposse/terraform-aws-components.git//modules/vpc-flow-logs-bucket?ref={{.Version}}"
version: "1.323.0"
targets:
- "components/terraform/infra/vpc-flow-logs-bucket/{{.Version}}"
- "components/terraform/infra/{{.Component}}/{{.Version}}"
excluded_paths:
- "**/*.yaml"
- "**/*.yml"
Expand Down Expand Up @@ -198,7 +198,7 @@ The `vendor.yaml` vendoring manifest supports Kubernetes-style YAML config to de

<dt>`source` and `targets` templates</dt>
<dd>
The `source` and `targets` attributes support [Go templates](https://pkg.go.dev/text/template) and [Sprig Functions](http://masterminds.github.io/sprig/). This can be used to templatise the `source` and `targets` paths with the artifact versions specified in the `version` attribute.
The `source` and `targets` attributes support [Go templates](https://pkg.go.dev/text/template) and [Sprig Functions](http://masterminds.github.io/sprig/). This can be used to templatise the `source` and `targets` paths with the component name specified in the `component` attribute and artifact versions specified in the `version` attribute.

Here's an advanced example showcasing how templates and Sprig functions can be used together with `targets`:

Expand Down

0 comments on commit 94e0226

Please sign in to comment.