@@ -46,15 +46,11 @@ type (
4646)
4747
4848type floatingipActuator struct {
49- osClient osclients.NetworkClient
50- }
51-
52- type floatingipCreateActuator struct {
53- floatingipActuator
49+ osClient osclients.NetworkClient
5450 k8sClient client.Client
5551}
5652
57- var _ createResourceActuator = floatingipCreateActuator {}
53+ var _ createResourceActuator = floatingipActuator {}
5854var _ deleteResourceActuator = floatingipActuator {}
5955
6056func (floatingipActuator ) GetResourceID (osResource * osResourceT ) string {
@@ -70,22 +66,69 @@ func (actuator floatingipActuator) GetOSResourceByID(ctx context.Context, id str
7066}
7167
7268func (actuator floatingipActuator ) ListOSResourcesForAdoption (ctx context.Context , obj * orcv1alpha1.FloatingIP ) (floatingipIterator , bool ) {
73- if obj .Spec .Resource == nil {
69+ resource := obj .Spec .Resource
70+ if resource == nil {
7471 return nil , false
7572 }
7673 // we only support adoption of floatingips by IP as they don't have name
77- if obj . Spec . Resource .FloatingIP == nil {
74+ if resource .FloatingIP == nil {
7875 return nil , false
7976 }
8077
78+ // Resolve the floating network ID from either FloatingNetworkRef or
79+ // FloatingSubnetRef. Exactly one of these must be set per API
80+ // validation. Without the network ID, adoption could match a floating
81+ // IP on the wrong network.
82+ var floatingNetworkID string
83+ if resource .FloatingNetworkRef != nil {
84+ network , rs := dependency .FetchDependency (
85+ ctx , actuator .k8sClient , obj .Namespace , resource .FloatingNetworkRef , "Network" ,
86+ func (dep * orcv1alpha1.Network ) bool {
87+ return orcv1alpha1 .IsAvailable (dep ) && dep .Status .ID != nil
88+ },
89+ )
90+ if needsReschedule , _ := rs .NeedsReschedule (); needsReschedule {
91+ return nil , false
92+ }
93+ floatingNetworkID = ptr .Deref (network .Status .ID , "" )
94+ } else if resource .FloatingSubnetRef != nil {
95+ subnet , rs := dependency .FetchDependency (
96+ ctx , actuator .k8sClient , obj .Namespace , resource .FloatingSubnetRef , "Subnet" ,
97+ func (dep * orcv1alpha1.Subnet ) bool {
98+ return orcv1alpha1 .IsAvailable (dep ) && dep .Status .ID != nil && dep .Status .Resource != nil
99+ },
100+ )
101+ if needsReschedule , _ := rs .NeedsReschedule (); needsReschedule {
102+ return nil , false
103+ }
104+ floatingNetworkID = subnet .Status .Resource .NetworkID
105+ }
106+
107+ // Resolve the project ID from ProjectRef if set.
108+ var projectID string
109+ if resource .ProjectRef != nil {
110+ project , rs := dependency .FetchDependency (
111+ ctx , actuator .k8sClient , obj .Namespace , resource .ProjectRef , "Project" ,
112+ func (dep * orcv1alpha1.Project ) bool {
113+ return orcv1alpha1 .IsAvailable (dep ) && dep .Status .ID != nil
114+ },
115+ )
116+ if needsReschedule , _ := rs .NeedsReschedule (); needsReschedule {
117+ return nil , false
118+ }
119+ projectID = ptr .Deref (project .Status .ID , "" )
120+ }
121+
81122 listOpts := floatingips.ListOpts {
82- FloatingIP : string (ptr .Deref (obj .Spec .Resource .FloatingIP , "" )),
83- Tags : tags .Join (obj .Spec .Resource .Tags ),
123+ FloatingIP : string (ptr .Deref (resource .FloatingIP , "" )),
124+ FloatingNetworkID : floatingNetworkID ,
125+ ProjectID : projectID ,
126+ Tags : tags .Join (resource .Tags ),
84127 }
85128 return actuator .osClient .ListFloatingIP (ctx , listOpts ), true
86129}
87130
88- func (actuator floatingipCreateActuator ) ListOSResourcesForImport (ctx context.Context , obj orcObjectPT , filter filterT ) (iter.Seq2 [* osResourceT , error ], progress.ReconcileStatus ) {
131+ func (actuator floatingipActuator ) ListOSResourcesForImport (ctx context.Context , obj orcObjectPT , filter filterT ) (iter.Seq2 [* osResourceT , error ], progress.ReconcileStatus ) {
89132 var reconcileStatus progress.ReconcileStatus
90133
91134 network , rs := dependency .FetchDependency [* orcv1alpha1.Network ](
@@ -126,7 +169,7 @@ func (actuator floatingipCreateActuator) ListOSResourcesForImport(ctx context.Co
126169 return actuator .osClient .ListFloatingIP (ctx , listOpts ), nil
127170}
128171
129- func (actuator floatingipCreateActuator ) CreateResource (ctx context.Context , obj * orcv1alpha1.FloatingIP ) (* osResourceT , progress.ReconcileStatus ) {
172+ func (actuator floatingipActuator ) CreateResource (ctx context.Context , obj * orcv1alpha1.FloatingIP ) (* osResourceT , progress.ReconcileStatus ) {
130173 resource := obj .Spec .Resource
131174 if resource == nil {
132175 // Should have been caught by API validation
@@ -290,7 +333,7 @@ func (floatingipHelperFactory) NewAPIObjectAdapter(obj orcObjectPT) adapterI {
290333}
291334
292335func (floatingipHelperFactory ) NewCreateActuator (ctx context.Context , orcObject orcObjectPT , controller interfaces.ResourceController ) (createResourceActuator , progress.ReconcileStatus ) {
293- return newCreateActuator (ctx , orcObject , controller )
336+ return newActuator (ctx , orcObject , controller )
294337}
295338
296339func (floatingipHelperFactory ) NewDeleteActuator (ctx context.Context , orcObject orcObjectPT , controller interfaces.ResourceController ) (deleteResourceActuator , progress.ReconcileStatus ) {
@@ -316,18 +359,7 @@ func newActuator(ctx context.Context, orcObject *orcv1alpha1.FloatingIP, control
316359 }
317360
318361 return floatingipActuator {
319- osClient : osClient ,
320- }, nil
321- }
322-
323- func newCreateActuator (ctx context.Context , orcObject * orcv1alpha1.FloatingIP , controller interfaces.ResourceController ) (floatingipCreateActuator , progress.ReconcileStatus ) {
324- floatingipActuator , reconcileStatus := newActuator (ctx , orcObject , controller )
325- if needsReschedule , _ := reconcileStatus .NeedsReschedule (); needsReschedule {
326- return floatingipCreateActuator {}, reconcileStatus
327- }
328-
329- return floatingipCreateActuator {
330- floatingipActuator : floatingipActuator ,
331- k8sClient : controller .GetK8sClient (),
362+ osClient : osClient ,
363+ k8sClient : controller .GetK8sClient (),
332364 }, nil
333365}
0 commit comments