Skip to content

Commit f25e7fb

Browse files
authored
🐛 Fix seed dup target labels (#776)
Fixes an issue with duplicate target labels. Also, add support for a hub build version needed to detect upgrades. The git _tag-hash_ is written to `/etc/hub-build`. The _abbreviated_ commit hash is appended when HEAD is a commit added to the latest label. Examples: ``` v0.6.0-beta.1-1-e161635 # tag+hash v0.6.0-beta.1-1 # tag only e161635 # hash only ``` After seeded, the build is stored in settings: `.hub.db.seed.build`. This is compared with current build to detect when the build has changed. Seeding is _skipped_ when both the build (version) and the seed data have not changed. --- The go.mod update was to bump go-utils for better logging of objects. Included here for better logging of the _Settings_. --------- Signed-off-by: Jeff Ortel <[email protected]>
1 parent 18950bb commit f25e7fb

File tree

8 files changed

+146
-29
lines changed

8 files changed

+146
-29
lines changed

Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ARG SEED_ROOT=/opt/app-root/src/tackle2-seed
2+
23
FROM registry.access.redhat.com/ubi9/go-toolset:latest as builder
34
ENV GOPATH=$APP_ROOT
45
COPY --chown=1001:0 . .
@@ -15,6 +16,7 @@ FROM quay.io/konveyor/static-report as report
1516
FROM registry.access.redhat.com/ubi9/ubi-minimal
1617
ARG SEED_ROOT
1718
COPY --from=builder /opt/app-root/src/bin/hub /usr/local/bin/tackle-hub
19+
COPY --from=builder /opt/app-root/src/bin/.build /etc/hub-build
1820
COPY --from=builder /opt/app-root/src/auth/roles.yaml /tmp/roles.yaml
1921
COPY --from=builder /opt/app-root/src/auth/users.yaml /tmp/users.yaml
2022
COPY --from=builder ${SEED_ROOT}/resources/ /tmp/seed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ debug: generate fmt vet
6060

6161
docker: vet
6262
go build $(BUILD)
63+
git describe --always --match "v[0-9]*" --tags HEAD > bin/.build
6364

6465
# Run against the configured Kubernetes cluster in ~/.kube/config
6566
run: fmt vet

go.mod

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ require (
77
github.com/PaesslerAG/gval v1.2.2
88
github.com/andygrunwald/go-jira v1.16.0
99
github.com/gin-gonic/gin v1.9.1
10-
github.com/go-logr/logr v1.2.4
10+
github.com/go-logr/logr v1.4.1
1111
github.com/go-playground/validator/v10 v10.14.0
1212
github.com/golang-jwt/jwt/v4 v4.5.0
13-
github.com/google/uuid v1.3.0
14-
github.com/jortel/go-utils v0.1.2
13+
github.com/google/uuid v1.6.0
14+
github.com/jortel/go-utils v0.1.4
1515
github.com/konveyor/tackle2-seed v0.0.0-20231025181853-8ce94f70f744
1616
github.com/mattn/go-sqlite3 v1.14.17
17-
github.com/onsi/gomega v1.27.6
17+
github.com/onsi/gomega v1.31.1
1818
github.com/prometheus/client_golang v1.15.0
1919
github.com/swaggo/swag v1.16.1
2020
golang.org/x/sys v0.19.0
@@ -54,7 +54,7 @@ require (
5454
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5555
github.com/golang/protobuf v1.5.3 // indirect
5656
github.com/google/gnostic v0.5.7-v3refs // indirect
57-
github.com/google/go-cmp v0.5.9 // indirect
57+
github.com/google/go-cmp v0.6.0 // indirect
5858
github.com/google/go-querystring v1.1.0 // indirect
5959
github.com/google/gofuzz v1.1.0 // indirect
6060
github.com/imdario/mergo v0.3.12 // indirect
@@ -80,7 +80,7 @@ require (
8080
github.com/rogpeppe/go-internal v1.10.0 // indirect
8181
github.com/segmentio/ksuid v1.0.4 // indirect
8282
github.com/shopspring/decimal v1.3.1 // indirect
83-
github.com/sirupsen/logrus v1.9.0 // indirect
83+
github.com/sirupsen/logrus v1.9.3 // indirect
8484
github.com/spf13/pflag v1.0.5 // indirect
8585
github.com/trivago/tgo v1.0.7 // indirect
8686
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
@@ -93,7 +93,7 @@ require (
9393
golang.org/x/term v0.19.0 // indirect
9494
golang.org/x/text v0.14.0 // indirect
9595
golang.org/x/time v0.5.0 // indirect
96-
golang.org/x/tools v0.7.0 // indirect
96+
golang.org/x/tools v0.16.1 // indirect
9797
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
9898
google.golang.org/appengine v1.6.7 // indirect
9999
google.golang.org/protobuf v1.33.0 // indirect

go.sum

+16-16
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
5151
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
5252
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
5353
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
54-
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
55-
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
54+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
55+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
5656
github.com/go-logr/zapr v1.2.3 h1:a9vnzlIBPQBBkeaR9IuMUfmVOrQlkoC4YfPoFkX3T7A=
5757
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
5858
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
@@ -113,16 +113,16 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
113113
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
114114
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
115115
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
116-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
117-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
116+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
117+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
118118
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
119119
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
120120
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
121121
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
122122
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
123123
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
124-
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
125-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
124+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
125+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
126126
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
127127
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
128128
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
@@ -131,8 +131,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
131131
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
132132
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
133133
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
134-
github.com/jortel/go-utils v0.1.2 h1:R0TcGRCcwoL793CymcKC5AF9idWXT2cR6eQ2xpBUsoI=
135-
github.com/jortel/go-utils v0.1.2/go.mod h1:sl6vav63ODI0sUfSz8e0pImNmCVFnVsuOFhZmwe9GDk=
134+
github.com/jortel/go-utils v0.1.4 h1:DzrID+gI9urEGkOoXYGYIKA/bozEIxmcYV/4cdVjW58=
135+
github.com/jortel/go-utils v0.1.4/go.mod h1:7vzFYbnb91+ltjjAmCv2jVL4CKX7Onz1i2EQPAvw9Cs=
136136
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
137137
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
138138
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -177,9 +177,9 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
177177
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
178178
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
179179
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
180-
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
181-
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
182-
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
180+
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
181+
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
182+
github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYGyAy0=
183183
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
184184
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
185185
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
@@ -204,8 +204,8 @@ github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c
204204
github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
205205
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
206206
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
207-
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
208-
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
207+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
208+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
209209
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
210210
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
211211
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
@@ -258,7 +258,7 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
258258
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
259259
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
260260
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
261-
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
261+
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
262262
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
263263
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
264264
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -342,8 +342,8 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY
342342
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
343343
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
344344
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
345-
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
346-
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
345+
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
346+
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
347347
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
348348
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
349349
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

seed/pkg.go

+37-6
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ import (
1212
"gorm.io/gorm"
1313
)
1414

15-
var log = logr.WithName("seeding")
15+
var (
16+
Settings = &settings.Settings
17+
log = logr.WithName("seeding")
18+
)
1619

17-
// SeedKey identifies the setting containing the applied seed digest.
18-
const SeedKey = ".hub.db.seed"
20+
const (
21+
// SeedKey identifies the setting containing the applied seed digest.
22+
SeedKey = ".hub.db.seed"
23+
// BuildKey identifies setting for the hub build that seeded.
24+
BuildKey = SeedKey + ".build"
25+
)
1926

2027
// Seeder specifies an interface for seeding DB models.
2128
type Seeder interface {
@@ -53,12 +60,12 @@ func Seed() (err error) {
5360
return
5461
}
5562

56-
match, err := compareChecksum(db, checksum)
63+
skipped, err := skip(db, checksum)
5764
if err != nil {
5865
return
5966
}
60-
if match {
61-
log.Info("Seed checksum match.")
67+
if skipped {
68+
log.Info("Seeding skipped.")
6269
return
6370
}
6471

@@ -80,7 +87,31 @@ func Seed() (err error) {
8087
if err != nil {
8188
return
8289
}
90+
err = saveBuild(tx)
91+
if err != nil {
92+
return
93+
}
8394
return
8495
})
8596
return
8697
}
98+
99+
// skip returns true when seeding can be skipped.
100+
func skip(db *gorm.DB, checksum []byte) (skip bool, err error) {
101+
match, err := compareChecksum(db, checksum)
102+
if err != nil {
103+
return
104+
}
105+
if !match {
106+
return
107+
}
108+
match, err = matchBuild(db)
109+
if err != nil {
110+
return
111+
}
112+
if !match {
113+
return
114+
}
115+
skip = true
116+
return
117+
}

seed/seed.go

+47
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func compareChecksum(db *gorm.DB, checksum []byte) (match bool, err error) {
8181
}
8282

8383
match = seededChecksum == fmt.Sprintf("%x", checksum)
84+
log.Info("Seed checksum", "matched", match)
8485
return
8586
}
8687

@@ -115,3 +116,49 @@ func migrationVersion(db *gorm.DB) (version uint, err error) {
115116
version = uint(v.Version)
116117
return
117118
}
119+
120+
// matchBuild
121+
func matchBuild(db *gorm.DB) (matched bool, err error) {
122+
build, err := getBuild(db)
123+
if err != nil {
124+
return
125+
}
126+
if build == "" {
127+
return
128+
}
129+
matched = build == Settings.Hub.Build
130+
log.Info("Seed build (version)", "matched", matched)
131+
return
132+
}
133+
134+
// getBuild returns the hub build version that seeded.
135+
func getBuild(db *gorm.DB) (version string, err error) {
136+
setting := &model.Setting{
137+
Key: BuildKey,
138+
}
139+
db = db.Where("key", BuildKey)
140+
err = db.FirstOrCreate(setting).Error
141+
if err != nil {
142+
err = liberr.Wrap(err)
143+
return
144+
}
145+
if n, cast := setting.Value.(string); cast {
146+
version = n
147+
}
148+
return
149+
}
150+
151+
// saveBuild update settings with current build that seeded.
152+
func saveBuild(db *gorm.DB) (err error) {
153+
setting := &model.Setting{
154+
Key: BuildKey,
155+
Value: Settings.Hub.Build,
156+
}
157+
db = db.Where("key", BuildKey)
158+
err = db.Updates(setting).Error
159+
if err != nil {
160+
err = liberr.Wrap(err)
161+
return
162+
}
163+
return
164+
}

seed/target.go

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func (r *Target) Apply(db *gorm.DB) (err error) {
8787
target.Provider = t.Provider
8888
target.Choice = t.Choice
8989
target.ImageID = f.ID
90+
target.Labels = []model.TargetLabel{}
9091
for _, l := range t.Labels {
9192
target.Labels = append(target.Labels, model.TargetLabel(l))
9293
}

settings/hub.go

+35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package settings
22

33
import (
4+
"bufio"
45
"os"
56
"os/user"
67
"strconv"
@@ -9,6 +10,7 @@ import (
910

1011
const (
1112
EnvNamespace = "NAMESPACE"
13+
EnvBuild = "BUILD"
1214
EnvDbPath = "DB_PATH"
1315
EnvDbMaxCon = "DB_MAX_CONNECTION"
1416
EnvDbSeedPath = "DB_SEED_PATH"
@@ -44,6 +46,8 @@ const (
4446
)
4547

4648
type Hub struct {
49+
// build version.
50+
Build string
4751
// k8s namespace.
4852
Namespace string
4953
// DB settings.
@@ -124,6 +128,10 @@ type Hub struct {
124128

125129
func (r *Hub) Load() (err error) {
126130
var found bool
131+
r.Build, err = r.build()
132+
if err != nil {
133+
return
134+
}
127135
r.Namespace, err = r.namespace()
128136
if err != nil {
129137
return
@@ -356,3 +364,30 @@ func (r *Hub) namespace() (ns string, err error) {
356364

357365
return
358366
}
367+
368+
// build returns the hub build version.
369+
// This is expected to be the output of `git describe`.
370+
// Examples:
371+
// v0.6.0-ea89gcd
372+
// v0.6.0
373+
func (r *Hub) build() (version string, err error) {
374+
version, found := os.LookupEnv(EnvBuild)
375+
if found {
376+
return
377+
}
378+
f, err := os.Open("/etc/hub-build")
379+
if err != nil {
380+
if os.IsNotExist(err) {
381+
err = nil
382+
return
383+
}
384+
}
385+
defer func() {
386+
_ = f.Close()
387+
}()
388+
scanner := bufio.NewScanner(f)
389+
if scanner.Scan() {
390+
version = scanner.Text()
391+
}
392+
return
393+
}

0 commit comments

Comments
 (0)