Skip to content

Commit

Permalink
add two more labels for ocp version (stolostron#529)
Browse files Browse the repository at this point in the history
Signed-off-by: Yang Le <[email protected]>

Signed-off-by: Yang Le <[email protected]>
  • Loading branch information
elgnay authored Oct 9, 2022
1 parent c634828 commit 84e76f4
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ require (
github.com/prometheus/common v0.32.1
github.com/smartystreets/goconvey v1.7.2
github.com/spf13/pflag v1.0.5
github.com/stolostron/cluster-lifecycle-api v0.0.0-20220714081119-eae2fe1f05fd
github.com/stolostron/cluster-lifecycle-api v0.0.0-20220930080346-456dd8fcbea8
github.com/stretchr/testify v1.7.2
golang.org/x/net v0.0.0-20220325170049-de3da57026de
k8s.io/api v0.24.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,8 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
github.com/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980/go.mod h1:AO3tvPzVZ/ayst6UlUKUv6rcPQInYe3IknH3jYhAKu8=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stolostron/cluster-lifecycle-api v0.0.0-20220714081119-eae2fe1f05fd h1:TitMXyGY/ld+hS6kjdHwcdR+Pl2UrtO812ehBTcu34Q=
github.com/stolostron/cluster-lifecycle-api v0.0.0-20220714081119-eae2fe1f05fd/go.mod h1:pNeVzujoHsTHDloNHVfp1QPYlQy8MkXMuuZme96/x8M=
github.com/stolostron/cluster-lifecycle-api v0.0.0-20220930080346-456dd8fcbea8 h1:uEQYpp/+UMqGK2Nxbr93YE7gWD8ndAJC5dxm0gzULJk=
github.com/stolostron/cluster-lifecycle-api v0.0.0-20220930080346-456dd8fcbea8/go.mod h1:pNeVzujoHsTHDloNHVfp1QPYlQy8MkXMuuZme96/x8M=
github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
35 changes: 34 additions & 1 deletion pkg/controllers/clusterinfo/autodetect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package clusterinfo

import (
"context"
"fmt"
"reflect"
"strings"

clusterinfov1beta1 "github.com/stolostron/cluster-lifecycle-api/clusterinfo/v1beta1"
clusterclaims "github.com/stolostron/multicloud-operators-foundation/pkg/klusterlet/clusterclaim"
Expand Down Expand Up @@ -80,10 +82,31 @@ func (r *AutoDetectReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

for _, claim := range cluster.Status.ClusterClaims {
if claim.Name == clusterclaims.ClaimOpenshiftVersion && labels[clusterinfov1beta1.OCPVersion] != claim.Value {
if claim.Name != clusterclaims.ClaimOpenshiftVersion {
continue
}

if labels[clusterinfov1beta1.OCPVersion] != claim.Value {
labels[clusterinfov1beta1.OCPVersion] = claim.Value
needUpdate = true
}

// parse the full OCP version and add two more labels
major, minor, _, err := parseOCPVersion(claim.Value)
if err != nil {
return reconcile.Result{}, err
}

if labels[clusterinfov1beta1.OCPVersionMajor] != major {
labels[clusterinfov1beta1.OCPVersionMajor] = major
needUpdate = true
}

ocpVersionMajorMinor := fmt.Sprintf("%s.%s", major, minor)
if labels[clusterinfov1beta1.OCPVersionMajorMinor] != ocpVersionMajorMinor {
labels[clusterinfov1beta1.OCPVersionMajorMinor] = ocpVersionMajorMinor
needUpdate = true
}
}

if needUpdate {
Expand All @@ -108,3 +131,13 @@ func (r *AutoDetectReconciler) Reconcile(ctx context.Context, req ctrl.Request)

return ctrl.Result{}, nil
}

// parseOCPVersion pasrses the full version of OCP and returns major, minor and patch.
func parseOCPVersion(version string) (string, string, string, error) {
parts := strings.Split(version, ".")
if len(parts) != 3 {
return "", "", "", fmt.Errorf("invalid Openshift version: %s", version)
}

return parts[0], parts[1], parts[2], nil
}
45 changes: 43 additions & 2 deletions pkg/controllers/clusterinfo/autodetect_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestOSDVendorOcpVersion(t *testing.T) {
ClusterClaims: []clusterv1.ManagedClusterClaim{
{
Name: clusterclaims.ClaimOpenshiftVersion,
Value: "4.6",
Value: "4.6.10",
},
},
},
Expand All @@ -218,7 +218,11 @@ func TestOSDVendorOcpVersion(t *testing.T) {
Spec: clusterv1beta1.ClusterInfoSpec{},
},
},
expectedLabel: map[string]string{clusterv1beta1.OCPVersion: "4.6"},
expectedLabel: map[string]string{
clusterv1beta1.OCPVersion: "4.6.10",
clusterv1beta1.OCPVersionMajor: "4",
clusterv1beta1.OCPVersionMajorMinor: "4.6",
},
expectedErrorType: nil,
req: reconcile.Request{
NamespacedName: types.NamespacedName{
Expand All @@ -243,3 +247,40 @@ func TestOSDVendorOcpVersion(t *testing.T) {
})
}
}

func TestParseOCPVersion(t *testing.T) {
tests := []struct {
name string
ocpVersion string
major string
minor string
patch string
expectedError string
}{
{
name: "valid ocp version",
ocpVersion: "4.11.3",
major: "4",
minor: "11",
patch: "3",
},
{
name: "invalid ocp version",
ocpVersion: "4.11",
expectedError: "invalid Openshift version: 4.11",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
major, minor, patch, err := parseOCPVersion(test.ocpVersion)
if err != nil {
assert.EqualError(t, err, test.expectedError, "unexpected error")
return
}
assert.Equal(t, test.major, major, "wrong major version")
assert.Equal(t, test.minor, minor, "wrong minor version")
assert.Equal(t, test.patch, patch, "wrong patch version")
})
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ github.com/spf13/cobra
# github.com/spf13/pflag v1.0.5
## explicit; go 1.12
github.com/spf13/pflag
# github.com/stolostron/cluster-lifecycle-api v0.0.0-20220714081119-eae2fe1f05fd
# github.com/stolostron/cluster-lifecycle-api v0.0.0-20220930080346-456dd8fcbea8
## explicit; go 1.18
github.com/stolostron/cluster-lifecycle-api/action/v1beta1
github.com/stolostron/cluster-lifecycle-api/clusterinfo/v1beta1
Expand Down

0 comments on commit 84e76f4

Please sign in to comment.