5

I am trying to rename my service using kustomize.

I am getting the following error:

Error: couldn't find target core_v1_Service|~X|~P|SERVICE_NAME|~S for json patch

Here is my Base service file:

apiVersion: v1
kind: Service
metadata:
  labels:
    version: IMAGE_TAG
  name: SERVICE_NAME
  namespace: my-namespace
spec:
  ports:
    - name: http-service
      port: 8080
      protocol: TCP
      targetPort: http-service
  selector:
    app.kubernetes.io/name:
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.name
  sessionAffinity: None
  type: ClusterIP

Here is my kustomization.yaml file where I have defined patchesJson6902:

patchesJson6902:
#- target:
#    group: apps
#    version: v1
#    kind: Deployment
#    name: DEPLOYMENT_NAME
#  path: patch_deployment.yaml
- target:
    group: core
    version: v1
    kind: Service
    name: SERVICE_NAME
  path: patch_service.yaml

I am able to replace my deployment name but not the service name. Kubectl Version: 1.16+

my patch_deployment.yaml and patch_service.yaml files are very simple

- op: replace
  path: /metadata/name
  value: ${SERVICE_NAME}
Ram
  • 655
  • 2
  • 7
  • 27

2 Answers2

3

I found a few issues in your scenario.

First of all, in selectors you are using downward api which will throw error:

error: error validating "svc.yaml": error validating data: ValidationError(Service.spec.selector.app.kubernetes.io/name): invalid type for io.k8s.api.core.v1.ServiceSpec.selector: got "map", expected "string"; if you choose to ignore these errors

Value here should be string. You could encounter similar situation if you would downward api in PVC. More information you can find here.

Second issue is with patchesJson6902. If you will check example form documentation and find service example, there will be no group: core as below:

patchesJson6902:
- target:
    version: v1
    kind: Deployment
    name: my-deployment
  path: add_init_container.yaml
- target:
    version: v1
    kind: Service
    name: my-service
  path: add_service_annotation.yaml

However, in your scenario, your service is using namespace: my-namespace so it should be also included in patchesJson6902.

There are two solution to make it working.

Option 1

Please keep in mind that your examples SERVICE_NAME and IMAGE_TAG will cause error:

The Service "SERVICE_NAME" is invalid: metadata.name: Invalid value: "SERVICE_NAME": a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic char
acter, and end with an alphanumeric character (e.g. 'my-name',  or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')

YAMLs below:

>cat svc.yaml 
apiVersion: v1
kind: Service
metadata:
  labels:
    version: image-tag
  name: SERVICE_NAME
  namespace: my-namespace
 
>cat kustomization.yaml 
resources:
- svc.yaml
 
patchesJson6902:
- path: patch_service.yaml
  target:
    version: v1
    kind: Service
    name: SERVICE_NAME
    namespace: my-namespace
 
>cat patch_service.yaml 
- op: replace
  path: /metadata/name
  value: ${SERVICE_NAME}
 
>kustomize build 
apiVersion: v1
kind: Service
metadata:
  labels:
    version: image-tag
  name: ${SERVICE_NAME}
  namespace: my-namespace

Variable SERVICE_NAME wont be evaluated during kustomization. It has to be substituted afterwards using sed or manually.

Option 2

It will require small script with sed command.

>cat kustomization.yaml 
resources:
- svc.yaml
 
patchesJson6902:
- path: patch_service.yaml
  target:
    version: v1
    kind: Service
    name: SERVICE_NAME
    namespace: my-namespace
 
 
>cat kustomsed.sh 
#!/bin/bash
 
SERVICE_NAME=testsvc
 
kustomize build | sed "s/SERVICE_NAME/${SERVICE_NAME}/"
 
>./kustomsed.sh 
apiVersion: v1
kind: Service
metadata:
  labels:
    version: image-tag
  name: testsvc
  namespace: my-namespace
  
PjoterS
  • 12,841
  • 1
  • 22
  • 54
  • 1
    This is not clear to me why `kustomize` requires `sed`. As a templating tool, shouldn't `kustomize` be able to fully manage yaml creation process? – Fabrice Jammes Mar 30 '21 at 07:30
  • 1
    @FabriceJammes `kustomize` is `surgical tool` without some boundaries and schema check, whilst sed is more general. In case there are cases couldn't achieve using kustomize (or difficult), we can use `sed`. – Brian Ng Sep 30 '22 at 06:08
1

It seems that is issue with kubectl kustomize version. It works with kustomize build.

Ram
  • 655
  • 2
  • 7
  • 27