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

feat: error early if min. supported version match #194

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
11 changes: 11 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}