'YAML placeholders with kubectl

I'm using below template in deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: httpbin-diffy
  labels:
    app: httpbin-diffy
  namespace: {{ Values.namespace.name }}

and using separate values.yaml for

namespace:
  name: diffy

when I run kubectl apply -f Deployment.yaml

I get the below error.

error: error parsing Deployment.yaml: error converting YAML to JSON: yaml: invalid map key: map[interface {}]interface {}{"Values.namespace.name":interface {}(nil)}

where I'm going wrong?



Solution 1:[1]

Using Go's template language with a values.yaml is a feature of Helm that is not native to Kubernetes. If you'd like to create a templated deployment.yaml file, you could use a native tool like sed to do find-and-replace before sending to kubectl, or could use yq for a more YAML-aware replacement tool.

$ cat deployment.yml
apiVersion: v1
kind: Service
metadata:
  name: httpbin-diffy
  labels:
    app: httpbin-diffy
  namespace: PLACEHOLDER

$ yq '.metadata.namespace = "NAMESPACE_VALUE"' deployment.yml 
apiVersion: v1
kind: Service
metadata:
  name: httpbin-diffy
  labels:
    app: httpbin-diffy
  namespace: NAMESPACE_VALUE

That file can either be written to the filesystem and applied with kubectl apply -f or could be read from stdin, e.g.

$ yq '.metadata.namespace = "NAMESPACE_VALUE"' deployment.yml | kubectl apply -f -

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 logyball