@@ -19,11 +19,12 @@ package controllers
1919import (
2020 "context"
2121 "fmt"
22- "k8s.io/utils/pointer"
2322 "math/rand"
2423 "reflect"
2524 "regexp"
2625
26+ "k8s.io/utils/pointer"
27+
2728 "github.com/pkg/errors"
2829 corev1 "k8s.io/api/core/v1"
2930 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -135,18 +136,32 @@ func (r *CloudStackMachineReconciliationRunner) ConsiderAffinity() (ctrl.Result,
135136 r .ReconciliationSubject .Spec .Affinity == "" { // No managed affinity.
136137 return ctrl.Result {}, nil
137138 }
139+ var agName string
140+ var err error
138141
139- agName , err := utils .GenerateAffinityGroupName (* r .ReconciliationSubject , r .CAPIMachine )
140- if err != nil {
141- return ctrl.Result {}, err
142+ if r .ReconciliationSubject .Spec .AffinityGroupRef != nil {
143+ agName = r .ReconciliationSubject .Spec .AffinityGroupRef .Name
144+ } else {
145+ agName , err = utils .GenerateAffinityGroupName (* r .ReconciliationSubject , r .CAPIMachine , r .CAPICluster )
146+ if err != nil {
147+ return ctrl.Result {}, err
148+ }
142149 }
143150
144151 // Set failure domain name and owners.
145152 r .AffinityGroup .Spec .FailureDomainName = r .ReconciliationSubject .Spec .FailureDomainName
146- if res , err := r .GetOrCreateAffinityGroup (
147- agName , r .ReconciliationSubject .Spec .Affinity , r .AffinityGroup , r .FailureDomain )(); r .ShouldReturn (res , err ) {
153+ res , err := r .GetOrCreateAffinityGroup (
154+ agName , r .ReconciliationSubject .Spec .Affinity , r .AffinityGroup , r .FailureDomain )()
155+ if r .ShouldReturn (res , err ) {
148156 return res , err
149157 }
158+ // Set affinity group reference.
159+ r .ReconciliationSubject .Spec .AffinityGroupRef = & corev1.ObjectReference {
160+ Kind : r .AffinityGroup .Kind ,
161+ UID : r .AffinityGroup .UID ,
162+ Name : r .AffinityGroup .Name ,
163+ Namespace : r .AffinityGroup .Namespace ,
164+ }
150165 if ! r .AffinityGroup .Status .Ready {
151166 return r .RequeueWithMessage ("Required affinity group not ready." )
152167 }
0 commit comments