diff --git a/go.mod b/go.mod index 549ac00ec4..b32009c33f 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,10 @@ go 1.19 require ( github.com/antihax/optional v1.0.0 + github.com/blang/semver/v4 v4.0.0 github.com/go-logr/logr v1.2.4 github.com/google/gofuzz v1.2.0 github.com/google/uuid v1.3.0 - github.com/hashicorp/go-version v1.3.0 github.com/onsi/ginkgo/v2 v2.9.2 github.com/onsi/gomega v1.27.6 github.com/pkg/errors v0.9.1 @@ -41,7 +41,6 @@ require ( ) require ( - github.com/blang/semver/v4 v4.0.0 // indirect github.com/emicklei/go-restful/v3 v3.9.0 // indirect github.com/go-logr/zapr v1.2.3 // indirect github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect diff --git a/go.sum b/go.sum index 0dcd015726..d0ef0f1963 100644 --- a/go.sum +++ b/go.sum @@ -353,8 +353,6 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw= -github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= diff --git a/pkg/util/networkutil.go b/pkg/util/networkutil.go index b31889d7f0..32e041be24 100644 --- a/pkg/util/networkutil.go +++ b/pkg/util/networkutil.go @@ -19,7 +19,7 @@ package util import ( "context" - "github.com/hashicorp/go-version" + "github.com/blang/semver/v4" "github.com/pkg/errors" v1 "k8s.io/api/core/v1" apitypes "k8s.io/apimachinery/pkg/types" @@ -40,6 +40,11 @@ const ( EmptyNCPSNATKeyMsg = NCPSNATKey + " key not found" ) +var ( + NCPVersionSupportFWSemver = semver.MustParse(NCPVersionSupportFW) + NCPVersionSupportFWEndedSemver = semver.MustParse(NCPVersionSupportFWEnded) +) + // GetNamespaceNetSnatIP finds out the namespace's corresponding network's SNAT IP. func GetNamespaceNetSnatIP(ctx context.Context, controllerClient client.Client, namespace string) (string, error) { namespaceObj := &v1.Namespace{} @@ -86,18 +91,10 @@ func NCPSupportFW(ctx context.Context, controllerClient client.Client) (bool, er if err != nil { return false, err } - currVersion, err := version.NewVersion(ncpVersion) - if err != nil { - return false, err - } - supportStartedVersion, err := version.NewVersion(NCPVersionSupportFW) - if err != nil { - return false, err - } - supportEndedVersion, err := version.NewVersion(NCPVersionSupportFWEnded) + currVersion, err := semver.Parse(ncpVersion) if err != nil { return false, err } - supported := currVersion.GreaterThanOrEqual(supportStartedVersion) && currVersion.LessThan(supportEndedVersion) + supported := currVersion.GTE(NCPVersionSupportFWSemver) && currVersion.LT(NCPVersionSupportFWEndedSemver) return supported, nil } diff --git a/pkg/util/networkutil_test.go b/pkg/util/networkutil_test.go new file mode 100644 index 0000000000..d21e779cdb --- /dev/null +++ b/pkg/util/networkutil_test.go @@ -0,0 +1,105 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "context" + "testing" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + clientgoscheme "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func TestNCPSupportFW(t *testing.T) { + + scheme := runtime.NewScheme() + _ = clientgoscheme.AddToScheme(scheme) + + tests := []struct { + name string + client client.Client + want bool + wantErr bool + }{ + { + "No version configmap", + fake.NewClientBuilder().WithScheme(scheme).WithObjects().Build(), + false, + true, + }, + { + "non-semver version", + fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("nosemver")).Build(), + false, + true, + }, + { + "compatible version lower end", + fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap(NCPVersionSupportFW)).Build(), + true, + false, + }, + { + "compatible version upper end", + fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("3.0.9999")).Build(), + true, + false, + }, + { + "incompatible version lower end", + fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("3.0.0")).Build(), + false, + false, + }, + { + "incompatible version upper end", + fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap(NCPVersionSupportFWEnded)).Build(), + false, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() + got, err := NCPSupportFW(ctx, tt.client) + if (err != nil) != tt.wantErr { + t.Errorf("NCPSupportFW() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("NCPSupportFW() = %v, want %v", got, tt.want) + } + }) + } +} + +func newNCPConfigMap(version string) client.Object { + return &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: NCPVersionConfigMap, + Namespace: NCPNamespace, + }, + Data: map[string]string{ + NCPVersionKey: version, + }, + } +}