Skip to content

Commit

Permalink
BUG/MEDIUM: display correct version of ingress controller in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
oktalz committed Feb 6, 2024
1 parent 13214c8 commit 3e0133c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
9 changes: 1 addition & 8 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,9 @@ COPY / /src

RUN mkdir -p /var/run/vars && \
cd /src && \
git config --get remote.origin.url > /var/run/vars/GIT_REPO && \
git rev-parse --short HEAD > /var/run/vars/GIT_HEAD_COMMIT && \
git log -1 --date=format:"%Y/%m/%d %T" --format="%ad" > /var/run/vars/GIT_DATE_LAST_COMMIT && \
git describe --abbrev=0 --tags > /var/run/vars/GIT_LAST_TAG && \
git rev-parse --short $(cat /var/run/vars/GIT_LAST_TAG) > /var/run/vars/GIT_TAG_COMMIT && \
git diff $(cat /var/run/vars/GIT_HEAD_COMMIT) $(cat /var/run/vars/GIT_TAG_COMMIT) --quiet > /var/run/vars/GIT_MODIFIED1 || echo '.dev' > /var/run/vars/GIT_MODIFIED1 && \
git diff --quiet > /var/run/vars/GIT_MODIFIED2 || echo '.dirty' > /var/run/vars/GIT_MODIFIED2 && \
cat /var/run/vars/GIT_MODIFIED1 /var/run/vars/GIT_MODIFIED2 | tr -d '\n' > /var/run/vars/GIT_MODIFIED && \
CGO_ENABLED=0 go build \
-ldflags "-X pkg.version.GitRepo=$(cat /var/run/vars/GIT_REPO) -X pkg.version.GitTag=$(cat /var/run/vars/GIT_LAST_TAG) -X pkg.version.GitCommit=$(cat /var/run/vars/GIT_HEAD_COMMIT) -X pkg.version.GitDirty=$(cat /var/run/vars/GIT_MODIFIED) -X \"pkg.version.GitCommitDate=$(cat /var/run/vars/GIT_DATE_LAST_COMMIT)\"" \
-ldflags "-X github.com/haproxytech/kubernetes-ingress/pkg/version.GitTag=$(cat /var/run/vars/GIT_LAST_TAG)" \
-o fs/haproxy-ingress-controller .

FROM haproxytech/haproxy-alpine:2.8
Expand Down
9 changes: 1 addition & 8 deletions build/Dockerfile.pebble
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,9 @@ RUN go install github.com/canonical/pebble/cmd/[email protected]

RUN mkdir -p /var/run/vars && \
cd /src && \
git config --get remote.origin.url > /var/run/vars/GIT_REPO && \
git rev-parse --short HEAD > /var/run/vars/GIT_HEAD_COMMIT && \
git log -1 --date=format:"%Y/%m/%d %T" --format="%ad" > /var/run/vars/GIT_DATE_LAST_COMMIT && \
git describe --abbrev=0 --tags > /var/run/vars/GIT_LAST_TAG && \
git rev-parse --short $(cat /var/run/vars/GIT_LAST_TAG) > /var/run/vars/GIT_TAG_COMMIT && \
git diff $(cat /var/run/vars/GIT_HEAD_COMMIT) $(cat /var/run/vars/GIT_TAG_COMMIT) --quiet > /var/run/vars/GIT_MODIFIED1 || echo '.dev' > /var/run/vars/GIT_MODIFIED1 && \
git diff --quiet > /var/run/vars/GIT_MODIFIED2 || echo '.dirty' > /var/run/vars/GIT_MODIFIED2 && \
cat /var/run/vars/GIT_MODIFIED1 /var/run/vars/GIT_MODIFIED2 | tr -d '\n' > /var/run/vars/GIT_MODIFIED && \
CGO_ENABLED=0 go build \
-ldflags "-X pkg.version.GitRepo=$(cat /var/run/vars/GIT_REPO) -X pkg.version.GitTag=$(cat /var/run/vars/GIT_LAST_TAG) -X pkg.version.GitCommit=$(cat /var/run/vars/GIT_HEAD_COMMIT) -X pkg.version.GitDirty=$(cat /var/run/vars/GIT_MODIFIED) -X \"pkg.version.GitCommitDate=$(cat /var/run/vars/GIT_DATE_LAST_COMMIT)\"" \
-ldflags "-X github.com/haproxytech/kubernetes-ingress/pkg/version.GitTag=$(cat /var/run/vars/GIT_LAST_TAG)" \
-o fs/haproxy-ingress-controller .

FROM haproxytech/haproxy-alpine:2.8
Expand Down
21 changes: 14 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,18 @@ func main() {

// Set Logger
logger := utils.GetLogger()
err = version.Set()
if err != nil {
logger.Error(err)
os.Exit(1) //nolint:gocritic
}

logger.SetLevel(osArgs.LogLevel.LogLevel)
if len(osArgs.Help) > 0 && osArgs.Help[0] {
parser.WriteHelp(os.Stdout)
return
}

if osArgs.JobCheckCRD {
logger.Print(version.IngressControllerInfo)
logger.Print(job.IngressControllerCRDUpdater)
Expand All @@ -82,7 +89,7 @@ func main() {
err := job.CRDRefresh(logger, osArgs)
if err != nil {
logger.Error(err)
os.Exit(1) //nolint:gocritic
os.Exit(1)
}
// exit, this is just a job
os.Exit(0)
Expand Down Expand Up @@ -156,13 +163,13 @@ func main() {

func logInfo(logger utils.Logger, osArgs utils.OSArgs) bool {
if len(osArgs.Version) > 0 {
fmt.Printf("HAProxy Ingress Controller %s %s%s", version.GitTag, version.GitCommit, version.GitDirty)
fmt.Printf("Build from: %s", version.GitRepo)
fmt.Printf("Git commit date: %s", version.GitCommitDate)
fmt.Printf("HAProxy Ingress Controller %s %s%s\n", version.GitTag, version.GitCommit, version.GitDirty)
fmt.Printf("Build from: %s\n", version.GitRepo)
fmt.Printf("Git commit date: %s\n", version.GitCommitDate)
if len(osArgs.Version) > 1 {
fmt.Printf("ConfigMap: %s", osArgs.ConfigMap)
fmt.Printf("Ingress class: %s", osArgs.IngressClass)
fmt.Printf("Empty Ingress class: %t", osArgs.EmptyIngressClass)
fmt.Printf("ConfigMap: %s\n", osArgs.ConfigMap)
fmt.Printf("Ingress class: %s\n", osArgs.IngressClass)
fmt.Printf("Empty Ingress class: %t\n", osArgs.EmptyIngressClass)
}
return true
}
Expand Down
49 changes: 49 additions & 0 deletions pkg/version/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2019 HAProxy Technologies LLC
//
// 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

import (
"errors"
"runtime/debug"
)

func Set() error {
buildinfo, ok := debug.ReadBuildInfo()
if !ok {
return errors.New("not able to read build data")
}
GitRepo = buildinfo.Main.Path
GitCommitDate = get(buildinfo, "vcs.time")
GitCommit = get(buildinfo, "vcs.revision")
if len(GitCommit) > 8 {
GitCommit = GitCommit[:8]
}
if get(buildinfo, "vcs.modified") == "true" {
GitDirty = ".dirty"
} else {
GitDirty = ""
}

return nil
}

func get(buildInfo *debug.BuildInfo, key string) string {
for _, setting := range buildInfo.Settings {
if setting.Key == key {
return setting.Value
}
}
return ""
}

0 comments on commit 3e0133c

Please sign in to comment.