Skip to content

Commit 6e82842

Browse files
committed
whereabouts watcher
we have to break the tokenwatcher section of the whereabouts script out because it was running in an init container, so it would never "complete", hanging the rollout to accomplish this I created a new whereabouts watcher ds to house this section of the script Signed-off-by: Benjamin Pickard <[email protected]>
1 parent 717bee1 commit 6e82842

File tree

1 file changed

+167
-33
lines changed

1 file changed

+167
-33
lines changed

bindata/network/multus/multus.yaml

Lines changed: 167 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ spec:
567567
568568
CNI_BIN_DIR=${CNI_BIN_DIR:-"/host/opt/cni/bin/"}
569569
WHEREABOUTS_KUBECONFIG_FILE_HOST=${WHEREABOUTS_KUBECONFIG_FILE_HOST:-"/etc/cni/net.d/whereabouts.d/whereabouts.kubeconfig"}
570-
CNI_CONF_DIR=${CNI_CONF_DIR:-"/host/etc/cni/net.d"}
570+
CNI_CONF_DIR=${CNI_CONF_DIR:-"/host{{ .SystemCNIConfDir }}"}
571571
WHEREABOUTS_RECONCILER_CRON=${WHEREABOUTS_RECONCILER_CRON:-30 4 * * *}
572572
573573
# Make a whereabouts.d directory (for our kubeconfig)
@@ -655,8 +655,8 @@ spec:
655655
EOF
656656
657657
# Copy the config from ConfigMap to the desired directory
658-
cp /etc/whereabouts/config/whereabouts.conf $WHEREABOUTS_GLOBALCONFIG
659-
chmod 600 $ WHEREABOUTS_CONF_FILE
658+
cp /etc/whereabouts/config/whereabouts.conf $WHEREABOUTS_CONF_FILE
659+
chmod 600 $WHEREABOUTS_CONF_FILE
660660
661661
else
662662
warn "Doesn't look like we're running in a kubernetes environment (no serviceaccount token)"
@@ -680,24 +680,12 @@ spec:
680680
681681
}
682682
683-
function get_token_md5sum {
684-
md5sum "$SERVICE_ACCOUNT_TOKEN_PATH" | awk '{print $1}'
685-
}
686-
687-
function get_ca_file_md5sum {
688-
if [ ! -f "$KUBE_CA_FILE" ]; then
689-
echo ""
690-
return
691-
fi
692-
md5sum "$KUBE_CA_FILE" | awk '{print $1}'
693-
}
694683
generateKubeConfig
695-
export LAST_SERVICEACCOUNT_MD5SUM="$(get_token_md5sum)"
696-
export LAST_KUBE_CA_FILE_MD5SUM="$(get_ca_file_md5sum)"
697684
# ------------------ end Generate a "kube-config"
698685
699686
# ----------------- Generate a whereabouts conf
700-
generateWhereaboutsConf
687+
# removed because we have the configmap
688+
#generateWhereaboutsConf
701689
# ---------------- End generate a whereabouts conf
702690
703691
@@ -707,22 +695,6 @@ spec:
707695
708696
# ---------------------- end generate a "kube-config".
709697
710-
echo "Sleep and Watching for service account token and CA file changes..."
711-
# enter sleep/watch loop
712-
while true; do
713-
# Check the md5sum of the service account token and ca.
714-
svcaccountsum="$(get_token_md5sum)"
715-
casum="$(get_ca_file_md5sum)"
716-
if [ "$svcaccountsum" != "$LAST_SERVICEACCOUNT_MD5SUM" ] || ! [ "$SKIP_TLS_VERIFY" == "true" ] && [ "$casum" != "$LAST_KUBE_CA_FILE_MD5SUM" ]; then
717-
log "Detected service account or CA file change, regenerating kubeconfig..."
718-
generateKubeConfig
719-
LAST_SERVICEACCOUNT_MD5SUM="$svcaccountsum"
720-
LAST_KUBE_CA_FILE_MD5SUM="$casum"
721-
fi
722-
723-
sleep 1s
724-
done
725-
726698
# Unless told otherwise, sleep forever.
727699
# This prevents Kubernetes from restarting the pod repeatedly.
728700
should_sleep=${SLEEP:-"true"}
@@ -953,5 +925,167 @@ spec:
953925
items:
954926
- key: whereabouts.conf
955927
path: whereabouts.conf
928+
---
929+
kind: DaemonSet
930+
apiVersion: apps/v1
931+
metadata:
932+
name: whereabouts-token-watcher
933+
namespace: openshift-multus
934+
annotations:
935+
kubernetes.io/description: |
936+
This deamon watches over the whereabouts service account token and CA
937+
file for changes and will regenerate a kubeconfig if changes are seen
938+
spec:
939+
selector:
940+
matchLabels:
941+
app: whereabouts-token-watcher
942+
updateStrategy:
943+
type: RollingUpdate
944+
rollingUpdate:
945+
maxUnavailable: 10%
946+
template:
947+
metadata:
948+
annotations:
949+
target.workload.openshift.io/management: '{"effect": "PreferredDuringScheduling"}'
950+
# prevent blocks when node critical pods get evicted prior to workloads
951+
cluster-autoscaler.kubernetes.io/enable-ds-eviction: "false"
952+
labels:
953+
app: whereabouts-token-watcher
954+
spec:
955+
nodeSelector:
956+
kubernetes.io/os: linux
957+
priorityClassName: "system-node-critical"
958+
tolerations:
959+
- operator: Exists
960+
serviceAccountName: multus-ancillary-tools
961+
containers:
962+
- name: whereabouts-token-watcher
963+
image: {{.WhereaboutsImage}}
964+
command:
965+
- /bin/sh
966+
- -c
967+
- |
968+
#!/bin/sh
969+
970+
set -u -e
971+
972+
SERVICE_ACCOUNT_PATH=/var/run/secrets/kubernetes.io/serviceaccount
973+
KUBE_CA_FILE=${KUBE_CA_FILE:-$SERVICE_ACCOUNT_PATH/ca.crt}
974+
SERVICE_ACCOUNT_TOKEN=$(cat $SERVICE_ACCOUNT_PATH/token)
975+
SERVICE_ACCOUNT_TOKEN_PATH=$SERVICE_ACCOUNT_PATH/token
976+
SKIP_TLS_VERIFY=${SKIP_TLS_VERIFY:-false}
977+
978+
function generateKubeConfig {
979+
# Check if we're running as a k8s pod.
980+
if [ -f "$SERVICE_ACCOUNT_PATH/token" ]; then
981+
# We're running as a k8d pod - expect some variables.
982+
if [ -z ${KUBERNETES_SERVICE_HOST} ]; then
983+
error "KUBERNETES_SERVICE_HOST not set"; exit 1;
984+
fi
985+
if [ -z ${KUBERNETES_SERVICE_PORT} ]; then
986+
error "KUBERNETES_SERVICE_PORT not set"; exit 1;
987+
fi
988+
989+
if [ "$SKIP_TLS_VERIFY" == "true" ]; then
990+
TLS_CFG="insecure-skip-tls-verify: true"
991+
elif [ -f "$KUBE_CA_FILE" ]; then
992+
TLS_CFG="certificate-authority-data: $(cat $KUBE_CA_FILE | base64 | tr -d '\n')"
993+
fi
994+
995+
# Kubernetes service address must be wrapped if it is IPv6 address
996+
KUBERNETES_SERVICE_HOST_WRAP=$KUBERNETES_SERVICE_HOST
997+
if [ "$KUBERNETES_SERVICE_HOST_WRAP" != "${KUBERNETES_SERVICE_HOST_WRAP#*:[0-9a-fA-F]}" ]; then
998+
KUBERNETES_SERVICE_HOST_WRAP=\[$KUBERNETES_SERVICE_HOST_WRAP\]
999+
fi
1000+
# Write a kubeconfig file for the CNI plugin. Do this
1001+
# to skip TLS verification for now. We should eventually support
1002+
# writing more complete kubeconfig files. This is only used
1003+
# if the provided CNI network config references it.
1004+
touch $WHEREABOUTS_KUBECONFIG
1005+
chmod ${KUBECONFIG_MODE:-600} $WHEREABOUTS_KUBECONFIG
1006+
cat > $WHEREABOUTS_KUBECONFIG <<EOF
1007+
# Kubeconfig file for the Whereabouts CNI plugin.
1008+
apiVersion: v1
1009+
kind: Config
1010+
clusters:
1011+
- name: local
1012+
cluster:
1013+
server: ${KUBERNETES_SERVICE_PROTOCOL:-https}://${KUBERNETES_SERVICE_HOST_WRAP}:${KUBERNETES_SERVICE_PORT}
1014+
$TLS_CFG
1015+
users:
1016+
- name: whereabouts
1017+
user:
1018+
token: "${SERVICE_ACCOUNT_TOKEN}"
1019+
contexts:
1020+
- name: whereabouts-context
1021+
context:
1022+
cluster: local
1023+
user: whereabouts
1024+
namespace: ${WHEREABOUTS_NAMESPACE}
1025+
current-context: whereabouts-context
1026+
EOF
1027+
1028+
else
1029+
warn "Doesn't look like we're running in a kubernetes environment (no serviceaccount token)"
1030+
fi
1031+
1032+
}
1033+
1034+
function get_token_md5sum {
1035+
md5sum "$SERVICE_ACCOUNT_TOKEN_PATH" | awk '{print $1}'
1036+
}
1037+
1038+
function get_ca_file_md5sum {
1039+
if [ ! -f "$KUBE_CA_FILE" ]; then
1040+
echo ""
1041+
return
1042+
fi
1043+
md5sum "$KUBE_CA_FILE" | awk '{print $1}'
1044+
}
1045+
1046+
export LAST_SERVICEACCOUNT_MD5SUM="$(get_token_md5sum)"
1047+
export LAST_KUBE_CA_FILE_MD5SUM="$(get_ca_file_md5sum)"
1048+
1049+
echo "Sleep and Watching for service account token and CA file changes..."
1050+
# enter sleep/watch loop
1051+
while true; do
1052+
# Check the md5sum of the service account token and ca.
1053+
svcaccountsum="$(get_token_md5sum)"
1054+
casum="$(get_ca_file_md5sum)"
1055+
if [ "$svcaccountsum" != "$LAST_SERVICEACCOUNT_MD5SUM" ] || ! [ "$SKIP_TLS_VERIFY" == "true" ] && [ "$casum" != "$LAST_KUBE_CA_FILE_MD5SUM" ]; then
1056+
log "Detected service account or CA file change, regenerating kubeconfig..."
1057+
generateKubeConfig
1058+
LAST_SERVICEACCOUNT_MD5SUM="$svcaccountsum"
1059+
LAST_KUBE_CA_FILE_MD5SUM="$casum"
1060+
fi
1061+
1062+
sleep 1s
1063+
done
1064+
1065+
resources:
1066+
requests:
1067+
cpu: 10m
1068+
memory: 10Mi
1069+
terminationMessagePolicy: FallbackToLogsOnError
1070+
volumeMounts:
1071+
- name: whereabouts-configmap
1072+
mountPath: /etc/wherabouts/config
1073+
env:
1074+
- name: KUBERNETES_SERVICE_PORT
1075+
value: "{{.KUBERNETES_SERVICE_PORT}}"
1076+
- name: KUBERNETES_SERVICE_HOST
1077+
value: "{{.KUBERNETES_SERVICE_HOST}}"
1078+
- name: CNI_BIN_DIR
1079+
value: "/host/opt/cni/bin/"
1080+
- name: CNI_CONF_DIR
1081+
value: "/host/etc/cni/net.d"
1082+
- name: SLEEP
1083+
value: "false"
1084+
- name: WHEREABOUTS_NAMESPACE
1085+
value: "openshift-multus"
1086+
volumes:
1087+
- name: whereabouts-configmap
1088+
configMap:
1089+
name: whereabouts-config
9561090
{{- end}}
9571091
---

0 commit comments

Comments
 (0)