0

I am trying to use a k8s cron job to schedule API calls to a service. I am new to k8s cron jobs. The service requires an Auth token in the request header for authorization. That auth token can be retrieved from Token service(another service) call. Is it possible to fetch from the cURL response by first calling the Token service from the cron job and then calling the actual service. Any help...

Also the Token service needs some client details for generating the auth token, so is there any way to feed the details to the job every time it gets triggered via cron job.

Here is what I want to achieve:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      suspend: false
      backoffLimit: 2
      template:
        spec:
          containers:
          - name: hello
            image: nightfury1204/alpine-curl
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - curl http://TokenService {get response data, use token in the next call}
            - curl http://TaskService -H 'Authorization:Bearer <above_token>'
          restartPolicy: OnFailure

Have searched through some documentations and gone through some stack overflow questions, but did not get any reference on the same.

Biswarup Nath
  • 196
  • 1
  • 7
  • what did you try? – rok Nov 21 '22 at 08:01
  • It depends what your `TokenService` returns. Basically there is no cronjob special helpers for such task. I believe all you need is just right Bash script. Another option is to create your own docker image with this logic implemented inside, written on your favorite programming language – Evgeny K Nov 21 '22 at 09:25
  • Have you tried using `postStart` with `pod hook`, does that solve your problem? Or use `init pod` `initContainers` , by initializing the container and sharing data with the normal container – 山河以无恙 Nov 21 '22 at 10:10
  • @Evgeny Agree to the bash script. I tried to do that, but facing some proxy issue with the alpine:latest image while running apk add command mentioning permission denied. "ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.16/main: Permission denied ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.16/community: Permission denied WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.16/community: No such file or directory 2 errors; 14 distinct packages available". Hope I would be able to resolve that pretty soon. – Biswarup Nath Nov 21 '22 at 14:09

0 Answers0