Skip to content

Commit 7e2e3ee

Browse files
committed
Inject build number for dev builds.
1 parent e8efc11 commit 7e2e3ee

File tree

6 files changed

+62
-19
lines changed

6 files changed

+62
-19
lines changed

Makefile

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ GIT_TREE_STATE = $(if $(shell test -d .git || echo missing),archive,$(if $(shell
3939
# Example: BUILD_TAGS="minimal provider_kubernetes"
4040
BUILD_TAGS ?=
4141

42+
# Build number passed in must be a monotonic int representing
43+
# the build.
44+
JUJU_BUILD_NUMBER ?=
45+
4246
# Compile with debug flags if requested.
4347
ifeq ($(DEBUG_JUJU), 1)
4448
COMPILE_FLAGS = -gcflags "all=-N -l"
45-
LINK_FLAGS = -ldflags "-X $(PROJECT)/version.GitCommit=$(GIT_COMMIT) -X $(PROJECT)/version.GitTreeState=$(GIT_TREE_STATE)"
49+
LINK_FLAGS = -ldflags "-X $(PROJECT)/version.GitCommit=$(GIT_COMMIT) -X $(PROJECT)/version.GitTreeState=$(GIT_TREE_STATE) -X $(PROJECT)/version.build=$(JUJU_BUILD_NUMBER)"
4650
else
4751
COMPILE_FLAGS =
48-
LINK_FLAGS = -ldflags "-s -w -X $(PROJECT)/version.GitCommit=$(GIT_COMMIT) -X $(PROJECT)/version.GitTreeState=$(GIT_TREE_STATE)"
52+
LINK_FLAGS = -ldflags "-s -w -X $(PROJECT)/version.GitCommit=$(GIT_COMMIT) -X $(PROJECT)/version.GitTreeState=$(GIT_TREE_STATE) -X $(PROJECT)/version.build=$(JUJU_BUILD_NUMBER)"
4953
endif
5054

5155
define DEPENDENCIES
@@ -184,29 +188,41 @@ check-deps:
184188
@echo "$(GOCHECK_COUNT) instances of gocheck not in test code"
185189

186190
# CAAS related targets
187-
DOCKER_USERNAME ?= jujusolutions
188-
JUJUD_STAGING_DIR ?= /tmp/jujud-operator
189-
JUJUD_BIN_DIR ?= ${GOPATH}/bin
190-
OPERATOR_IMAGE_BUILD_SRC ?= true
191-
OPERATOR_IMAGE_TAG ?= $(shell jujud version | rev | cut -d- -f3- | rev)
192-
OPERATOR_IMAGE_PATH = ${DOCKER_USERNAME}/jujud-operator:${OPERATOR_IMAGE_TAG}
193-
194-
operator-image:
195-
ifeq ($(OPERATOR_IMAGE_BUILD_SRC),true)
196-
make install
197-
else
198-
@echo "skipping to build jujud bin, use existing one at ${JUJUD_BIN_DIR}/jujud."
199-
endif
191+
DOCKER_USERNAME ?= jujusolutions
192+
JUJUD_STAGING_DIR ?= /tmp/jujud-operator
193+
JUJUD_BIN_DIR ?= ${GOPATH}/bin
194+
OPERATOR_IMAGE_BUILD_SRC ?= true
195+
# By default the image tag is the full version number, including the build number.
196+
OPERATOR_IMAGE_TAG ?= $(shell jujud version | grep -E -o "^[[:digit:]]{1,9}\.[[:digit:]]{1,9}(\.|-[[:alpha:]]+)[[:digit:]]{1,9}(\.[[:digit:]]{1,9})?")
197+
# Legacy tags never have a build number.
198+
OPERATOR_IMAGE_TAG_LEGACY ?= $(shell jujud version | grep -E -o "^[[:digit:]]{1,9}\.[[:digit:]]{1,9}(\.|-[[:alpha:]]+)[[:digit:]]{1,9}")
199+
OPERATOR_IMAGE_PATH = ${DOCKER_USERNAME}/jujud-operator:${OPERATOR_IMAGE_TAG}
200+
OPERATOR_IMAGE_PATH_LEGACY = ${DOCKER_USERNAME}/jujud-operator:${OPERATOR_IMAGE_TAG_LEGACY}
201+
202+
operator-check-build:
203+
ifeq ($(OPERATOR_IMAGE_BUILD_SRC),true)
204+
make install
205+
else
206+
@echo "skipping to build jujud bin, use existing one at ${JUJUD_BIN_DIR}/jujud."
207+
endif
208+
209+
operator-image: operator-check-build
200210
rm -rf ${JUJUD_STAGING_DIR}
201211
mkdir ${JUJUD_STAGING_DIR}
202212
cp ${JUJUD_BIN_DIR}/jujud ${JUJUD_STAGING_DIR}
203213
cp caas/jujud-operator-dockerfile ${JUJUD_STAGING_DIR}
204214
cp caas/jujud-operator-requirements.txt ${JUJUD_STAGING_DIR}
205215
docker build -f ${JUJUD_STAGING_DIR}/jujud-operator-dockerfile -t ${OPERATOR_IMAGE_PATH} ${JUJUD_STAGING_DIR}
216+
ifneq ($(OPERATOR_IMAGE_PATH),$(OPERATOR_IMAGE_PATH_LEGACY))
217+
docker tag ${OPERATOR_IMAGE_PATH} ${OPERATOR_IMAGE_PATH_LEGACY}
218+
endif
206219
rm -rf ${JUJUD_STAGING_DIR}
207220

208221
push-operator-image: operator-image
209222
docker push ${OPERATOR_IMAGE_PATH}
223+
ifneq ($(OPERATOR_IMAGE_PATH),$(OPERATOR_IMAGE_PATH_LEGACY))
224+
docker push ${OPERATOR_IMAGE_PATH_LEGACY}
225+
endif
210226

211227
microk8s-operator-update: operator-image
212228
docker save ${OPERATOR_IMAGE_PATH} | microk8s.ctr --namespace k8s.io image import -

apiserver/facades/controller/caasoperatorprovisioner/provisioner.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ func (a *API) OperatorProvisioningInfo() (params.OperatorProvisioningInfo, error
112112
fmt.Sprintf("agent version is missing in model config %q", modelConfig.Name()),
113113
)
114114
}
115-
vers.Build = 0
116115

117116
imagePath := podcfg.GetJujuOCIImagePath(cfg, vers)
118117
storageClassName, _ := modelConfig.AllAttrs()[provider.OperatorStorageKey].(string)

apiserver/facades/controller/caasunitprovisioner/provisioner.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ func (f *Facade) provisioningInfo(model Model, tagString string) (*params.Kubern
276276
fmt.Sprintf("agent version is missing in model config %q", modelConfig.Name()),
277277
)
278278
}
279-
vers.Build = 0
280279
operatorImagePath := podcfg.GetJujuOCIImagePath(controllerCfg, vers)
281280

282281
filesystemParams, err := f.applicationFilesystemParams(app, controllerCfg, modelConfig)

cloudconfig/podcfg/image.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ func tagImagePath(path string, ver version.Number) string {
6666
verString = splittedPath[1]
6767
}
6868
if ver != version.Zero {
69-
ver.Build = 0
7069
verString = ver.String()
7170
}
7271
if verString != "" {

cmd/supercommand.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type versionDetail struct {
4242
GitTreeState string `json:"git-tree-state,omitempty" yaml:"git-tree-state,omitempty"`
4343
// Compiler reported by runtime.Compiler
4444
Compiler string `json:"compiler" yaml:"compiler"`
45+
// Build is a monotonic integer set by Jenkins.
46+
Build int `json:"build,omitempty" yaml:"build,omitempty"`
4547
}
4648

4749
// NewSuperCommand is like cmd.NewSuperCommand but
@@ -64,6 +66,7 @@ func NewSuperCommand(p cmd.SuperCommandParams) *cmd.SuperCommand {
6466
GitCommit: jujuversion.GitCommit,
6567
GitTreeState: jujuversion.GitTreeState,
6668
Compiler: jujuversion.Compiler,
69+
Build: jujuversion.Build,
6770
}
6871

6972
// p.Version should be a version.Binary, but juju/cmd does not

version/version.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313
"runtime"
14+
"strconv"
1415
"strings"
1516

1617
semversion "github.com/juju/version"
@@ -33,10 +34,17 @@ const (
3334
// The version that we switched over from old style numbering to new style.
3435
var switchOverVersion = semversion.MustParse("1.19.9")
3536

37+
// build is injected by Jenkins, it must be an integer or empty.
38+
var build string
39+
40+
// Build is a monotonic number injected by Jenkins. It is added to the Current version only when
41+
// the version is a development build (see IsDev(1))
42+
var Build = mustParseBuildInt(build)
43+
3644
// Current gives the current version of the system. If the file
3745
// "FORCE-VERSION" is present in the same directory as the running
3846
// binary, it will override this.
39-
var Current = semversion.MustParse(version)
47+
var Current = mustParseVersion(version, build)
4048

4149
// Compiler is the go compiler used to build the binary.
4250
var Compiler = runtime.Compiler
@@ -76,3 +84,22 @@ func IsDev(v semversion.Number) bool {
7684
}
7785
return v.Tag != "" || v.Build > 0
7886
}
87+
88+
func mustParseVersion(versionStr string, buildInt string) semversion.Number {
89+
ver := semversion.MustParse(versionStr)
90+
if IsDev(ver) {
91+
ver.Build = mustParseBuildInt(buildInt)
92+
}
93+
return ver
94+
}
95+
96+
func mustParseBuildInt(buildInt string) int {
97+
if buildInt == "" {
98+
return 0
99+
}
100+
i, err := strconv.Atoi(buildInt)
101+
if err != nil {
102+
panic(err)
103+
}
104+
return i
105+
}

0 commit comments

Comments
 (0)