Skip to content

Commit

Permalink
Merge pull request #7654 from adrianmoisey/add-local-build-script
Browse files Browse the repository at this point in the history
VPA: Create a build and deploy locally script
  • Loading branch information
k8s-ci-robot authored Jan 8, 2025
2 parents a587c55 + 8c1671f commit 66d5e4a
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 14 deletions.
26 changes: 24 additions & 2 deletions vertical-pod-autoscaler/common/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,27 @@ limitations under the License.

package common

// VerticalPodAutoscalerVersion is the version of VPA.
const VerticalPodAutoscalerVersion = "1.2.0"
// gitCommit is the commit used to build the VPA binaries, if available.
// It is injected at build time.
var gitCommit = ""

// 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 != "" {
// 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]
}
70 changes: 70 additions & 0 deletions vertical-pod-autoscaler/hack/dev-deploy-locally.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

# 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.
# 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 restart deployment/vpa-$i
done
3 changes: 2 additions & 1 deletion vertical-pod-autoscaler/pkg/admission-controller/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions vertical-pod-autoscaler/pkg/admission-controller/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
Expand Down Expand Up @@ -91,4 +92,4 @@ format:

.PHONY: document-flags
document-flags:
go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-admission-flags.md
go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-admission-flags.md
2 changes: 1 addition & 1 deletion vertical-pod-autoscaler/pkg/admission-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.ErrorS(nil, "--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
Expand Down
3 changes: 2 additions & 1 deletion vertical-pod-autoscaler/pkg/recommender/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions vertical-pod-autoscaler/pkg/recommender/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
Expand Down Expand Up @@ -93,4 +94,4 @@ format:

.PHONY: document-flags
document-flags:
go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-recommender-flags.md
go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-recommender-flags.md
2 changes: 1 addition & 1 deletion vertical-pod-autoscaler/pkg/recommender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.ErrorS(nil, "--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
Expand Down
3 changes: 2 additions & 1 deletion vertical-pod-autoscaler/pkg/updater/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions vertical-pod-autoscaler/pkg/updater/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
Expand Down Expand Up @@ -91,4 +92,4 @@ format:

.PHONY: document-flags
document-flags:
go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-updater-flags.md
go run ../../hack/vpa-generate-flags.go . ../../docs/vpa-updater-flags.md
2 changes: 1 addition & 1 deletion vertical-pod-autoscaler/pkg/updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.ErrorS(nil, "--vpa-object-namespace and --ignored-vpa-object-namespaces are mutually exclusive and can't be set together.")
Expand Down

0 comments on commit 66d5e4a

Please sign in to comment.