diff --git a/cmd/main.go b/cmd/main.go index f7821a9..ac4d60a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -27,6 +27,7 @@ import ( "github.com/mercedes-benz/garm-operator/pkg/client" "github.com/mercedes-benz/garm-operator/pkg/config" "github.com/mercedes-benz/garm-operator/pkg/flags" + "github.com/mercedes-benz/garm-operator/pkg/version" ) var ( @@ -129,6 +130,20 @@ func run() error { return fmt.Errorf("unable to setup garm: %w", err) } + // create a controller client to get controller info + // for the version check + controllerClient := client.NewControllerClient() + controllerInfo, err := controllerClient.GetControllerInfo() + if err != nil { + return fmt.Errorf("unable to get controller info: %w", err) + } + + // if the version of the controller is not compatible with the Garm version + // return an error and stop the operator + if !version.EnsureMinimalVersion(controllerInfo.Payload.Version) { + return fmt.Errorf("garm-operator is not compatible with Garm version %s. Minimal required version is %s", controllerInfo.Payload.Version, version.MinVersion) + } + if err = (&garmcontroller.EnterpriseReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), @@ -273,5 +288,6 @@ func run() error { if err := mgr.Start(ctx); err != nil { return fmt.Errorf("unable to start manager: %w", err) } + return nil } diff --git a/go.mod b/go.mod index e4dbf02..9e9b474 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.9.0 go.uber.org/mock v0.5.0 + golang.org/x/mod v0.18.0 gopkg.in/yaml.v2 v2.4.0 k8s.io/api v0.29.9 k8s.io/apimachinery v0.29.9 diff --git a/go.sum b/go.sum index 25faf27..09d7d76 100644 --- a/go.sum +++ b/go.sum @@ -207,6 +207,8 @@ golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfU golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= +golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 0000000..7a1ec8c --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: MIT + +package version + +import "golang.org/x/mod/semver" + +const MinVersion = "v0.1.5" + +func EnsureMinimalVersion(version string) bool { + return semver.Compare(version, MinVersion) >= 0 +} diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go new file mode 100644 index 0000000..06b6e5d --- /dev/null +++ b/pkg/version/version_test.go @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: MIT + +package version + +import "testing" + +func TestCompare(t *testing.T) { + tests := []struct { + name string + version string + isValid bool + }{ + { + name: "is equal", + version: "v0.1.5", + isValid: true, + }, + { + name: "garm is newer", + version: "v0.1.6", + isValid: true, + }, + { + name: "garm version to old", + version: "v0.1.4", + isValid: false, + }, + { + name: "garm is based on custom build without using build tags", + version: "v0.0.0-unknown.", + isValid: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := EnsureMinimalVersion(tt.version); got != tt.isValid { + t.Errorf("Compare() = %v, want %v", got, tt.isValid) + } + }) + } +}