From e89b6d6f190eb730b00126d2bd2762a18bcfe0be Mon Sep 17 00:00:00 2001 From: Madhav Bhargava Date: Tue, 29 Aug 2023 12:53:49 +0530 Subject: [PATCH] corrected local_setup.sh, added local_restore.sh, added script to add license headers --- .env | 3 + .gitignore | 1 + Makefile | 33 +- hack/add_license_headers.sh | 33 ++ hack/admin-kube-config-request-template.json | 7 + hack/api-reference/generate-spec-doc.sh | 14 + hack/local_restore.sh | 134 ++++++++ hack/local_setup.sh | 302 +++++++++++------- hack/tools.mk | 33 ++ .../validation/validation_suite_test.go | 14 + pkg/apis/machine/zz_generated.defaults.go | 14 + pkg/controller/autoscaler/types.go | 14 + pkg/openapi/doc.go | 14 + pkg/test/integration/common/framework.go | 14 + .../integration/common/helpers/cluster.go | 14 + .../common/helpers/handling_files.go | 14 + .../common/helpers/handling_git.go | 14 + .../common/helpers/machine_resources.go | 14 + pkg/test/integration/common/helpers/nodes.go | 14 + .../helpers/resources_tracker_interface.go | 14 + .../annotations/annotations_suite_test.go | 14 + pkg/util/provider/drain/drain_suite_test.go | 14 + .../machinecontroller/machine_safety_util.go | 14 + .../machine_safety_util_test.go | 14 + pkg/util/time/time_suite_test.go | 14 + pkg/util/worker/worker.go | 14 + 26 files changed, 667 insertions(+), 131 deletions(-) create mode 100644 .env create mode 100755 hack/add_license_headers.sh create mode 100644 hack/admin-kube-config-request-template.json create mode 100755 hack/local_restore.sh create mode 100644 hack/tools.mk diff --git a/.env b/.env new file mode 100644 index 000000000..67a19ce42 --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +# This file is used to set variables which will be referenced in the Makefile +# Currently variables defined in this file are used for local testing only. +# hack/local_setup.sh will populate variables and hack/local_restore.sh will clear the variables. diff --git a/.gitignore b/.gitignore index e3cfd0b00..6550b4c76 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ *.html .vscode .idea +**/.env *.DS_Store managevm controller_manager diff --git a/Makefile b/Makefile index 37399af56..d5b921d80 100644 --- a/Makefile +++ b/Makefile @@ -12,14 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. +include .env + IMAGE_REPOSITORY := eu.gcr.io/gardener-project/gardener/machine-controller-manager IMAGE_TAG := $(shell cat VERSION) COVERPROFILE := test/output/coverprofile.out -CONTROL_NAMESPACE := default -CONTROL_KUBECONFIG := dev/target-kubeconfig.yaml -TARGET_KUBECONFIG := dev/target-kubeconfig.yaml - LEADER_ELECT := "true" MACHINE_SAFETY_OVERSHOOTING_PERIOD:=1m @@ -34,8 +32,8 @@ endif # Rules When K8s cluster is Gardener Shoot# ########################################### -.PHONY: downlaod-kubeconfigs -download-kubeconfigs: +.PHONY: setup +setup: @echo "enter project name"; \ read PROJECT; \ echo "enter seed name"; \ @@ -44,18 +42,21 @@ download-kubeconfigs: read SHOOT; \ echo "enter cluster provider(gcp|aws|azure|vsphere|openstack|alicloud|metal|equinix-metal)"; \ read PROVIDER; \ - ./hack/local_setup.sh --SEED $$SEED --SHOOT $$SHOOT --PROJECT $$PROJECT --PROVIDER $$PROVIDER + ./hack/local_setup.sh --seed $$SEED --shoot $$SHOOT --project $$PROJECT --provider $$PROVIDER .PHONY: local-mcm-up -local-mcm-up: download-kubeconfigs +local-mcm-up: setup $(MAKE) start; -.PHONY: local-mcm-down -local-mcm-down: - @kubectl --kubeconfig=${CONTROL_KUBECONFIG} -n ${CONTROL_NAMESPACE} annotate --overwrite=true deployment/machine-controller-manager dependency-watchdog.gardener.cloud/ignore-scaling- - @kubectl --kubeconfig=${CONTROL_KUBECONFIG} scale -n ${CONTROL_NAMESPACE} deployment/machine-controller-manager --replicas=1 - @rm ${CONTROL_KUBECONFIG} - @rm ${TARGET_KUBECONFIG} +.PHONY: restore +restore: + @echo "enter project name"; \ + read PROJECT; \ + echo "enter shoot name"; \ + read SHOOT; \ + echo "enter cluster provider(gcp|aws|azure|vsphere|openstack|alicloud|metal|equinix-metal)"; \ + read PROVIDER; \ + ./hack/local_restore.sh --shoot $$SHOOT --project $$PROJECT --provider $$PROVIDER ######################################### # Rules for local development scenarios # @@ -163,3 +164,7 @@ CONTROLLER_GEN=$(GOBIN)/controller-gen else CONTROLLER_GEN=$(shell which controller-gen) endif + +.PHONY: add-license-headers +add-license-headers: $(GO_ADD_LICENSE) + @./hack/add_license_headers.sh \ No newline at end of file diff --git a/hack/add_license_headers.sh b/hack/add_license_headers.sh new file mode 100755 index 000000000..1aefbb10d --- /dev/null +++ b/hack/add_license_headers.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# Copyright 2023 SAP SE or an SAP affiliate company +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +set -e + +echo "> Adding Apache License header to all go files where it is not present" + +# Uses the tool https://github.com/google/addlicense +YEAR="$(date +%Y)" +addlicense \ + -c "SAP SE or an SAP affiliate company" \ + -y "${YEAR}" \ + -l apache \ + -ignore ".idea/**" \ + -ignore ".vscode/**" \ + -ignore "vendor/**" \ + -ignore "**/*.md" \ + -ignore "**/*.yaml" \ + -ignore "**/Dockerfile" \ + . \ No newline at end of file diff --git a/hack/admin-kube-config-request-template.json b/hack/admin-kube-config-request-template.json new file mode 100644 index 000000000..58d70a74c --- /dev/null +++ b/hack/admin-kube-config-request-template.json @@ -0,0 +1,7 @@ +{ + "apiVersion": "authentication.gardener.cloud/v1alpha1", + "kind": "AdminKubeconfigRequest", + "spec": { + "expirationSeconds": ${expiry_seconds} + } +} \ No newline at end of file diff --git a/hack/api-reference/generate-spec-doc.sh b/hack/api-reference/generate-spec-doc.sh index 166f5c074..3940ae4c1 100755 --- a/hack/api-reference/generate-spec-doc.sh +++ b/hack/api-reference/generate-spec-doc.sh @@ -1,3 +1,17 @@ +# Copyright 2023 SAP SE or an SAP affiliate company +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + cd ./hack/api-reference ./gen-crd-api-reference-docs -config "providerspec-config.json" -api-dir "../../pkg/apis/machine/v1alpha1" -out-file="../../docs/documents/apis.md" sed 's/?id=//g' ../../docs/documents/apis.md > ../../docs/documents/apis-1.md diff --git a/hack/local_restore.sh b/hack/local_restore.sh new file mode 100755 index 000000000..2fa0f79ab --- /dev/null +++ b/hack/local_restore.sh @@ -0,0 +1,134 @@ +#!/usr/bin/env bash +# Copyright 2023 SAP SE or an SAP affiliate company +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +set -o errexit +set -o pipefail + +############################################################################################################## +# Script restores MCM deployment and removes the annotation dependency-watchdog.gardener.cloud/ignore-scaling +# This script should be called once you are done using local_setup.sh +############################################################################################################## + +declare SHOOT PROJECT PROVIDER +declare PROVIDER_MCM_PROJECT_DIR +declare RELATIVE_KUBECONFIG_PATH="dev/kube-configs" +declare LANDSCAPE_NAME="dev" + + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" +PROJECT_DIR="$(cd "$(dirname "${SCRIPT_DIR}")" &>/dev/null && pwd)" + +function create_usage() { + usage=$(printf '%s\n' " + Usage: $(basename $0) [Options] + Options: + -t | --shoot (Required) Name of the Gardener Shoot Cluster + -p | --project (Required) Name of the Gardener Project + -l | --landscape (Optional) Name of the landscape. Defaults to dev + -i | --provider (Required) Infrastructure provider name. Supported providers (gcp|aws|azure|vsphere|openstack|alicloud|metal|equinix-metal) + -k | --kubeconfig-path (Optional) Relative path to the where kubeconfigs will be downloaded. Path should not start with '/'. Default: /dev/kube-configs + -m | --mcm-provider-project-path (Optional) MCM Provider project directory. If not provided then it assumes that both mcm and mcm-provider projects are under the same parent directory + ") + echo "${usage}" +} + +function parse_flags() { + while test $# -gt 0; do + case "$1" in + --shoot | -t) + shift + SHOOT="$1" + ;; + --project | -p) + shift + PROJECT="$1" + ;; + --landscape | -l) + shift + LANDSCAPE_NAME="$1" + ;; + --provider | -i) + shift + PROVIDER="$1" + ;; + --kubeconfig-path | -k) + shift + RELATIVE_KUBECONFIG_PATH="$1" + ;; + --mcm-provider-project-path | -m) + shift + PROVIDER_MCM_PROJECT_DIR="$1" + ;; + --help | -h) + shift + echo "${USAGE}" + exit 0 + ;; + esac + shift + done +} + +function validate_args() { + if [[ -z "${SHOOT}" ]]; then + echo -e "Shoot has not been passed. Please provide Shoot either by specifying --shoot or -t argument" + exit 1 + fi + if [[ -z "${PROJECT}" ]]; then + echo -e "Project name has not been passed. Please provide Project name either by specifying --project or -p argument" + exit 1 + fi +} + +function initialize_variables() { + if [[ -z "${PROVIDER_MCM_PROJECT_DIR}" ]]; then + PROVIDER_MCM_PROJECT_DIR=$(dirname "${PROJECT_DIR}")/machine-controller-manager-provider-"${PROVIDER}" + fi + ABSOLUTE_KUBE_CONFIG_PATH="$(cd "$(dirname "${RELATIVE_KUBECONFIG_PATH}")"; pwd)/$(basename "${RELATIVE_KUBECONFIG_PATH}")" + ABSOLUTE_PROVIDER_KUBECONFIG_PATH="${PROVIDER_MCM_PROJECT_DIR}/dev/kube-configs" +} + +function restore_mcm_deployment() { + local virtual_garden_cluster="sap-landscape-${LANDSCAPE_NAME}" + gardenctl target --garden "${virtual_garden_cluster}" --project "${PROJECT}" --shoot "${SHOOT}" --control-plane + eval "$(gardenctl kubectl-env zsh)" + echo "removing annotation dependency-watchdog.gardener.cloud/ignore-scaling from mcm deployment..." + kubectl annotate --overwrite=true deployment/machine-controller-manager dependency-watchdog.gardener.cloud/ignore-scaling- + echo "scaling up mcm deployment to 1..." + kubectl scale deployment/machine-controller-manager --replicas=1 +} + +function delete_generated_configs() { + echo "Removing downloaded kubeconfigs..." + rm -f "${ABSOLUTE_KUBE_CONFIG_PATH}"/*.yaml + rm -f "${ABSOLUTE_PROVIDER_KUBECONFIG_PATH}"/*.yaml + echo "Clearing .env files..." + sed -r -i '/^#/!d' "${PROJECT_DIR}"/.env + sed -r -i '/^#/!d' "${PROVIDER_MCM_PROJECT_DIR}"/.env + echo "Removing generated admin kube config json..." + rm -f "${SCRIPT_DIR}"/admin-kube-config-request.json +} + +function main() { + parse_flags "$@" + validate_args + initialize_variables + restore_mcm_deployment + delete_generated_configs +} + +USAGE=$(create_usage) +main "$@" \ No newline at end of file diff --git a/hack/local_setup.sh b/hack/local_setup.sh index a7615faca..52a0e2248 100755 --- a/hack/local_setup.sh +++ b/hack/local_setup.sh @@ -1,150 +1,218 @@ -# /* -# Copyright (c) YEAR SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file - +#!/usr/bin/env bash +# Copyright 2023 SAP SE or an SAP affiliate company +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at - +# # http://www.apache.org/licenses/LICENSE-2.0 - +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# */ - - -# THE SCRIPT SHOULD BE RUN WHILE IN THE `machine-controller-manager/hack` FOLDER & IT ASSUMES THAT IN THE PARENT DIRECTORY OF MCM THE MCM PROVIDER IS ALSO PRESENT -# THE SCRIPT ALSO ASSUMES THAT THERE THE `machine-controller-manager-provider-/dev/kubeconfigs` FOLDER EXISTS. - -#HOW TO CALL -################################################################################################ - -# ./local_setup.sh --PROJECT --SEED --SHOOT --PROVIDER - -################################################################################################ -#!/usr/bin/env bash set -o errexit -set -o nounset set -o pipefail -declare SEED -declare SHOOT -declare PROJECT -declare PROVIDER -declare CURRENT_DIR -declare PROJECT_ROOT -declare KUBECONFIG_PATH -declare PROVIDER_PATH -declare PROVIDER_KUBECONFIG_PATH - -while [ $# -gt 0 ]; do - -if [[ $1 == *"--"* ]]; then - v="${1/--/}" - declare $v="$2" -fi - -shift -done - -main() { - setPaths - echo "Targeting gardener to get the kubeconfig of ${SHOOT} cluster control plane" - setControlKubeconfig - echo "Targeting seed cluster to get the kubeconfig of ${SHOOT} cluster" - setTargetKubeconfig - echo "Placing kubeconfigs at /dev/kubeconfigs/kubeconfig_.yaml" - copyFilesToProvider - # All the kubeconfigs are at place - echo "Annotating deployment/machine-controller-manager with dependency-watchdog.gardener.cloud/ignore-scaling=true" - addAnnotation - echo "Scaling down deployment/machine-controller-manager to 0" - scaleDownMCM - echo "Updating and exporting variables for starting the local MCM instance" - updateMCMMakefile - exportVariables - updateProviderMakefile +############################################################################################################## +# Script sets up local development environment enabling you to start MCM process locally. +# It does the following: +# 1. Downloads short-lived control and target cluster kube-configs +# 2. Ensures that kube-configs are copied to both mcm and provider-mcm project directory +# 3. Scales down MCM to 0 +# 4. Places annotation dependency-watchdog.gardener.cloud/ignore-scaling on MCM deployment, thus preventing +# DWD from scaling it back up. +# 5. Updates Makefile for mcm and provider-mcm projects and exports variables used by the makefile. +############################################################################################################## + +declare SEED SHOOT PROJECT PROVIDER # these are mandatory cli flags to be provided by the user +# these are optional cli flags +declare PROVIDER_MCM_PROJECT_DIR +declare RELATIVE_KUBECONFIG_PATH="dev/kube-configs" +declare LANDSCAPE_NAME="dev" +declare KUBECONFIG_EXPIRY_SECONDS="3600" +declare VIRTUAL_GARDEN_CLUSTER +declare ABSOLUTE_KUBE_CONFIG_PATH +declare ABSOLUTE_PROVIDER_KUBECONFIG_PATH + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" +PROJECT_DIR="$(cd "$(dirname "${SCRIPT_DIR}")" &>/dev/null && pwd)" + +function create_usage() { + usage=$(printf '%s\n' " + Usage: $(basename $0) [Options] + Options: + -d | --seed (Required) Name of the Gardener Seed Cluster + -t | --shoot (Required) Name of the Gardener Shoot Cluster + -p | --project (Required) Name of the Gardener Project + -i | --provider (Required) Infrastructure provider name. Supported providers (gcp|aws|azure|vsphere|openstack|alicloud|metal|equinix-metal) + -l | --landscape (Optional) Name of the landscape. Defaults to dev + -k | --kubeconfig-path (Optional) Relative path to the where kubeconfigs will be downloaded. Path should not start with '/'. Default: /dev/kube-configs + -m | --mcm-provider-project-path (Optional) MCM Provider project directory. If not provided then it assumes that both mcm and mcm-provider projects are under the same parent directory + -e | --kubeconfig-expiry-seconds (Optional) Common expiry durations in seconds for control and target KubeConfigs. Default: 3600 seconds + ") + echo "${usage}" } -setPaths() { - CURRENT_DIR=$(dirname $0) - PROJECT_ROOT="${CURRENT_DIR}"/.. - KUBECONFIG_PATH="${PROJECT_ROOT}"/dev/kubeconfigs - PROVIDER_PATH="${PROJECT_ROOT}"/../machine-controller-manager-provider-${PROVIDER} - PROVIDER_KUBECONFIG_PATH="${PROVIDER_PATH}"/dev/kubeconfigs +function parse_flags() { + while test $# -gt 0; do + case "$1" in + --seed | -d) + shift + SEED="$1" + ;; + --shoot | -t) + shift + SHOOT="$1" + ;; + --project | -p) + shift + PROJECT="$1" + ;; + --provider | -i) + shift + PROVIDER="$1" + ;; + --kubeconfig-path | -k) + shift + RELATIVE_KUBECONFIG_PATH="$1" + ;; + --mcm-provider-project-path | -m) + shift + PROVIDER_MCM_PROJECT_DIR="$1" + ;; + --kubeconfig-expiry-seconds | -e) + shift + KUBECONFIG_EXPIRY_SECONDS="$1" + ;; + --landscape | -l) + shift + LANDSCAPE_NAME="$1" + ;; + --help | -h) + shift + echo "${USAGE}" + exit 0 + ;; + esac + shift + done } -setControlKubeconfig() { - gardenctl target --garden sap-landscape-dev --project garden - - eval $(gardenctl kubectl-env bash) - - echo "$(kubectl get secret/"${SEED}".kubeconfig --template={{.data.kubeconfig}} | base64 -d)" > "${KUBECONFIG_PATH}"/kubeconfig_control.yaml +function validate_args() { + if [[ -z "${SEED}" ]]; then + echo -e "Seed has not been passed. Please provide Seed either by specifying --seed or -d argument" + exit 1 + fi + if [[ -z "${SHOOT}" ]]; then + echo -e "Shoot has not been passed. Please provide Shoot either by specifying --shoot or -t argument" + exit 1 + fi + if [[ -z "${PROJECT}" ]]; then + echo -e "Project name has not been passed. Please provide Project name either by specifying --project or -p argument" + exit 1 + fi + if [[ -z "${PROVIDER}" ]]; then + echo -e "Infrastructure provider name has not been passed. Please provide infrastructure provider name either by specifying --provider or -i argument" + exit 1 + fi } -setTargetKubeconfig() { - - #setting up the target config - - gardenctl target --garden sap-landscape-dev --project "${PROJECT}" --shoot "${SHOOT}" --control-plane - - eval $(gardenctl kubectl-env bash) - - secret=$(kubectl get secrets | awk '{ print $1 }' |grep user-kubeconfig-) - - echo "$(kubectl get secret/"${secret}" -n shoot--"${PROJECT}"--"${SHOOT}" --template={{.data.kubeconfig}} | base64 -d)" > "${KUBECONFIG_PATH}"/kubeconfig_target.yaml +function initialize() { + if [[ -z "${PROVIDER_MCM_PROJECT_DIR}" ]]; then + PROVIDER_MCM_PROJECT_DIR=$(dirname "${PROJECT_DIR}")/machine-controller-manager-provider-"${PROVIDER}" + fi + echo "Creating directory ${RELATIVE_KUBECONFIG_PATH} if it does not exist..." + mkdir -p "${RELATIVE_KUBECONFIG_PATH}" + ABSOLUTE_KUBE_CONFIG_PATH="$(cd "$(dirname "${RELATIVE_KUBECONFIG_PATH}")"; pwd)/$(basename "${RELATIVE_KUBECONFIG_PATH}")" + ABSOLUTE_PROVIDER_KUBECONFIG_PATH="${PROVIDER_MCM_PROJECT_DIR}/dev/kube-configs" + echo "Creating directory ${ABSOLUTE_PROVIDER_KUBECONFIG_PATH} if it does not exist..." + mkdir -p "${ABSOLUTE_PROVIDER_KUBECONFIG_PATH}" + VIRTUAL_GARDEN_CLUSTER="sap-landscape-${LANDSCAPE_NAME}" } -copyFilesToProvider() { - # Copy both the kubeconfigs to dev folder of provider - - cp "${KUBECONFIG_PATH}"/kubeconfig_target.yaml "${PROVIDER_KUBECONFIG_PATH}"/kubeconfig_target.yaml - cp "${KUBECONFIG_PATH}"/kubeconfig_control.yaml "${PROVIDER_KUBECONFIG_PATH}"/kubeconfig_control.yaml +function download_kubeconfigs() { + readonly garden_namespace="garden" + local project_namespace="${garden_namespace}-${PROJECT}" + + kubeconfig_request_path=$(create_kubeconfig_request_yaml) + + echo "Targeting Virtual Garden Cluster ${VIRTUAL_GARDEN_CLUSTER}..." + gardenctl target --garden "${VIRTUAL_GARDEN_CLUSTER}" + eval "$(gardenctl kubectl-env zsh)" + + echo "Downloading kubeconfig for control cluster to ${ABSOLUTE_KUBE_CONFIG_PATH}..." + kubectl create \ + -f "${kubeconfig_request_path}" \ + --raw "/apis/core.gardener.cloud/v1beta1/namespaces/${garden_namespace}/shoots/${SEED}/adminkubeconfig" | + jq -r '.status.kubeconfig' | + base64 -d >"${ABSOLUTE_KUBE_CONFIG_PATH}/kubeconfig_control.yaml" + + echo "Downloading kubeconfig for target cluster to ${ABSOLUTE_KUBE_CONFIG_PATH}..." + kubectl create \ + -f "${kubeconfig_request_path}" \ + --raw "/apis/core.gardener.cloud/v1beta1/namespaces/${project_namespace}/shoots/${SHOOT}/adminkubeconfig" | + jq -r '.status.kubeconfig' | + base64 -d >"${ABSOLUTE_KUBE_CONFIG_PATH}/kubeconfig_target.yaml" } -addAnnotation() { - # Adding annotation - - kubectl annotate --overwrite=true deployment/machine-controller-manager dependency-watchdog.gardener.cloud/ignore-scaling=true - - # Another way of adding annotation - # kubectl patch deployment/machine-controller-manager -p '{"metadata":{"annotations":{"dependency-watchdog.gardener.cloud/ignore-scaling":true}} }' - - # Removing the annotation, needs to be called after done with local setup - - # kubectl annotate --overwrite=true deployment/machine-controller-manager dependency-watchdog.gardener.cloud/ignore-scaling- - +function create_kubeconfig_request_yaml() { + local expiry_seconds template_path target_request_path + expiry_seconds=${KUBECONFIG_EXPIRY_SECONDS} + template_path="${SCRIPT_DIR}"/admin-kube-config-request-template.json + target_request_path="${SCRIPT_DIR}"/admin-kube-config-request.json + export expiry_seconds + envsubst <"${template_path}" >"${target_request_path}" + unset expiry_seconds + echo "${target_request_path}" } -scaleDownMCM() { - # Scale down the MCM - kubectl scale deployment/machine-controller-manager --replicas=0 - - # If delete is required, when Scale Down operation is not working due to some reason - # kubectl delete deployment/machine-controller-manager +function copy_kubeconfigs_to_provider_mcm() { + for kc in "${ABSOLUTE_KUBE_CONFIG_PATH}"/*.yaml; do + cp -v "${kc}" "${ABSOLUTE_PROVIDER_KUBECONFIG_PATH}" + done } -updateMCMMakefile() { - # Point the makefiles to the correct kubeconfigs to use for local run - sed -i -e "s/\(CONTROL_NAMESPACE *:=\).*/\1 shoot--"${PROJECT}"--"${SHOOT}"/1" "${PROJECT_ROOT}"/makefile - sed -i -e "s/\(CONTROL_KUBECONFIG *:=\).*/\1 dev\/kubeconfigs\/kubeconfig_control.yaml/1" "${PROJECT_ROOT}"/makefile - sed -i -e "s/\(TARGET_KUBECONFIG *:=\).*/\1 dev\/kubeconfigs\/kubeconfig_target.yaml/1" "${PROJECT_ROOT}"/makefile +function scale_down_mcm() { + gardenctl target --garden "${VIRTUAL_GARDEN_CLUSTER}" --project "${PROJECT}" --shoot "${SHOOT}" --control-plane + eval "$(gardenctl kubectl-env zsh)" + + echo "annotating deployment/machine-controller-manager with dependency-watchdog.gardener.cloud/ignore-scaling=true..." + kubectl annotate --overwrite=true deployment/machine-controller-manager dependency-watchdog.gardener.cloud/ignore-scaling=true + # NOTE: One must remove the annotation after local setup is no longer needed. Use the below command to remove the annotation on machine-controller-manager deployment resource: + # kubectl annotate --overwrite=true deployment/machine-controller-manager dependency-watchdog.gardener.cloud/ignore-scaling- + echo "scaling down deployment/machine-controller-manager to 0..." + kubectl scale deployment/machine-controller-manager --replicas=0 } -exportVariables() { - export CONTROL_NAMESPACE=shoot--"${PROJECT}"--"${SHOOT}" - export CONTROL_KUBECONFIG="${KUBECONFIG_PATH}"/kubeconfig_control.yaml - export TARGET_KUBECONFIG="${KUBECONFIG_PATH}"/kubeconfig_target.yaml +# create_makefile_env creates a .env file that will get copied to both mcm and mcm-provider project directories for their +# respective Makefile to use. +function set_makefile_env() { + if [[ $# -ne 2 ]]; then + echo -e "${FUNCNAME[0]} expects two arguments - project-directory and target-kube-config-path" + fi + local target_project_dir target_kube_config_path + target_project_dir="$1" + target_kube_config_path="$2" + { + printf "\n%s" "CONTROL_NAMESPACE=shoot--${PROJECT}--${SHOOT}" + printf "\n%s" "CONTROL_KUBECONFIG=${target_kube_config_path}/kubeconfig_control.yaml" >> "${target_project_dir}/.env" + printf "\n%s" "TARGET_KUBECONFIG=${target_kube_config_path}/kubeconfig_target.yaml" >> "${target_project_dir}/.env" + } >> "${target_project_dir}/.env" } -updateProviderMakefile() { - # Makefile of provider also needs to point at those kubeconfigs - sed -i -e "s/\(CONTROL_NAMESPACE *:= *\).*/\1 shoot--"${PROJECT}"--"${SHOOT}"/1" "${PROVIDER_PATH}"/makefile - sed -i -e "s/\(CONTROL_KUBECONFIG *:= *\).*/\1 dev\/kubeconfigs\/kubeconfig_control.yaml/1" "${PROVIDER_PATH}"/makefile - sed -i -e "s/\(TARGET_KUBECONFIG *:= *\).*/\1 dev\/kubeconfigs\/kubeconfig_target.yaml/1" "${PROVIDER_PATH}"/makefile +function main() { + parse_flags "$@" + validate_args + initialize + download_kubeconfigs + copy_kubeconfigs_to_provider_mcm + scale_down_mcm + set_makefile_env "${PROJECT_DIR}" "${ABSOLUTE_KUBE_CONFIG_PATH}" + set_makefile_env "${PROVIDER_MCM_PROJECT_DIR}" "${ABSOLUTE_PROVIDER_KUBECONFIG_PATH}" } -main +USAGE=$(create_usage) +main "$@" diff --git a/hack/tools.mk b/hack/tools.mk new file mode 100644 index 000000000..04112d443 --- /dev/null +++ b/hack/tools.mk @@ -0,0 +1,33 @@ +# Copyright (c) 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +TOOLS_DIR := hack/tools +TOOLS_BIN_DIR := $(TOOLS_DIR)/bin +GO_ADD_LICENSE := $(TOOLS_BIN_DIR)/addlicense + +# default tool versions +GO_ADD_LICENSE_VERSION ?= latest + +export TOOLS_BIN_DIR := $(TOOLS_BIN_DIR) +export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH) +$(info "TOOLS_BIN_DIR from tools.mk", $(TOOLS_BIN_DIR)) +$(info "TOOLS_DIR from tools.mk", $(TOOLS_DIR)) +$(info "PATH from tools.mk", $(PATH)) + +######################################### +# Tools # +######################################### + +$(GO_ADD_LICENSE): + GOBIN=$(abspath $(TOOLS_BIN_DIR)) go install github.com/google/addlicense@$(GO_ADD_LICENSE_VERSION) \ No newline at end of file diff --git a/pkg/apis/machine/validation/validation_suite_test.go b/pkg/apis/machine/validation/validation_suite_test.go index 37c9ac1fc..520a7bcef 100644 --- a/pkg/apis/machine/validation/validation_suite_test.go +++ b/pkg/apis/machine/validation/validation_suite_test.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package validation import ( diff --git a/pkg/apis/machine/zz_generated.defaults.go b/pkg/apis/machine/zz_generated.defaults.go index 3fa68441f..d5513ac6d 100644 --- a/pkg/apis/machine/zz_generated.defaults.go +++ b/pkg/apis/machine/zz_generated.defaults.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + // +build !ignore_autogenerated // This file was autogenerated by defaulter-gen. Do not edit it manually! diff --git a/pkg/controller/autoscaler/types.go b/pkg/controller/autoscaler/types.go index e61b3c8c7..e5a0941fd 100644 --- a/pkg/controller/autoscaler/types.go +++ b/pkg/controller/autoscaler/types.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package autoscaler const ( diff --git a/pkg/openapi/doc.go b/pkg/openapi/doc.go index 251286530..850a8ea8d 100644 --- a/pkg/openapi/doc.go +++ b/pkg/openapi/doc.go @@ -1,2 +1,16 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + // Package openapi exists to hold generated openapi code package openapi diff --git a/pkg/test/integration/common/framework.go b/pkg/test/integration/common/framework.go index 403789edf..ac9063c7e 100644 --- a/pkg/test/integration/common/framework.go +++ b/pkg/test/integration/common/framework.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package common import ( diff --git a/pkg/test/integration/common/helpers/cluster.go b/pkg/test/integration/common/helpers/cluster.go index d0b8f8059..2b6580f3b 100644 --- a/pkg/test/integration/common/helpers/cluster.go +++ b/pkg/test/integration/common/helpers/cluster.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package helpers import ( diff --git a/pkg/test/integration/common/helpers/handling_files.go b/pkg/test/integration/common/helpers/handling_files.go index 736805273..402f9424e 100644 --- a/pkg/test/integration/common/helpers/handling_files.go +++ b/pkg/test/integration/common/helpers/handling_files.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package helpers import ( diff --git a/pkg/test/integration/common/helpers/handling_git.go b/pkg/test/integration/common/helpers/handling_git.go index 86b808ba5..5859456a8 100644 --- a/pkg/test/integration/common/helpers/handling_git.go +++ b/pkg/test/integration/common/helpers/handling_git.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package helpers import ( diff --git a/pkg/test/integration/common/helpers/machine_resources.go b/pkg/test/integration/common/helpers/machine_resources.go index 5ddb98ba6..6ecc8adb4 100644 --- a/pkg/test/integration/common/helpers/machine_resources.go +++ b/pkg/test/integration/common/helpers/machine_resources.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package helpers import ( diff --git a/pkg/test/integration/common/helpers/nodes.go b/pkg/test/integration/common/helpers/nodes.go index 10345118f..2f4089f3a 100644 --- a/pkg/test/integration/common/helpers/nodes.go +++ b/pkg/test/integration/common/helpers/nodes.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package helpers import ( diff --git a/pkg/test/integration/common/helpers/resources_tracker_interface.go b/pkg/test/integration/common/helpers/resources_tracker_interface.go index d8722f04a..20d59deb4 100644 --- a/pkg/test/integration/common/helpers/resources_tracker_interface.go +++ b/pkg/test/integration/common/helpers/resources_tracker_interface.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package helpers import ( diff --git a/pkg/util/annotations/annotations_suite_test.go b/pkg/util/annotations/annotations_suite_test.go index 755c27844..85919c7e4 100644 --- a/pkg/util/annotations/annotations_suite_test.go +++ b/pkg/util/annotations/annotations_suite_test.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package annotations_test import ( diff --git a/pkg/util/provider/drain/drain_suite_test.go b/pkg/util/provider/drain/drain_suite_test.go index fae04932d..d1fc550f0 100644 --- a/pkg/util/provider/drain/drain_suite_test.go +++ b/pkg/util/provider/drain/drain_suite_test.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package drain_test import ( diff --git a/pkg/util/provider/machinecontroller/machine_safety_util.go b/pkg/util/provider/machinecontroller/machine_safety_util.go index ef27f7c7c..0ab682f95 100644 --- a/pkg/util/provider/machinecontroller/machine_safety_util.go +++ b/pkg/util/provider/machinecontroller/machine_safety_util.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package controller import ( diff --git a/pkg/util/provider/machinecontroller/machine_safety_util_test.go b/pkg/util/provider/machinecontroller/machine_safety_util_test.go index 5c0ae2eb5..4d67e02d6 100644 --- a/pkg/util/provider/machinecontroller/machine_safety_util_test.go +++ b/pkg/util/provider/machinecontroller/machine_safety_util_test.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package controller import ( diff --git a/pkg/util/time/time_suite_test.go b/pkg/util/time/time_suite_test.go index 69f1ac6f7..3a1ef4dc7 100644 --- a/pkg/util/time/time_suite_test.go +++ b/pkg/util/time/time_suite_test.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package time_test import ( diff --git a/pkg/util/worker/worker.go b/pkg/util/worker/worker.go index 2ac3f7d48..3e8334cca 100644 --- a/pkg/util/worker/worker.go +++ b/pkg/util/worker/worker.go @@ -1,3 +1,17 @@ +// Copyright 2023 SAP SE or an SAP affiliate company +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package worker import (