Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update versions information in HostAgent binary #454

Merged
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: turn off swap
run: sudo swapoff -a
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/draft-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
go-version: 1.16

- name: Build Release Artifacts
run: IMG="projects.registry.vmware.com/cluster_api_provider_bringyourownhost/cluster-api-byoh-controller:${{ github.ref_name }}" AGENT_BINARY_VERSION="${{ github.ref_name }}" make build-release-artifacts
run: IMG="projects.registry.vmware.com/cluster_api_provider_bringyourownhost/cluster-api-byoh-controller:${{ github.ref_name }}" make build-release-artifacts

- name: Publish Release
uses: softprops/action-gh-release@v1
Expand Down
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ GINKGO := $(TOOLS_BIN_DIR)/ginkgo

BYOH_TEMPLATES := $(REPO_ROOT)/test/e2e/data/infrastructure-provider-byoh

LDFLAGS=-w -s
LDFLAGS := -w -s $(shell hack/version.sh)
STATIC=-extldflags '-static'
AGENT_BINARY_VERSION ?= dev

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -201,9 +200,7 @@ kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])

host-agent-binaries: ## Builds the binaries for the host-agent
RELEASE_BINARY=./byoh-hostagent GOOS=linux GOARCH=amd64 GOLDFLAGS="$(LDFLAGS) $(STATIC) \
-X 'github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/version.Version=${AGENT_BINARY_VERSION}' \
-X 'github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/version.BuildDate=$(shell date -u +\"%Y-%m-%dT%H:%M:%SZ\")'" \
RELEASE_BINARY=./byoh-hostagent GOOS=linux GOARCH=amd64 GOLDFLAGS="$(LDFLAGS) $(STATIC)" \
HOST_AGENT_DIR=./$(HOST_AGENT_DIR) $(MAKE) host-agent-binary

