blob: e22e4d18bc6094af922e9d17b6fc237afe98abe6 [file] [log] [blame]
Joey Armstrongac97b072024-08-23 14:35:21 -04001# -*- makefile -*-
2# -----------------------------------------------------------------------
3# Copyright 2024 Open Networking Foundation Contributors
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# SPDX-FileCopyrightText: 2024 Open Networking Foundation Contributors
18# SPDX-License-Identifier: Apache-2.0
19# -----------------------------------------------------------------------
20# Intent: Centralized logic for installing the kubectl command
21# -----------------------------------------------------------------------
22
23MAKEDIR ?= $(error MAKEDIR= is required)
24
25# -----------------------------------------------------------------------
26# Install command by version to prevent one bad download from
27# taking all jobs out of action. Do not rely on /usr/local/bin/kubectl,
28# command can easily become stagnant.
29# -----------------------------------------------------------------------
30
31# Supported versions
32kubectl-versions += v1.23
33kubectl-versions += v1.30.3
34kubectl-versions += v1.31.0
35
36# kubectl-ver ?= v1.23# # voltha v2.12 (?)
37kubectl-ver ?= v1.31.0# # 2024-08-23: latest release
38kubectl-ver ?= $(shell curl -L -s https://dl.k8s.io/release/stable.txt)
39kubectl-ver ?= $(error kubectl-ver= is required)
40
41kube-url := https://dl.k8s.io/release/$(kubectl-ver)/bin/linux/amd64/kubectl
42
43# -----------------------------------------------------------------------
44# Install the 'kubectl' tool if needed: https://github.com/boz/kubectl
45# o WORKSPACE - jenkins aware
46# o Default to /usr/local/bin/kubectl
47# + revisit this, system directories should not be a default path.
48# + requires sudo and potential exists for overwrite conflict.
49# -----------------------------------------------------------------------
50KUBECTL_PATH ?= $(if $(WORKSPACE),$(WORKSPACE)/bin,/usr/local/bin)
51kubectl-cmd ?= $(KUBECTL_PATH)/kubectl
52kubectl-ver-cmd := $(kubectl-cmd).$(kubectl-ver)
53
54# -----------------------------------------------------------------------
55# 1) Generic target for installing kubectl
56# -----------------------------------------------------------------------
57.PHONY: kubectl
58kubectl : $(kubectl-cmd) $(kubectl-version)
59
60# -----------------------------------------------------------------------
61# 2) Activate by copying the version approved by voltha release into place.
62# bin/kubectl.123
63# bin/kubectl.456
64# cp bin/kubectl.123 bin/kubectl
65# -----------------------------------------------------------------------
66$(kubectl-cmd) : $(kubectl-ver-cmd)
67
68 $(call banner-enter,Target $@ (ver=$(kubectl-ver)))
69 ln -fns $< $@
70 $(call banner-leave,Target $@ (ver=$(kubectl-ver)))
71
72# -----------------------------------------------------------------------
73# 3) Intent: Download versioned kubectl into the local build directory
74# -----------------------------------------------------------------------
75# [NOTE] Remove --no-progress-meter switch for now, not supporte by
76# curl in ubuntu 18.04-LTS.
77# -----------------------------------------------------------------------
78$(kubectl-ver-cmd):
79
80# $(call banner,(kubectl install: $(kubectl-ver)))
81 @echo "kubectl install: $(kubectl-ver)"
82
83 @mkdir --mode 0755 -p $(dir $@)
84
85 curl \
86 --output $@ \
87 --location "$(kube-url)"
88
89 @umask 0 && chmod 0555 $@
90
91# -----------------------------------------------------------------------
92# Intent: Display command version
93# -----------------------------------------------------------------------
94# NOTE:
95# - kubectl version requires connection to a running server.
96# - use a simple display answer to avoid installation failure source
97# -----------------------------------------------------------------------
98kubectl-version :
99
100 @echo
101 realpath --canonicalize-existing $(kubectl-cmd)
102
103 @echo
104 -$(kubectl-cmd) version
105
106## -----------------------------------------------------------------------
107## Intent: Display target help
108## -----------------------------------------------------------------------
109commands-help :: kubectl-help
110
111.PHONY: kubectl-help
112kubectl-help::
113 @printf ' %-33.33s %s\n' 'kubectl' 'Install the kubectl command'
114
115 @$(foreach ver,$(kubectl-versions),\
116 @printf ' %-33.33s %s\n' 'kubectl-$(ver)' 'Install versioned kubectl' \
117 )
118
119 @printf ' %-33.33s %s\n' 'kubectl-version' \
120 'Display installed command version'
121
122ifdef VERBOSE
123 @echo " make kubectl KUBECTL_PATH="
124endif
125
126## -----------------------------------------------------------------------
127## Intent: Remove binaries to force clean download and install
128## -----------------------------------------------------------------------
129clean ::
130 $(RM) $(kubectl-cmd)
131
132sterile :: clean
133 $(RM) $(kubectl-ver-cmd)
134
135# [SEE ALSO]
136# https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/
137
138# [EOF]