Joey Armstrong | 096db2d | 2024-07-22 16:04:44 -0400 | [diff] [blame^] | 1 | # -*- 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 | |
| 23 | export .DEFAULT_GOAL := help |
| 24 | |
| 25 | MAKEDIR ?= $(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 |
| 34 | kubectl-versions += v1.23 |
| 35 | kubectl-versions += v1.30.3 |
| 36 | |
| 37 | # kubectl-ver ?= v1.23# # voltha v2.12 (?) |
| 38 | kubectl-ver ?= v1.30.3# # 2024-07-22: latest release |
| 39 | kubectl-ver ?= $(shell curl -L -s https://dl.k8s.io/release/stable.txt) |
| 40 | kubectl-ver ?= $(error kubectl-ver= is required) |
| 41 | |
| 42 | kube-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 | # ----------------------------------------------------------------------- |
| 51 | KUBECTL_PATH ?= $(if $(WORKSPACE),$(WORKSPACE)/bin,/usr/local/bin) |
| 52 | kubectl-cmd ?= $(KUBECTL_PATH)/kubectl |
| 53 | kubectl-ver-cmd := $(kubectl-cmd).$(kubectl-ver) |
| 54 | |
| 55 | # ----------------------------------------------------------------------- |
| 56 | # 1) Generic target for installing kubectl |
| 57 | # ----------------------------------------------------------------------- |
| 58 | .PHONY: kubectl |
| 59 | kubectl : $(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 | # ----------------------------------------------------------------------- |
| 76 | $(kubectl-ver-cmd): |
| 77 | |
| 78 | # $(call banner,(kubectl install: $(kubectl-ver))) |
| 79 | @echo "kubectl install: $(kubectl-ver)" |
| 80 | |
| 81 | @mkdir --mode 0755 -p $(dir $@) |
| 82 | |
| 83 | curl \ |
| 84 | --output $@ \ |
| 85 | --location "$(kube-url)" \ |
| 86 | --no-progress-meter |
| 87 | |
| 88 | @umask 0 && chmod 0555 $@ |
| 89 | |
| 90 | # ----------------------------------------------------------------------- |
| 91 | # Intent: Display command version |
| 92 | # ----------------------------------------------------------------------- |
| 93 | # NOTE: |
| 94 | # - kubectl version requires connection to a running server. |
| 95 | # - use a simple display answer to avoid installation failure source |
| 96 | # ----------------------------------------------------------------------- |
| 97 | kubectl-version : |
| 98 | |
| 99 | @echo |
| 100 | realpath --canonicalize-existing $(kubectl-cmd) |
| 101 | |
| 102 | @echo |
| 103 | -$(kubectl-cmd) version |
| 104 | |
| 105 | ## ----------------------------------------------------------------------- |
| 106 | ## Intent: Display target help |
| 107 | ## ----------------------------------------------------------------------- |
| 108 | help:: |
| 109 | @printf ' %-33.33s %s\n' 'kubectl' 'Install the kubectl command' |
| 110 | |
| 111 | @$(foreach ver,$(kubectl-versions),\ |
| 112 | @printf ' %-33.33s %s\n' 'kubectl-$(ver)' 'Install versioned kubectl' \ |
| 113 | ) |
| 114 | |
| 115 | @printf ' %-33.33s %s\n' 'kubectl-version' \ |
| 116 | 'Display installed command version' |
| 117 | |
| 118 | ifdef VERBOSE |
| 119 | @echo " make kubectl KUBECTL_PATH=" |
| 120 | endif |
| 121 | |
| 122 | ## ----------------------------------------------------------------------- |
| 123 | ## Intent: Remove binaries to force clean download and install |
| 124 | ## ----------------------------------------------------------------------- |
| 125 | clean :: |
| 126 | $(RM) $(kubectl-cmd) |
| 127 | |
| 128 | sterile :: clean |
| 129 | $(RM) $(kubectl-ver-cmd) |
| 130 | |
| 131 | # [SEE ALSO] |
| 132 | # https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/ |
| 133 | |
| 134 | # [EOF] |