Skip to content

Commit 0a265f6

Browse files
fix: Adding delete timestamp check (#4078)
* Adding delete timestamp check * Adding UT and refactoring getmtpnc * Update cns/middlewares/k8sSwiftV2_linux_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: shreyashastantram <105284415+shreyashastantram@users.noreply.github.com> * Changing the MTPNC error message * Removing unused fields * Revert manifest changes for CRD files * Fixing error message and adding comments * Revert CRD API files to master state * fix: correcting comment assertion --------- Signed-off-by: shreyashastantram <105284415+shreyashastantram@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent fdc9b72 commit 0a265f6

File tree

4 files changed

+77
-43
lines changed

4 files changed

+77
-43
lines changed

cns/middlewares/k8sSwiftV2.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
errGetMTPNC = errors.New(NetworkNotReadyErrorMsg + " - failed to get MTPNC")
2929
errInvalidSWIFTv2NICType = errors.New("invalid NIC type for SWIFT v2 scenario")
3030
errInvalidMTPNCPrefixLength = errors.New("invalid prefix length for MTPNC primaryIP, must be 32")
31+
errMTPNCDeleting = errors.New(NetworkNotReadyErrorMsg + " - mtpnc for previous pod is being deleted, waiting for new mtpnc to be ready")
3132
)
3233

