Skip to content

Commit 16d988c

Browse files
authored
reafactor: replace individual err msg with const from common package
Signed-off-by: GitHub <[email protected]>
1 parent cf3d350 commit 16d988c

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

cns/middlewares/k8sSwiftV2.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/Azure/azure-container-networking/cns/logger"
1010
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
1111
"github.com/Azure/azure-container-networking/cns/types"
12+
"github.com/Azure/azure-container-networking/common"
1213
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
1314
"github.com/pkg/errors"
1415
v1 "k8s.io/api/core/v1"
@@ -17,10 +18,8 @@ import (
1718
)
1819

1920
var (
20-
// In AKS, for kubelet to retry faster it is particularly looking for "network is not ready" error string.
21-
// https://github.com/kubernetes/kubernetes/blob/efd2a0d1f514be96a2f012fc3cb40f7c872b4e67/pkg/kubelet/pod_workers.go#L1495C2-L1501C3
22-
errMTPNCNotReady = errors.New("network is not ready - mtpnc is not ready")
23-
errMTPNCNotFound = errors.New("network is not ready - mtpnc not found")
21+
errMTPNCNotReady = errors.New(common.KubeletNetworkNotReadyMsg + " - mtpnc is not ready")
22+
errMTPNCNotFound = errors.New(common.KubeletNetworkNotReadyMsg + " - mtpnc not found")
2423
errInvalidSWIFTv2NICType = errors.New("invalid NIC type for SWIFT v2 scenario")
2524
errInvalidMTPNCPrefixLength = errors.New("invalid prefix length for MTPNC primaryIP, must be 32")
2625
)

cns/middlewares/k8sSwiftV2_linux_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/Azure/azure-container-networking/cns/logger"
1313
"github.com/Azure/azure-container-networking/cns/middlewares/mock"
1414
"github.com/Azure/azure-container-networking/cns/types"
15+
"github.com/Azure/azure-container-networking/common"
1516
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
1617
"github.com/google/go-cmp/cmp"
1718
"gotest.tools/v3/assert"
@@ -41,8 +42,6 @@ var (
4142

4243
testPod9GUID = "2006cad4-e54d-472e-863d-c4bac66200a7"
4344
testPod9Info = cns.NewPodInfo("2006cad4-eth0", testPod9GUID, "testpod9", "testpod9namespace")
44-
45-
kubeletExpectedString = "network is not ready"
4645
)
4746

