|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2020 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Import required functions. The addon manager is installed to /opt in |
| 18 | +# production use (see the Dockerfile) |
| 19 | +# Disabling shellcheck following files as the full path would be required. |
| 20 | +if [ -f "kube-addons.sh" ]; then |
| 21 | + # shellcheck disable=SC1091 |
| 22 | + source "kube-addons.sh" |
| 23 | +elif [ -f "/opt/kube-addons.sh" ]; then |
| 24 | + # shellcheck disable=SC1091 |
| 25 | + source "/opt/kube-addons.sh" |
| 26 | +else |
| 27 | + # If the required source is missing, we have to fail. |
| 28 | + log ERR "== Could not find kube-addons.sh (not in working directory or /opt) at $(date -Is) ==" |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +# The business logic for whether a given object should be created |
| 33 | +# was already enforced by salt, and /etc/kubernetes/addons is the |
| 34 | +# managed result of that. Start everything below that directory. |
| 35 | +log INFO "== Kubernetes addon manager started at $(date -Is) with ADDON_CHECK_INTERVAL_SEC=${ADDON_CHECK_INTERVAL_SEC} ==" |
| 36 | + |
| 37 | +# Wait for the default service account to be created in the kube-system namespace. |
| 38 | +token_found="" |
| 39 | +while [ -z "${token_found}" ]; do |
| 40 | + sleep .5 |
| 41 | + # shellcheck disable=SC2086 |
| 42 | + # Disabling because "${KUBECTL_OPTS}" needs to allow for expansion here |
| 43 | + if ! token_found=$(${KUBECTL} ${KUBECTL_OPTS} get --namespace="${SYSTEM_NAMESPACE}" serviceaccount default -o go-template="{{with index .secrets 0}}{{.name}}{{end}}"); then |
| 44 | + token_found=""; |
| 45 | + log WRN "== Error getting default service account, retry in 0.5 second ==" |
| 46 | + fi |
| 47 | +done |
| 48 | + |
| 49 | +log INFO "== Default service account in the ${SYSTEM_NAMESPACE} namespace has token ${token_found} ==" |
| 50 | + |
| 51 | +# Create admission_control objects if defined before any other addon services. If the limits |
| 52 | +# are defined in a namespace other than default, we should still create the limits for the |
| 53 | +# default namespace. |
| 54 | +while IFS=$'\n' read -r obj; do |
| 55 | + start_addon "${obj}" 100 10 default & |
| 56 | + log INFO "++ obj ${obj} is created ++" |
| 57 | +done < <(find /etc/kubernetes/admission-controls \( -name \*.yaml -o -name \*.json \)) |
| 58 | + |
| 59 | +# Start the apply loop. |
| 60 | +# Check if the configuration has changed recently - in case the user |
| 61 | +# created/updated/deleted the files on the master. |
| 62 | +log INFO "== Entering periodical apply loop at $(date -Is) ==" |
| 63 | +while true; do |
| 64 | + start_sec=$(date +"%s") |
| 65 | + if is_leader; then |
| 66 | + ensure_addons |
| 67 | + reconcile_addons |
| 68 | + else |
| 69 | + log INFO "Not elected leader, going back to sleep." |
| 70 | + fi |
| 71 | + end_sec=$(date +"%s") |
| 72 | + len_sec=$((end_sec-start_sec)) |
| 73 | + # subtract the time passed from the sleep time |
| 74 | + if [[ ${len_sec} -lt ${ADDON_CHECK_INTERVAL_SEC} ]]; then |
| 75 | + sleep_time=$((ADDON_CHECK_INTERVAL_SEC-len_sec)) |
| 76 | + sleep ${sleep_time} |
| 77 | + fi |
| 78 | +done |
0 commit comments