Skip to content

Commit

Permalink
Remove direct dependency to github.com/hashicorp/go-version
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Aug 15, 2023
1 parent 26d81f6 commit d195041
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 15 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
19 changes: 8 additions & 11 deletions pkg/util/networkutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{}
Expand Down Expand Up @@ -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
}
105 changes: 105 additions & 0 deletions pkg/util/networkutil_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
}
}

0 comments on commit d195041

Please sign in to comment.