0

Assume I have cronjobservice and deployment just like below:

# Create a directory to hold the base
mkdir base
# Create a base/deployment.yaml
cat <<EOF > base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
EOF

# Create a base/service.yaml file
cat <<EOF > base/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-nginx
  labels:
    run: my-nginx
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    run: my-nginx
EOF

# Create a base/cronjob.yaml file
cat <<EOF > base/cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure
EOF

# Create a base/kustomization.yaml
cat <<EOF > base/kustomization.yaml
resources:
- deployment.yaml
- service.yaml
- cronjob.yaml
EOF

Normally, everything is working fine, but sometime my project don't require to run a cronjob, so I want to disable the cronjob.yaml import. So, Is there a way to do that? for example like the jinja2,

- deployment.yaml
- service.yaml
{{ IF set CRONJOB }}
- cronjob.yaml
{{ ENDIF }}

I Understand @AniAggarwal posted, i can use a filter before run my kubectl apply -f but it no a very good for my project. Any suggestions are welcome.

Syscall
  • 19,327
  • 10
  • 37
  • 52
Frank AK
  • 1,705
  • 15
  • 28
  • Have you made a change to cronjob.yaml? If so it will be changed.. if you want to selectively apply resources then you might want to consider using some yml templating too such as ytt. You can pass a flag there which will then apply your desired resource. – Ani Aggarwal Apr 12 '21 at 03:16
  • @AniAggarwal Oh really? how to do that? For me, sometime I want my template only support service and deployment . – Frank AK Apr 12 '21 at 03:18
  • Checkout the below projects: https://github.com/Agilicus/yaml_filter or https://carvel.dev. more specifically check out this example https://carvel.dev/ytt/#example:example-if – Ani Aggarwal Apr 12 '21 at 03:19
  • @AniAggarwal I have tried your way, but not good for me, any good idea? for example, can i use the patchesStrategicMerge? – Frank AK Apr 12 '21 at 03:42
  • Can you explain the use case a bit more? I'm a trying to get a picture of what is it that you're trying to achieve? – Ani Aggarwal Apr 12 '21 at 03:46

1 Answers1

0

Assume the following is your file that gets generated after kustomize command. And the service is supposed to be conditional like your cronjob.

kind: Pod
apiVersion: v1
metadata:
  name: echo-app
  labels: 
    app: demo
spec:
  containers:
  - name: nginx
    image: nginx

#@ if/end data.values.service.enabled:
---
kind: Service
apiVersion: v1
metadata:
  name: echo-service
spec:
  selector:
    labels:
      app: name
  ports:
  - name: port
    port: 80

You can pipe the output of Your kustomize command to ytt to add or remove the service.

kustomize build | ytt --data-value service.enabled=false -f - | kubectl apply -f - 

Checkout the project playground for another example https://carvel.dev/ytt/#playground https://github.com/vmware-tanzu/carvel-ytt/blob/develop/examples/data-values/run.sh

Hope it is the solution you're looking for. Good luck!

Ani Aggarwal
  • 361
  • 4
  • 16