From 435bb91f00370aa23a9d936622f6667e116441fd Mon Sep 17 00:00:00 2001 From: Antonin Bas Date: Mon, 4 Dec 2023 10:54:46 -0800 Subject: [PATCH] Replace version/version.go with internal/version/version.go On case-insensitive filesystems (which includes default macOS installations), it is not possible to have a "version" directory and a "VERSION" file in the same parent directory, and the repo cannot be cloned succesfully. To resolve this, we replace version/version.go with internal/version/version.go. Additionally, we stop using "0.0.1" as the version string, as it is not consistent with the contents of the VERSION file, nor with the Docker tag. Instead, we use the VERSION file as the one source of truth. We also add a "--version" command-line flag to the operator binary, which displays the version and exits. Signed-off-by: Antonin Bas --- Makefile | 18 ++++++++---------- antrea-manifest/antrea.yml | 6 +++--- build/Dockerfile | 14 ++++++-------- build/yamls/configMap/antrea-agent.conf | 2 +- build/yamls/configMap/antrea-cni.conflist | 2 +- build/yamls/configMap/antrea-controller.conf | 2 +- config/samples/operator_v1_antreainstall.yaml | 4 ++-- controllers/config/config.go | 4 ++-- controllers/config/config_test.go | 10 +++++----- controllers/statusmanager/status_manager.go | 4 ++-- hack/generate-antrea-samples.py | 2 +- hack/generate-role-yaml.py | 2 +- internal/version/version.go | 14 ++++++++++++++ main.go | 12 ++++++++++-- version/version.go | 8 -------- versioning.mk | 2 ++ 16 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 internal/version/version.go delete mode 100644 version/version.go diff --git a/Makefile b/Makefile index 04ef49e9..019619ce 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ SHELL := /bin/bash +LDFLAGS := # Options for 'bundle-build' ifneq ($(origin CHANNELS), undefined) @@ -21,6 +22,7 @@ ifndef IS_CERTIFICATION endif include versioning.mk +LDFLAGS += $(VERSION_LDFLAGS) # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) ifeq (,$(shell go env GOBIN)) @@ -41,7 +43,7 @@ PKG_IS_DEFAULT_CHANNEL := --default-channel endif PKG_MAN_OPTS ?= $(FROM_VERSION) $(PKG_CHANNELS) $(PKG_IS_DEFAULT_CHANNEL) -all: manager +all: generate golangci manager .golangci-bin: @echo "===> Installing Golangci-lint <===" @@ -65,13 +67,13 @@ test: generate golangci manifests source $(ENVTEST_ASSETS_DIR)/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out # Build manager binary -manager: generate golangci +manager: @echo "===> Building antrea-operator binary <===" - go build -o bin/manager main.go + go build -o bin/manager -ldflags '$(LDFLAGS)' main.go # Run against the configured Kubernetes cluster in ~/.kube/config run: generate golangci manifests - go run ./main.go + go run -ldflags '$(LDFLAGS)' ./main.go # Install CRDs into a cluster install: manifests kustomize @@ -97,7 +99,7 @@ generate: controller-gen # Build the docker image docker-build: - docker build -f build/Dockerfile . -t ${IMG} + docker build -f build/Dockerfile --label version="$(VERSION)" . -t ${IMG} docker tag ${IMG} antrea/antrea-operator # find or download controller-gen @@ -117,11 +119,7 @@ kustomize: ifeq (, $(shell which kustomize)) @{ \ set -e ;\ - KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$KUSTOMIZE_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/kustomize/kustomize/v3@v3.5.4 ;\ - rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\ + go install sigs.k8s.io/kustomize/kustomize/v5@v5.2.1 ;\ } KUSTOMIZE=$(GOBIN)/kustomize else diff --git a/antrea-manifest/antrea.yml b/antrea-manifest/antrea.yml index ddc4cdf1..8ae6fcc9 100644 --- a/antrea-manifest/antrea.yml +++ b/antrea-manifest/antrea.yml @@ -6413,11 +6413,11 @@ subjects: apiVersion: v1 data: antrea-agent.conf: | -{{.AntreaAgentConfig | indent 4}} + {{- .AntreaAgentConfig | nindent 4 }} antrea-cni.conflist: | -{{.AntreaCNIConfig | indent 4}} + {{- .AntreaCNIConfig | nindent 4 }} antrea-controller.conf: | -{{.AntreaControllerConfig | indent 4}} + {{- .AntreaControllerConfig | nindent 4 }} kind: ConfigMap metadata: labels: diff --git a/build/Dockerfile b/build/Dockerfile index e2e2abb4..a3b69d0c 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -8,14 +8,11 @@ COPY go.sum go.sum # and so that source changes don't invalidate our downloaded layer RUN go mod download -# Copy the go source -COPY main.go main.go -COPY api/ api/ -COPY controllers/ controllers/ -COPY version/ version/ +# Copy source files +COPY . . # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on make manager FROM registry.access.redhat.com/ubi8/ubi-minimal:latest @@ -24,7 +21,8 @@ LABEL name="antrea-operator-for-kubernetes" LABEL maintainer="Antrea " LABEL vendor="Project Antrea" LABEL summary="A cluster operator to deploy Antrea CNI plugin" -LABEL version="0.0.1" +# set at build time +LABEL version="" LABEL release="1" LABEL description="Manage deployments, daemonsets, and config maps for Antrea" @@ -37,7 +35,7 @@ ENV OPERATOR=/usr/local/bin/antrea-operator \ COPY build/bin /usr/local/bin # install operator binary -COPY --from=antrea-operator-build /workspace/manager ${OPERATOR} +COPY --from=antrea-operator-build /workspace/bin/manager ${OPERATOR} COPY antrea-manifest /antrea-manifest RUN /usr/local/bin/user_setup diff --git a/build/yamls/configMap/antrea-agent.conf b/build/yamls/configMap/antrea-agent.conf index 555a2d05..b60c9e88 100644 --- a/build/yamls/configMap/antrea-agent.conf +++ b/build/yamls/configMap/antrea-agent.conf @@ -1 +1 @@ -{{.AntreaAgentConfig | indent 4}} +{{- .AntreaAgentConfig | nindent 4 }} diff --git a/build/yamls/configMap/antrea-cni.conflist b/build/yamls/configMap/antrea-cni.conflist index 1cf660bc..1a43f706 100644 --- a/build/yamls/configMap/antrea-cni.conflist +++ b/build/yamls/configMap/antrea-cni.conflist @@ -1 +1 @@ -{{.AntreaCNIConfig | indent 4}} +{{- .AntreaCNIConfig | nindent 4 }} diff --git a/build/yamls/configMap/antrea-controller.conf b/build/yamls/configMap/antrea-controller.conf index 792b5b78..b9ec8873 100644 --- a/build/yamls/configMap/antrea-controller.conf +++ b/build/yamls/configMap/antrea-controller.conf @@ -1 +1 @@ -{{.AntreaControllerConfig | indent 4}} +{{- .AntreaControllerConfig | nindent 4 }} diff --git a/config/samples/operator_v1_antreainstall.yaml b/config/samples/operator_v1_antreainstall.yaml index f51df8e0..9be225fc 100644 --- a/config/samples/operator_v1_antreainstall.yaml +++ b/config/samples/operator_v1_antreainstall.yaml @@ -543,6 +543,6 @@ spec: multicluster: # Enable Multi-cluster NetworkPolicy. enableStretchedNetworkPolicy: false - antreaImage: antrea/antrea-ubi:v1.14.0 - antreaPlatform: openshift + antreaImage: antrea/antrea-ubuntu:v1.14.0 + antreaPlatform: kubernetes diff --git a/controllers/config/config.go b/controllers/config/config.go index 33f9d2cd..bd163317 100644 --- a/controllers/config/config.go +++ b/controllers/config/config.go @@ -21,7 +21,7 @@ import ( operatorv1 "github.com/vmware/antrea-operator-for-kubernetes/api/v1" "github.com/vmware/antrea-operator-for-kubernetes/controllers/types" - "github.com/vmware/antrea-operator-for-kubernetes/version" + "github.com/vmware/antrea-operator-for-kubernetes/internal/version" ) var log = ctrl.Log.WithName("config") @@ -322,7 +322,7 @@ func pluginCNIConfDir(conf *ocoperv1.NetworkSpec) string { func generateRenderData(operatorNetwork *ocoperv1.Network, operConfig *operatorv1.AntreaInstall) *render.RenderData { renderData := render.MakeRenderData() - renderData.Data[types.ReleaseVersion] = version.Version + renderData.Data[types.ReleaseVersion] = version.GetVersion() renderData.Data[types.AntreaAgentConfigRenderKey] = operConfig.Spec.AntreaAgentConfig renderData.Data[types.AntreaCNIConfigRenderKey] = operConfig.Spec.AntreaCNIConfig renderData.Data[types.AntreaControllerConfigRenderKey] = operConfig.Spec.AntreaControllerConfig diff --git a/controllers/config/config_test.go b/controllers/config/config_test.go index 58758390..6bb30ade 100644 --- a/controllers/config/config_test.go +++ b/controllers/config/config_test.go @@ -19,7 +19,7 @@ import ( operatorv1 "github.com/vmware/antrea-operator-for-kubernetes/api/v1" operatortypes "github.com/vmware/antrea-operator-for-kubernetes/controllers/types" - "github.com/vmware/antrea-operator-for-kubernetes/version" + "github.com/vmware/antrea-operator-for-kubernetes/internal/version" ) var mockClusterConfig = configv1.Network{ @@ -179,12 +179,12 @@ func TestRenderOc(t *testing.T) { err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), antreaDeployment) g.Expect(err).ShouldNot(HaveOccurred()) g.Expect(antreaDeployment.Spec.Template.Spec.Containers[0].Image).Should(Equal(operatortypes.DefaultAntreaImage)) - g.Expect(antreaDeployment.Annotations["release.openshift.io/version"]).Should(Equal(version.Version)) + g.Expect(antreaDeployment.Annotations["release.openshift.io/version"]).Should(Equal(version.GetVersion())) } else if obj.GetKind() == "DaemonSet" && obj.GetNamespace() == "kube-system" && obj.GetName() == "antrea-agent" { antreaDaemonSet := &appsv1.DaemonSet{} err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), antreaDaemonSet) g.Expect(err).ShouldNot(HaveOccurred()) - g.Expect(antreaDaemonSet.Annotations["release.openshift.io/version"]).Should(Equal(version.Version)) + g.Expect(antreaDaemonSet.Annotations["release.openshift.io/version"]).Should(Equal(version.GetVersion())) } } } @@ -212,12 +212,12 @@ func TestRenderK8s(t *testing.T) { err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), antreaDeployment) g.Expect(err).ShouldNot(HaveOccurred()) g.Expect(antreaDeployment.Spec.Template.Spec.Containers[0].Image).Should(Equal(operatortypes.DefaultAntreaImage)) - g.Expect(antreaDeployment.Annotations["release.openshift.io/version"]).Should(Equal(version.Version)) + g.Expect(antreaDeployment.Annotations["release.openshift.io/version"]).Should(Equal(version.GetVersion())) } else if obj.GetKind() == "DaemonSet" && obj.GetNamespace() == "kube-system" && obj.GetName() == "antrea-agent" { antreaDaemonSet := &appsv1.DaemonSet{} err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), antreaDaemonSet) g.Expect(err).ShouldNot(HaveOccurred()) - g.Expect(antreaDaemonSet.Annotations["release.openshift.io/version"]).Should(Equal(version.Version)) + g.Expect(antreaDaemonSet.Annotations["release.openshift.io/version"]).Should(Equal(version.GetVersion())) } } } diff --git a/controllers/statusmanager/status_manager.go b/controllers/statusmanager/status_manager.go index 94a439ae..8c39fc16 100644 --- a/controllers/statusmanager/status_manager.go +++ b/controllers/statusmanager/status_manager.go @@ -27,7 +27,7 @@ import ( operatorv1 "github.com/vmware/antrea-operator-for-kubernetes/api/v1" "github.com/vmware/antrea-operator-for-kubernetes/controllers/sharedinfo" operatortypes "github.com/vmware/antrea-operator-for-kubernetes/controllers/types" - "github.com/vmware/antrea-operator-for-kubernetes/version" + "github.com/vmware/antrea-operator-for-kubernetes/internal/version" ) var log = logf.Log.WithName("status_manager") @@ -177,7 +177,7 @@ func (status *StatusManager) setConditions(progressing []string, reachedAvailabl func (status *StatusManager) setClusterOperatorConditions(co *configv1.ClusterOperator, reachedAvailableLevel bool, conditions *[]configv1.ClusterOperatorStatusCondition) { if reachedAvailableLevel { co.Status.Versions = []configv1.OperandVersion{ - {Name: "operator", Version: version.Version}, + {Name: "operator", Version: version.GetVersion()}, } } status.CombineConditions(&co.Status.Conditions, conditions) diff --git a/hack/generate-antrea-samples.py b/hack/generate-antrea-samples.py index 139f48f2..17c0c514 100755 --- a/hack/generate-antrea-samples.py +++ b/hack/generate-antrea-samples.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 import argparse import yaml diff --git a/hack/generate-role-yaml.py b/hack/generate-role-yaml.py index e5c50e3a..4d57a28e 100755 --- a/hack/generate-role-yaml.py +++ b/hack/generate-role-yaml.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 import sys import yaml diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 00000000..ed84d0a4 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,14 @@ +/* Copyright © 2020 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 */ + +package version + +// Version is set at build-time. +var Version string + +func GetVersion() string { + if Version == "" { + return "UNKNOWN" + } + return Version +} diff --git a/main.go b/main.go index 5d110208..37676eea 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ package main import ( "flag" + "fmt" "os" configv1 "github.com/openshift/api/config/v1" @@ -22,7 +23,7 @@ import ( "github.com/vmware/antrea-operator-for-kubernetes/controllers/sharedinfo" "github.com/vmware/antrea-operator-for-kubernetes/controllers/statusmanager" "github.com/vmware/antrea-operator-for-kubernetes/controllers/types" - "github.com/vmware/antrea-operator-for-kubernetes/version" + "github.com/vmware/antrea-operator-for-kubernetes/internal/version" // +kubebuilder:scaffold:imports ) @@ -41,14 +42,21 @@ func init() { } func main() { + var printVersion bool var metricsAddr string var enableLeaderElection bool + flag.BoolVar(&printVersion, "version", false, "Show version and exit") flag.StringVar(&metricsAddr, "metrics-addr", "0", "The address the metric endpoint binds to.") flag.BoolVar(&enableLeaderElection, "enable-leader-election", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") flag.Parse() + if printVersion { + fmt.Println(version.GetVersion()) + os.Exit(0) + } + ctrl.SetLogger(zap.New(zap.UseDevMode(true))) cfg := ctrl.GetConfigOrDie() @@ -68,7 +76,7 @@ func main() { setupLog.Error(err, "unable to get shareinfo") os.Exit(1) } - statusManager, err := statusmanager.New(mgr.GetClient(), mgr.GetRESTMapper(), types.AntreaClusterOperatorName, types.OperatorNameSpace, version.Version, sharedInfo) + statusManager, err := statusmanager.New(mgr.GetClient(), mgr.GetRESTMapper(), types.AntreaClusterOperatorName, types.OperatorNameSpace, version.GetVersion(), sharedInfo) if err != nil { setupLog.Error(err, "unable to get status manager") os.Exit(1) diff --git a/version/version.go b/version/version.go deleted file mode 100644 index d39c1468..00000000 --- a/version/version.go +++ /dev/null @@ -1,8 +0,0 @@ -/* Copyright © 2020 VMware, Inc. All Rights Reserved. - SPDX-License-Identifier: Apache-2.0 */ - -package version - -var ( - Version = "0.0.1" -) diff --git a/versioning.mk b/versioning.mk index 7c2c1594..a0381b8e 100644 --- a/versioning.mk +++ b/versioning.mk @@ -45,6 +45,8 @@ endif # Image URL to use all building/pushing image targets IMG ?= antrea/antrea-operator:$(OPERATOR_IMG) +VERSION_LDFLAGS = -X github.com/vmware/antrea-operator-for-kubernetes/internal/version.Version=$(VERSION) + version-info: @echo "===> Version information <===" @echo "VERSION: $(VERSION)"