Skip to content

Commit

Permalink
Replace version/version.go with internal/version/version.go
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
antoninbas committed Dec 4, 2023
1 parent 354e869 commit 435bb91
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 47 deletions.
18 changes: 8 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SHELL := /bin/bash
LDFLAGS :=

# Options for 'bundle-build'
ifneq ($(origin CHANNELS), undefined)
Expand All @@ -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))
Expand All @@ -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 <==="
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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/[email protected] ;\
rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\
go install sigs.k8s.io/kustomize/kustomize/[email protected] ;\
}
KUSTOMIZE=$(GOBIN)/kustomize
else
Expand Down
6 changes: 3 additions & 3 deletions antrea-manifest/antrea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 6 additions & 8 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +21,8 @@ LABEL name="antrea-operator-for-kubernetes"
LABEL maintainer="Antrea <[email protected]>"
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"

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion build/yamls/configMap/antrea-agent.conf
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{.AntreaAgentConfig | indent 4}}
{{- .AntreaAgentConfig | nindent 4 }}
2 changes: 1 addition & 1 deletion build/yamls/configMap/antrea-cni.conflist
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{.AntreaCNIConfig | indent 4}}
{{- .AntreaCNIConfig | nindent 4 }}
2 changes: 1 addition & 1 deletion build/yamls/configMap/antrea-controller.conf
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{.AntreaControllerConfig | indent 4}}
{{- .AntreaControllerConfig | nindent 4 }}
4 changes: 2 additions & 2 deletions config/samples/operator_v1_antreainstall.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 changes: 2 additions & 2 deletions controllers/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions controllers/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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()))
}
}
}
Expand Down Expand Up @@ -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()))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/statusmanager/status_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion hack/generate-antrea-samples.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python3
#!/usr/bin/env python3

import argparse
import yaml
Expand Down
2 changes: 1 addition & 1 deletion hack/generate-role-yaml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python3
#!/usr/bin/env python3

import sys
import yaml
Expand Down
14 changes: 14 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"flag"
"fmt"
"os"

configv1 "github.com/openshift/api/config/v1"
Expand All @@ -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
)

Expand All @@ -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()
Expand All @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions version/version.go

This file was deleted.

2 changes: 2 additions & 0 deletions versioning.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down

0 comments on commit 435bb91

Please sign in to comment.