Skip to content

Commit

Permalink
Merge pull request kubernetes#6384 from erictune/cleanup-addons.sh
Browse files Browse the repository at this point in the history
Use same addons script for init.d and systemd.
  • Loading branch information
zmerlynn committed Apr 2, 2015
2 parents 66c6c48 + b9570b3 commit c627a35
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 62 deletions.
14 changes: 7 additions & 7 deletions cluster/saltbase/salt/kube-addons/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@
- makedirs: True
{% endif %}

{% if grains['os_family'] == 'RedHat' %}

/usr/lib/systemd/system/kube-addons.service:
/etc/kubernetes/kube-addons.sh:
file.managed:
- source: salt://kube-addons/kube-addons.service
- source: salt://kube-addons/kube-addons.sh
- user: root
- group: root
- mode: 755

/etc/kubernetes/kube-addons.sh:
{% if grains['os_family'] == 'RedHat' %}

/usr/lib/systemd/system/kube-addons.service:
file.managed:
- source: salt://kube-addons/kube-addons.sh
- source: salt://kube-addons/kube-addons.service
- user: root
- group: root
- mode: 755

{% else %}

Expand Down
46 changes: 2 additions & 44 deletions cluster/saltbase/salt/kube-addons/initd
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,22 @@ NAME=kube-addons
DAEMON_LOG_FILE=/var/log/$NAME.log
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
KUBECTL=/usr/local/bin/kubectl
KUBE_ADDONS_SH=/etc/kubernetes/kube-addons.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

# $1 addon to start.
# $2 count of tries to start the addon.
# $3 delay in seconds between two consecutive tries
function start_addon() {
addon=$1;
tries=$2;
delay=$3;
while [ ${tries} -gt 0 ]; do
${KUBECTL} create -f ${addon} && \
echo "== Successfully started ${addon} at $(date -Is)" && \
return 0;
let tries=tries-1;
echo "== Failed to start ${addon} at $(date -Is). ${tries} tries remaining. =="
sleep ${delay};
done
return 1;
}

function addon_manager_async() {
# The business logic for whether a given object should be created
# was already enforced by salt, and /etc/kubernetes/addons is the
# managed result is of that. Start everything below that directory.
echo "== Kubernetes addon manager started at $(date -Is) =="
for obj in $(find /etc/kubernetes/addons -name \*.yaml); do
start_addon ${obj} 100 10 &
echo "++ addon ${obj} starting in pid $! ++"
done
noerrors="true"
for pid in $(jobs -p); do
wait ${pid} || noerrors="false"
echo "++ pid ${pid} complete ++"
done
if [ ${noerrors} == "true" ]; then
echo "== Kubernetes addon manager completed successfully at $(date -Is) =="
else
echo "== Kubernetes addon manager completed with errors at $(date -Is) =="
fi

# We stay around so that status checks by salt make it look like
# the service is good. (We could do this is other ways, but this
# is simple.)
sleep infinity
}


#
# Function that starts the daemon/service
#
do_start()
{
addon_manager_async </dev/null >>${DAEMON_LOG_FILE} 2>&1 &
${KUBE_ADDONS_SH} </dev/null >>${DAEMON_LOG_FILE} 2>&1 &
echo $! > ${PIDFILE}
disown
}
Expand Down
31 changes: 20 additions & 11 deletions cluster/saltbase/salt/kube-addons/kube-addons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,31 @@
# managed result is of that. Start everything below that directory.
KUBECTL=/usr/local/bin/kubectl

function create-object() {
obj=$1

for tries in {1..5}; do
if ${KUBECTL} --server="127.0.0.1:8080" create --validate=true -f ${obj}; then
return
fi
echo "++ ${obj} failed, attempt ${try} (sleeping 5) ++"
sleep 5
# $1 addon to start.
# $2 count of tries to start the addon.
# $3 delay in seconds between two consecutive tries
function start_addon() {
addon=$1;
tries=$2;
delay=$3;
while [ ${tries} -gt 0 ]; do
${KUBECTL} create -f ${addon} && \
echo "== Successfully started ${addon} at $(date -Is)" && \
return 0;
let tries=tries-1;
echo "== Failed to start ${addon} at $(date -Is). ${tries} tries remaining. =="
sleep ${delay};
done
return 1;
}

# The business logic for whether a given object should be created
# was already enforced by salt, and /etc/kubernetes/addons is the
# managed result is of that. Start everything below that directory.
echo "== Kubernetes addon manager started at $(date -Is) =="
for obj in $(find /etc/kubernetes/addons -name \*.yaml); do
create-object ${obj} &
echo "++ addon ${obj} started in pid $! ++"
start_addon ${obj} 100 10 &
echo "++ addon ${obj} starting in pid $! ++"
done
noerrors="true"
for pid in $(jobs -p); do
Expand Down

0 comments on commit c627a35

Please sign in to comment.