Skip to content

Commit 0a149c1

Browse files
updating logic and adding some simple test support
1 parent c094eb5 commit 0a149c1

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

Dockerfile

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
FROM alpine:3.6 AS builder
1+
FROM alpine:3.9 AS builder
22

33
RUN apk update && apk add curl
44

5-
RUN curl -o kubectl1.14 -L https://storage.googleapis.com/kubernetes-release/release/v1.14.0/bin/linux/amd64/kubectl
65
RUN curl -o kubectl1.15 -L https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/linux/amd64/kubectl
6+
RUN curl -o kubectl1.14 -L https://storage.googleapis.com/kubernetes-release/release/v1.14.0/bin/linux/amd64/kubectl
7+
RUN curl -o kubectl1.12 -L https://storage.googleapis.com/kubernetes-release/release/v1.10.0/bin/linux/amd64/kubectl
78
RUN curl -o kubectl1.10 -L https://storage.googleapis.com/kubernetes-release/release/v1.10.0/bin/linux/amd64/kubectl
89
RUN curl -o kubectl1.6 -L https://storage.googleapis.com/kubernetes-release/release/v1.6.0/bin/linux/amd64/kubectl
910

10-
11-
FROM alpine:3.6
11+
FROM alpine:3.9
1212

1313
RUN apk add --update bash
1414

1515
#copy all versions of kubectl to switch between them later.
1616
COPY --from=builder kubectl1.15 /usr/local/bin/
17-
COPY --from=builder kubectl1.14 /usr/local/bin/
18-
COPY --from=builder kubectl1.10 /usr/local/bin/kubectl
17+
COPY --from=builder kubectl1.14 /usr/local/bin/kubectl
18+
COPY --from=builder kubectl1.12 /usr/local/bin/
19+
COPY --from=builder kubectl1.10 /usr/local/bin/
1920
COPY --from=builder kubectl1.6 /usr/local/bin/
2021

21-
RUN chmod +x /usr/local/bin/kubectl /usr/local/bin/kubectl1.6 /usr/local/bin/kubectl1.14 /usr/local/bin/kubectl1.15
22+
RUN chmod +x /usr/local/bin/kubectl /usr/local/bin/kubectl1.6 /usr/local/bin/kubectl1.10 /usr/local/bin/kubectl1.12 /usr/local/bin/kubectl1.14 /usr/local/bin/kubectl1.15
2223

2324
WORKDIR /
2425

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ The following env variables control the deployment configuration:
2424
3. KUBERNETES_NAMESPACE - The namespace to deploy
2525
4. KUBECTL_ACTION - means an action for `kubectl <action>`. Valid values are apply|create|replace. Default is "apply"
2626

27+
Optional:
28+
`SERVER_VERSION` - Used mainly for testing. Manually set the Minor Kubernetes version.
29+
2730
# Usage in codefresh.io
2831

2932
### deployment.yml

cf-deploy-kubernetes.sh

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,49 @@ else
5959
fi
6060
fi
6161

62-
#check the cluster version and decide which version of kubectl to use:
63-
SERVER_VERSION=$(kubectl version --short=true --context "${KUBECONTEXT}" | grep -i server | cut -d ':' -f2 | cut -d '.' -f2 | sed 's/[^0-9]*//g')
64-
echo "Server minor version: $SERVER_VERSION"
65-
if (( "$SERVER_VERSION" <= "6" )); then cp -f /usr/local/bin/kubectl1.6 /usr/local/bin/kubectl; fi 2>/dev/null
66-
if (( "$SERVER_VERSION" == "14" )); then cp -f /usr/local/bin/kubectl1.14 /usr/local/bin/kubectl; fi 2>/dev/null
67-
if (( "$SERVER_VERSION" >= "15" )); then cp -f /usr/local/bin/kubectl1.15 /usr/local/bin/kubectl; fi 2>/dev/null
62+
# Add SERVER_VERSION override and testing capabilities
63+
64+
if [[ -z "${SERVER_VERSION}" ]]; then
65+
# Dynamically define SERVER_VERSION using kube context
66+
echo "Statically defined version: ${SERVER_VERSION}"
67+
else
68+
# Dynamically define SERVER_VERSION using kube context
69+
SERVER_VERSION=$(kubectl version --short=true --context "${KUBECONTEXT}" | grep -i server | cut -d ':' -f2 | cut -d '.' -f2 | sed 's/[^0-9]*//g')
70+
echo "Dynamically defined version: ${SERVER_VERSION}"
71+
fi
72+
73+
# Determine appropriate kubectl version
74+
if (( "$SERVER_VERSION" >= "15" )); then
75+
KUBE_CTL="15"
76+
elif (( "${SERVER_VERSION}" >= "13" && "${SERVER_VERSION}" <= "14")); )); then
77+
KUBE_CTL="14"
78+
elif (( "${SERVER_VERSION}" >= "12" && "${SERVER_VERSION}" <= "11")); )); then
79+
KUBE_CTL="12"
80+
elif (( "${SERVER_VERSION}" >= "10" && "${SERVER_VERSION}" <= "9")); )); then
81+
KUBE_CTL="10"
82+
elif (( "${SERVER_VERSION}" <= "6" )); then
83+
KUBE_CTL="6"
84+
fi
85+
86+
# Simple testing logic for making sure versions are set
87+
if [[ -z "${KUBE_CTL_TEST_VERSION}" ]]; then
88+
if [ "${KUBE_CTL}" == "${KUBE_CTL_TEST_VERSION}" ]; then
89+
echo "Version correctly set"
90+
echo "Kubectl Version: ${KUBE_CTL}"
91+
echo "Test Version: ${KUBE_CTL_TEST_VERSION}"
92+
exit 0
93+
else
94+
echo "Version Mismatch"
95+
echo "Kubectl Version: ${KUBE_CTL}"
96+
echo "Test Version: ${KUBE_CTL_TEST_VERSION}"
97+
exit 1
98+
fi
99+
100+
# Assign kubectl version unless default
101+
if (( "$KUBE_CTL" != "14" )); then
102+
cp -f /usr/local/bin/kubectl1.${KUBE_CTL} /usr/local/bin/kubectl
103+
fi
104+
68105
[ ! -f "${deployment_file}" ] && echo "Couldn't find $deployment_file file at $(pwd)" && exit 1;
69106

70107
DEPLOYMENT_FILE=${deployment_file}-$(date '+%y-%m-%d_%H-%M-%S').yml

0 commit comments

Comments
 (0)