From 0d46bfa691828df3b5968d84f851937e6b9f4ca1 Mon Sep 17 00:00:00 2001 From: Adrian Moisey Date: Sat, 4 Jan 2025 17:12:43 +0200 Subject: [PATCH 1/4] VPA: Create a build and deploy locally script This is for easier setup of local environments to test PRs and the like. Some notes: 1. I thought it was useful to display the git commit in the version number, for debugging purposes 2. The `LD_FLAGS=-s` env-var that used to be set doesn't actually do anything, so I've moved it to the correct location 3. Most of this re-uses existing scripts 4. It doesn't handle setup of the Kubernetes cluster or teardown of the VPA --- vertical-pod-autoscaler/common/version.go | 23 +++++- .../hack/dev-deploy-locally.sh | 70 +++++++++++++++++++ .../pkg/admission-controller/Dockerfile | 3 +- .../pkg/admission-controller/Makefile | 5 +- .../pkg/admission-controller/main.go | 2 +- .../pkg/recommender/Dockerfile | 3 +- .../pkg/recommender/Makefile | 5 +- .../pkg/recommender/main.go | 2 +- .../pkg/updater/Dockerfile | 3 +- vertical-pod-autoscaler/pkg/updater/Makefile | 5 +- vertical-pod-autoscaler/pkg/updater/main.go | 2 +- 11 files changed, 110 insertions(+), 13 deletions(-) create mode 100755 vertical-pod-autoscaler/hack/dev-deploy-locally.sh diff --git a/vertical-pod-autoscaler/common/version.go b/vertical-pod-autoscaler/common/version.go index 15bd2d01bcbc..04132e646115 100644 --- a/vertical-pod-autoscaler/common/version.go +++ b/vertical-pod-autoscaler/common/version.go @@ -16,5 +16,26 @@ limitations under the License. package common +// gitCommit is the commit used to build the VPA binaries, if available. +// It is injected at build time. +var gitCommit = "" + // VerticalPodAutoscalerVersion is the version of VPA. -const VerticalPodAutoscalerVersion = "1.2.0" +const versionCore = "1.2.0" + +func VerticalPodAutoscalerVersion() string { + v := versionCore + if gitCommit != "" { + // NOTE: use 14 character short hash, like Kubernetes + v += "+" + truncate(gitCommit, 14) + } + + return v +} + +func truncate(s string, maxLen int) string { + if len(s) < maxLen { + return s + } + return s[:maxLen] +} diff --git a/vertical-pod-autoscaler/hack/dev-deploy-locally.sh b/vertical-pod-autoscaler/hack/dev-deploy-locally.sh new file mode 100755 index 000000000000..170c2f2cae96 --- /dev/null +++ b/vertical-pod-autoscaler/hack/dev-deploy-locally.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +# Copyright 2023 The Kubernetes Authors. +# +# 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 nounset +set -o pipefail + +SCRIPT_ROOT=$(dirname ${BASH_SOURCE})/.. + +SUITE=full-vpa +REQUIRED_COMMANDS=" +docker +git +go +kubectl +make +" + +for i in $REQUIRED_COMMANDS; do + if ! command -v $i > /dev/null 2>&1 + then + echo "$i could not be found, please ensure it is installed" + echo + echo "The following commands are required to run these tests:" + echo $REQUIRED_COMMANDS + exit 1; + fi +done + +if ! docker ps >/dev/null 2>&1 +then + echo "docker isn't running" + echo + echo "Please ensure that docker is running" + exit 1 +fi + +if ! kubectl version >/dev/null 2>&1 +then + echo "Kubernetes isn't running" + echo + echo "Please ensure that Kubernetes is running" + exit 1 +fi + +COMMIT=$(git rev-parse HEAD 2>/dev/null) +COMMIT=${COMMIT:0:14} +export BUILD_LD_FLAGS="-s -X=k8s.io/autoscaler/vertical-pod-autoscaler/common.gitCommit=$COMMIT" +export TAG=$COMMIT + + +echo " ** Deploying building and deploying all VPA components" +${SCRIPT_ROOT}/hack/deploy-for-e2e-locally.sh full-vpa + +echo " ** Restarting all VPA components" +for i in admission-controller updater recommender; do + kubectl -n kube-system rollout status deployment/vpa-$i +done diff --git a/vertical-pod-autoscaler/pkg/admission-controller/Dockerfile b/vertical-pod-autoscaler/pkg/admission-controller/Dockerfile index 57a1f9a2dda7..28f7bde7e29c 100644 --- a/vertical-pod-autoscaler/pkg/admission-controller/Dockerfile +++ b/vertical-pod-autoscaler/pkg/admission-controller/Dockerfile @@ -26,8 +26,9 @@ COPY common common COPY pkg pkg ARG TARGETOS TARGETARCH +ARG BUILD_LD_FLAGS="-s" -RUN CGO_ENABLED=0 LD_FLAGS=-s GOARCH=$TARGETARCH GOOS=$TARGETOS go build -C pkg/admission-controller -o admission-controller-$TARGETARCH +RUN CGO_ENABLED=0 GOARCH=$TARGETARCH GOOS=$TARGETOS go build -C pkg/admission-controller -ldflags="${BUILD_LD_FLAGS}" -o admission-controller-$TARGETARCH FROM gcr.io/distroless/static:nonroot diff --git a/vertical-pod-autoscaler/pkg/admission-controller/Makefile b/vertical-pod-autoscaler/pkg/admission-controller/Makefile index 444aacf1e79f..999b37663bd2 100644 --- a/vertical-pod-autoscaler/pkg/admission-controller/Makefile +++ b/vertical-pod-autoscaler/pkg/admission-controller/Makefile @@ -9,6 +9,7 @@ GOOS?=linux COMPONENT=admission-controller FULL_COMPONENT=vpa-${COMPONENT} +BUILD_LD_FLAGS?=-s ALL_ARCHITECTURES?=amd64 arm arm64 ppc64le s390x export DOCKER_CLI_EXPERIMENTAL=enabled @@ -35,7 +36,7 @@ ifndef TAG ERR = $(error TAG is undefined) $(ERR) endif - docker buildx build --pull --load --platform linux/$* -t ${REGISTRY}/${FULL_COMPONENT}-$*:${TAG} -f ./Dockerfile ../../ + docker buildx build --pull --load --platform linux/$* --build-arg BUILD_LD_FLAGS="${BUILD_LD_FLAGS}" -t ${REGISTRY}/${FULL_COMPONENT}-$*:${TAG} -f ./Dockerfile ../../ .PHONY: docker-push docker-push: $(addprefix do-push-,$(ALL_ARCHITECTURES)) push-multi-arch; @@ -91,4 +92,4 @@ format: .PHONY: document-flags document-flags: - go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-admission-flags.md \ No newline at end of file + go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-admission-flags.md diff --git a/vertical-pod-autoscaler/pkg/admission-controller/main.go b/vertical-pod-autoscaler/pkg/admission-controller/main.go index 08aca96f0882..79807274204f 100644 --- a/vertical-pod-autoscaler/pkg/admission-controller/main.go +++ b/vertical-pod-autoscaler/pkg/admission-controller/main.go @@ -82,7 +82,7 @@ func main() { klog.InitFlags(nil) common.InitLoggingFlags() kube_flag.InitFlags() - klog.V(1).InfoS("Starting Vertical Pod Autoscaler Admission Controller", "version", common.VerticalPodAutoscalerVersion) + klog.V(1).InfoS("Starting Vertical Pod Autoscaler Admission Controller", "version", common.VerticalPodAutoscalerVersion()) if len(commonFlags.VpaObjectNamespace) > 0 && len(commonFlags.IgnoredVpaObjectNamespaces) > 0 { klog.Fatalf("--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.") diff --git a/vertical-pod-autoscaler/pkg/recommender/Dockerfile b/vertical-pod-autoscaler/pkg/recommender/Dockerfile index cdf7921d5589..ed6fe8637fa0 100644 --- a/vertical-pod-autoscaler/pkg/recommender/Dockerfile +++ b/vertical-pod-autoscaler/pkg/recommender/Dockerfile @@ -26,8 +26,9 @@ COPY common common COPY pkg pkg ARG TARGETOS TARGETARCH +ARG BUILD_LD_FLAGS="-s" -RUN CGO_ENABLED=0 LD_FLAGS=-s GOARCH=$TARGETARCH GOOS=$TARGETOS go build -C pkg/recommender -o recommender-$TARGETARCH +RUN CGO_ENABLED=0 GOARCH=$TARGETARCH GOOS=$TARGETOS go build -C pkg/recommender -ldflags="${BUILD_LD_FLAGS}" -o recommender-$TARGETARCH FROM gcr.io/distroless/static:nonroot diff --git a/vertical-pod-autoscaler/pkg/recommender/Makefile b/vertical-pod-autoscaler/pkg/recommender/Makefile index e6d06a008e90..3c64ca4ff422 100644 --- a/vertical-pod-autoscaler/pkg/recommender/Makefile +++ b/vertical-pod-autoscaler/pkg/recommender/Makefile @@ -11,6 +11,7 @@ FULL_COMPONENT=vpa-${COMPONENT} # localhost registries need --insecure for some docker commands. INSECURE=$(subst localhost,--insecure,$(findstring localhost,$(REGISTRY))) +BUILD_LD_FLAGS?=-s ALL_ARCHITECTURES?=amd64 arm arm64 ppc64le s390x export DOCKER_CLI_EXPERIMENTAL=enabled @@ -37,7 +38,7 @@ ifndef TAG ERR = $(error TAG is undefined) $(ERR) endif - docker buildx build --pull --load --platform linux/$* -t ${REGISTRY}/${FULL_COMPONENT}-$*:${TAG} -f ./Dockerfile ../../ + docker buildx build --pull --load --platform linux/$* --build-arg BUILD_LD_FLAGS="${BUILD_LD_FLAGS}" -t ${REGISTRY}/${FULL_COMPONENT}-$*:${TAG} -f ./Dockerfile ../../ .PHONY: docker-push docker-push: $(addprefix do-push-,$(ALL_ARCHITECTURES)) push-multi-arch; @@ -93,4 +94,4 @@ format: .PHONY: document-flags document-flags: - go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-recommender-flags.md \ No newline at end of file + go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-recommender-flags.md diff --git a/vertical-pod-autoscaler/pkg/recommender/main.go b/vertical-pod-autoscaler/pkg/recommender/main.go index 8a53137f1e5c..14ab07a184fb 100644 --- a/vertical-pod-autoscaler/pkg/recommender/main.go +++ b/vertical-pod-autoscaler/pkg/recommender/main.go @@ -124,7 +124,7 @@ func main() { componentbaseoptions.BindLeaderElectionFlags(&leaderElection, pflag.CommandLine) kube_flag.InitFlags() - klog.V(1).InfoS("Vertical Pod Autoscaler Recommender", "version", common.VerticalPodAutoscalerVersion, "recommenderName", *recommenderName) + klog.V(1).InfoS("Vertical Pod Autoscaler Recommender", "version", common.VerticalPodAutoscalerVersion(), "recommenderName", *recommenderName) if len(commonFlags.VpaObjectNamespace) > 0 && len(commonFlags.IgnoredVpaObjectNamespaces) > 0 { klog.Fatalf("--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.") diff --git a/vertical-pod-autoscaler/pkg/updater/Dockerfile b/vertical-pod-autoscaler/pkg/updater/Dockerfile index cf03102f95d2..54dd14506eeb 100644 --- a/vertical-pod-autoscaler/pkg/updater/Dockerfile +++ b/vertical-pod-autoscaler/pkg/updater/Dockerfile @@ -26,8 +26,9 @@ COPY common common COPY pkg pkg ARG TARGETOS TARGETARCH +ARG BUILD_LD_FLAGS="-s" -RUN CGO_ENABLED=0 LD_FLAGS=-s GOARCH=$TARGETARCH GOOS=$TARGETOS go build -C pkg/updater -o updater-$TARGETARCH +RUN CGO_ENABLED=0 LD_FLAGS=-s GOARCH=$TARGETARCH GOOS=$TARGETOS go build -C pkg/updater -ldflags="${BUILD_LD_FLAGS}" -o updater-$TARGETARCH FROM gcr.io/distroless/static:nonroot diff --git a/vertical-pod-autoscaler/pkg/updater/Makefile b/vertical-pod-autoscaler/pkg/updater/Makefile index ae9b1f78ec69..517e95685421 100644 --- a/vertical-pod-autoscaler/pkg/updater/Makefile +++ b/vertical-pod-autoscaler/pkg/updater/Makefile @@ -9,6 +9,7 @@ GOOS?=linux COMPONENT=updater FULL_COMPONENT=vpa-${COMPONENT} +BUILD_LD_FLAGS?=-s ALL_ARCHITECTURES?=amd64 arm arm64 ppc64le s390x export DOCKER_CLI_EXPERIMENTAL=enabled @@ -35,7 +36,7 @@ ifndef TAG ERR = $(error TAG is undefined) $(ERR) endif - docker buildx build --pull --load --platform linux/$* -t ${REGISTRY}/${FULL_COMPONENT}-$*:${TAG} -f ./Dockerfile ../../ + docker buildx build --pull --load --platform linux/$* --build-arg BUILD_LD_FLAGS="${BUILD_LD_FLAGS}" -t ${REGISTRY}/${FULL_COMPONENT}-$*:${TAG} -f ./Dockerfile ../../ .PHONY: docker-push docker-push: $(addprefix do-push-,$(ALL_ARCHITECTURES)) push-multi-arch; @@ -91,4 +92,4 @@ format: .PHONY: document-flags document-flags: - go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-updater-flags.md \ No newline at end of file + go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-updater-flags.md diff --git a/vertical-pod-autoscaler/pkg/updater/main.go b/vertical-pod-autoscaler/pkg/updater/main.go index 762a301c1f8a..b5421e3f4feb 100644 --- a/vertical-pod-autoscaler/pkg/updater/main.go +++ b/vertical-pod-autoscaler/pkg/updater/main.go @@ -89,7 +89,7 @@ func main() { componentbaseoptions.BindLeaderElectionFlags(&leaderElection, pflag.CommandLine) kube_flag.InitFlags() - klog.V(1).InfoS("Vertical Pod Autoscaler Updater", "version", common.VerticalPodAutoscalerVersion) + klog.V(1).InfoS("Vertical Pod Autoscaler Updater", "version", common.VerticalPodAutoscalerVersion()) if len(commonFlags.VpaObjectNamespace) > 0 && len(commonFlags.IgnoredVpaObjectNamespaces) > 0 { klog.Fatalf("--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.") From b8851df26f140a94485cea357d1ef9408a67c7a3 Mon Sep 17 00:00:00 2001 From: Adrian Moisey Date: Sat, 4 Jan 2025 17:26:26 +0200 Subject: [PATCH 2/4] Linting --- vertical-pod-autoscaler/common/version.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vertical-pod-autoscaler/common/version.go b/vertical-pod-autoscaler/common/version.go index 04132e646115..1513ee97df9b 100644 --- a/vertical-pod-autoscaler/common/version.go +++ b/vertical-pod-autoscaler/common/version.go @@ -20,9 +20,10 @@ package common // It is injected at build time. var gitCommit = "" -// VerticalPodAutoscalerVersion is the version of VPA. +// versionCore is the version of VPA. const versionCore = "1.2.0" +// VerticalPodAutoscalerVersion returns the version of the VPA. func VerticalPodAutoscalerVersion() string { v := versionCore if gitCommit != "" { From 40f0c9b31220cfe63a4192fc5c84eabdc33afd18 Mon Sep 17 00:00:00 2001 From: Adrian Moisey Date: Sat, 4 Jan 2025 19:34:08 +0200 Subject: [PATCH 3/4] Update copyright date --- vertical-pod-autoscaler/hack/dev-deploy-locally.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vertical-pod-autoscaler/hack/dev-deploy-locally.sh b/vertical-pod-autoscaler/hack/dev-deploy-locally.sh index 170c2f2cae96..704e79c1a3fd 100755 --- a/vertical-pod-autoscaler/hack/dev-deploy-locally.sh +++ b/vertical-pod-autoscaler/hack/dev-deploy-locally.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Copyright 2023 The Kubernetes Authors. +# Copyright 2025 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From 8c1671f0e9e53ec8623539bce1bd531feb472bc3 Mon Sep 17 00:00:00 2001 From: Adrian Moisey Date: Tue, 7 Jan 2025 21:43:33 +0200 Subject: [PATCH 4/4] Fix typo --- vertical-pod-autoscaler/hack/dev-deploy-locally.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vertical-pod-autoscaler/hack/dev-deploy-locally.sh b/vertical-pod-autoscaler/hack/dev-deploy-locally.sh index 704e79c1a3fd..40ed85b983a5 100755 --- a/vertical-pod-autoscaler/hack/dev-deploy-locally.sh +++ b/vertical-pod-autoscaler/hack/dev-deploy-locally.sh @@ -66,5 +66,5 @@ ${SCRIPT_ROOT}/hack/deploy-for-e2e-locally.sh full-vpa echo " ** Restarting all VPA components" for i in admission-controller updater recommender; do - kubectl -n kube-system rollout status deployment/vpa-$i + kubectl -n kube-system rollout restart deployment/vpa-$i done