Skip to content

Commit 37c3c69

Browse files
committed
reconcile maasmachinetemplate static ip
1 parent e3c46b1 commit 37c3c69

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

controllers/maasmachine_controller.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type MaasMachineReconciler struct {
4444
}
4545

4646
// +kubebuilder:rbac:groups=controlplane.cluster.x-k8s.io,resources=kubeadmcontrolplanes,verbs=get;list;watch
47-
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=maasmachinetemplates,verbs=get;list;watch
47+
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=maasmachinetemplates,verbs=get;list;watch;update;patch
4848
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=maasmachines,verbs=get;list;watch;update;patch
4949
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=maasmachines/status,verbs=get;update;patch
5050
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines;machines/status,verbs=get;list;watch
@@ -177,6 +177,31 @@ func (r *MaasMachineReconciler) reconcileMaasMachineIPAddress(cluster *capi.Clus
177177
maasMachine.Spec.StaticIP = &infrav1.StaticIPConfig{}
178178
}
179179

180+
// Update the MaasMachineTemplate with the IP address
181+
vmTemplateName, ok := maasMachine.GetAnnotations()[capi.TemplateClonedFromNameAnnotation]
182+
if ok {
183+
maasMachineTemplate := &infrav1.MaasMachineTemplate{}
184+
key := types.NamespacedName{Namespace: maasMachine.Namespace, Name: vmTemplateName}
185+
186+
if err := r.Get(context.TODO(), key, maasMachineTemplate); err == nil {
187+
// Check if template already has the correct IP
188+
if maasMachineTemplate.Spec.Template.Spec.StaticIP == nil ||
189+
maasMachineTemplate.Spec.Template.Spec.StaticIP.IP != ipAddr {
190+
templatePatch := client.MergeFrom(maasMachineTemplate.DeepCopy())
191+
192+
// Initialize StaticIP if nil
193+
if maasMachineTemplate.Spec.Template.Spec.StaticIP == nil {
194+
maasMachineTemplate.Spec.Template.Spec.StaticIP = &infrav1.StaticIPConfig{}
195+
}
196+
197+
maasMachineTemplate.Spec.Template.Spec.StaticIP.IP = ipAddr
198+
199+
if err := r.Patch(context.TODO(), maasMachineTemplate, templatePatch); err != nil {
200+
log.Error(err, "failed to patch MaasMachineTemplate with static IP")
201+
}
202+
}
203+
}
204+
}
180205
// Set the allocated IP address
181206
maasMachine.Spec.StaticIP.IP = ipAddr
182207

0 commit comments

Comments
 (0)