Skip to content

Conversation

bigkevmcd
Copy link
Contributor

Closes https://github.com/weaveworks/weave-gitops-interlock/issues/244

What changed?
This exposes some template metadata for the surrounding CAPI/GitOps template via the .template.meta key in the "templating" renderType.

The data can be used when rendering file paths etc.

Why was this change made?
Allow rendering templates to dynamically generated paths.

How was this change implemented?
I opted not to expose the entire Template resource, as a recent(ish) PR upstream got a review that highlighted that this binds the metadata to the struct too tightly.

How did you validate the change?
Tests!

Release notes
Template path generation can now include metadata from the template when generating the output.

Documentation Changes
The user guide should include an example...

apiVersion: capi.weave.works/v1alpha2
kind: CAPITemplate
metadata:
  name: cluster-template-1
  namespace: test-namespace
spec:
  description: this is test template 1
  renderType: templating
  resourcetemplates:
  - content:
    - apiVersion: cluster.x-k8s.io/v1alpha3
      kind: Cluster
      metadata:
        name: "{{ .template.meta.namespace }}-cluster"
    path: "./clusters/{{ .template.meta.name }}/capi-cluster.yaml"

Currently, the template meta only includes the namespace and name fields from the CAPITemplate.

@bigkevmcd bigkevmcd marked this pull request as ready for review January 4, 2023 09:09
@bigkevmcd bigkevmcd requested a review from a team January 4, 2023 09:09
@bigkevmcd bigkevmcd added the enhancement New feature or request label Jan 4, 2023
@@ -329,3 +332,13 @@ func makeTemplateFunctions() template.FuncMap {
}
return f
}

// this could add additional fields from the data.
func templateMetadata(t templatesv1.Template) map[string]any {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any is a new predeclared identifier and a type alias of interface{}.

Neat. I have also just repeated the word "any" too many times and it does not look right anymore.

@@ -242,7 +242,10 @@ func (p *TextTemplateProcessor) Render(tmpl []byte, values map[string]string) ([
}

var out bytes.Buffer
if err := parsed.Execute(&out, map[string]interface{}{"params": values}); err != nil {
if err := parsed.Execute(&out, map[string]interface{}{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the only template.Execute invocation in the codebase. Well done us.

This exposes some template metadata for the surrounding CAPI/GitOps
template via the .template.meta key in the "templating" renderType.
@bigkevmcd bigkevmcd force-pushed the template-object-meta branch from 156d2d8 to 5d503be Compare January 6, 2023 08:19
@bigkevmcd bigkevmcd merged commit 0950570 into main Jan 6, 2023
@bigkevmcd bigkevmcd deleted the template-object-meta branch January 6, 2023 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants