Skip to content

Commit df43143

Browse files
authored
fix: multitenantnetwork reconciler should check errors correctly (#1791)
1 parent 7ae0a74 commit df43143

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

cns/multitenantcontroller/mockclients/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ MOCKGEN = $(TOOLS_BIN_DIR)/mockgen
55

66
generate: $(MOCKGEN) ## Generate mock clients
77
$(MOCKGEN) -source=$(REPO_ROOT)/cns/multitenantcontroller/multitenantoperator/multitenantcrdreconciler.go -package=mockclients cnsRESTservice > cnsrestservice_generated.go
8-
$(MOCKGEN) -source=$(REPO_ROOT)/vendor/sigs.k8s.io/controller-runtime/pkg/client/interfaces.go -package=mockclients Client > kubeclient_generated.go
8+
$(MOCKGEN) -source=$(REPO_ROOT)/vendor/sigs.k8s.io/controller-runtime/pkg/client/interfaces.go -package=mockclients Client,SubResourceWriter > kubeclient_generated.go
99
@sed -i s,$(REPO_ROOT)/,,g cnsrestservice_generated.go kubeclient_generated.go
1010

1111
$(MOCKGEN):

cns/multitenantcontroller/mockclients/cnsrestservice_generated.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cns/multitenantcontroller/multitenantoperator/multitenantcrdreconciler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package multitenantoperator
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"net"
78
"reflect"
89
"strings"
@@ -112,7 +113,11 @@ func (r *multiTenantCrdReconciler) Reconcile(ctx context.Context, request reconc
112113
if err == nil {
113114
logger.Printf("NC %s (UUID: %s) has already been created in CNS", request.NamespacedName.String(), nc.Spec.UUID)
114115
return ctrl.Result{}, nil
115-
} else if !strings.EqualFold(err.Error(), types.UnknownContainerID.String()) {
116+
}
117+
118+
// return any error except UnknownContainerID
119+
var cnsRESTErr *restserver.CNSRESTError
120+
if !errors.As(err, &cnsRESTErr) || cnsRESTErr.ResponseCode != types.UnknownContainerID {
116121
logger.Errorf("Failed to fetch NC %s (UUID: %s) from CNS: %v", request.NamespacedName.String(), nc.Spec.UUID, err)
117122
return ctrl.Result{}, err
118123
}

cns/multitenantcontroller/multitenantoperator/multitenantcrdreconciler_test.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import (
1515
apierrors "k8s.io/apimachinery/pkg/api/errors"
1616
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1717
"k8s.io/apimachinery/pkg/types"
18+
"sigs.k8s.io/controller-runtime/pkg/client"
1819
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1920
)
2021

2122
var _ = Describe("multiTenantCrdReconciler", func() {
2223
var kubeClient *mockclients.MockClient
2324
var cnsRestService *mockclients.MockcnsRESTservice
25+
var statusWriter *mockclients.MockSubResourceWriter
2426
var mockCtl *gomock.Controller
2527
var reconciler *multiTenantCrdReconciler
2628
const uuidValue = "uuid"
@@ -39,6 +41,7 @@ var _ = Describe("multiTenantCrdReconciler", func() {
3941
mockCtl = gomock.NewController(GinkgoT())
4042
kubeClient = mockclients.NewMockClient(mockCtl)
4143
cnsRestService = mockclients.NewMockcnsRESTservice(mockCtl)
44+
statusWriter = mockclients.NewMockSubResourceWriter(mockCtl)
4245
reconciler = &multiTenantCrdReconciler{
4346
KubeClient: kubeClient,
4447
NodeName: mockNodeName,
@@ -103,6 +106,67 @@ var _ = Describe("multiTenantCrdReconciler", func() {
103106
Expect(err).To(BeNil())
104107
})
105108

109+
It("Should succeed when the NC is in Initialized state and it has not yet been persisted in CNS", func() {
110+
uuid := uuidValue
111+
var nc ncapi.MultiTenantNetworkContainer = ncapi.MultiTenantNetworkContainer{
112+
ObjectMeta: metav1.ObjectMeta{
113+
Name: namespacedName.Name,
114+
Namespace: namespacedName.Namespace,
115+
},
116+
Spec: ncapi.MultiTenantNetworkContainerSpec{
117+
UUID: uuid,
118+
},
119+
Status: ncapi.MultiTenantNetworkContainerStatus{
120+
State: "Initialized",
121+
MultiTenantInfo: ncapi.MultiTenantInfo{
122+
EncapType: "Vlan",
123+
ID: 1,
124+
},
125+
IPSubnet: "10.0.0.0/8",
126+
IP: "10.1.0.0",
127+
},
128+
}
129+
130+
orchestratorContext, err := json.Marshal(podInfo)
131+
Expect(err).To(BeNil())
132+
133+
kubeClient.EXPECT().Get(gomock.Any(), namespacedName, gomock.Any()).SetArg(2, nc)
134+
cnsRestService.EXPECT().GetNetworkContainerInternal(cns.GetNetworkContainerRequest{
135+
NetworkContainerid: uuid,
136+
OrchestratorContext: orchestratorContext,
137+
}).Return(cns.GetNetworkContainerResponse{}, cnstypes.UnknownContainerID)
138+
139+
cnsRestService.EXPECT().CreateOrUpdateNetworkContainerInternal(&cns.CreateNetworkContainerRequest{
140+
NetworkContainerid: nc.Spec.UUID,
141+
OrchestratorContext: orchestratorContext,
142+
NetworkContainerType: cns.Kubernetes,
143+
Version: "0",
144+
IPConfiguration: cns.IPConfiguration{
145+
IPSubnet: cns.IPSubnet{
146+
IPAddress: nc.Status.IP,
147+
PrefixLength: 8,
148+
},
149+
GatewayIPAddress: nc.Status.Gateway,
150+
},
151+
PrimaryInterfaceIdentifier: nc.Status.PrimaryInterfaceIdentifier,
152+
MultiTenancyInfo: cns.MultiTenancyInfo{
153+
EncapType: nc.Status.MultiTenantInfo.EncapType,
154+
ID: int(nc.Status.MultiTenantInfo.ID),
155+
},
156+
}).Return(cnstypes.Success)
157+
158+
kubeClient.EXPECT().Status().DoAndReturn(func() client.SubResourceWriter {
159+
nc.Status.State = NCStateSucceeded
160+
statusWriter.EXPECT().Update(gomock.Any(), gomock.Any()).SetArg(1, nc)
161+
return statusWriter
162+
})
163+
164+
_, err = reconciler.Reconcile(context.TODO(), reconcile.Request{
165+
NamespacedName: namespacedName,
166+
})
167+
Expect(err).To(BeNil())
168+
})
169+
106170
It("Should succeed when the NC is in Initialized state and it has already been persisted in CNS", func() {
107171
uuid := uuidValue
108172
var nc ncapi.MultiTenantNetworkContainer = ncapi.MultiTenantNetworkContainer{
@@ -168,7 +232,7 @@ var _ = Describe("multiTenantCrdReconciler", func() {
168232
NamespacedName: namespacedName,
169233
})
170234
Expect(err).NotTo(BeNil())
171-
Expect(err.Error()).To(ContainSubstring("UnknownContainerID"))
235+
Expect(err.Error()).To(ContainSubstring("invalid CIDR address"))
172236
})
173237

174238
It("Should succeed when the NC subnet is in correct format", func() {

0 commit comments

Comments
 (0)