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