Skip to content

Commit 06e5a4f

Browse files
authored
Improve version handling for main module and when this project is imported (#69)
1 parent 6263974 commit 06e5a4f

File tree

8 files changed

+119
-81
lines changed

8 files changed

+119
-81
lines changed

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ GOBIN=$(shell go env GOBIN)
1111
endif
1212

1313
# GO_BUILD_ARGS should be set when running 'go build' or 'go install'.
14-
REPO = $(shell go list -m)
15-
#TODO (anrastog): set version to repo build/tag after v1 plugin is available in master.
16-
VERSION = master
14+
VERSION_PKG = "$(shell go list -m)/internal/version"
15+
SCAFFOLD_VERSION = $(shell git describe --abbrev=0)
16+
GIT_VERSION = $(shell git describe --dirty --tags --always)
1717
GIT_COMMIT = $(shell git rev-parse HEAD)
1818
GO_BUILD_ARGS = \
1919
-gcflags "all=-trimpath=$(shell dirname $(shell pwd))" \
2020
-asmflags "all=-trimpath=$(shell dirname $(shell pwd))" \
2121
-ldflags " \
22-
-X '$(REPO)/internal/version.Version=$(VERSION)' \
23-
-X '$(REPO)/internal/version.GitCommit=$(GIT_COMMIT)' \
22+
-X '$(VERSION_PKG).ScaffoldVersion=$(SCAFFOLD_VERSION)' \
23+
-X '$(VERSION_PKG).GitVersion=$(GIT_VERSION)' \
24+
-X '$(VERSION_PKG).GitCommit=$(GIT_COMMIT)' \
2425
" \
2526

2627
#all: manager

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/joelanford/helm-operator
33
go 1.15
44

55
require (
6+
github.com/blang/semver/v4 v4.0.0
67
github.com/go-logr/logr v0.3.0
78
github.com/iancoleman/strcase v0.1.2
89
github.com/kr/text v0.1.0

go.sum

Lines changed: 4 additions & 26 deletions
Large diffs are not rendered by default.

internal/cmd/run/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func printVersion() {
9090
"Go Version", runtime.Version(),
9191
"GOOS", runtime.GOOS,
9292
"GOARCH", runtime.GOARCH,
93-
"helm-operator", version.Version)
93+
"helm-operator", version.GitVersion)
9494
}
9595

9696
func (r *run) run(cmd *cobra.Command) {

internal/cmd/version/cmd.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

internal/version/version.go

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,103 @@ limitations under the License.
1616

1717
package version
1818

19+
import (
20+
"fmt"
21+
"runtime/debug"
22+
"strings"
23+
24+
"github.com/blang/semver/v4"
25+
)
26+
27+
const (
28+
Unknown = "unknown"
29+
modulePath = "github.com/joelanford/helm-operator"
30+
)
31+
1932
var (
20-
//TODO: Hardcode it to version string to support importing and usage in operator-sdk
21-
Version = "unknown"
22-
GitCommit = "unknown"
33+
GitVersion = Unknown
34+
GitCommit = Unknown
35+
ScaffoldVersion = Unknown
2336
)
37+
38+
func init() {
39+
// If the ScaffoldVersion was not set during the
40+
// build (e.g. if this module was imported by
41+
// another), try to deduce the scaffold version
42+
// from the binary's embedded build info.
43+
if ScaffoldVersion == Unknown {
44+
ScaffoldVersion = getScaffoldVersion()
45+
}
46+
}
47+
48+
// getScaffoldVersion parses build info embedded in
49+
// the binary to deduce a tag version that is
50+
// appropriate to use when scaffolding files that
51+
// need to reference the most recent release from
52+
// this repository.
53+
//
54+
// This allows other projects to import and use the
55+
// helm plugin and have versions populated correctly
56+
// in the scaffolded Makefile, Dockerfile, etc.
57+
func getScaffoldVersion() string {
58+
info, ok := debug.ReadBuildInfo()
59+
if ok {
60+
// Search the dependencies of the main module and
61+
// if we find this module, return its most recent
62+
// tag.
63+
for _, m := range info.Deps {
64+
if m == nil {
65+
continue
66+
}
67+
if m.Path == modulePath {
68+
return getMostRecentTag(*m)
69+
}
70+
}
71+
}
72+
73+
// If there isn't build info or we couldn't
74+
// find our module in the main module's deps,
75+
// return Unknown.
76+
return Unknown
77+
}
78+
79+
// getMostRecentTag translates m into a version string
80+
// that corresponds to the tag at (or that precedes) the
81+
// commit referenced by the m's version, accounting
82+
// for any replacements.
83+
//
84+
// If it can't deduce a tag, it returns "unknown".
85+
func getMostRecentTag(m debug.Module) string {
86+
// Unwind all of the replacements.
87+
for m.Replace != nil {
88+
m = *m.Replace
89+
}
90+
91+
// We need to handle the possibility of a pseudo-version.
92+
// See: https://golang.org/cmd/go/#hdr-Pseudo_versions
93+
//
94+
// We'll get the first segment and attempt to parse it as
95+
// semver.
96+
split := strings.Split(m.Version, "-")
97+
sv, err := semver.Parse(strings.TrimPrefix(split[0], "v"))
98+
99+
// If the first segment was not a valid semver string,
100+
// return Unknown.
101+
if err != nil {
102+
return Unknown
103+
}
104+
105+
// If there were multiple segments, m.Version is
106+
// a pseudo-version, in which case Go will have
107+
// incremented the patch version. If the patch
108+
// version is greater than zero (it should always
109+
// be), we'll decrement it to get back to the
110+
// previous tag.
111+
//
112+
// This is necessary to handle projects that
113+
// import this project at untagged commits.
114+
if len(split) > 1 && sv.Patch > 0 {
115+
sv.Patch -= 1
116+
}
117+
return fmt.Sprintf("v%s", sv.FinalizeVersion())
118+
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ func main() {
5555

5656
func getVersion() string {
5757
return fmt.Sprintf("helm-operator version: %q, commit: %q, go version: %q, GOOS: %q, GOARCH: %q\n",
58-
version.Version, version.GitCommit, runtime.Version(), runtime.GOOS, runtime.GOARCH)
58+
version.GitVersion, version.GitCommit, runtime.Version(), runtime.GOOS, runtime.GOARCH)
5959

6060
}

pkg/plugins/v1/scaffolds/init.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package scaffolds
1919

2020
import (
2121
"os"
22-
"strings"
2322

2423
"sigs.k8s.io/kubebuilder/v2/pkg/model"
2524
"sigs.k8s.io/kubebuilder/v2/pkg/model/config"
@@ -43,8 +42,7 @@ const (
4342
)
4443

4544
// helmOperatorVersion is set to the version of helm-operator at compile-time.
46-
var helmOperatorVersion = strings.TrimSuffix(version.Version, "+git")
47-
45+
var helmOperatorVersion = mustGetScaffoldVersion()
4846
var _ cmdutil.Scaffolder = &initScaffolder{}
4947

5048
type initScaffolder struct {
@@ -110,3 +108,10 @@ func (s *initScaffolder) scaffold() error {
110108
&kdefault.Kustomization{},
111109
)
112110
}
111+
112+
func mustGetScaffoldVersion() string {
113+
if version.ScaffoldVersion == "" || version.ScaffoldVersion == version.Unknown {
114+
panic("helm-operator scaffold version is unknown; it must be set during build or by importing this plugin via go modules")
115+
}
116+
return version.ScaffoldVersion
117+
}

0 commit comments

Comments
 (0)