Skip to content

Commit 4870ac2

Browse files
Merge pull request #21 from codefresh-io/update-kubectl-version-logic
Update kubectl version logic
2 parents c094eb5 + 027fa3d commit 4870ac2

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

Dockerfile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
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.12.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/
1717
COPY --from=builder kubectl1.14 /usr/local/bin/
18-
COPY --from=builder kubectl1.10 /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+
# Set Default
23+
COPY --from=builder kubectl1.14 /usr/local/bin/kubectl
24+
25+
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
2226

2327
WORKDIR /
2428

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: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,55 @@ 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 [[ -n "${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}" -eq "15" ]]; then
75+
KUBE_CTL="15"
76+
elif [[ "${SERVER_VERSION}" -le "14" && "${SERVER_VERSION}" -ge "13" ]]; then
77+
KUBE_CTL="14"
78+
elif [[ "${SERVER_VERSION}" -le "12" && "${SERVER_VERSION}" -ge "11" ]]; then
79+
KUBE_CTL="12"
80+
elif [[ "${SERVER_VERSION}" -le "10" && "${SERVER_VERSION}" -ge "9" ]]; then
81+
KUBE_CTL="10"
82+
elif [[ "${SERVER_VERSION}" -ge "6" ]]; then
83+
KUBE_CTL="6"
84+
else
85+
echo "kubectl version: v1.${SERVER_VERSION}"
86+
fatal "Version Not Supported!!!"
87+
exit 1
88+
fi
89+
90+
# Assign kubectl version
91+
echo "Setting kubectl to version 1.${KUBE_CTL}"
92+
cp -f "/usr/local/bin/kubectl1.${KUBE_CTL}" /usr/local/bin/kubectl
93+
94+
# Simple testing logic for making sure versions are set
95+
if [[ -n "${KUBE_CTL_TEST_VERSION}" ]]; then
96+
KUBE_CTL_VERSION=`kubectl version --client --short`
97+
echo "Testing kubectl version is set..."
98+
if [[ "${KUBE_CTL_VERSION}" == *"${KUBE_CTL_TEST_VERSION}"* ]]; then
99+
echo "Version correctly set"
100+
echo "Kubectl Version: ${KUBE_CTL_VERSION}"
101+
echo "Test Version: ${KUBE_CTL_TEST_VERSION}"
102+
exit 0
103+
else
104+
echo "Kubectl Version: ${KUBE_CTL_VERSION}"
105+
echo "Test Version: ${KUBE_CTL_TEST_VERSION}"
106+
fatal "Version Mismatch!!!"
107+
exit 1
108+
fi
109+
fi
110+
68111
[ ! -f "${deployment_file}" ] && echo "Couldn't find $deployment_file file at $(pwd)" && exit 1;
69112

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

0 commit comments

Comments
 (0)