yaml anchor a list object, and merge with kustomize

8/25/2021

I am interested in using YAML anchors for a list of objects. I have a Kubernetes pod manifest which contains a container list. I am trying to build this with kustomize, but I can't find the right syntax.

So, I think this looks the most correct.

apiVersion: v1
kind: Pod
metadata: { name: anchor-test }
spec:
  containers:
  - &foo
    name: foo
    image: traefik/whoami
    ports: [ containerPort: 80 ]
  - <<: *foo
    name: bar

But kustomize is throwing this error:

Error: map[string]interface {}{"apiVersion":"v1", "kind":"Pod", "metadata":map[string]interface {}{"name":"anchor-test"}, "spec":map[string]interface {}{"containers":[]interface {}{map[string]interface {}{"image":"traefik/whoami", "name":"foo", "ports":[]interface {}{map[string]interface {}{"containerPort":80}}}, map[interface {}]interface {}{"image":"traefik/whoami", "name":"bar", "ports":[]interface {}{map[string]interface {}{"containerPort":80}}}}}}: 
json: unsupported type: map[interface {}]interface {}
-- The Fool
kubernetes
kustomize
yaml

1 Answer

8/25/2021

So, this problem is specific to kustomize. It works with kubectl apply -f. That means the YAML is actually valid like this. It can be tested with http://www.yamllint.com/.

So for the time being, this is not possible with kustomize. There are some issues on GitHub around this.

-- The Fool
Source: StackOverflow