Skip to content

Commit

Permalink
Merge pull request #2797 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…2796-to-release-1.9

[release-1.9] 🐛 Fixes issue with NCP versioning while using the semver package
  • Loading branch information
k8s-ci-robot authored Mar 5, 2024
2 parents bcc72ca + 5c5d0d8 commit c8ebef9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/util/networkutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package util

import (
"context"
"strings"

"github.com/blang/semver/v4"
"github.com/pkg/errors"
Expand Down Expand Up @@ -78,6 +79,7 @@ func GetNamespaceNetSnatIP(ctx context.Context, controllerClient client.Client,
}

// GetNCPVersion finds out the running ncp's version from its configmap.
// If the version contains more than 3 segments, it will get trimmed down to 3.
func GetNCPVersion(ctx context.Context, controllerClient client.Client) (string, error) {
configmapObj := &corev1.ConfigMap{}
namespacedName := apitypes.NamespacedName{
Expand All @@ -90,6 +92,13 @@ func GetNCPVersion(ctx context.Context, controllerClient client.Client) (string,
}

version := configmapObj.Data[NCPVersionKey]

// NSX doesn't stritcly follow SemVer and there are versions like 4.0.1.3
// This will cause an error if directly used in semver.Parse() and prevent the cluster from reconciling.
// Since GetNCPVersion is only used to check >= 3.0.1 and < 3.1.0, it's safe to trim the last segment
if segments := strings.Split(version, "."); len(segments) > 3 {
return strings.Join(segments[:3], "."), nil
}
return version, nil
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/util/networkutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func TestNCPSupportFW(t *testing.T) {
true,
false,
},
{
"compatible version with more than 3 segments",
fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("3.0.1.1.1")).Build(),
true,
false,
},
{
"incompatible version lower end",
fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("3.0.0")).Build(),
Expand All @@ -74,6 +80,12 @@ func TestNCPSupportFW(t *testing.T) {
false,
false,
},
{
"incompatible version with more than 3 segments",
fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("3.1.0.1")).Build(),
false,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit c8ebef9

Please sign in to comment.