#!/bin/bash

# sudo snap install yq
# 0,30 * * * * ~/driving/sim/marvel/jenkins/scripts/refresh_jenkins_kube_token.sh >/dev/null 2>&1

TOKEN=$(~/.local/bin/gettoken.py | jq -r .status.token)
KUBE_CONFIG=$(cat ~/.kube/config | /snap/bin/yq w - 'users[0].user[token]' $TOKEN | /snap/bin/yq d - 'users[0].user[exec]' | /snap/bin/yq w - 'contexts[0].context.namespace' marvel2-dev)

curl -X POST --user ${JENKINS_API_USER}:${JENKINS_API_TOKEN} \
-H 'Content-Type: application/xml' \
--url 'https://marvel-jenkins.zooxlabs.com/credentials/store/system/domain/_/credential/tkang-kube-config/config.xml' \
--data "<com.microsoft.jenkins.kubernetes.credentials.KubeconfigCredentials plugin=\"kubernetes-cd@2.3.0\">
    <scope>GLOBAL</scope>
    <id>tkang-kube-config</id>
    <description>tkang&apos;s personal kube config</description>
    <kubeconfigSource class=\"com.microsoft.jenkins.kubernetes.credentials.KubeconfigCredentials\$DirectEntryKubeconfigSource\">
        <content>${KUBE_CONFIG}</content>
    </kubeconfigSource>
</com.microsoft.jenkins.kubernetes.credentials.KubeconfigCredentials>"
