@@ -1303,16 +1303,39 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
13031303 // TODO(rbtr): nodename and namespace should be in the cns config
13041304 directscopedcli := nncctrl .NewScopedClient (directnnccli , types.NamespacedName {Namespace : "kube-system" , Name : nodeName })
13051305
1306- nnc := v1alpha. NodeNetworkConfig {}
1306+ // Create the base NNC CRD if HomeAz is enabled
13071307 if cnsconfig .EnableHomeAz {
1308- // Create Node Network Config CRD and update the Home Az field with the cache value from the HomeAz Monitor
1309- nnc = createBaseNNC (node )
13101308 homeAzResponse := httpRestServiceImplementation .GetHomeAz (ctx )
1311- nnc .Spec .AvailabilityZone = strconv .FormatUint (uint64 (homeAzResponse .HomeAzResponse .HomeAz ), 10 )
1312- }
1309+ availabilityZone := strconv .FormatUint (uint64 (homeAzResponse .HomeAzResponse .HomeAz ), 10 )
1310+ logger .Printf ("[Azure CNS] HomeAz: %s" , availabilityZone )
1311+ // Create Node Network Config CRD and update the Home Az field with the cache value from the HomeAz Monitor
1312+ var nnc * v1alpha.NodeNetworkConfig
1313+ if nnc , err = directnnccli .Get (ctx , types.NamespacedName {Namespace : "kube-system" , Name : nodeName }); err != nil {
1314+ logger .Errorf ("[Azure CNS] failed to get existing NNC: %v" , err )
1315+ }
13131316
1314- if err = directcli .Create (ctx , & nnc ); err != nil {
1315- return errors .Wrap (err , "failed to create base NNC" )
1317+ if nnc == nil {
1318+ logger .Printf ("[Azure CNS] Creating new base NNC" )
1319+ newNNC := createBaseNNC (node )
1320+ newNNC .Spec .AvailabilityZone = availabilityZone
1321+ if err = directcli .Create (ctx , newNNC ); err != nil {
1322+ return errors .Wrap (err , "failed to create base NNC" )
1323+ }
1324+ } else {
1325+ // nnc.Spec.AvailabilityZone = availabilityZone
1326+ // if err = directcli.Update(ctx, nnc); err != nil {
1327+ // return errors.Wrap(err, "failed to update base NNC")
1328+ // }
1329+ logger .Printf ("[Azure CNS] Patching existing NNC with new Spec with HomeAz" )
1330+ newSpec := v1alpha.NodeNetworkConfigSpec {}
1331+ newSpec .AvailabilityZone = availabilityZone
1332+ newSpec .RequestedIPCount = nnc .Spec .RequestedIPCount
1333+ newSpec .IPsNotInUse = nnc .Spec .IPsNotInUse
1334+ if _ , err := directnnccli .PatchSpec (ctx , types.NamespacedName {Namespace : "kube-system" , Name : nodeName }, & newSpec , "azure-cns" ); err != nil {
1335+ return errors .Wrap (err , "failed to update base NNC" )
1336+ }
1337+ }
1338+ logger .Printf ("[Azure CNS] Updated HomeAz in NNC" )
13161339 }
13171340
13181341 logger .Printf ("Reconciling initial CNS state" )
@@ -1525,14 +1548,15 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
15251548 return nil
15261549}
15271550
1528- func createBaseNNC (node * corev1.Node ) v1alpha.NodeNetworkConfig {
1529- return v1alpha.NodeNetworkConfig {ObjectMeta : metav1.ObjectMeta {
1551+ func createBaseNNC (node * corev1.Node ) * v1alpha.NodeNetworkConfig {
1552+ return & v1alpha.NodeNetworkConfig {ObjectMeta : metav1.ObjectMeta {
15301553 Annotations : make (map [string ]string ),
15311554 Labels : map [string ]string {
15321555 "managed" : "true" ,
15331556 "owner" : node .Name ,
15341557 },
1535- Name : node .Name ,
1558+ Name : node .Name ,
1559+ Namespace : "kube-system" ,
15361560 }}
15371561}
15381562
0 commit comments