Skip to content

Commit 42ae3c5

Browse files
Add version to controller info response
This change adds the GARM server version to the controller info response. Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent dcee092 commit 42ae3c5

File tree

10 files changed

+43
-16
lines changed

10 files changed

+43
-16
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ RUN git clone https://github.com/cloudbase/garm-provider-equinix /build/garm-pro
1919

2020
RUN cd /build/garm && go build -o /bin/garm \
2121
-tags osusergo,netgo,sqlite_omit_load_extension \
22-
-ldflags "-linkmode external -extldflags '-static' -s -w -X main.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" \
22+
-ldflags "-linkmode external -extldflags '-static' -s -w -X github.com/cloudbase/garm/util/appdefaults.Version=$(git describe --tags --match='v[0-9]*' --dirty --always)" \
2323
/build/garm/cmd/garm && upx /bin/garm
2424
RUN mkdir -p /opt/garm/providers.d
2525
RUN cd /build/garm-provider-azure && go build -ldflags="-linkmode external -extldflags '-static' -s -w" -o /opt/garm/providers.d/garm-provider-azure . && upx /opt/garm/providers.d/garm-provider-azure

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ clean: ## Clean up build artifacts
4444
build: ## Build garm
4545
@echo Building garm ${VERSION}
4646
$(shell mkdir -p ./bin)
47-
@$(GO) build -ldflags "-s -w -X main.Version=${VERSION}" -tags osusergo,netgo,sqlite_omit_load_extension -o bin/garm ./cmd/garm
48-
@$(GO) build -ldflags "-s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=${VERSION}" -tags osusergo,netgo,sqlite_omit_load_extension -o bin/garm-cli ./cmd/garm-cli
47+
@$(GO) build -ldflags "-s -w -X github.com/cloudbase/garm/util/appdefaults.Version=${VERSION}" -tags osusergo,netgo,sqlite_omit_load_extension -o bin/garm ./cmd/garm
48+
@$(GO) build -ldflags "-s -w -X github.com/cloudbase/garm/util/appdefaults.Version=${VERSION}" -tags osusergo,netgo,sqlite_omit_load_extension -o bin/garm-cli ./cmd/garm-cli
4949
@echo Binaries are available in $(PWD)/bin
5050

5151
test: verify go-test ## Run tests