host-agent-binary: $(RELEASE_DIR)
Expand Down
91 changes: 80 additions & 11 deletions agent/host_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,29 +337,47 @@ var _ = Describe("Agent", func() {
BeforeEach(func() {
date, err := exec.Command("date").Output()
Expect(err).NotTo(HaveOccurred())

version.GitMajor = "1"
version.GitMinor = "2"
version.GitVersion = "v1.2.3"
version.GitCommit = "abc"
version.GitTreeState = "clean"
version.BuildDate = string(date)
version.Version = "v1.2.3"
ldflags := fmt.Sprintf("-X 'github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/version.Version=%s'"+
" -X 'github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/version.BuildDate=%s'", version.Version, version.BuildDate)

ldflags := fmt.Sprintf("-X 'github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/version.GitMajor=%s'"+
"-X 'github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/version.GitMinor=%s'"+
"-X 'github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/version.GitVersion=%s'"+
"-X 'github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/version.GitCommit=%s'"+
"-X 'github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/version.GitTreeState=%s'"+
"-X 'github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/version.BuildDate=%s'",
version.GitMajor, version.GitMinor, version.GitVersion, version.GitCommit, version.GitTreeState, version.BuildDate)

tmpHostAgentBinary, err = gexec.Build("github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent", "-ldflags", ldflags)
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
version.GitMajor = ""
version.GitMinor = ""
version.GitVersion = ""
version.GitCommit = ""
version.GitTreeState = ""
version.BuildDate = ""
version.Version = ""
tmpHostAgentBinary = ""
})

It("Shows the appropriate version of the agent", func() {
expectedStruct := version.Info{
Major: "1",
Minor: "2",
Patch: "3",
BuildDate: version.BuildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
Major: "1",
Minor: "2",
GitVersion: "v1.2.3",
GitCommit: "abc",
GitTreeState: "clean",
BuildDate: version.BuildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
expected := fmt.Sprintf("byoh-hostagent version: %#v\n", expectedStruct)
out, err := exec.Command(tmpHostAgentBinary, "--version").Output()
Expand All @@ -369,6 +387,57 @@ var _ = Describe("Agent", func() {
})
})

Context("When --version flag is created using 'version.sh' script", func() {
var (
tmpHostAgentBinary string
gitMajor string
gitMinor string
gitVersion string
err error
)
BeforeEach(func() {
command := exec.Command("/bin/sh", "-c", "git describe --tags --abbrev=14 --match 'v[0-9]*' 2>/dev/null")
command.Stderr = os.Stderr
cmdOut, _ := command.Output()
gitVersion = strings.TrimSuffix(string(cmdOut), "\n")

gitVersion = strings.Split(gitVersion, "-")[0]
gitVars := strings.Split(gitVersion, ".")
if len(gitVars) > 1 {
gitMajor = gitVars[0][1:]
gitMinor = gitVars[1]
}

root, _ := exec.Command("/bin/sh", "-c", "git rev-parse --show-toplevel").Output()
cmd := exec.Command("/bin/sh", "-c", strings.TrimSuffix(string(root), "\n")+"/hack/version.sh")
ldflags, _ := cmd.Output()
tmpHostAgentBinary, err = gexec.Build("github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent", "-ldflags", string(ldflags))
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
tmpHostAgentBinary = ""
gitMajor = ""
gitMinor = ""
gitVersion = ""
})

It("should match local generated git values", func() {
out, err := exec.Command(tmpHostAgentBinary, "--version").Output()
Expect(err).NotTo(HaveOccurred())

majorExpected := "Major:\"" + gitMajor + "\""
Expect(out).Should(ContainSubstring(majorExpected))

minorExpected := "Minor:\"" + gitMinor + "\""
Expect(out).Should(ContainSubstring(minorExpected))

gitVersionExpected := "GitVersion:\"" + gitVersion
Expect(out).Should(ContainSubstring(gitVersionExpected))

})
})

Context("When the host agent is executed with --skip-installation flag", func() {
var (
ns *corev1.Namespace
Expand Down
72 changes: 24 additions & 48 deletions agent/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,41 @@ package version
import (
"fmt"
"runtime"
"strings"
)

var (
// Version is the version of the agent.
Version string
// BuildDate is the date the agent was built.
BuildDate string
)

const (
// Dev development version string
Dev = "dev"
gitTagLength = 3
GitMajor string // major version, always numeric
GitMinor string // minor version, numeric possibly followed by "+"
GitVersion string // semantic version, derived by build scripts
GitCommit string // sha1 from git, output of $(git rev-parse HEAD)
GitTreeState string // state of git tree, either "clean" or "dirty"
BuildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)

// Info exposes information about the version used for the current running code.
type Info struct {
Major string `json:"major,omitempty"`
Minor string `json:"minor,omitempty"`
Patch string `json:"patch,omitempty"`
BuildDate string `json:"BuildDate,omitempty"`
GoVersion string `json:"goVersion,omitempty"`
Platform string `json:"platform,omitempty"`
Compiler string `json:"compiler,omitempty"`
Major string `json:"major,omitempty"`
Minor string `json:"minor,omitempty"`
GitVersion string `json:"gitVersion,omitempty"`
GitCommit string `json:"gitCommit,omitempty"`
GitTreeState string `json:"gitTreeState,omitempty"`
BuildDate string `json:"buildDate,omitempty"`
GoVersion string `json:"goVersion,omitempty"`
Compiler string `json:"compiler,omitempty"`
Platform string `json:"platform,omitempty"`
}

// Get returns an Info object with all the information about the current running code.
func Get() Info {
var major, minor, patch string
extractVersion(&major, &minor, &patch)
return Info{
Major: major,
Minor: minor,
Patch: patch,
BuildDate: BuildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

func extractVersion(major, minor, patch *string) {
if Version == Dev {
*major = Dev
return
Major: GitMajor,
Minor: GitMinor,
GitVersion: GitVersion,
GitCommit: GitCommit,
GitTreeState: GitTreeState,
BuildDate: BuildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}

version := strings.Split(Version, ".")
if len(version) != gitTagLength {
return
}

// The git tag is preceded by a 'v', eg. v1.2.3
if len(version[0]) != 2 || version[0][0:1] != "v" {
return
}

*major = version[0][1:2]
*minor = version[1]
*patch = version[2]
}
16 changes: 0 additions & 16 deletions agent/version/version_suite_test.go

This file was deleted.

130 changes: 0 additions & 130 deletions agent/version/version_test.go

This file was deleted.

Loading