Skip to content

Commit

Permalink
Improve version handling for main module and when this project is imp…
Browse files Browse the repository at this point in the history
…orted (#69)
  • Loading branch information
joelanford committed Jan 15, 2021
1 parent 6263974 commit 06e5a4f
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 81 deletions.
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ GOBIN=$(shell go env GOBIN)
endif

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

#all: manager
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/joelanford/helm-operator
go 1.15

require (
github.com/blang/semver/v4 v4.0.0
github.com/go-logr/logr v0.3.0
github.com/iancoleman/strcase v0.1.2
github.com/kr/text v0.1.0
Expand Down
30 changes: 4 additions & 26 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/cmd/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func printVersion() {
"Go Version", runtime.Version(),
"GOOS", runtime.GOOS,
"GOARCH", runtime.GOARCH,
"helm-operator", version.Version)
"helm-operator", version.GitVersion)
}

func (r *run) run(cmd *cobra.Command) {
Expand Down
42 changes: 0 additions & 42 deletions internal/cmd/version/cmd.go

This file was deleted.

101 changes: 98 additions & 3 deletions internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,103 @@ limitations under the License.

package version

import (
"fmt"
"runtime/debug"
"strings"

"github.com/blang/semver/v4"
)

const (
Unknown = "unknown"
modulePath = "github.com/joelanford/helm-operator"
)

var (
//TODO: Hardcode it to version string to support importing and usage in operator-sdk
Version = "unknown"
GitCommit = "unknown"
GitVersion = Unknown
GitCommit = Unknown
ScaffoldVersion = Unknown
)

func init() {
// If the ScaffoldVersion was not set during the
// build (e.g. if this module was imported by
// another), try to deduce the scaffold version
// from the binary's embedded build info.
if ScaffoldVersion == Unknown {
ScaffoldVersion = getScaffoldVersion()
}
}

// getScaffoldVersion parses build info embedded in
// the binary to deduce a tag version that is
// appropriate to use when scaffolding files that
// need to reference the most recent release from
// this repository.
//
// This allows other projects to import and use the
// helm plugin and have versions populated correctly
// in the scaffolded Makefile, Dockerfile, etc.
func getScaffoldVersion() string {
info, ok := debug.ReadBuildInfo()
if ok {
// Search the dependencies of the main module and
// if we find this module, return its most recent
// tag.
for _, m := range info.Deps {
if m == nil {
continue
}
if m.Path == modulePath {
return getMostRecentTag(*m)
}
}
}

// If there isn't build info or we couldn't
// find our module in the main module's deps,
// return Unknown.
return Unknown
}

// getMostRecentTag translates m into a version string
// that corresponds to the tag at (or that precedes) the
// commit referenced by the m's version, accounting
// for any replacements.
//
// If it can't deduce a tag, it returns "unknown".
func getMostRecentTag(m debug.Module) string {
// Unwind all of the replacements.
for m.Replace != nil {
m = *m.Replace
}

// We need to handle the possibility of a pseudo-version.
// See: https://golang.org/cmd/go/#hdr-Pseudo_versions
//
// We'll get the first segment and attempt to parse it as
// semver.
split := strings.Split(m.Version, "-")
sv, err := semver.Parse(strings.TrimPrefix(split[0], "v"))

// If the first segment was not a valid semver string,
// return Unknown.
if err != nil {
return Unknown
}

// If there were multiple segments, m.Version is
// a pseudo-version, in which case Go will have
// incremented the patch version. If the patch
// version is greater than zero (it should always
// be), we'll decrement it to get back to the
// previous tag.
//
// This is necessary to handle projects that
// import this project at untagged commits.
if len(split) > 1 && sv.Patch > 0 {
sv.Patch -= 1
}
return fmt.Sprintf("v%s", sv.FinalizeVersion())
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ func main() {

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

}
11 changes: 8 additions & 3 deletions pkg/plugins/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package scaffolds

import (
"os"
"strings"

"sigs.k8s.io/kubebuilder/v2/pkg/model"
"sigs.k8s.io/kubebuilder/v2/pkg/model/config"
Expand All @@ -43,8 +42,7 @@ const (
)

// helmOperatorVersion is set to the version of helm-operator at compile-time.
var helmOperatorVersion = strings.TrimSuffix(version.Version, "+git")

var helmOperatorVersion = mustGetScaffoldVersion()
var _ cmdutil.Scaffolder = &initScaffolder{}

type initScaffolder struct {
Expand Down Expand Up @@ -110,3 +108,10 @@ func (s *initScaffolder) scaffold() error {
&kdefault.Kustomization{},
)
}

func mustGetScaffoldVersion() string {
if version.ScaffoldVersion == "" || version.ScaffoldVersion == version.Unknown {
panic("helm-operator scaffold version is unknown; it must be set during build or by importing this plugin via go modules")
}
return version.ScaffoldVersion
}

0 comments on commit 06e5a4f

Please sign in to comment.