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