From 94cbd67b40cc11d1e5fcf46979e04082e78f9845 Mon Sep 17 00:00:00 2001 From: Aldo Culquicondor Date: Wed, 17 Aug 2022 15:00:35 -0400 Subject: [PATCH] Set user agent with semantic version Change-Id: I4cc1893392783e020822336f7e804cee1f2a49a8 --- .dockerignore | 5 +-- Dockerfile | 8 ++-- Makefile | 10 +++-- main.go | 19 +++++++--- pkg/constants/constants.go | 5 ++- pkg/util/useragent/useragent.go | 57 ++++++++++++++++++++++++++++ pkg/util/useragent/useragent_test.go | 31 +++++++++++++++ pkg/version/version.go | 29 ++++++++++++++ 8 files changed, 145 insertions(+), 19 deletions(-) mode change 100644 => 120000 .dockerignore create mode 100644 pkg/util/useragent/useragent.go create mode 100644 pkg/util/useragent/useragent_test.go create mode 100644 pkg/version/version.go diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 0f046820f1..0000000000 --- a/.dockerignore +++ /dev/null @@ -1,4 +0,0 @@ -# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file -# Ignore build and test binaries. -bin/ -testbin/ diff --git a/.dockerignore b/.dockerignore new file mode 120000 index 0000000000..3e4e48b0b5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.gitignore \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3260c8dd07..7ccfb282d2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,16 +12,14 @@ COPY go.sum go.sum RUN go mod download # Copy the go source -COPY main.go main.go -COPY apis/ apis/ -COPY pkg/ pkg/ +COPY . . # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go +RUN make build GO_BUILD_ENV='CGO_ENABLED=0 GOOS=linux GOARCH=amd64' FROM ${BASE_IMAGE} WORKDIR / -COPY --from=builder /workspace/manager . +COPY --from=builder /workspace/bin/manager . USER 65532:65532 ENTRYPOINT ["/manager"] diff --git a/Makefile b/Makefile index 06f5e38dcd..fa006dd346 100644 --- a/Makefile +++ b/Makefile @@ -57,8 +57,12 @@ GO_TEST_FLAGS ?= -race SHELL = /usr/bin/env bash -o pipefail .SHELLFLAGS = -ec +version_pkg = sigs.k8s.io/kueue/pkg/version +LD_FLAGS += -X '$(version_pkg).GitVersion=$(GIT_TAG)' +LD_FLAGS += -X '$(version_pkg).GitCommit=$(shell git rev-parse HEAD)' + .PHONY: all -all: build +all: generate fmt vet build ##@ General @@ -128,8 +132,8 @@ verify: gomod-verify vet ci-lint fmt-verify manifests generate ##@ Build .PHONY: build -build: generate fmt vet ## Build manager binary. - $(GO_CMD) build -o bin/manager main.go +build: + $(GO_BUILD_ENV) $(GO_CMD) build -ldflags="$(LD_FLAGS)" -o bin/manager main.go .PHONY: run run: manifests generate fmt vet ## Run a controller from your host. diff --git a/main.go b/main.go index 2a944f8493..9ad5900498 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,8 @@ import ( "sigs.k8s.io/kueue/pkg/queue" "sigs.k8s.io/kueue/pkg/scheduler" "sigs.k8s.io/kueue/pkg/util/cert" + "sigs.k8s.io/kueue/pkg/util/useragent" + "sigs.k8s.io/kueue/pkg/version" // +kubebuilder:scaffold:imports ) @@ -67,6 +69,8 @@ func init() { } func main() { + setupLog.Info("Initializing", "gitVersion", version.GitVersion, "gitCommit", version.GitCommit) + var configFile string flag.StringVar(&configFile, "config", "", "The controller will load its initial configuration from this file. "+ @@ -85,9 +89,14 @@ func main() { metrics.Register() - mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options) + kubeConfig := ctrl.GetConfigOrDie() + if kubeConfig.UserAgent == "" { + kubeConfig.UserAgent = useragent.Default() + } + + mgr, err := ctrl.NewManager(kubeConfig, options) if err != nil { - setupLog.Error(err, "unable to start manager") + setupLog.Error(err, "Unable to start manager") os.Exit(1) } @@ -95,7 +104,7 @@ func main() { if *config.EnableInternalCertManagement { if err = cert.ManageCerts(mgr, certsReady); err != nil { - setupLog.Error(err, "unable to set up cert rotation") + setupLog.Error(err, "Unable to set up cert rotation") os.Exit(1) } } else { @@ -120,9 +129,9 @@ func main() { setupScheduler(ctx, mgr, cCache, queues) - setupLog.Info("starting manager") + setupLog.Info("Starting manager") if err := mgr.Start(ctx); err != nil { - setupLog.Error(err, "problem running manager") + setupLog.Error(err, "Could not run manager") os.Exit(1) } } diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index a06266b320..447a989c3f 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -24,8 +24,9 @@ const ( // TODO(#23): Use the kubernetes.io domain when graduating APIs to beta. QueueAnnotation = "kueue.x-k8s.io/queue-name" - ManagerName = "kueue-manager" - JobControllerName = "kueue-job-controller" + KueueName = "kueue" + ManagerName = KueueName + "-manager" + JobControllerName = KueueName + "-job-controller" // UpdatesBatchPeriod is the batch period to hold workload updates // before syncing a Queue and ClusterQueue objects. diff --git a/pkg/util/useragent/useragent.go b/pkg/util/useragent/useragent.go new file mode 100644 index 0000000000..45072bed03 --- /dev/null +++ b/pkg/util/useragent/useragent.go @@ -0,0 +1,57 @@ +/* +Copyright 2022 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. +*/ + +package useragent + +import ( + "fmt" + "runtime" + "strings" + + "sigs.k8s.io/kueue/pkg/constants" + "sigs.k8s.io/kueue/pkg/version" +) + +// adjustVersion strips "alpha", "beta", etc. from version in form +// major.minor.patch-[alpha|beta|etc]. +func adjustVersion(v string) string { + if len(v) == 0 { + return "unknown" + } + seg := strings.SplitN(v, "-", 2) + return seg[0] +} + +// adjustCommit returns sufficient significant figures of the commit's git hash. +func adjustCommit(c string) string { + if len(c) == 0 { + return "unknown" + } + if len(c) > 7 { + return c[:7] + } + return c +} + +// Default returns User-Agent string built from static global vars. +func Default() string { + return fmt.Sprintf("%s/%s (%s/%s) %s", + constants.KueueName, + adjustVersion(version.GitVersion), + runtime.GOOS, + runtime.GOARCH, + adjustCommit(version.GitCommit)) +} diff --git a/pkg/util/useragent/useragent_test.go b/pkg/util/useragent/useragent_test.go new file mode 100644 index 0000000000..508d433e7c --- /dev/null +++ b/pkg/util/useragent/useragent_test.go @@ -0,0 +1,31 @@ +/* +Copyright 2022 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. +*/ + +package useragent + +import ( + "fmt" + "runtime" + "testing" +) + +func TestDefault(t *testing.T) { + want := fmt.Sprintf("kueue/v0.0.0 (%s/%s) abcd012", runtime.GOOS, runtime.GOARCH) + ua := Default() + if ua != want { + t.Errorf("Default()=%q, want %q", ua, want) + } +} diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 0000000000..8c7141b09e --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,29 @@ +/* +Copyright 2022 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. +*/ + +package version + +// Base version information. +// +// This is the fallback data used when version information from git is not +// provided via go ldflags. +// +// If you are looking at these fields in the git tree, they look +// strange. They are modified on the fly by the build process. +var ( + GitVersion string = "v0.0.0-main" + GitCommit string = "abcd01234" // sha1 from git, output of $(git rev-parse HEAD) +)