2

I want to create a crone in the cluster which should have access to all of the namespaces, I don't want to configure that job in each and every namespace as I have multiple namespaces. Is this possible? Edit: I want to run same cronjob in all namespace

Rohit W
  • 45
  • 7
  • >should have access to all of the namespaces, with that cron you want to access something in other namespace or what? or you want to run same job on all namespace? – Adiii Sep 29 '22 at 18:32
  • I want to run same job in all namespaces – Rohit W Sep 29 '22 at 18:33
  • there might be other approaches if you explain your use case – Adiii Sep 29 '22 at 18:44
  • How are you deploying your cluster? This can wildly affect the complexity of achieving your desired goal. For example - ArgoCD/Flux make it much easier to assure each namespace contains a set of predefined objects. – Yaron Idan Sep 29 '22 at 19:17

1 Answers1

1

You can use helm to achieve this, either static namespaces or dynamic all namespaces.

{{- range $namespaces := .Values.namespaces }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
  namespace: {{ $namespaces }}
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox:1.28
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure
---
{{- end }} 

and values file

namespaces:
  - namesapce-1
  - namesapce-2

Dynamic namespaces:

helm install my-cronjob  helm-chart --set "namespaces={$(k get ns | awk 'BEGIN{ORS=","} { if (NR>1) print $1}')}"

A complete working example multi-namespace-deployment

Adiii
  • 54,482
  • 7
  • 145
  • 148