@@ -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,41 +106,33 @@ 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[*orcv1alpha1.{{ . }}, orcv1alpha1.{{ . }}](
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
129- if needsReschedule, _ := reconcileStatus.NeedsReschedule(); needsReschedule {
130- return nil, reconcileStatus
117+ var {{ range $i, $dep := .ImportDependencies }}{{ if $i }}, {{ end }}{{ $dep | camelCase }}ID{{ end }} string
118+ {{- range .ImportDependencies }}
119+ {{ $depNameCamelCase := . | camelCase }}
120+ if {{ $depNameCamelCase }} != nil {
121+ {{ $depNameCamelCase }}ID = ptr.Deref({{ $depNameCamelCase }}.Status.ID, "")
131122 }
123+ {{- end }}
132124{{- end }}
133125
134126 listOpts := {{ .GophercloudPackage }}.ListOpts{
135127 Name: string(ptr.Deref(filter.Name, "")),
136128 Description: string(ptr.Deref(filter.Description, "")),
137129{{- range .ImportDependencies }}
138- {{ . }}: ptr.Deref( {{ . | camelCase }}.Status.ID, "") ,
130+ {{ . }}ID : {{ . | camelCase }}ID ,
139131{{- end }}
140132 // TODO(scaffolding): Add more import filters
141133 }
142134
143- return actuator.osClient.List{{ .Kind }}s(ctx, listOpts), nil
135+ return actuator.osClient.List{{ .Kind }}s(ctx, listOpts), {{ if len .ImportDependencies }}reconcileStatus{{ else }} nil{{ end }}
144136}
145137
146138func (actuator {{ .PackageName }}Actuator) CreateResource(ctx context.Context, obj orcObjectPT) (*osResourceT, progress.ReconcileStatus) {
0 commit comments