Skip to content

Commit

Permalink
migrate to patch for nnc spec updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rbtr committed Aug 29, 2023
1 parent 0b743ac commit 805e19d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
10 changes: 6 additions & 4 deletions cns/ipampool/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ const (
DefaultRefreshDelay = 1 * time.Second
// DefaultMaxIPs default maximum allocatable IPs
DefaultMaxIPs = 250
// fieldManager is the field manager used when patching the NodeNetworkConfig.
fieldManager = "azure-cns"
// Subnet ARM ID /subscriptions/$(SUB)/resourceGroups/$(GROUP)/providers/Microsoft.Network/virtualNetworks/$(VNET)/subnets/$(SUBNET)
subnetARMIDTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s/subnets/%s"
)

type nodeNetworkConfigSpecUpdater interface {
UpdateSpec(context.Context, *v1alpha.NodeNetworkConfigSpec) (*v1alpha.NodeNetworkConfig, error)
PatchSpec(context.Context, *v1alpha.NodeNetworkConfigSpec, string) (*v1alpha.NodeNetworkConfig, error)
}

// metaState is the Monitor's configuration state for the IP pool.
Expand Down Expand Up @@ -281,7 +283,7 @@ func (pm *Monitor) increasePoolSize(ctx context.Context, meta metaState, state i

logger.Printf("[ipam-pool-monitor] Increasing pool size, pool %+v, spec %+v", state, tempNNCSpec)

if _, err := pm.nnccli.UpdateSpec(ctx, &tempNNCSpec); err != nil {
if _, err := pm.nnccli.PatchSpec(ctx, &tempNNCSpec, fieldManager); err != nil {
// caller will retry to update the CRD again
return errors.Wrap(err, "executing UpdateSpec with NNC client")
}
Expand Down Expand Up @@ -347,7 +349,7 @@ func (pm *Monitor) decreasePoolSize(ctx context.Context, meta metaState, state i
attempts := 0
if err := retry.Do(func() error {
attempts++
_, err := pm.nnccli.UpdateSpec(ctx, &tempNNCSpec)
_, err := pm.nnccli.PatchSpec(ctx, &tempNNCSpec, fieldManager)
if err != nil {
// caller will retry to update the CRD again
logger.Printf("failed to update NNC spec attempt #%d, err: %v", attempts, err)
Expand Down Expand Up @@ -378,7 +380,7 @@ func (pm *Monitor) decreasePoolSize(ctx context.Context, meta metaState, state i
func (pm *Monitor) cleanPendingRelease(ctx context.Context) error {
tempNNCSpec := pm.createNNCSpecForCRD()

_, err := pm.nnccli.UpdateSpec(ctx, &tempNNCSpec)
_, err := pm.nnccli.PatchSpec(ctx, &tempNNCSpec, fieldManager)
if err != nil {
// caller will retry to update the CRD again
return errors.Wrap(err, "executing UpdateSpec with NNC client")
Expand Down
10 changes: 5 additions & 5 deletions cns/ipampool/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ type fakeNodeNetworkConfigUpdater struct {
nnc *v1alpha.NodeNetworkConfig
}

func (f *fakeNodeNetworkConfigUpdater) UpdateSpec(ctx context.Context, spec *v1alpha.NodeNetworkConfigSpec) (*v1alpha.NodeNetworkConfig, error) {
func (f *fakeNodeNetworkConfigUpdater) PatchSpec(_ context.Context, spec *v1alpha.NodeNetworkConfigSpec, _ string) (*v1alpha.NodeNetworkConfig, error) {
f.nnc.Spec = *spec
return f.nnc, nil
}

type fakeNodeNetworkConfigUpdaterFunc func(ctx context.Context, spec *v1alpha.NodeNetworkConfigSpec) (*v1alpha.NodeNetworkConfig, error)
type fakeNodeNetworkConfigUpdaterFunc func(context.Context, *v1alpha.NodeNetworkConfigSpec, string) (*v1alpha.NodeNetworkConfig, error)

func (f fakeNodeNetworkConfigUpdaterFunc) UpdateSpec(ctx context.Context, spec *v1alpha.NodeNetworkConfigSpec) (*v1alpha.NodeNetworkConfig, error) {
return f(ctx, spec)
func (f fakeNodeNetworkConfigUpdaterFunc) PatchSpec(ctx context.Context, spec *v1alpha.NodeNetworkConfigSpec, owner string) (*v1alpha.NodeNetworkConfig, error) {
return f(ctx, spec, owner)
}

type directUpdatePoolMonitor struct {
Expand Down Expand Up @@ -568,7 +568,7 @@ func TestDecreaseWithAPIServerFailure(t *testing.T) {
totalIPs: 64,
max: 250,
}
var errNNCCLi fakeNodeNetworkConfigUpdaterFunc = func(ctx context.Context, spec *v1alpha.NodeNetworkConfigSpec) (*v1alpha.NodeNetworkConfig, error) {
var errNNCCLi fakeNodeNetworkConfigUpdaterFunc = func(context.Context, *v1alpha.NodeNetworkConfigSpec, string) (*v1alpha.NodeNetworkConfig, error) {
return nil, errors.New("fake APIServer failure") //nolint:goerr113 // this is a fake error
}

Expand Down
8 changes: 4 additions & 4 deletions cns/kubecontroller/nodenetworkconfig/scopedclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func (sc *ScopedClient) Get(ctx context.Context) (*v1alpha.NodeNetworkConfig, er
return nnc, errors.Wrapf(err, "failed to get nnc %v", sc.NamespacedName)
}

// UpdateSpec updates the associated NodeNetworkConfig with the passed NodeNetworkConfigSpec.
func (sc *ScopedClient) UpdateSpec(ctx context.Context, spec *v1alpha.NodeNetworkConfigSpec) (*v1alpha.NodeNetworkConfig, error) {
nnc, err := sc.Client.UpdateSpec(ctx, sc.NamespacedName, spec)
return nnc, errors.Wrapf(err, "failed to update nnc %v", sc.NamespacedName)
// PatchSpec updates the associated NodeNetworkConfig with the passed NodeNetworkConfigSpec.
func (sc *ScopedClient) PatchSpec(ctx context.Context, spec *v1alpha.NodeNetworkConfigSpec, fieldManager string) (*v1alpha.NodeNetworkConfig, error) {
nnc, err := sc.Client.PatchSpec(ctx, sc.NamespacedName, spec, fieldManager)
return nnc, errors.Wrapf(err, "failed to patch nnc %v", sc.NamespacedName)
}

0 comments on commit 805e19d

Please sign in to comment.