Skip to content

Commit a864d49

Browse files
paulyufan2Copilot
andauthored
Chore: update k8s.io dependencies (#4042)
* Chore: update k8s.io dependencies * Update cns/middlewares/k8sSwiftV2.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Paul Yu <129891899+paulyufan2@users.noreply.github.com> * controller-runtime needs to be updated * fix controller-runtime v0.22.1 Apply method missing * fix UT * fix linter issues * fix linter issue * fix healthz issues * fix linter issues * add new error var * upgrade apiserver to v0.34.1 --------- Signed-off-by: Paul Yu <129891899+paulyufan2@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a1550f2 commit a864d49

File tree

9 files changed

+126
-311
lines changed

9 files changed

+126
-311
lines changed

cns/healthserver/healthz.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import (
55

66
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
77
"github.com/pkg/errors"
8+
meta "k8s.io/apimachinery/pkg/api/meta"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910
"k8s.io/apimachinery/pkg/runtime"
1011
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
12+
"k8s.io/client-go/rest"
1113
ctrl "sigs.k8s.io/controller-runtime"
1214
"sigs.k8s.io/controller-runtime/pkg/client"
15+
apiutil "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
1316
"sigs.k8s.io/controller-runtime/pkg/healthz"
1417
)
1518

@@ -21,6 +24,7 @@ func init() {
2124

2225
type Config struct {
2326
PingAPIServer bool
27+
Mapper meta.RESTMapper
2428
}
2529

2630
// NewHealthzHandlerWithChecks will return a [http.Handler] for CNS's /healthz endpoint.
@@ -30,12 +34,25 @@ type Config struct {
3034
func NewHealthzHandlerWithChecks(cfg *Config) (http.Handler, error) {
3135
checks := make(map[string]healthz.Checker)
3236
if cfg.PingAPIServer {
33-
cfg, err := ctrl.GetConfig()
37+
restCfg, err := ctrl.GetConfig()
3438
if err != nil {
3539
return nil, errors.Wrap(err, "failed to get kubeconfig")
3640
}
37-
cli, err := client.New(cfg, client.Options{
41+
// Use the provided (test) RESTMapper when present; otherwise fall back to a dynamic, discovery-based mapper for production.
42+
mapper := cfg.Mapper
43+
if mapper == nil {
44+
httpClient, httpErr := rest.HTTPClientFor(restCfg)
45+
if httpErr != nil {
46+
return nil, errors.Wrap(httpErr, "build http client for REST mapper")
47+
}
48+
mapper, err = apiutil.NewDynamicRESTMapper(restCfg, httpClient)
49+
if err != nil {
50+
return nil, errors.Wrap(err, "build rest mapper")
51+
}
52+
}
53+
cli, err := client.New(restCfg, client.Options{
3854
Scheme: scheme,
55+
Mapper: mapper,
3956
})
4057
if err != nil {
4158
return nil, errors.Wrap(err, "failed to build client")

cns/healthserver/healthz_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/require"
11+
meta "k8s.io/apimachinery/pkg/api/meta"
12+
"k8s.io/apimachinery/pkg/runtime/schema"
1113
)
1214

1315
const nncCRD = `{
@@ -158,6 +160,13 @@ const nncResult = `{
158160
}
159161
}`
160162

163+
func staticNNCMapper() meta.RESTMapper {
164+
gv := schema.GroupVersion{Group: "acn.azure.com", Version: "v1alpha"}
165+
m := meta.NewDefaultRESTMapper([]schema.GroupVersion{gv})
166+
m.Add(schema.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: "NodeNetworkConfig"}, meta.RESTScopeNamespace)
167+
return m
168+
}
169+
161170
func TestNewHealthzHandlerWithChecks(t *testing.T) {
162171
tests := []struct {
163172
name string
@@ -169,6 +178,7 @@ func TestNewHealthzHandlerWithChecks(t *testing.T) {
169178
name: "list NNC gives 200 should indicate healthy",
170179
config: &Config{
171180
PingAPIServer: true,
181+
Mapper: staticNNCMapper(),
172182
},
173183
apiStatusCode: http.StatusOK,
174184
expectedHealthy: true,
@@ -177,6 +187,7 @@ func TestNewHealthzHandlerWithChecks(t *testing.T) {
177187
name: "unauthorized (401) from apiserver should be unhealthy",
178188
config: &Config{
179189
PingAPIServer: true,
190+
Mapper: staticNNCMapper(),
180191
},
181192
apiStatusCode: http.StatusUnauthorized,
182193
expectedHealthy: false,
@@ -185,6 +196,7 @@ func TestNewHealthzHandlerWithChecks(t *testing.T) {
185196
name: "channel nodesubnet should not call apiserver so it doesn't matter if the status code is a 401",
186197
config: &Config{
187198
PingAPIServer: false,
199+
Mapper: staticNNCMapper(),
188200
},
189201
apiStatusCode: http.StatusUnauthorized,
190202
expectedHealthy: true,

cns/middlewares/k8sSwiftV2.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ import (
1212
"github.com/pkg/errors"
1313
v1 "k8s.io/api/core/v1"
1414
k8stypes "k8s.io/apimachinery/pkg/types"
15-
"k8s.io/kubernetes/pkg/kubelet"
1615
"sigs.k8s.io/controller-runtime/pkg/client"
1716
)
1817

18+
const (
19+
prefixLength = 32
20+
overlayGatewayv4 = "169.254.1.1"
21+
virtualGW = "169.254.2.1"
22+
overlayGatewayV6 = "fe80::1234:5678:9abc"
23+
NetworkNotReadyErrorMsg = "network is not ready"
24+
)
25+
1926
var (
20-
errMTPNCNotReady = errors.New(kubelet.NetworkNotReadyErrorMsg + " - mtpnc is not ready")
21-
errGetMTPNC = errors.New(kubelet.NetworkNotReadyErrorMsg + " - failed to get MTPNC")
27+
errMTPNCNotReady = errors.New(NetworkNotReadyErrorMsg + " - mtpnc is not ready")
28+
errGetMTPNC = errors.New(NetworkNotReadyErrorMsg + " - failed to get MTPNC")
2229
errInvalidSWIFTv2NICType = errors.New("invalid NIC type for SWIFT v2 scenario")
2330
errInvalidMTPNCPrefixLength = errors.New("invalid prefix length for MTPNC primaryIP, must be 32")
2431
)
2532

26-
const (
27-
prefixLength = 32
28-
overlayGatewayv4 = "169.254.1.1"
29-
virtualGW = "169.254.2.1"
30-
overlayGatewayV6 = "fe80::1234:5678:9abc"
31-
)
32-
3333
type K8sSWIFTv2Middleware struct {
3434
Cli client.Client
3535
}

cns/middlewares/k8sSwiftV2_linux_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
1616
"github.com/google/go-cmp/cmp"
1717
"gotest.tools/v3/assert"
18-
"k8s.io/kubernetes/pkg/kubelet"
1918
)
2019

2120
var (
@@ -210,14 +209,14 @@ func TestValidateMultitenantIPConfigsRequestFailure(t *testing.T) {
210209
failReq.OrchestratorContext = b
211210
_, respCode, msg := middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
212211
assert.Equal(t, respCode, types.UnexpectedError)
213-
assert.Assert(t, strings.Contains(msg, kubelet.NetworkNotReadyErrorMsg), "expected error message to contain '%s', got '%s'", kubelet.NetworkNotReadyErrorMsg, msg)
212+
assert.Assert(t, strings.Contains(msg, NetworkNotReadyErrorMsg), "expected error message to contain '%s', got '%s'", NetworkNotReadyErrorMsg, msg)
214213

215214
// MTPNC not ready
216215
b, _ = testPod4Info.OrchestratorContext()
217216
failReq.OrchestratorContext = b
218217
_, respCode, msg = middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
219218
assert.Equal(t, respCode, types.UnexpectedError)
220-
assert.Assert(t, strings.Contains(msg, kubelet.NetworkNotReadyErrorMsg), "expected error message to contain '%s', got '%s'", kubelet.NetworkNotReadyErrorMsg, msg)
219+
assert.Assert(t, strings.Contains(msg, NetworkNotReadyErrorMsg), "expected error message to contain '%s', got '%s'", NetworkNotReadyErrorMsg, msg)
221220
}
222221

223222
func TestGetSWIFTv2IPConfigSuccess(t *testing.T) {
@@ -242,12 +241,12 @@ func TestGetSWIFTv2IPConfigFailure(t *testing.T) {
242241
// Pod's MTPNC doesn't exist in cache test
243242
_, err := middleware.getIPConfig(context.TODO(), testPod3Info)
244243
assert.Assert(t, strings.Contains(err.Error(), errGetMTPNC.Error()), "expected error to wrap errMTPNCNotFound, got: %v", err)
245-
assert.ErrorContains(t, err, kubelet.NetworkNotReadyErrorMsg)
244+
assert.ErrorContains(t, err, NetworkNotReadyErrorMsg)
246245

247246
// Pod's MTPNC is not ready test
248247
_, err = middleware.getIPConfig(context.TODO(), testPod4Info)
249248
assert.Assert(t, errors.Is(err, errMTPNCNotReady), "expected error to wrap errMTPNCNotReady, got: %v", err)
250-
assert.ErrorContains(t, err, kubelet.NetworkNotReadyErrorMsg)
249+
assert.ErrorContains(t, err, NetworkNotReadyErrorMsg)
251250
}
252251

253252
func TestSetRoutesSuccess(t *testing.T) {

cns/multitenantcontroller/multitenantoperator/multitenantcrdreconciler_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ import (
1414
. "github.com/onsi/gomega"
1515
apierrors "k8s.io/apimachinery/pkg/api/errors"
1616
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17+
"k8s.io/apimachinery/pkg/runtime"
1718
"k8s.io/apimachinery/pkg/types"
1819
"sigs.k8s.io/controller-runtime/pkg/client"
1920
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2021
)
2122

23+
type clientWithApply struct{ *mockclients.MockClient }
24+
25+
// Satisfy controller-runtime v0.22.1 Writer.Apply:
26+
func (c clientWithApply) Apply(_ context.Context, _ runtime.ApplyConfiguration, _ ...client.ApplyOption) error {
27+
return nil // not used in these tests
28+
}
29+
2230
var _ = Describe("multiTenantCrdReconciler", func() {
2331
var kubeClient *mockclients.MockClient
2432
var cnsRestService *mockclients.MockcnsRESTservice
@@ -43,7 +51,7 @@ var _ = Describe("multiTenantCrdReconciler", func() {
4351
cnsRestService = mockclients.NewMockcnsRESTservice(mockCtl)
4452
statusWriter = mockclients.NewMockSubResourceWriter(mockCtl)
4553
reconciler = &multiTenantCrdReconciler{
46-
KubeClient: kubeClient,
54+
KubeClient: clientWithApply{kubeClient},
4755
NodeName: mockNodeName,
4856
CNSRestService: cnsRestService,
4957
}

0 commit comments

Comments
 (0)