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 2fb4fd7 commit c7e771f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 15 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ go 1.20
replace sigs.k8s.io/cluster-api => sigs.k8s.io/cluster-api v1.5.0

require (
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.11.0
github.com/onsi/gomega v1.27.10
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -43,7 +43,6 @@ require (

require (
github.com/adrg/xdg v0.4.0 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
github.com/go-logr/zapr v1.2.4 // 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 @@ -351,8 +351,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
}
103 changes: 103 additions & 0 deletions pkg/util/networkutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
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"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

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 c7e771f

Please sign in to comment.