cmd/garm-cli/cmd/controller.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ func renderControllerInfoTable(info params.ControllerInfo) string {
145145
if info.ControllerWebhookURL == "" {
146146
info.ControllerWebhookURL = "N/A"
147147
}
148-
148+
serverVersion := "v0.0.0-unknown"
149+
if info.Version != "" {
150+
serverVersion = info.Version
151+
}
149152
t.AppendHeader(header)
150153
t.AppendRow(table.Row{"Controller ID", info.ControllerID})
151154
if info.Hostname != "" {
@@ -156,6 +159,7 @@ func renderControllerInfoTable(info params.ControllerInfo) string {
156159
t.AppendRow(table.Row{"Webhook Base URL", info.WebhookURL})
157160
t.AppendRow(table.Row{"Controller Webhook URL", info.ControllerWebhookURL})
158161
t.AppendRow(table.Row{"Minimum Job Age Backoff", info.MinimumJobAgeBackoff})
162+
t.AppendRow(table.Row{"Version", serverVersion})
159163
return t.Render()
160164
}
161165

cmd/garm-cli/cmd/root.go

-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import (
2929
"github.com/cloudbase/garm/params"
3030
)
3131

32-
var Version string
33-
3432
var (
3533
cfg *config.Config
3634
mgr config.Manager

cmd/garm-cli/cmd/version.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
"fmt"
1919

2020
"github.com/spf13/cobra"
21+
22+
apiClientControllerInfo "github.com/cloudbase/garm/client/controller_info"
23+
"github.com/cloudbase/garm/util/appdefaults"
2124
)
2225

2326
// runnerCmd represents the runner command
@@ -26,7 +29,18 @@ var versionCmd = &cobra.Command{
2629
SilenceUsage: true,
2730
Short: "Print version and exit",
2831
Run: func(_ *cobra.Command, _ []string) {
29-
fmt.Println(Version)
32+
serverVersion := "v0.0.0-unknown"
33+
34+
if !needsInit {
35+
showInfo := apiClientControllerInfo.NewControllerInfoParams()
36+
response, err := apiCli.ControllerInfo.ControllerInfo(showInfo, authToken)
37+
if err == nil {
38+
serverVersion = response.Payload.Version
39+
}
40+
}
41+
42+
fmt.Printf("garm-cli: %s\n", appdefaults.GetVersion())
43+
fmt.Printf("garm server: %s\n", serverVersion)
3044
},
3145
}
3246

cmd/garm/main.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ var (
5555
version = flag.Bool("version", false, "prints version")
5656
)
5757

58-
var Version string
59-
6058
var signals = []os.Signal{
6159
os.Interrupt,
6260
syscall.SIGTERM,
@@ -179,7 +177,7 @@ func maybeUpdateURLsFromConfig(cfg config.Config, store common.Store) error {
179177
func main() {
180178
flag.Parse()
181179
if *version {
182-
fmt.Println(Version)
180+
fmt.Println(appdefaults.GetVersion())
183181
return
184182
}
185183
ctx, stop := signal.NotifyContext(context.Background(), signals...)

database/sql/controller.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
2525
"github.com/cloudbase/garm/database/common"
2626
"github.com/cloudbase/garm/params"
27+
"github.com/cloudbase/garm/util/appdefaults"
2728
)
2829

2930
func dbControllerToCommonController(dbInfo ControllerInfo) (params.ControllerInfo, error) {
@@ -39,6 +40,7 @@ func dbControllerToCommonController(dbInfo ControllerInfo) (params.ControllerInf
3940
ControllerWebhookURL: url,
4041
CallbackURL: dbInfo.CallbackURL,
4142
MinimumJobAgeBackoff: dbInfo.MinimumJobAgeBackoff,
43+
Version: appdefaults.GetVersion(),
4244
}, nil
4345
}
4446

params/params.go

+2
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ type ControllerInfo struct {
594594
// runners to pick up the job before GARM attempts to allocate a new runner, thus avoiding
595595
// the need to potentially scale down runners later.
596596
MinimumJobAgeBackoff uint `json:"minimum_job_age_backoff"`
597+
// Version is the version of the GARM controller.
598+
Version string `json:"version"`
597599
}
598600

599601
type GithubCredentials struct {

scripts/build-static.sh

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ cd $GARM_SOURCE/cmd/garm
3030
GOOS=linux GOARCH=amd64 go build -mod vendor \
3131
-o $BUILD_DIR/linux/amd64/garm \
3232
-tags osusergo,netgo,sqlite_omit_load_extension \
33-
-ldflags "-extldflags '-static' -s -w -X main.Version=$VERSION" .
33+
-ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/util/appdefaults.Version=$VERSION" .
3434
GOOS=linux GOARCH=arm64 CC=aarch64-linux-musl-gcc go build \
3535
-mod vendor \
3636
-o $BUILD_DIR/linux/arm64/garm \
3737
-tags osusergo,netgo,sqlite_omit_load_extension \
38-
-ldflags "-extldflags '-static' -s -w -X main.Version=$VERSION" .
38+
-ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/util/appdefaults.Version=$VERSION" .
3939

4040
# Windows
4141
GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-cc go build -mod vendor \
4242
-o $BUILD_DIR/windows/amd64/garm.exe \
4343
-tags osusergo,netgo,sqlite_omit_load_extension \
44-
-ldflags "-s -w -X main.Version=$VERSION" .
44+
-ldflags "-s -w -X github.com/cloudbase/garm/util/appdefaults.Version=$VERSION" .
4545

4646
# garm-cli
4747
cd $GARM_SOURCE/cmd/garm-cli
@@ -50,17 +50,17 @@ cd $GARM_SOURCE/cmd/garm-cli
5050
GOOS=linux GOARCH=amd64 go build -mod vendor \
5151
-o $BUILD_DIR/linux/amd64/garm-cli \
5252
-tags osusergo,netgo,sqlite_omit_load_extension \
53-
-ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" .
53+
-ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/util/appdefaults.Version=$VERSION" .
5454
GOOS=linux GOARCH=arm64 CC=aarch64-linux-musl-gcc go build -mod vendor \
5555
-o $BUILD_DIR/linux/arm64/garm-cli \
5656
-tags osusergo,netgo,sqlite_omit_load_extension \
57-
-ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" .
57+
-ldflags "-extldflags '-static' -s -w -X github.com/cloudbase/garm/util/appdefaults.Version=$VERSION" .
5858

5959
# Windows
6060
GOOS=windows GOARCH=amd64 go build -mod vendor \
6161
-o $BUILD_DIR/windows/amd64/garm-cli.exe \
6262
-tags osusergo,netgo,sqlite_omit_load_extension \
63-
-ldflags "-s -w -X github.com/cloudbase/garm/cmd/garm-cli/cmd.Version=$VERSION" .
63+
-ldflags "-s -w -X github.com/cloudbase/garm/util/appdefaults.Version=$VERSION" .
6464

6565

6666
git checkout $CURRENT_BRANCH || true

util/appdefaults/appdefaults.go

+9
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ const (
3131
// metrics data update interval
3232
DefaultMetricsUpdateInterval = 60 * time.Second
3333
)
34+
35+
var Version string
36+
37+
func GetVersion() string {
38+
if Version == "" {
39+
Version = "v0.0.0-unknown"
40+
}
41+
return Version
42+
}

0 commit comments

Comments
 (0)