Skip to content

Commit 6a516d7

Browse files
authored
Merge pull request #621 from eshulman2/depHelper
Cyclomatic complexity: globalize fetch dependency helper
2 parents bbcafed + 153dba0 commit 6a516d7

11 files changed

Lines changed: 206 additions & 322 deletions

File tree

cmd/scaffold-controller/data/controller/actuator.go.template

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ import (
2525

2626
"{{ .GophercloudModule }}"
2727
corev1 "k8s.io/api/core/v1"
28-
{{- if len .ImportDependencies }}
29-
apierrors "k8s.io/apimachinery/pkg/api/errors"
30-
{{- end }}
3128
"k8s.io/utils/ptr"
3229
ctrl "sigs.k8s.io/controller-runtime"
3330
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -37,6 +34,9 @@ import (
3734
"github.com/k-orc/openstack-resource-controller/v2/internal/controllers/generic/progress"
3835
"github.com/k-orc/openstack-resource-controller/v2/internal/logging"
3936
"github.com/k-orc/openstack-resource-controller/v2/internal/osclients"
37+
{{- if len .ImportDependencies }}
38+
"github.com/k-orc/openstack-resource-controller/v2/internal/util/dependency"
39+
{{- end }}
4040
orcerrors "github.com/k-orc/openstack-resource-controller/v2/internal/util/errors"
4141
)
4242

@@ -106,24 +106,12 @@ func (actuator {{ .PackageName }}Actuator) ListOSResourcesForImport(ctx context.
106106
var reconcileStatus progress.ReconcileStatus
107107
{{- range .ImportDependencies }}
108108
{{ $depNameCamelCase := . | camelCase }}
109-
{{ $depNameCamelCase }} := &orcv1alpha1.{{ . }}{}
110-
if filter.{{ . }}Ref != nil {
111-
{{ $depNameCamelCase }}Key := client.ObjectKey{Name: string(*filter.{{ . }}Ref), Namespace: obj.Namespace}
112-
if err := actuator.k8sClient.Get(ctx, {{ $depNameCamelCase }}Key, {{ $depNameCamelCase }}); err != nil {
113-
if apierrors.IsNotFound(err) {
114-
reconcileStatus = reconcileStatus.WithReconcileStatus(
115-
progress.WaitingOnObject("{{ . }}", {{ $depNameCamelCase }}Key.Name, progress.WaitingOnCreation))
116-
} else {
117-
reconcileStatus = reconcileStatus.WithReconcileStatus(
118-
progress.WrapError(fmt.Errorf("fetching {{ $depNameCamelCase }} %s: %w", {{ $depNameCamelCase }}Key.Name, err)))
119-
}
120-
} else {
121-
if !orcv1alpha1.IsAvailable({{ $depNameCamelCase }}) || {{ $depNameCamelCase }}.Status.ID == nil {
122-
reconcileStatus = reconcileStatus.WithReconcileStatus(
123-
progress.WaitingOnObject("{{ . }}", {{ $depNameCamelCase }}Key.Name, progress.WaitingOnReady))
124-
}
125-
}
126-
}
109+
{{ $depNameCamelCase }}, rs := dependency.FetchDependency(
110+
ctx, actuator.k8sClient, obj.Namespace,
111+
filter.{{ . }}Ref, "{{ . }}",
112+
func(dep *orcv1alpha1.{{ . }}) bool { return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil },
113+
)
114+
reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
127115
{{- end }}
128116

129117
if needsReschedule, _ := reconcileStatus.NeedsReschedule(); needsReschedule {
@@ -135,12 +123,12 @@ func (actuator {{ .PackageName }}Actuator) ListOSResourcesForImport(ctx context.
135123
Name: string(ptr.Deref(filter.Name, "")),
136124
Description: string(ptr.Deref(filter.Description, "")),
137125
{{- range .ImportDependencies }}
138-
{{ . }}: ptr.Deref({{ . | camelCase }}.Status.ID, ""),
126+
{{ . }}ID: ptr.Deref({{ . | camelCase }}.Status.ID, ""),
139127
{{- end }}
140128
// TODO(scaffolding): Add more import filters
141129
}
142130

143-
return actuator.osClient.List{{ .Kind }}s(ctx, listOpts), nil
131+
return actuator.osClient.List{{ .Kind }}s(ctx, listOpts), {{ if len .ImportDependencies }}reconcileStatus{{ else }}nil{{ end }}
144132
}
145133

146134
func (actuator {{ .PackageName }}Actuator) CreateResource(ctx context.Context, obj orcObjectPT) (*osResourceT, progress.ReconcileStatus) {

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

0 commit comments

Comments
 (0)