Skip to content

Commit 153dba0

Browse files
committed
refactor actuators to use global FetchDependency helper
- Remove local getDependencyHelper and wrapper functions from server actuator - Use dependency.FetchDependency directly in CreateResource and ListOSResourcesForImport
1 parent a57831b commit 153dba0

10 files changed

Lines changed: 129 additions & 300 deletions

File tree

internal/controllers/floatingip/actuator.go

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package floatingip
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"iter"
2322

2423
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips"
@@ -27,10 +26,10 @@ import (
2726
"github.com/k-orc/openstack-resource-controller/v2/internal/controllers/generic/progress"
2827
"github.com/k-orc/openstack-resource-controller/v2/internal/logging"
2928
osclients "github.com/k-orc/openstack-resource-controller/v2/internal/osclients"
29+
"github.com/k-orc/openstack-resource-controller/v2/internal/util/dependency"
3030
orcerrors "github.com/k-orc/openstack-resource-controller/v2/internal/util/errors"
3131
"github.com/k-orc/openstack-resource-controller/v2/internal/util/tags"
3232
corev1 "k8s.io/api/core/v1"
33-
apierrors "k8s.io/apimachinery/pkg/api/errors"
3433
"k8s.io/utils/ptr"
3534
ctrl "sigs.k8s.io/controller-runtime"
3635
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -89,62 +88,29 @@ func (actuator floatingipActuator) ListOSResourcesForAdoption(ctx context.Contex
8988
func (actuator floatingipCreateActuator) ListOSResourcesForImport(ctx context.Context, obj orcObjectPT, filter filterT) (iter.Seq2[*osResourceT, error], progress.ReconcileStatus) {
9089
var reconcileStatus progress.ReconcileStatus
9190

92-
network := &orcv1alpha1.Network{}
93-
if filter.FloatingNetworkRef != nil {
94-
networkKey := client.ObjectKey{Name: string(ptr.Deref(filter.FloatingNetworkRef, "")), Namespace: obj.Namespace}
95-
if err := actuator.k8sClient.Get(ctx, networkKey, network); err != nil {
96-
if apierrors.IsNotFound(err) {
97-
reconcileStatus = reconcileStatus.WithReconcileStatus(
98-
progress.WaitingOnObject("Network", networkKey.Name, progress.WaitingOnCreation))
99-
} else {
100-
reconcileStatus = reconcileStatus.WithReconcileStatus(
101-
progress.WrapError(fmt.Errorf("fetching network %s: %w", networkKey.Name, err)))
102-
}
103-
} else {
104-
if !orcv1alpha1.IsAvailable(network) || network.Status.ID == nil {
105-
reconcileStatus = reconcileStatus.WithReconcileStatus(
106-
progress.WaitingOnObject("Network", networkKey.Name, progress.WaitingOnReady))
107-
}
108-
}
109-
}
110-
111-
port := &orcv1alpha1.Port{}
112-
if filter.PortRef != nil {
113-
portKey := client.ObjectKey{Name: string(ptr.Deref(filter.PortRef, "")), Namespace: obj.Namespace}
114-
if err := actuator.k8sClient.Get(ctx, portKey, port); err != nil {
115-
if apierrors.IsNotFound(err) {
116-
reconcileStatus = reconcileStatus.WithReconcileStatus(
117-
progress.WaitingOnObject("Port", portKey.Name, progress.WaitingOnCreation))
118-
} else {
119-
reconcileStatus = reconcileStatus.WithReconcileStatus(
120-
progress.WrapError(fmt.Errorf("fetching port %s: %w", portKey.Name, err)))
121-
}
122-
} else {
123-
if !orcv1alpha1.IsAvailable(port) || port.Status.ID == nil {
124-
reconcileStatus = reconcileStatus.WithReconcileStatus(
125-
progress.WaitingOnObject("Port", portKey.Name, progress.WaitingOnReady))
126-
}
127-
}
128-
}
129-
130-
project := &orcv1alpha1.Project{}
131-
if filter.ProjectRef != nil {
132-
projectKey := client.ObjectKey{Name: string(*filter.ProjectRef), Namespace: obj.Namespace}
133-
if err := actuator.k8sClient.Get(ctx, projectKey, project); err != nil {
134-
if apierrors.IsNotFound(err) {
135-
reconcileStatus = reconcileStatus.WithReconcileStatus(
136-
progress.WaitingOnObject("Project", projectKey.Name, progress.WaitingOnCreation))
137-
} else {
138-
reconcileStatus = reconcileStatus.WithReconcileStatus(
139-
progress.WrapError(fmt.Errorf("fetching project %s: %w", projectKey.Name, err)))
140-
}
141-
} else {
142-
if !orcv1alpha1.IsAvailable(project) || project.Status.ID == nil {
143-
reconcileStatus = reconcileStatus.WithReconcileStatus(
144-
progress.WaitingOnObject("Project", projectKey.Name, progress.WaitingOnReady))
145-
}
146-
}
147-
}
91+
network, rs := dependency.FetchDependency(
92+
ctx, actuator.k8sClient, obj.Namespace, filter.FloatingNetworkRef, "Network",
93+
func(dep *orcv1alpha1.Network) bool {
94+
return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil
95+
},
96+
)
97+
reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
98+
99+
port, rs := dependency.FetchDependency(
100+
ctx, actuator.k8sClient, obj.Namespace, filter.PortRef, "Port",
101+
func(dep *orcv1alpha1.Port) bool {
102+
return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil
103+
},
104+
)
105+
reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
106+
107+
project, rs := dependency.FetchDependency(
108+
ctx, actuator.k8sClient, obj.Namespace, filter.ProjectRef, "Project",
109+
func(dep *orcv1alpha1.Project) bool {
110+
return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil
111+
},
112+
)
113+
reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
148114

149115
if needsReschedule, _ := reconcileStatus.NeedsReschedule(); needsReschedule {
150116
return nil, reconcileStatus

internal/controllers/group/actuator.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ package group
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"iter"
2322

2423
"github.com/gophercloud/gophercloud/v2/openstack/identity/v3/groups"
2524
corev1 "k8s.io/api/core/v1"
26-
apierrors "k8s.io/apimachinery/pkg/api/errors"
2725
"k8s.io/utils/ptr"
2826
ctrl "sigs.k8s.io/controller-runtime"
2927
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -33,6 +31,7 @@ import (
3331
"github.com/k-orc/openstack-resource-controller/v2/internal/controllers/generic/progress"
3432
"github.com/k-orc/openstack-resource-controller/v2/internal/logging"
3533
"github.com/k-orc/openstack-resource-controller/v2/internal/osclients"
34+
"github.com/k-orc/openstack-resource-controller/v2/internal/util/dependency"
3635
orcerrors "github.com/k-orc/openstack-resource-controller/v2/internal/util/errors"
3736
)
3837

@@ -83,24 +82,13 @@ func (actuator groupActuator) ListOSResourcesForImport(ctx context.Context, obj
8382

8483
var reconcileStatus progress.ReconcileStatus
8584

86-
domain := &orcv1alpha1.Domain{}
87-
if filter.DomainRef != nil {
88-
domainKey := client.ObjectKey{Name: string(*filter.DomainRef), Namespace: obj.Namespace}
89-
if err := actuator.k8sClient.Get(ctx, domainKey, domain); err != nil {
90-
if apierrors.IsNotFound(err) {
91-
reconcileStatus = reconcileStatus.WithReconcileStatus(
92-
progress.WaitingOnObject("Domain", domainKey.Name, progress.WaitingOnCreation))
93-
} else {
94-
reconcileStatus = reconcileStatus.WithReconcileStatus(
95-
progress.WrapError(fmt.Errorf("fetching domain %s: %w", domainKey.Name, err)))
96-
}
97-
} else {
98-
if !orcv1alpha1.IsAvailable(domain) || domain.Status.ID == nil {
99-
reconcileStatus = reconcileStatus.WithReconcileStatus(
100-
progress.WaitingOnObject("Domain", domainKey.Name, progress.WaitingOnReady))
101-
}
102-
}
103-
}
85+
domain, rs := dependency.FetchDependency(
86+
ctx, actuator.k8sClient, obj.Namespace, filter.DomainRef, "Domain",
87+
func(dep *orcv1alpha1.Domain) bool {
88+
return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil
89+
},
90+
)
91+
reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
10492

10593
if needsReschedule, _ := reconcileStatus.NeedsReschedule(); needsReschedule {
10694
return nil, reconcileStatus

internal/controllers/network/actuator.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package network
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"iter"
2322

2423
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/dns"
@@ -27,7 +26,6 @@ import (
2726
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/portsecurity"
2827
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/networks"
2928
corev1 "k8s.io/api/core/v1"
30-
apierrors "k8s.io/apimachinery/pkg/api/errors"
3129
"k8s.io/utils/ptr"
3230
ctrl "sigs.k8s.io/controller-runtime"
3331
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -37,6 +35,7 @@ import (
3735
"github.com/k-orc/openstack-resource-controller/v2/internal/controllers/generic/progress"
3836
"github.com/k-orc/openstack-resource-controller/v2/internal/logging"
3937
"github.com/k-orc/openstack-resource-controller/v2/internal/osclients"
38+
"github.com/k-orc/openstack-resource-controller/v2/internal/util/dependency"
4039
orcerrors "github.com/k-orc/openstack-resource-controller/v2/internal/util/errors"
4140
"github.com/k-orc/openstack-resource-controller/v2/internal/util/tags"
4241
)
@@ -84,24 +83,13 @@ func (actuator networkActuator) ListOSResourcesForAdoption(ctx context.Context,
8483
func (actuator networkActuator) ListOSResourcesForImport(ctx context.Context, obj orcObjectPT, filter filterT) (iter.Seq2[*osResourceT, error], progress.ReconcileStatus) {
8584
var reconcileStatus progress.ReconcileStatus
8685

87-
project := &orcv1alpha1.Project{}
88-
if filter.ProjectRef != nil {
89-
projectKey := client.ObjectKey{Name: string(*filter.ProjectRef), Namespace: obj.Namespace}
90-
if err := actuator.k8sClient.Get(ctx, projectKey, project); err != nil {
91-
if apierrors.IsNotFound(err) {
92-
reconcileStatus = reconcileStatus.WithReconcileStatus(
93-
progress.WaitingOnObject("Project", projectKey.Name, progress.WaitingOnCreation))
94-
} else {
95-
reconcileStatus = reconcileStatus.WithReconcileStatus(
96-
progress.WrapError(fmt.Errorf("fetching project %s: %w", projectKey.Name, err)))
97-
}
98-
} else {
99-
if !orcv1alpha1.IsAvailable(project) || project.Status.ID == nil {
100-
reconcileStatus = reconcileStatus.WithReconcileStatus(
101-
progress.WaitingOnObject("Project", projectKey.Name, progress.WaitingOnReady))
102-
}
103-
}
104-
}
86+
project, rs := dependency.FetchDependency(
87+
ctx, actuator.k8sClient, obj.Namespace, filter.ProjectRef, "Project",
88+
func(dep *orcv1alpha1.Project) bool {
89+
return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil
90+
},
91+
)
92+
reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
10593

10694
if needsReschedule, _ := reconcileStatus.NeedsReschedule(); needsReschedule {
10795
return nil, reconcileStatus

internal/controllers/port/actuator.go

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/portsecurity"
2828
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports"
2929
corev1 "k8s.io/api/core/v1"
30-
apierrors "k8s.io/apimachinery/pkg/api/errors"
3130
"k8s.io/utils/ptr"
3231
ctrl "sigs.k8s.io/controller-runtime"
3332
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -37,6 +36,7 @@ import (
3736
"github.com/k-orc/openstack-resource-controller/v2/internal/controllers/generic/progress"
3837
"github.com/k-orc/openstack-resource-controller/v2/internal/logging"
3938
osclients "github.com/k-orc/openstack-resource-controller/v2/internal/osclients"
39+
"github.com/k-orc/openstack-resource-controller/v2/internal/util/dependency"
4040
orcerrors "github.com/k-orc/openstack-resource-controller/v2/internal/util/errors"
4141
"github.com/k-orc/openstack-resource-controller/v2/internal/util/tags"
4242
)
@@ -89,43 +89,21 @@ func (actuator portActuator) ListOSResourcesForAdoption(ctx context.Context, obj
8989
func (actuator portActuator) ListOSResourcesForImport(ctx context.Context, obj orcObjectPT, filter filterT) (iter.Seq2[*osResourceT, error], progress.ReconcileStatus) {
9090
var reconcileStatus progress.ReconcileStatus
9191

92-
network := &orcv1alpha1.Network{}
93-
if filter.NetworkRef != "" {
94-
networkKey := client.ObjectKey{Name: string(filter.NetworkRef), Namespace: obj.Namespace}
95-
if err := actuator.k8sClient.Get(ctx, networkKey, network); err != nil {
96-
if apierrors.IsNotFound(err) {
97-
reconcileStatus = reconcileStatus.WithReconcileStatus(
98-
progress.WaitingOnObject("Network", networkKey.Name, progress.WaitingOnCreation))
99-
} else {
100-
reconcileStatus = reconcileStatus.WithReconcileStatus(
101-
progress.WrapError(fmt.Errorf("fetching network %s: %w", networkKey.Name, err)))
102-
}
103-
} else {
104-
if !orcv1alpha1.IsAvailable(network) || network.Status.ID == nil {
105-
reconcileStatus = reconcileStatus.WithReconcileStatus(
106-
progress.WaitingOnObject("Network", networkKey.Name, progress.WaitingOnReady))
107-
}
108-
}
109-
}
92+
network, rs := dependency.FetchDependency(
93+
ctx, actuator.k8sClient, obj.Namespace, &filter.NetworkRef, "Network",
94+
func(dep *orcv1alpha1.Network) bool {
95+
return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil
96+
},
97+
)
98+
reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
11099

111-
project := &orcv1alpha1.Project{}
112-
if filter.ProjectRef != nil {
113-
projectKey := client.ObjectKey{Name: string(*filter.ProjectRef), Namespace: obj.Namespace}
114-
if err := actuator.k8sClient.Get(ctx, projectKey, project); err != nil {
115-
if apierrors.IsNotFound(err) {
116-
reconcileStatus = reconcileStatus.WithReconcileStatus(
117-
progress.WaitingOnObject("Project", projectKey.Name, progress.WaitingOnCreation))
118-
} else {
119-
reconcileStatus = reconcileStatus.WithReconcileStatus(
120-
progress.WrapError(fmt.Errorf("fetching project %s: %w", projectKey.Name, err)))
121-
}
122-
} else {
123-
if !orcv1alpha1.IsAvailable(project) || project.Status.ID == nil {
124-
reconcileStatus = reconcileStatus.WithReconcileStatus(
125-
progress.WaitingOnObject("Project", projectKey.Name, progress.WaitingOnReady))
126-
}
127-
}
128-
}
100+
project, rs := dependency.FetchDependency(
101+
ctx, actuator.k8sClient, obj.Namespace, filter.ProjectRef, "Project",
102+
func(dep *orcv1alpha1.Project) bool {
103+
return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil
104+
},
105+
)
106+
reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
129107

130108
if needsReschedule, _ := reconcileStatus.NeedsReschedule(); needsReschedule {
131109
return nil, reconcileStatus

internal/controllers/role/actuator.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ package role
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"iter"
2322

2423
"github.com/gophercloud/gophercloud/v2/openstack/identity/v3/roles"
24+
"github.com/k-orc/openstack-resource-controller/v2/internal/util/dependency"
2525
corev1 "k8s.io/api/core/v1"
26-
apierrors "k8s.io/apimachinery/pkg/api/errors"
2726
"k8s.io/utils/ptr"
2827
ctrl "sigs.k8s.io/controller-runtime"
2928
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -82,24 +81,13 @@ func (actuator roleActuator) ListOSResourcesForAdoption(ctx context.Context, orc
8281
func (actuator roleActuator) ListOSResourcesForImport(ctx context.Context, obj orcObjectPT, filter filterT) (iter.Seq2[*osResourceT, error], progress.ReconcileStatus) {
8382
var reconcileStatus progress.ReconcileStatus
8483

85-
domain := &orcv1alpha1.Domain{}
86-
if filter.DomainRef != nil {
87-
domainKey := client.ObjectKey{Name: string(*filter.DomainRef), Namespace: obj.Namespace}
88-
if err := actuator.k8sClient.Get(ctx, domainKey, domain); err != nil {
89-
if apierrors.IsNotFound(err) {
90-
reconcileStatus = reconcileStatus.WithReconcileStatus(
91-
progress.WaitingOnObject("Domain", domainKey.Name, progress.WaitingOnCreation))
92-
} else {
93-
reconcileStatus = reconcileStatus.WithReconcileStatus(
94-
progress.WrapError(fmt.Errorf("fetching domain %s: %w", domainKey.Name, err)))
95-
}
96-
} else {
97-
if !orcv1alpha1.IsAvailable(domain) || domain.Status.ID == nil {
98-
reconcileStatus = reconcileStatus.WithReconcileStatus(
99-
progress.WaitingOnObject("Domain", domainKey.Name, progress.WaitingOnReady))
100-
}
101-
}
102-
}
84+
domain, rs := dependency.FetchDependency(
85+
ctx, actuator.k8sClient, obj.Namespace, filter.DomainRef, "Domain",
86+
func(dep *orcv1alpha1.Domain) bool {
87+
return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil
88+
},
89+
)
90+
reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
10391

10492
if needsReschedule, _ := reconcileStatus.NeedsReschedule(); needsReschedule {
10593
return nil, reconcileStatus

internal/controllers/router/actuator.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ package router
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"iter"
2322

2423
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/routers"
2524
corev1 "k8s.io/api/core/v1"
26-
apierrors "k8s.io/apimachinery/pkg/api/errors"
2725
"k8s.io/utils/ptr"
2826
ctrl "sigs.k8s.io/controller-runtime"
2927
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -33,6 +31,7 @@ import (
3331
"github.com/k-orc/openstack-resource-controller/v2/internal/controllers/generic/progress"
3432
"github.com/k-orc/openstack-resource-controller/v2/internal/logging"
3533
osclients "github.com/k-orc/openstack-resource-controller/v2/internal/osclients"
34+
"github.com/k-orc/openstack-resource-controller/v2/internal/util/dependency"
3635
orcerrors "github.com/k-orc/openstack-resource-controller/v2/internal/util/errors"
3736
"github.com/k-orc/openstack-resource-controller/v2/internal/util/tags"
3837
)
@@ -84,24 +83,13 @@ func (actuator routerActuator) ListOSResourcesForAdoption(ctx context.Context, o
8483
func (actuator routerCreateActuator) ListOSResourcesForImport(ctx context.Context, obj orcObjectPT, filter filterT) (iter.Seq2[*osResourceT, error], progress.ReconcileStatus) {
8584
var reconcileStatus progress.ReconcileStatus
8685

87-
project := &orcv1alpha1.Project{}
88-
if filter.ProjectRef != nil {
89-
projectKey := client.ObjectKey{Name: string(*filter.ProjectRef), Namespace: obj.Namespace}
90-
if err := actuator.k8sClient.Get(ctx, projectKey, project); err != nil {
91-
if apierrors.IsNotFound(err) {
92-
reconcileStatus = reconcileStatus.WithReconcileStatus(
93-
progress.WaitingOnObject("Project", projectKey.Name, progress.WaitingOnCreation))
94-
} else {
95-
reconcileStatus = reconcileStatus.WithReconcileStatus(
96-
progress.WrapError(fmt.Errorf("fetching project %s: %w", projectKey.Name, err)))
97-
}
98-
} else {
99-
if !orcv1alpha1.IsAvailable(project) || project.Status.ID == nil {
100-
reconcileStatus = reconcileStatus.WithReconcileStatus(
101-
progress.WaitingOnObject("Project", projectKey.Name, progress.WaitingOnReady))
102-
}
103-
}
104-
}
86+
project, rs := dependency.FetchDependency(
87+
ctx, actuator.k8sClient, obj.Namespace, filter.ProjectRef, "Project",
88+
func(dep *orcv1alpha1.Project) bool {
89+
return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil
90+
},
91+
)
92+
reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
10593

10694
if needsReschedule, _ := reconcileStatus.NeedsReschedule(); needsReschedule {
10795
return nil, reconcileStatus

0 commit comments

Comments
 (0)