3334
type K8sSWIFTv2Middleware struct {
@@ -54,7 +55,9 @@ func (k *K8sSWIFTv2Middleware) GetPodInfoForIPConfigsRequest(ctx context.Context
5455
if respCode != types.Success {
5556
return nil, respCode, message
5657
}
57-
58+
if mtpnc.IsDeleting() {
59+
return nil, types.UnexpectedError, errMTPNCDeleting.Error()
60+
}
5861
// update ipConfigRequest
5962
respCode, message = k.UpdateIPConfigRequest(mtpnc, req)
6063
if respCode != types.Success {

cns/middlewares/k8sSwiftV2_linux_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ var (
4141

4242
testPod9GUID = "2006cad4-e54d-472e-863d-c4bac66200a7"
4343
testPod9Info = cns.NewPodInfo("2006cad4-eth0", testPod9GUID, "testpod9", "testpod9namespace")
44+
45+
testPodMtpncTerminatingGUID = "e3b0c442-98fc-1fc1-9b93-7a1c2e5c8e6f"
46+
testPodMtpncTerminatingInfo = cns.NewPodInfo("2006cad4-eth0", testPodMtpncTerminatingGUID, "testpodMtpncTerminating", "testpodMtpncTerminatingnamespace")
4447
)
4548

4649
func TestMain(m *testing.M) {
@@ -217,6 +220,13 @@ func TestValidateMultitenantIPConfigsRequestFailure(t *testing.T) {
217220
_, respCode, msg = middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
218221
assert.Equal(t, respCode, types.UnexpectedError)
219222
assert.Assert(t, strings.Contains(msg, NetworkNotReadyErrorMsg), "expected error message to contain '%s', got '%s'", NetworkNotReadyErrorMsg, msg)
223+
224+
// Delete Timestamp is set
225+
b, _ = testPodMtpncTerminatingInfo.OrchestratorContext()
226+
failReq.OrchestratorContext = b
227+
_, respCode, msg = middleware.GetPodInfoForIPConfigsRequest(context.TODO(), failReq)
228+
assert.Equal(t, respCode, types.UnexpectedError)
229+
assert.Assert(t, strings.Contains(msg, NetworkNotReadyErrorMsg), "expected error message to contain '%s', got '%s'", NetworkNotReadyErrorMsg, msg)
220230
}
221231

222232
func TestGetSWIFTv2IPConfigSuccess(t *testing.T) {

cns/middlewares/mock/mockClient.go

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
88
"github.com/pkg/errors"
99
v1 "k8s.io/api/core/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"sigs.k8s.io/controller-runtime/pkg/client"
1112
)
1213

@@ -66,31 +67,35 @@ func NewClient() *Client {
6667
testPod10.Labels = make(map[string]string)
6768
testPod10.Labels[configuration.LabelPodNetworkInstanceSwiftV2] = podNetwork
6869

70+
testPodMtpncTerminating := v1.Pod{}
71+
testPodMtpncTerminating.Labels = make(map[string]string)
72+
testPodMtpncTerminating.Labels[configuration.LabelPodSwiftV2] = podNetwork
73+
6974
testInterfaceInfos1 := v1alpha1.InterfaceInfo{
70-
NCID: "testncid",
71-
PrimaryIP: "192.168.0.1/32",
72-
MacAddress: "00:00:00:00:00:00",
73-
GatewayIP: "10.0.0.1",
74-
DeviceType: v1alpha1.DeviceTypeVnetNIC,
75-
AccelnetEnabled: false,
75+
NCID: "testncid",
76+
PrimaryIP: "192.168.0.1/32",
77+
MacAddress: "00:00:00:00:00:00",
78+
GatewayIP: "10.0.0.1",
79+
DeviceType: v1alpha1.DeviceTypeVnetNIC,
80+
AccelnetEnabled: false,
7681
SubnetAddressSpace: "192.168.0.0/24",
7782
}
7883
testInterfaceInfos3 := v1alpha1.InterfaceInfo{
79-
NCID: "testncid",
80-
PrimaryIP: "192.168.0.1/32",
81-
MacAddress: "00:00:00:00:00:00",
82-
GatewayIP: "10.0.0.1",
83-
DeviceType: v1alpha1.DeviceTypeVnetNIC,
84-
AccelnetEnabled: false,
84+
NCID: "testncid",
85+
PrimaryIP: "192.168.0.1/32",
86+
MacAddress: "00:00:00:00:00:00",
87+
GatewayIP: "10.0.0.1",
88+
DeviceType: v1alpha1.DeviceTypeVnetNIC,
89+
AccelnetEnabled: false,
8590
SubnetAddressSpace: "192.168.0.0/24",
8691
}
8792
testInterfaceInfos5 := v1alpha1.InterfaceInfo{
88-
NCID: "testncid",
89-
PrimaryIP: "192.168.0.1/32",
90-
MacAddress: "00:00:00:00:00:00",
91-
GatewayIP: "10.0.0.1",
92-
DeviceType: v1alpha1.DeviceTypeInfiniBandNIC,
93-
AccelnetEnabled: true,
93+
NCID: "testncid",
94+
PrimaryIP: "192.168.0.1/32",
95+
MacAddress: "00:00:00:00:00:00",
96+
GatewayIP: "10.0.0.1",
97+
DeviceType: v1alpha1.DeviceTypeInfiniBandNIC,
98+
AccelnetEnabled: true,
9499
SubnetAddressSpace: "192.168.0.0/24",
95100
}
96101

@@ -166,28 +171,38 @@ func NewClient() *Client {
166171
Status: v1alpha1.MultitenantPodNetworkConfigStatus{},
167172
}
168173

174+
testMTPNCTerminating := v1alpha1.MultitenantPodNetworkConfig{
175+
Status: v1alpha1.MultitenantPodNetworkConfigStatus{
176+
InterfaceInfos: []v1alpha1.InterfaceInfo{testInterfaceInfos1},
177+
},
178+
}
179+
now := metav1.Now()
180+
testMTPNCTerminating.DeletionTimestamp = &now
181+
169182
return &Client{
170183
mtPodCache: map[string]*v1.Pod{
171-
"testpod1namespace/testpod1": &testPod1,
172-
"testpod3namespace/testpod3": &testPod3,
173-
"testpod4namespace/testpod4": &testPod4,
174-
"testpod5namespace/testpod5": &testPod5,
175-
"testpod6namespace/testpod6": &testPod6,
176-
"testpod7namespace/testpod7": &testPod7,
177-
"testpod8namespace/testpod8": &testPod8,
178-
"testpod9namespace/testpod9": &testPod9,
179-
"testpod10namespace/testpod10": &testPod10,
184+
"testpod1namespace/testpod1": &testPod1,
185+
"testpod3namespace/testpod3": &testPod3,
186+
"testpod4namespace/testpod4": &testPod4,
187+
"testpod5namespace/testpod5": &testPod5,
188+
"testpod6namespace/testpod6": &testPod6,
189+
"testpod7namespace/testpod7": &testPod7,
190+
"testpod8namespace/testpod8": &testPod8,
191+
"testpod9namespace/testpod9": &testPod9,
192+
"testpod10namespace/testpod10": &testPod10,
193+
"testpodMtpncTerminatingnamespace/testpodMtpncTerminating": &testPodMtpncTerminating,
180194
},
181195
mtpncCache: map[string]*v1alpha1.MultitenantPodNetworkConfig{
182-
"testpod1namespace/testpod1": &testMTPNC1,
183-
"testpod2namespace/testpod2": &testMTPNC2,
184-
"testpod4namespace/testpod4": &testMTPNC4,
185-
"testpod5namespace/testpod5": &testMTPNC3,
186-
"testpod6namespace/testpod6": &testMTPNC5,
187-
"testpod7namespace/testpod7": &testMTPNCMulti,
188-
"testpod8namespace/testpod8": &testMTPNC8,
189-
"testpod9namespace/testpod9": &testMTPNC9,
190-
"testpod10namespace/testpod10": &testMTPNC10,
196+
"testpod1namespace/testpod1": &testMTPNC1,
197+
"testpod2namespace/testpod2": &testMTPNC2,
198+
"testpod4namespace/testpod4": &testMTPNC4,
199+
"testpod5namespace/testpod5": &testMTPNC3,
200+
"testpod6namespace/testpod6": &testMTPNC5,
201+
"testpod7namespace/testpod7": &testMTPNCMulti,
202+
"testpod8namespace/testpod8": &testMTPNC8,
203+
"testpod9namespace/testpod9": &testMTPNC9,
204+
"testpod10namespace/testpod10": &testMTPNC10,
205+
"testpodMtpncTerminatingnamespace/testpodMtpncTerminating": &testMTPNCTerminating,
191206
},
192207
}
193208
}
@@ -213,12 +228,12 @@ func (c *Client) Get(_ context.Context, key client.ObjectKey, obj client.Object,
213228

214229
func (c *Client) SetMTPNCReady() {
215230
testInterfaceInfos1 := v1alpha1.InterfaceInfo{
216-
NCID: "testncid",
217-
PrimaryIP: "192.168.0.1/32",
218-
MacAddress: "00:00:00:00:00:00",
219-
GatewayIP: "10.0.0.1",
220-
DeviceType: v1alpha1.DeviceTypeVnetNIC,
221-
AccelnetEnabled: false,
231+
NCID: "testncid",
232+
PrimaryIP: "192.168.0.1/32",
233+
MacAddress: "00:00:00:00:00:00",
234+
GatewayIP: "10.0.0.1",
235+
DeviceType: v1alpha1.DeviceTypeVnetNIC,
236+
AccelnetEnabled: false,
222237
SubnetAddressSpace: "192.168.0.0/24",
223238
}
224239

crd/multitenancy/api/v1alpha1/utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ func (m *MultitenantPodNetworkConfig) IsReady() bool {
99
// Check if InterfaceInfos slice is not empty
1010
return !reflect.DeepEqual(m.Status, MultitenantPodNetworkConfigStatus{})
1111
}
12+
13+
// IsDeleting returns true if the MultitenantPodNetworkConfig resource has been marked for deletion.
14+
// A resource is considered to be deleting when its DeletionTimestamp field is set.
15+
func (m *MultitenantPodNetworkConfig) IsDeleting() bool {
16+
return !m.DeletionTimestamp.IsZero()
17+
}

0 commit comments

Comments
 (0)