1

I'm trying to split a method definition from the schema. This is a case loosely inspired by the official how-to. If the following code is present in the same file, it works well. However if I'm splitting the method definition into a separate file the following way, it results in an error.

svc.yaml:

#@ load("@ytt:data", "data")
---
apiVersion: v1
kind: Service
metadata:
  labels: #@ labels(with_version=True)
  name: #@ data.values.promOperatorName
  namespace: #@ data.values.namespace
spec:
  clusterIP: None
  ports:
  - name: http
    port: 8080
    targetPort: http
  selector: #@ labels()

labels.yaml:

#@ load("@ytt:data", "data")

#@ def labels(with_version=False):
  app.kubernetes.io/component: controller
  app.kubernetes.io/name: prometheus-operator
  #@ if with_version:
  app.kubernetes.io/version: 0.53.1
  #@ end
#@ end

Applied via: ytt -f labels.yaml -f svc.yaml -f data.yaml this yields

ytt: Error: 
- undefined: labels
    svc.yaml:7 |   labels: #@ labels(with_version=True)
- undefined: labels
    svc.yaml:16 |   selector: #@ labels()

Apparently the def labels() method definition is not picked-up during processing.

ytt version 0.38.0

Bernard Halas
  • 972
  • 11
  • 24

1 Answers1

1

The file carrying the method definition should be loaded as a library. Therefore it should be renamed to labels.lib.yaml (.lib.yaml suffix is mandatory for libraries).

Afterwards the library and the respective methods should be loaded in the appropriate template file, in our case in svc.yaml via:

#@ load("labels.lib.yaml", "labels")
Bernard Halas
  • 972
  • 11
  • 24
  • Nice! Details about how this `load()` function works can be found here: https://carvel.dev/ytt/docs/v0.40.0/lang-ref-load/#docs – JTigger May 22 '22 at 15:17