Skip to content

Commit

Permalink
feat: error early if min. supported version match
Browse files Browse the repository at this point in the history
  • Loading branch information
bavarianbidi committed Oct 22, 2024
1 parent 495a9ec commit 413ebd2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
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
}
42 changes: 42 additions & 0 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: MIT

package version

import "testing"

func TestCompare(t *testing.T) {

Check failure on line 7 in pkg/version/version_test.go

View workflow job for this annotation

GitHub Actions / lint and test

unnecessary leading newline (whitespace)

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)
}
})
}
}

0 comments on commit 413ebd2

Please sign in to comment.