diff --git a/Makefile b/Makefile index bce02949..06da7c99 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,8 @@ IMAGE_TAGS ?= $(or $(IMG_TAG_FROM_REF),$(DEFAULT_IMAGE_TAGS)) IMG ?= $(KO_DOCKER_REPO):$(IMAGE_TAGS) RELEASE_DIR ?= release PLATFORM ?= linux/amd64 +VERSION ?= $(shell git describe --always --tag --dirty=-dev) +LDFLAGS := -X=github.com/linode/linode-cloud-controller-manager/version.version=$(VERSION) ##################################################################### # Dev Setup @@ -100,13 +102,13 @@ build-linux: codegen echo "cross compiling linode-cloud-controller-manager for linux/amd64" && \ GOOS=linux GOARCH=amd64 \ CGO_ENABLED=0 \ - go build -o dist/linode-cloud-controller-manager-linux-amd64 . + go build -ldflags="$(LDFLAGS)" -o dist/linode-cloud-controller-manager-linux-amd64 . .PHONY: build build: codegen echo "compiling linode-cloud-controller-manager" && \ CGO_ENABLED=0 \ - go build -o dist/linode-cloud-controller-manager . + go build -ldflags="$(LDFLAGS)" -o dist/linode-cloud-controller-manager . .PHONY: release release: @@ -123,12 +125,12 @@ imgname: .PHONY: ko-build # build the container image locally without pushing it to a registry ko-build: - CGO_ENABLED=0 ko build --local --bare --tags "$(IMAGE_TAGS)" --platform=$(PLATFORM) . + CGO_ENABLED=0 GOFLAGS="-ldflags=$(LDFLAGS)" ko build --local --bare --tags "$(IMAGE_TAGS)" --platform=$(PLATFORM) . .PHONY: ko-publish # build the container image and publish it to the registry named by IMG ko-publish: - CGO_ENABLED=0 KO_DOCKER_REPO="$(KO_DOCKER_REPO)" ko build --bare --tags "$(IMAGE_TAGS)" --platform=$(PLATFORM) . + CGO_ENABLED=0 KO_DOCKER_REPO="$(KO_DOCKER_REPO)" GOFLAGS="-ldflags=$(LDFLAGS)" ko build --bare --tags "$(IMAGE_TAGS)" --platform=$(PLATFORM) . .PHONY: run # run the ccm locally, really only makes sense on linux anyway diff --git a/cloud/linode/client/client.go b/cloud/linode/client/client.go index 64b52d19..ecfb1d94 100644 --- a/cloud/linode/client/client.go +++ b/cloud/linode/client/client.go @@ -14,6 +14,8 @@ import ( "github.com/linode/linodego/v2" "k8s.io/klog/v2" + "github.com/linode/linode-cloud-controller-manager/version" + _ "github.com/hexdigest/gowrap" ) @@ -98,7 +100,6 @@ func (t *tokenTransport) RoundTrip(req *http.Request) (*http.Response, error) { // New creates a new linode client with a given token and default timeout. func New(timeout time.Duration, tokenProvider TokenProvider) (*linodego.Client, error) { var client *linodego.Client - userAgent := fmt.Sprintf("linode-cloud-controller-manager %s", linodego.DefaultUserAgent) apiURL := os.Getenv("LINODE_URL") if apiURL == "" { apiURL = DefaultLinodeAPIURL @@ -117,7 +118,7 @@ func New(timeout time.Duration, tokenProvider TokenProvider) (*linodego.Client, if err != nil { return nil, err } - client.SetUserAgent(userAgent) + client.SetUserAgent(fmt.Sprintf("linode-CCM/%s", version.GetVersion())) klog.V(3).Infof("Linode client created with default timeout of %v", timeout) return client, nil diff --git a/version/version.go b/version/version.go new file mode 100644 index 00000000..314caf78 --- /dev/null +++ b/version/version.go @@ -0,0 +1,12 @@ +package version + +// version is overridden by build time flags +var version string + +func GetVersion() string { + if version == "" { + return "dev" + } + + return version +} diff --git a/version/version_test.go b/version/version_test.go new file mode 100644 index 00000000..28581d2c --- /dev/null +++ b/version/version_test.go @@ -0,0 +1,11 @@ +package version + +import "testing" + +func TestVersion(t *testing.T) { + t.Parallel() + vers := GetVersion() + if vers != "dev" { + t.Errorf("unset version should be dev, got %s", vers) + } +}