Victoria Agafonova
Victoria Agafonova

Reputation: 2178

kubectl kustomize leaves placeholder lines

I'm trying to use Kustomize but as a result I have wrong placeholder lines.

I have base/deployment.yaml like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
        - image: IMAGE_PLACEHOLDER
          name: CONTAINER_NAME_PLACEHOLDER

And also base/kustomization.yaml:

resources:
  - deployment.yaml

And also overlays/dev/kustomization.yaml:

resources:
  - ../../base
commonLabels:
  app: myname
patchesStrategicMerge:
  - change_container_name.yaml

And overlays/dev/change_container_name.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
spec:
  template:
    spec:
      containers:
        - name: myname
          image: registry.mycompany.ru/project/app:VERSION

And as a result of running kubectl kustomize ./overlays/dev i see in my console something like this:

...
containers:
      - image: registry.mycompany.ru/project/app:VERSION
        name: myname
        image: IMAGE_PLACEHOLDER #--- unwanted line
        name: CONTAINER_NAME_PLACEHOLDER #--- unwanted line
        ports:
        - containerPort: 80

How to fix this problem?

Upvotes: 0

Views: 947

Answers (1)

larsks
larsks

Reputation: 312203

This isn't going to work the way you want: because containers: is a list, Kustomize uses the container name to identify which element you want to modify. Because the container name in your patch doesn't match an existing container name, you are adding a new container to the deployment. That's why you end up with:

      containers:
      - image: registry.mycompany.ru/project/app:VERSION
        name: myname
      - image: IMAGE_PLACEHOLDER
        name: CONTAINER_NAME_PLACEHOLDER

I'm not sure what your motivation is here, but if you really want to modify the name of an existing container you can do that using a jsonpatch patch instead of a strategic merge patch. The patch might look like this:

- op: replace
  path: /spec/template/spec/containers/0/name
  value: myname

And I would use it in a kustomization file like this:

resources:
  - ../../base

commonLabels:
  app: myname

patches:
  - path: change_container_name.yaml
    target:
      kind: Deployment
      name: app

Given your examples, this would produce the following output:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: myname
  name: app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myname
  template:
    metadata:
      labels:
        app: myname
    spec:
      containers:
      - image: IMAGE_PLACEHOLDER
        name: myname

You could do the same thing with the image name, but you could also use an image transformer:

resources:
  - ../../base
commonLabels:
  app: myname

patches:
  - path: change_container_name.yaml
    target:
      kind: Deployment
      name: app

images:
  - name: IMAGE_PLACEHOLDER
    newName: registry.mycompany.ru/project/app
    newTag: VERSION

Which gets us:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: myname
  name: app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myname
  template:
    metadata:
      labels:
        app: myname
    spec:
      containers:
      - image: registry.mycompany.ru/project/app:VERSION
        name: myname

Upvotes: 1

Related Questions