seek-oss / kpt-functions

A library of Kpt functions for extending Kpt's functionality
MIT License
3 stars 3 forks source link

Kpt template interprets templates in subsequent fields #38

Closed marlenepereira closed 3 years ago

marlenepereira commented 3 years ago

Affected Function(s)

Configuration Files

Package packages/test/alerts.yaml

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: alerts
spec:
  groups:
  - name: my_alerts
    rules:
    - alert: TestAlert
      annotations:
        # {"$kpt-template":"true"}
        runbook_url: |
          {{render "runbook-url" "TestAlert"}}
        summary: Summary with template `{{ $labels.name }}` and `{{ $metadata.namespace }}`.

Kptfile test/Kptfile

apiVersion: kpt.dev/v1alpha1
kind: Kptfile
metadata:
  name: test
packageMetadata:
  shortDescription: sample description
openAPI:
  definitions:
    io.k8s.cli.setters.runbook-url:
      x-k8s-cli:
        setter:
          name: runbook-url
          value: https://runbooks.com

packages.yaml

apiVersion: kpt.seek.com/v1alpha1
kind: ClusterPackages
metadata:
  name: test-package
spec:
  baseDir: test
  packages:
    - name: test
      variables:
        - name: runbook-url
          value: https://runbooks.com/runbooks#{{args 0}}
      local:
        directory: packages/test

Output

2021-05-13T00:03:05Z | FATAL | Error performing sync operation error="template: render:1: undefined variable \"$labels\""

Steps to Reproduce

  1. kpt fn source target/packages.yaml | kpt fn run --image seek/kpt-sync:0.0.8 \ --mount "type=bind,source=$(pwd)/packages/test,target=/packages/test" \ | kpt fn sink .

Important Factoids

Placing the field with template before the field containing the # {"$kpt-template":"true"} comment results in the expected behavior. Package packages/test/alerts.yaml

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: alerts
spec:
  groups:
  - name: my_alerts
    rules:
    - alert: TestAlert
      annotations:
        summary: Summary with template `{{ $labels.name }}` and `{{ $metadata.namespace }}`.
        # {"$kpt-template":"true"}
        runbook_url: |
          {{render "runbook-url" "TestAlert"}}

Output

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: alerts
spec:
  groups:
  - name: my_alerts
    rules:
    - alert: TestAlert
      annotations:
        summary: Summary with template `{{ $labels.name }}` and `{{ $metadata.namespace }}`.
        # {"$kpt-template":"true"}
        runbook_url: |
          https://runbooks.com/runbooks#TestAlert
marlenepereira commented 3 years ago

This was addressed by https://github.com/seek-oss/kpt-functions/pull/39.