Skip to content

Commit 3e0133c

Browse files
committed
BUG/MEDIUM: display correct version of ingress controller in logs
1 parent 13214c8 commit 3e0133c

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

build/Dockerfile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,9 @@ COPY / /src
2424

2525
RUN mkdir -p /var/run/vars && \
2626
cd /src && \
27-
git config --get remote.origin.url > /var/run/vars/GIT_REPO && \
28-
git rev-parse --short HEAD > /var/run/vars/GIT_HEAD_COMMIT && \
29-
git log -1 --date=format:"%Y/%m/%d %T" --format="%ad" > /var/run/vars/GIT_DATE_LAST_COMMIT && \
3027
git describe --abbrev=0 --tags > /var/run/vars/GIT_LAST_TAG && \
31-
git rev-parse --short $(cat /var/run/vars/GIT_LAST_TAG) > /var/run/vars/GIT_TAG_COMMIT && \
32-
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 && \
33-
git diff --quiet > /var/run/vars/GIT_MODIFIED2 || echo '.dirty' > /var/run/vars/GIT_MODIFIED2 && \
34-
cat /var/run/vars/GIT_MODIFIED1 /var/run/vars/GIT_MODIFIED2 | tr -d '\n' > /var/run/vars/GIT_MODIFIED && \
3528
CGO_ENABLED=0 go build \
36-
-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)\"" \
29+
-ldflags "-X github.com/haproxytech/kubernetes-ingress/pkg/version.GitTag=$(cat /var/run/vars/GIT_LAST_TAG)" \
3730
-o fs/haproxy-ingress-controller .
3831

3932
FROM haproxytech/haproxy-alpine:2.8

build/Dockerfile.pebble

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,9 @@ RUN go install github.com/canonical/pebble/cmd/[email protected]
2626

2727
RUN mkdir -p /var/run/vars && \
2828
cd /src && \
29-
git config --get remote.origin.url > /var/run/vars/GIT_REPO && \
30-
git rev-parse --short HEAD > /var/run/vars/GIT_HEAD_COMMIT && \
31-
git log -1 --date=format:"%Y/%m/%d %T" --format="%ad" > /var/run/vars/GIT_DATE_LAST_COMMIT && \
3229
git describe --abbrev=0 --tags > /var/run/vars/GIT_LAST_TAG && \
33-
git rev-parse --short $(cat /var/run/vars/GIT_LAST_TAG) > /var/run/vars/GIT_TAG_COMMIT && \
34-
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 && \
35-
git diff --quiet > /var/run/vars/GIT_MODIFIED2 || echo '.dirty' > /var/run/vars/GIT_MODIFIED2 && \
36-
cat /var/run/vars/GIT_MODIFIED1 /var/run/vars/GIT_MODIFIED2 | tr -d '\n' > /var/run/vars/GIT_MODIFIED && \
3730
CGO_ENABLED=0 go build \
38-
-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)\"" \
31+
-ldflags "-X github.com/haproxytech/kubernetes-ingress/pkg/version.GitTag=$(cat /var/run/vars/GIT_LAST_TAG)" \
3932
-o fs/haproxy-ingress-controller .
4033

4134
FROM haproxytech/haproxy-alpine:2.8

main.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,18 @@ func main() {
6767

6868
// Set Logger
6969
logger := utils.GetLogger()
70+
err = version.Set()
71+
if err != nil {
72+
logger.Error(err)
73+
os.Exit(1) //nolint:gocritic
74+
}
75+
7076
logger.SetLevel(osArgs.LogLevel.LogLevel)
7177
if len(osArgs.Help) > 0 && osArgs.Help[0] {
7278
parser.WriteHelp(os.Stdout)
7379
return
7480
}
81+
7582
if osArgs.JobCheckCRD {
7683
logger.Print(version.IngressControllerInfo)
7784
logger.Print(job.IngressControllerCRDUpdater)
@@ -82,7 +89,7 @@ func main() {
8289
err := job.CRDRefresh(logger, osArgs)
8390
if err != nil {
8491
logger.Error(err)
85-
os.Exit(1) //nolint:gocritic
92+
os.Exit(1)
8693
}
8794
// exit, this is just a job
8895
os.Exit(0)
@@ -156,13 +163,13 @@ func main() {
156163

157164
func logInfo(logger utils.Logger, osArgs utils.OSArgs) bool {
158165
if len(osArgs.Version) > 0 {
159-
fmt.Printf("HAProxy Ingress Controller %s %s%s", version.GitTag, version.GitCommit, version.GitDirty)
160-
fmt.Printf("Build from: %s", version.GitRepo)
161-
fmt.Printf("Git commit date: %s", version.GitCommitDate)
166+
fmt.Printf("HAProxy Ingress Controller %s %s%s\n", version.GitTag, version.GitCommit, version.GitDirty)
167+
fmt.Printf("Build from: %s\n", version.GitRepo)
168+
fmt.Printf("Git commit date: %s\n", version.GitCommitDate)
162169
if len(osArgs.Version) > 1 {
163-
fmt.Printf("ConfigMap: %s", osArgs.ConfigMap)
164-
fmt.Printf("Ingress class: %s", osArgs.IngressClass)
165-
fmt.Printf("Empty Ingress class: %t", osArgs.EmptyIngressClass)
170+
fmt.Printf("ConfigMap: %s\n", osArgs.ConfigMap)
171+
fmt.Printf("Ingress class: %s\n", osArgs.IngressClass)
172+
fmt.Printf("Empty Ingress class: %t\n", osArgs.EmptyIngressClass)
166173
}
167174
return true
168175
}

pkg/version/runtime.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019 HAProxy Technologies LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
import (
18+
"errors"
19+
"runtime/debug"
20+
)
21+
22+
func Set() error {
23+
buildinfo, ok := debug.ReadBuildInfo()
24+
if !ok {
25+
return errors.New("not able to read build data")
26+
}
27+
GitRepo = buildinfo.Main.Path
28+
GitCommitDate = get(buildinfo, "vcs.time")
29+
GitCommit = get(buildinfo, "vcs.revision")
30+
if len(GitCommit) > 8 {
31+
GitCommit = GitCommit[:8]
32+
}
33+
if get(buildinfo, "vcs.modified") == "true" {
34+
GitDirty = ".dirty"
35+
} else {
36+
GitDirty = ""
37+
}
38+
39+
return nil
40+
}
41+
42+
func get(buildInfo *debug.BuildInfo, key string) string {
43+
for _, setting := range buildInfo.Settings {
44+
if setting.Key == key {
45+
return setting.Value
46+
}
47+
}
48+
return ""
49+
}

0 commit comments

Comments
 (0)