4847
func TestMain(m *testing.M) {
@@ -211,14 +210,14 @@ func TestValidateMultitenantIPConfigsRequestFailure(t *testing.T) {
211210
failReq.OrchestratorContext = b
212211
_, respCode, msg := middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
213212
assert.Equal(t, respCode, types.UnexpectedError)
214-
assert.Assert(t, strings.Contains(msg, kubeletExpectedString), "expected error message to be '%s', got '%s'", kubeletExpectedString, msg)
213+
assert.Assert(t, strings.Contains(msg, common.KubeletNetworkNotReadyMsg), "expected error message to contain '%s', got '%s'", common.KubeletNetworkNotReadyMsg, msg)
215214

216215
// MTPNC not ready
217216
b, _ = testPod4Info.OrchestratorContext()
218217
failReq.OrchestratorContext = b
219218
_, respCode, msg = middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
220219
assert.Equal(t, respCode, types.UnexpectedError)
221-
assert.Assert(t, strings.Contains(msg, kubeletExpectedString), "expected error message to be '%s', got '%s'", kubeletExpectedString, msg)
220+
assert.Assert(t, strings.Contains(msg, common.KubeletNetworkNotReadyMsg), "expected error message to contain '%s', got '%s'", common.KubeletNetworkNotReadyMsg, msg)
222221
}
223222

224223
func TestGetSWIFTv2IPConfigSuccess(t *testing.T) {
@@ -237,21 +236,18 @@ func TestGetSWIFTv2IPConfigSuccess(t *testing.T) {
237236
assert.Equal(t, ipInfos[0].SkipDefaultRoutes, false)
238237
}
239238

240-
// In AKS, for kubelet to retry faster it is particularly looking for "network is not ready" error string.
241-
// https://github.com/kubernetes/kubernetes/blob/efd2a0d1f514be96a2f012fc3cb40f7c872b4e67/pkg/kubelet/pod_workers.go#L1495C2-L1501C3
242-
// This test is to ensure that the error string contains "network is not ready"
243239
func TestGetSWIFTv2IPConfigFailure(t *testing.T) {
244240
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
245241

246242
// Pod's MTPNC doesn't exist in cache test
247243
_, err := middleware.getIPConfig(context.TODO(), testPod3Info)
248244
assert.Assert(t, errors.Is(err, errMTPNCNotFound), "expected error to wrap errMTPNCNotFound, got: %v", err)
249-
assert.ErrorContains(t, err, kubeletExpectedString)
245+
assert.ErrorContains(t, err, common.KubeletNetworkNotReadyMsg)
250246

251247
// Pod's MTPNC is not ready test
252248
_, err = middleware.getIPConfig(context.TODO(), testPod4Info)
253249
assert.Assert(t, errors.Is(err, errMTPNCNotReady), "expected error to wrap errMTPNCNotReady, got: %v", err)
254-
assert.ErrorContains(t, err, kubeletExpectedString)
250+
assert.ErrorContains(t, err, common.KubeletNetworkNotReadyMsg)
255251
}
256252

257253
func TestSetRoutesSuccess(t *testing.T) {

common/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const (
3030
FiveSeconds = 5 * time.Second
3131
JsonContent = "application/json; charset=UTF-8"
3232
ContentType = "Content-Type"
33+
// In AKS, for kubelet to retry faster it is particularly looking for "network is not ready" error string.
34+
// https://github.com/kubernetes/kubernetes/blob/efd2a0d1f514be96a2f012fc3cb40f7c872b4e67/pkg/kubelet/pod_workers.go#L1495C2-L1501C3
35+
KubeletNetworkNotReadyMsg = "network is not ready"
3336
)
3437

3538
// XmlDocument - Azure host agent XML document format.

network/secondary_endpoint_client_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"time"
88

9+
"github.com/Azure/azure-container-networking/common"
910
"github.com/Azure/azure-container-networking/netio"
1011
"github.com/Azure/azure-container-networking/netlink"
1112
"github.com/Azure/azure-container-networking/netns"
@@ -141,7 +142,7 @@ func (client *SecondaryEndpointClient) ConfigureContainerInterfacesAndRoutes(epI
141142
logger.Info("Sending DHCP packet", zap.Any("macAddress", epInfo.MacAddress), zap.String("ifName", epInfo.IfName))
142143
err := client.dhcpClient.DiscoverRequest(ctx, epInfo.MacAddress, epInfo.IfName)
143144
if err != nil {
144-
return errors.Wrapf(err, "network is not ready - failed to issue dhcp discover packet to create mapping in host")
145+
return errors.Wrapf(err, common.KubeletNetworkNotReadyMsg+" - failed to issue dhcp discover packet to create mapping in host")
145146
}
146147
logger.Info("Finished configuring container interfaces and routes for secondary endpoint client")
147148

network/secondary_endpoint_linux_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111

1212
"github.com/Azure/azure-container-networking/cns"
13+
"github.com/Azure/azure-container-networking/common"
1314
"github.com/Azure/azure-container-networking/netio"
1415
"github.com/Azure/azure-container-networking/netlink"
1516
"github.com/Azure/azure-container-networking/network/networkutils"
@@ -399,7 +400,7 @@ func TestSecondaryConfigureContainerInterfacesAndRoutes(t *testing.T) {
399400
},
400401
},
401402
wantErr: true,
402-
wantErrMsg: "network is not ready",
403+
wantErrMsg: common.KubeletNetworkNotReadyMsg,
403404
},
404405
}
405406

0 commit comments

Comments
 (0)