Skip to content

Commit 7b2c530

Browse files
Merge pull request #26 from codefresh-io/add-version-overrides
Add version overrides
2 parents 8fb3ed2 + 6532b7d commit 7b2c530

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

Dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ FROM alpine:3.6 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
5+
RUN curl -o kubectl1.16 -L https://storage.googleapis.com/kubernetes-release/release/v1.16.0/bin/linux/amd64/kubectl
66
RUN curl -o kubectl1.15 -L https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/linux/amd64/kubectl
7+
RUN curl -o kubectl1.14 -L https://storage.googleapis.com/kubernetes-release/release/v1.14.0/bin/linux/amd64/kubectl
8+
RUN curl -o kubectl1.13 -L https://storage.googleapis.com/kubernetes-release/release/v1.13.0/bin/linux/amd64/kubectl
9+
RUN curl -o kubectl1.12 -L https://storage.googleapis.com/kubernetes-release/release/v1.12.0/bin/linux/amd64/kubectl
10+
RUN curl -o kubectl1.11 -L https://storage.googleapis.com/kubernetes-release/release/v1.11.0/bin/linux/amd64/kubectl
711
RUN curl -o kubectl1.10 -L https://storage.googleapis.com/kubernetes-release/release/v1.10.0/bin/linux/amd64/kubectl
812
RUN curl -o kubectl1.6 -L https://storage.googleapis.com/kubernetes-release/release/v1.6.0/bin/linux/amd64/kubectl
913

@@ -13,12 +17,16 @@ FROM alpine:3.6
1317
RUN apk add --update bash
1418

1519
#copy all versions of kubectl to switch between them later.
20+
COPY --from=builder kubectl1.16 /usr/local/bin/
1621
COPY --from=builder kubectl1.15 /usr/local/bin/
1722
COPY --from=builder kubectl1.14 /usr/local/bin/
23+
COPY --from=builder kubectl1.13 /usr/local/bin/
24+
COPY --from=builder kubectl1.12 /usr/local/bin/
25+
COPY --from=builder kubectl1.11 /usr/local/bin/
1826
COPY --from=builder kubectl1.10 /usr/local/bin/kubectl
1927
COPY --from=builder kubectl1.6 /usr/local/bin/
2028

21-
RUN chmod +x /usr/local/bin/kubectl /usr/local/bin/kubectl1.6 /usr/local/bin/kubectl1.14 /usr/local/bin/kubectl1.15
29+
RUN chmod +x /usr/local/bin/kubectl /usr/local/bin/kubectl1.6 /usr/local/bin/kubectl1.11 /usr/local/bin/kubectl1.12 /usr/local/bin/kubectl1.13 /usr/local/bin/kubectl1.14 /usr/local/bin/kubectl1.15 /usr/local/bin/kubectl1.16
2230

2331
WORKDIR /
2432

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ 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+
29+
`SERVER_VERSION` - Manually set the Minor kubectl version. Supports 10-16.
30+
2731
# Usage in codefresh.io
2832

2933
### deployment.yml

cf-deploy-kubernetes.sh

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ if [[ -n "$KUBERNETES_SERVER" && -n "$KUBERNETES_USER" && -n "$KUBERNETES_PASSWO
4848
else
4949
if [[ -z "${KUBECONTEXT}" ]]; then
5050
KUBECONTEXT=$(kubectl config current-context)
51-
If KUBECONFIG is set we obligate to set KUBECONTEXT to valid context name
51+
# If KUBECONFIG is set we obligate to set KUBECONTEXT to valid context name
5252
if [[ -n "${KUBECONFIG}" ]]; then
5353
echo -e "--- ERROR - KUBECONTEXT Environment variable is not set, please set it to one of integrated contexts: "
5454
kubectl config get-contexts
@@ -59,13 +59,40 @@ 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
68-
[ ! -f "${deployment_file}" ] && echo "Couldn't find $deployment_file file at $(pwd)" && exit 1;
62+
# Add SERVER_VERSION override and testing capabilities
63+
64+
if [[ -n "${SERVER_VERSION}" ]]; then
65+
# Statically define SERVER_VERSION from variable override
66+
echo "Statically defined version: ${SERVER_VERSION}"
67+
# Assign kubectl version
68+
echo "Setting kubectl to version 1.${SERVER_VERSION}"
69+
cp -f "/usr/local/bin/kubectl1.${SERVER_VERSION}" /usr/local/bin/kubectl 2>/dev/null
70+
else
71+
#check the cluster version and decide which version of kubectl to use:
72+
SERVER_VERSION=$(kubectl version --short=true --context "${KUBECONTEXT}" | grep -i server | cut -d ':' -f2 | cut -d '.' -f2 | sed 's/[^0-9]*//g')
73+
echo "Server minor version: $SERVER_VERSION"
74+
if (( "$SERVER_VERSION" <= "6" )); then cp -f /usr/local/bin/kubectl1.6 /usr/local/bin/kubectl; fi 2>/dev/null
75+
if (( "$SERVER_VERSION" == "14" )); then cp -f /usr/local/bin/kubectl1.14 /usr/local/bin/kubectl; fi 2>/dev/null
76+
if (( "$SERVER_VERSION" >= "15" )); then cp -f /usr/local/bin/kubectl1.15 /usr/local/bin/kubectl; fi 2>/dev/null
77+
[ ! -f "${deployment_file}" ] && echo "Couldn't find $deployment_file file at $(pwd)" && exit 1;
78+
fi
79+
80+
# Simple testing logic for making sure override versions are set
81+
if [[ -n "${KUBE_CTL_TEST_VERSION}" ]]; then
82+
KUBE_CTL_VERSION=`kubectl version --client --short`
83+
echo "Testing kubectl version is set..."
84+
if [[ "${KUBE_CTL_VERSION}" == *"${KUBE_CTL_TEST_VERSION}"* ]]; then
85+
echo "Version correctly set"
86+
echo "Kubectl Version: ${KUBE_CTL_VERSION}"
87+
echo "Test Version: ${KUBE_CTL_TEST_VERSION}"
88+
exit 0
89+
else
90+
echo "Kubectl Version: ${KUBE_CTL_VERSION}"
91+
echo "Test Version: ${KUBE_CTL_TEST_VERSION}"
92+
fatal "Version Mismatch!!!"
93+
exit 1
94+
fi
95+
fi
6996

7097
DEPLOYMENT_FILE=${deployment_file}-$(date '+%y-%m-%d_%H-%M-%S').yml
7198
$(dirname $0)/template.sh "$deployment_file" > "$DEPLOYMENT_FILE" || fatal "Failed to apply deployment template on $deployment_file"

0 commit comments

Comments
 (0)