Skip to content

Commit bd44ef8

Browse files
committed
Fix kube-addon-manager overwriting resources with EnsureExists
The addon manager readme states > - Addons with label `addonmanager.kubernetes.io/mode=EnsureExists` will be checked for > existence only. Users can edit these addons as they want. However, the start_addon function was using `kubectl apply` to create resources regardless of mode. This change switches between `kubectl create` and `kubectl apply` depending on mode. Additionally implemented tests for create_resource fn - Refactor functions and main executable - Quick tests in bash - Tests for Reconcile, EnsureExists behavior - Check for completeness with multi resource configs
1 parent d159ae3 commit bd44ef8

File tree

6 files changed

+442
-57
lines changed

6 files changed

+442
-57
lines changed

cluster/addons/addon-manager/CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
## Version 9.1.1 (Wed May 19 2020 Antoni Zawodny <[email protected]>)
1+
### Version 9.1.2 (Thu August 6 2020 Spencer Peterson <[email protected]>)
2+
- Fix `start_addon` overwriting resources with `addonmanager.kubernetes.io/mode=EnsureExists`.
3+
4+
### Version 9.1.1 (Wed May 19 2020 Antoni Zawodny <[email protected]>)
25
- Fix kube-addons.sh and kubectl permissions
36

4-
## Version 9.1.0 (Wed May 13 2020 Antoni Zawodny <[email protected]>)
7+
### Version 9.1.0 (Wed May 13 2020 Antoni Zawodny <[email protected]>)
58
- Enable overriding the default list of whitelisted resources
69

710
### Version 9.0.2 (Thu August 1 2019 Maciej Borsz <[email protected]>

cluster/addons/addon-manager/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ FROM BASEIMAGE
1717
RUN clean-install bash
1818

1919
ADD kube-addons.sh /opt/
20+
ADD kube-addons-main.sh /opt/
2021
ADD kubectl /usr/local/bin/
2122

22-
CMD ["/opt/kube-addons.sh"]
23+
CMD ["/opt/kube-addons-main.sh"]

cluster/addons/addon-manager/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
IMAGE=staging-k8s.gcr.io/kube-addon-manager
1616
ARCH?=amd64
1717
TEMP_DIR:=$(shell mktemp -d)
18-
VERSION=v9.1.1
18+
VERSION=v9.1.2
1919
KUBECTL_VERSION?=v1.13.2
2020

2121
BASEIMAGE=k8s.gcr.io/debian-base-$(ARCH):v1.0.0
@@ -29,7 +29,7 @@ all: build
2929
build:
3030
cp ./* $(TEMP_DIR)
3131
curl -sSL --retry 5 https://dl.k8s.io/release/$(KUBECTL_VERSION)/bin/linux/$(ARCH)/kubectl > $(TEMP_DIR)/kubectl
32-
chmod a+rx $(TEMP_DIR)/kube-addons.sh $(TEMP_DIR)/kubectl
32+
chmod a+rx $(TEMP_DIR)/kube-addons.sh $(TEMP_DIR)/kube-addons-main.sh $(TEMP_DIR)/kubectl
3333
cd $(TEMP_DIR) && sed -i.back "s|BASEIMAGE|$(BASEIMAGE)|g" Dockerfile
3434

3535
ifneq ($(ARCH),amd64)
@@ -48,5 +48,11 @@ ifeq ($(ARCH),amd64)
4848
docker push $(IMAGE):$(VERSION)
4949
endif
5050

51+
test:
52+
cp ./* $(TEMP_DIR)
53+
curl -sSL --retry 5 https://dl.k8s.io/release/$(KUBECTL_VERSION)/bin/linux/$(ARCH)/kubectl > $(TEMP_DIR)/kubectl
54+
chmod a+rx $(TEMP_DIR)/kube-addons.sh $(TEMP_DIR)/kube-addons-test.sh $(TEMP_DIR)/kubectl
55+
cd $(TEMP_DIR) && KUBECTL_BIN=$(TEMP_DIR)/kubectl ./kube-addons-test.sh
56+
5157
clean:
5258
docker rmi -f $(IMAGE)-$(ARCH):$(VERSION)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)