Skip to content

Commit

Permalink
refactor: support multiple podCIDRs in the node patch
Browse files Browse the repository at this point in the history
Breaking down the metal support into bite-sized chunks.
  • Loading branch information
justinsb committed Jul 30, 2024
1 parent f19c837 commit 839914a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
19 changes: 11 additions & 8 deletions cmd/kops-controller/controllers/awsipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ func (r *AWSIPAMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}

ipv6Address := aws.ToString(eni.NetworkInterfaces[0].Ipv6Prefixes[0].Ipv6Prefix)
if err := patchNodePodCIDRs(r.coreV1Client, ctx, node, ipv6Address); err != nil {
podCIDRs := []string{ipv6Address}
if err := patchNodePodCIDRs(r.coreV1Client, ctx, node, podCIDRs); err != nil {
return ctrl.Result{}, err
}
}
Expand All @@ -162,26 +163,28 @@ type nodePatchSpec struct {
PodCIDRs []string `json:"podCIDRs,omitempty"`
}

// patchNodePodCIDRs patches the node podCIDR to the specified value.
func patchNodePodCIDRs(client *corev1client.CoreV1Client, ctx context.Context, node *corev1.Node, cidr string) error {
klog.Infof("assigning cidr %q to node %q", cidr, node.ObjectMeta.Name)
// patchNodePodCIDRs patches the node podCIDRs to the specified value(s).
func patchNodePodCIDRs(client *corev1client.CoreV1Client, ctx context.Context, node *corev1.Node, podCIDRs []string) error {
klog.Infof("assigning podCIDRs %v to node %q", podCIDRs, node.ObjectMeta.Name)
nodePatchSpec := &nodePatchSpec{
PodCIDR: cidr,
PodCIDRs: []string{cidr},
PodCIDRs: podCIDRs,
}
if len(podCIDRs) > 0 {
nodePatchSpec.PodCIDR = podCIDRs[0]
}
nodePatch := &nodePatch{
Spec: nodePatchSpec,
}
nodePatchJson, err := json.Marshal(nodePatch)
if err != nil {
return fmt.Errorf("error building node patch: %v", err)
return fmt.Errorf("building node patch: %w", err)
}

klog.V(2).Infof("sending patch for node %q: %q", node.Name, string(nodePatchJson))

_, err = client.Nodes().Patch(ctx, node.Name, types.StrategicMergePatchType, nodePatchJson, metav1.PatchOptions{})
if err != nil {
return fmt.Errorf("error applying patch to node: %v", err)
return fmt.Errorf("applying patch to node: %w", err)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion cmd/kops-controller/controllers/gceipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ func (r *GCEIPAMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}

ipv6Address := ipv6Addresses[0]
if err := patchNodePodCIDRs(r.coreV1Client, ctx, node, ipv6Address); err != nil {
podCIDRs := []string{ipv6Address}
if err := patchNodePodCIDRs(r.coreV1Client, ctx, node, podCIDRs); err != nil {
return ctrl.Result{}, err
}
}
Expand Down

0 comments on commit 839914a

Please sign in to comment.