diff --git a/go.sum b/go.sum index 0a1a605..7ab0005 100644 --- a/go.sum +++ b/go.sum @@ -824,8 +824,6 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/pkg/controller/ipam/multi_cidr_range_allocator.go b/pkg/controller/ipam/multi_cidr_range_allocator.go index 726ba58..84f67f5 100644 --- a/pkg/controller/ipam/multi_cidr_range_allocator.go +++ b/pkg/controller/ipam/multi_cidr_range_allocator.go @@ -23,6 +23,7 @@ import ( "fmt" "math" "net" + "slices" "sync" "time" @@ -1090,12 +1091,10 @@ func (r *multiCIDRRangeAllocator) reconcileCreate(ctx context.Context, clusterCI defer r.lock.Unlock() logger := klog.FromContext(ctx) - if needToAddFinalizer(clusterCIDR, clusterCIDRFinalizer) { - logger.V(3).Info("Creating ClusterCIDR", "clusterCIDR", clusterCIDR.Name) - if err := r.createClusterCIDR(ctx, clusterCIDR, false); err != nil { - logger.Error(err, "Unable to create ClusterCIDR", "clusterCIDR", clusterCIDR.Name) - return err - } + logger.V(3).Info("Reconciling ClusterCIDR", "clusterCIDR", clusterCIDR.Name) + if err := r.createClusterCIDR(ctx, clusterCIDR, false); err != nil { + logger.Error(err, "failed to reconcile ClusterCIDR", "clusterCIDR", clusterCIDR.Name) + return err } return nil } @@ -1154,13 +1153,13 @@ func (r *multiCIDRRangeAllocator) createClusterCIDR(ctx context.Context, cluster if updatedClusterCIDR.ResourceVersion == "" { // Create is only used for creating default ClusterCIDR. if _, err := r.networkClient.Create(ctx, updatedClusterCIDR, metav1.CreateOptions{}); err != nil { - logger.V(2).Info("Error creating ClusterCIDR", "clusterCIDR", klog.KObj(clusterCIDR), "err", err) + logger.V(2).Info("failed to create ClusterCIDR", "clusterCIDR", klog.KObj(clusterCIDR), "err", err) return err } } else { // Update the ClusterCIDR object when called from reconcileCreate. if _, err := r.networkClient.Update(ctx, updatedClusterCIDR, metav1.UpdateOptions{}); err != nil { - logger.V(2).Info("Error creating ClusterCIDR", "clusterCIDR", clusterCIDR.Name, "err", err) + logger.V(2).Info("failed to update ClusterCIDR", "clusterCIDR", clusterCIDR.Name, "err", err) return err } } @@ -1208,7 +1207,13 @@ func (r *multiCIDRRangeAllocator) mapClusterCIDRSet(cidrMap map[string][]*cidrse } if clusterCIDRSetList, ok := cidrMap[nodeSelector]; ok { - cidrMap[nodeSelector] = append(clusterCIDRSetList, clusterCIDRSet) + containsClusterCIDRSet := slices.ContainsFunc(clusterCIDRSetList, func(c *cidrset.ClusterCIDR) bool { + return clusterCIDRSet.Name == c.Name + }) + + if !containsClusterCIDRSet { + cidrMap[nodeSelector] = append(clusterCIDRSetList, clusterCIDRSet) + } } else { cidrMap[nodeSelector] = []*cidrset.ClusterCIDR{clusterCIDRSet} } diff --git a/pkg/controller/ipam/multi_cidr_range_allocator_test.go b/pkg/controller/ipam/multi_cidr_range_allocator_test.go index e4fc210..6739c68 100644 --- a/pkg/controller/ipam/multi_cidr_range_allocator_test.go +++ b/pkg/controller/ipam/multi_cidr_range_allocator_test.go @@ -1693,6 +1693,7 @@ func TestClusterCIDRDefault(t *testing.T) { createdCCC, err := client.NetworkingV1().ClusterCIDRs().Get(context.TODO(), defaultClusterCIDRName, metav1.GetOptions{}) assert.Nil(t, err, "Expected no error getting clustercidr objects") assert.Equal(t, defaultCCC.Spec, createdCCC.Spec) + assert.Contains(t, createdCCC.GetFinalizers(), clusterCIDRFinalizer) } // Ensure SyncClusterCIDR creates a new valid ClusterCIDR. @@ -1772,19 +1773,23 @@ func TestSyncClusterCIDRCreate(t *testing.T) { _, ctx := ktesting.NewTestContext(t) client, cccController := newController(ctx) for _, tc := range tests { - cccController.clusterCIDRStore.Add(tc.ccc) - err := cccController.syncClusterCIDR(ctx, tc.ccc.Name) - if tc.wantErr { - assert.Error(t, err) - continue - } - assert.NoError(t, err) - expectActions(t, client.Actions(), 1, "create", "clustercidrs") + t.Run(tc.name, func(t *testing.T) { + cccController.clusterCIDRStore.Add(tc.ccc) + err := cccController.syncClusterCIDR(ctx, tc.ccc.Name) + + if tc.wantErr { + assert.Error(t, err) + return + } - createdCCC, err := client.NetworkingV1().ClusterCIDRs().Get(context.TODO(), tc.ccc.Name, metav1.GetOptions{}) - require.NoError(t, err, "Expected no error getting clustercidr object") - assert.Equal(t, tc.ccc.Spec, createdCCC.Spec) - assert.Equal(t, []string{clusterCIDRFinalizer}, createdCCC.Finalizers) + assert.NoError(t, err) + expectActions(t, client.Actions(), 1, "create", "clustercidrs") + + createdCCC, err := client.NetworkingV1().ClusterCIDRs().Get(context.TODO(), tc.ccc.Name, metav1.GetOptions{}) + require.NoError(t, err, "Expected no error getting clustercidr object") + assert.Equal(t, tc.ccc.Spec, createdCCC.Spec) + assert.Equal(t, []string{clusterCIDRFinalizer}, createdCCC.Finalizers) + }) } }