Skip to content

Commit

Permalink
Set user agent with semantic version
Browse files Browse the repository at this point in the history
Change-Id: I4cc1893392783e020822336f7e804cee1f2a49a8
  • Loading branch information
alculquicondor committed Aug 17, 2022
1 parent 1f19339 commit 94cbd67
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 19 deletions.
4 changes: 0 additions & 4 deletions .dockerignore

This file was deleted.

1 change: 1 addition & 0 deletions .dockerignore
8 changes: 3 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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. "+
Expand All @@ -85,17 +89,22 @@ 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)
}

certsReady := make(chan struct{})

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 {
Expand All @@ -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)
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
57 changes: 57 additions & 0 deletions pkg/util/useragent/useragent.go
Original file line number Diff line number Diff line change
@@ -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))
}
31 changes: 31 additions & 0 deletions pkg/util/useragent/useragent_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
29 changes: 29 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -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)
)

0 comments on commit 94cbd67

Please sign in to comment.