kuberneteskustomize

Unable to override port values using kustomization in k8s


I am doing some practices. one of the test cases i tried to override the values of port value. while doing that getting an error as replace operation does not apply: doc is missing key: /spec/ports/port: missing value.

patches:
  - target:
      kind: Service
      name: cdq-ui-service
    patch: |
      - op: replace
        path: /spec/ports/0/port
        value: 8080

while applying kustomization in Kubernetes with the patch. that path will be override the port value.


Solution

  • You are trying to modify /spec/ports/port, but there is no such path in a Kubernetes service. Recall that a service looks like this:

    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
    spec:
      selector:
        app.kubernetes.io/name: MyApp
      ports:
        - protocol: TCP
          port: 80
          targetPort: 9376
    

    The path /spec/ports is a list, not a dictionary. You could patch /spec/ports/0/port:

    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    
    resources:
    - service.yaml
    
    patches:
      - target:
          kind: Service
          name: my-service
        patch: |
          - op: replace
            path: /spec/ports/0/port
            value: 8080
    

    Which would result in:

    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
    spec:
      ports:
      - port: 8080
        protocol: TCP
        targetPort: 9376
      selector:
        app.kubernetes.io/name: MyApp