Skip to content

Commit 20940d2

Browse files
authored
Merge pull request #2219 from chrischdi/pr-remove-hashicorp-version
🌱 Remove direct dependency to github.com/hashicorp/go-version
2 parents 126c9da + e2acda8 commit 20940d2

File tree

4 files changed

+112
-15
lines changed

4 files changed

+112
-15
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ go 1.20
55
replace sigs.k8s.io/cluster-api => sigs.k8s.io/cluster-api v1.5.0
66

77
require (
8+
github.com/blang/semver/v4 v4.0.0
89
github.com/go-logr/logr v1.2.4
910
github.com/google/gofuzz v1.2.0
1011
github.com/google/uuid v1.3.0
11-
github.com/hashicorp/go-version v1.3.0
1212
github.com/onsi/ginkgo/v2 v2.11.0
1313
github.com/onsi/gomega v1.27.10
1414
github.com/pkg/errors v0.9.1
@@ -43,7 +43,6 @@ require (
4343

4444
require (
4545
github.com/adrg/xdg v0.4.0 // indirect
46-
github.com/blang/semver/v4 v4.0.0 // indirect
4746
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
4847
github.com/go-logr/zapr v1.2.4 // indirect
4948
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,6 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
351351
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
352352
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
353353
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
354-
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
355-
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
356354
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
357355
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
358356
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

pkg/util/networkutil.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package util
1919
import (
2020
"context"
2121

22-
"github.com/hashicorp/go-version"
22+
"github.com/blang/semver/v4"
2323
"github.com/pkg/errors"
2424
v1 "k8s.io/api/core/v1"
2525
apitypes "k8s.io/apimachinery/pkg/types"
@@ -40,6 +40,11 @@ const (
4040
EmptyNCPSNATKeyMsg = NCPSNATKey + " key not found"
4141
)
4242

43+
var (
44+
NCPVersionSupportFWSemver = semver.MustParse(NCPVersionSupportFW)
45+
NCPVersionSupportFWEndedSemver = semver.MustParse(NCPVersionSupportFWEnded)
46+
)
47+
4348
// GetNamespaceNetSnatIP finds out the namespace's corresponding network's SNAT IP.
4449
func GetNamespaceNetSnatIP(ctx context.Context, controllerClient client.Client, namespace string) (string, error) {
4550
namespaceObj := &v1.Namespace{}
@@ -86,18 +91,10 @@ func NCPSupportFW(ctx context.Context, controllerClient client.Client) (bool, er
8691
if err != nil {
8792
return false, err
8893
}
89-
currVersion, err := version.NewVersion(ncpVersion)
90-
if err != nil {
91-
return false, err
92-
}
93-
supportStartedVersion, err := version.NewVersion(NCPVersionSupportFW)
94-
if err != nil {
95-
return false, err
96-
}
97-
supportEndedVersion, err := version.NewVersion(NCPVersionSupportFWEnded)
94+
currVersion, err := semver.Parse(ncpVersion)
9895
if err != nil {
9996
return false, err
10097
}
101-
supported := currVersion.GreaterThanOrEqual(supportStartedVersion) && currVersion.LessThan(supportEndedVersion)
98+
supported := currVersion.GTE(NCPVersionSupportFWSemver) && currVersion.LT(NCPVersionSupportFWEndedSemver)
10299
return supported, nil
103100
}

pkg/util/networkutil_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
corev1 "k8s.io/api/core/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
28+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
29+
)
30+
31+
func TestNCPSupportFW(t *testing.T) {
32+
scheme := runtime.NewScheme()
33+
_ = clientgoscheme.AddToScheme(scheme)
34+
35+
tests := []struct {
36+
name string
37+
client client.Client
38+
want bool
39+
wantErr bool
40+
}{
41+
{
42+
"No version configmap",
43+
fake.NewClientBuilder().WithScheme(scheme).WithObjects().Build(),
44+
false,
45+
true,
46+
},
47+
{
48+
"non-semver version",
49+
fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("nosemver")).Build(),
50+
false,
51+
true,
52+
},
53+
{
54+
"compatible version lower end",
55+
fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap(NCPVersionSupportFW)).Build(),
56+
true,
57+
false,
58+
},
59+
{
60+
"compatible version upper end",
61+
fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("3.0.9999")).Build(),
62+
true,
63+
false,
64+
},
65+
{
66+
"incompatible version lower end",
67+
fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap("3.0.0")).Build(),
68+
false,
69+
false,
70+
},
71+
{
72+
"incompatible version upper end",
73+
fake.NewClientBuilder().WithScheme(scheme).WithObjects(newNCPConfigMap(NCPVersionSupportFWEnded)).Build(),
74+
false,
75+
false,
76+
},
77+
}
78+
for _, tt := range tests {
79+
t.Run(tt.name, func(t *testing.T) {
80+
ctx := context.Background()
81+
got, err := NCPSupportFW(ctx, tt.client)
82+
if (err != nil) != tt.wantErr {
83+
t.Errorf("NCPSupportFW() error = %v, wantErr %v", err, tt.wantErr)
84+
return
85+
}
86+
if got != tt.want {
87+
t.Errorf("NCPSupportFW() = %v, want %v", got, tt.want)
88+
}
89+
})
90+
}
91+
}
92+
93+
func newNCPConfigMap(version string) client.Object {
94+
return &corev1.ConfigMap{
95+
ObjectMeta: metav1.ObjectMeta{
96+
Name: NCPVersionConfigMap,
97+
Namespace: NCPNamespace,
98+
},
99+
Data: map[string]string{
100+
NCPVersionKey: version,
101+
},
102+
}
103+
}

0 commit comments

Comments
 (0)