Skip to content

Commit

Permalink
Update GetClusterVersion to return cv.Status.History[0] if no complet…
Browse files Browse the repository at this point in the history
…ed update exists
  • Loading branch information
bennerv committed Feb 14, 2024
1 parent 4df205e commit 141babd
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/operator/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func ShouldUsePodSecurityStandard(ctx context.Context, client client.Client) (bo
if err != nil {
return false, err
}

vers, err := version.GetClusterVersion(cv)
if err != nil {
return false, err
Expand Down
18 changes: 17 additions & 1 deletion pkg/util/version/clusterversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@ import (
configv1 "github.com/openshift/api/config/v1"
)

// GetClusterVersion fetches the version of the openshift cluster.
// Note that it assumes the most recently applied version is
// cv.Status.History[0] assuming the State == Completed.
// If for some reason there is no cluster version history, it will
// return the most recently updated version in history
func GetClusterVersion(cv *configv1.ClusterVersion) (*Version, error) {
unknownErr := errors.New("unknown cluster version")
if cv == nil {
return nil, unknownErr
}

for _, history := range cv.Status.History {
if history.State == configv1.CompletedUpdate {
return ParseVersion(history.Version)
}
}

return nil, errors.New("unknown cluster version")
// If the cluster history has no completed version, we're most likely installing
// so grab the first history version and use it even if it's not completed
if len(cv.Status.History) > 0 {
return ParseVersion(cv.Status.History[0].Version)
}

return nil, unknownErr
}
110 changes: 110 additions & 0 deletions pkg/util/version/clusterversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package version

Check failure on line 1 in pkg/util/version/clusterversion_test.go

View workflow job for this annotation

GitHub Actions / validate-go

group 2: mixed import type

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"reflect"
"testing"

utilerror "github.com/Azure/ARO-RP/test/util/error"
configv1 "github.com/openshift/api/config/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Check failure on line 12 in pkg/util/version/clusterversion_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

import "k8s.io/apimachinery/pkg/apis/meta/v1" imported as "v1" but must be "metav1" according to config (importas)
)

func TestGetClusterVersion(t *testing.T) {
for _, tt := range []struct {
name string
wantVers *Version
mocks func(*configv1.ClusterVersion)
wantErr string
}{
{
name: "cluster version nil returns error",
mocks: func(cv *configv1.ClusterVersion) {

Check failure on line 24 in pkg/util/version/clusterversion_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA4009: argument cv is overwritten before first use (staticcheck)
cv = nil

Check failure on line 25 in pkg/util/version/clusterversion_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA4009(related information): assignment to cv (staticcheck)
},
wantErr: "unknown cluster version",
},
{
name: "no update history returns error",
wantErr: "unknown cluster version",
},
{
name: "multiple completed updates returns top most",
wantVers: NewVersion(4, 10, 0),
mocks: func(cv *configv1.ClusterVersion) {
cv.Status.History = append(cv.Status.History,
configv1.UpdateHistory{
State: configv1.CompletedUpdate,
Version: "4.10.0",
},
configv1.UpdateHistory{
State: configv1.CompletedUpdate,
Version: "4.9.0",
},
)
},
},
{
name: "pending update topmost, returns most recent completed update",
wantVers: NewVersion(4, 9, 0),
mocks: func(cv *configv1.ClusterVersion) {
cv.Status.History = append(cv.Status.History,
configv1.UpdateHistory{
State: configv1.PartialUpdate,
Version: "4.10.0",
},
configv1.UpdateHistory{
State: configv1.CompletedUpdate,
Version: "4.9.0",
},
)
},
},
{
name: "only partial update in history returns partial update",
wantVers: NewVersion(4, 10, 0),
mocks: func(cv *configv1.ClusterVersion) {
cv.Status.History = append(cv.Status.History,
configv1.UpdateHistory{
State: configv1.PartialUpdate,
Version: "4.10.0",
},
)
},
},
{
name: "missing update history state and no completed returns version",
wantVers: NewVersion(4, 10, 0),
mocks: func(cv *configv1.ClusterVersion) {
cv.Status.History = append(cv.Status.History,
configv1.UpdateHistory{
Version: "4.10.0",
},
)
},
},
} {
t.Run(tt.name, func(t *testing.T) {
cv := &configv1.ClusterVersion{
ObjectMeta: v1.ObjectMeta{
Name: "version",
},
Status: configv1.ClusterVersionStatus{
History: []configv1.UpdateHistory{},
},
}

if tt.mocks != nil {
tt.mocks(cv)
}
version, err := GetClusterVersion(cv)
utilerror.AssertErrorMessage(t, err, tt.wantErr)

if !reflect.DeepEqual(version, tt.wantVers) {
t.Error(version)
}
})
}
}

0 comments on commit 141babd

Please sign in to comment.