Skip to content

Commit 218212a

Browse files
authored
Merge pull request #2192 from shiftstack/image-api
✨ ORC Image API update
2 parents 19ddf0a + 47063b6 commit 218212a

37 files changed

+1805
-1259
lines changed

controllers/openstackserver_controller.go

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import (
5858
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
5959
capoerrors "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/errors"
6060
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/names"
61-
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/objects"
6261
)
6362

6463
const (
@@ -171,24 +170,33 @@ func patchServer(ctx context.Context, patchHelper *patch.Helper, openStackServer
171170
}
172171

173172
func (r *OpenStackServerReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, _ controller.Options) error {
173+
const imageRefPath = "spec.image.imageRef.name"
174+
174175
log := ctrl.LoggerFrom(ctx)
175176

177+
// Index servers by referenced image
178+
if err := mgr.GetFieldIndexer().IndexField(ctx, &infrav1alpha1.OpenStackServer{}, imageRefPath, func(obj client.Object) []string {
179+
server, ok := obj.(*infrav1alpha1.OpenStackServer)
180+
if !ok {
181+
return nil
182+
}
183+
if server.Spec.Image.ImageRef == nil {
184+
return nil
185+
}
186+
return []string{server.Spec.Image.ImageRef.Name}
187+
}); err != nil {
188+
return fmt.Errorf("adding servers by image index: %w", err)
189+
}
190+
176191
return ctrl.NewControllerManagedBy(mgr).
177192
For(&infrav1alpha1.OpenStackServer{}).
178193
Watches(&orcv1alpha1.Image{},
179194
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request {
180195
log = log.WithValues("watch", "Image")
181196

182197
k8sClient := mgr.GetClient()
183-
label, err := objToDependencyLabel(obj, k8sClient.Scheme())
184-
if err != nil {
185-
log.Error(err, "objToDependencyLabel")
186-
}
187-
188-
labels := map[string]string{label: ""}
189198
serverList := &infrav1alpha1.OpenStackServerList{}
190-
err = k8sClient.List(ctx, serverList, client.MatchingLabels(labels), client.InNamespace(obj.GetNamespace()))
191-
if err != nil {
199+
if err := k8sClient.List(ctx, serverList, client.InNamespace(obj.GetNamespace()), client.MatchingFields{imageRefPath: obj.GetName()}); err != nil {
192200
log.Error(err, "listing OpenStackServers")
193201
return nil
194202
}
@@ -267,27 +275,6 @@ func (r *OpenStackServerReconciler) reconcileDelete(scope *scope.WithLogger, ope
267275
return nil
268276
}
269277

270-
func objToDependencyLabel(obj client.Object, scheme *runtime.Scheme) (string, error) {
271-
gvk, err := objects.GetGVK(obj, scheme)
272-
if err != nil {
273-
return "", err
274-
}
275-
276-
if gvk.Group != orcv1alpha1.SchemeGroupVersion.Group {
277-
return "", fmt.Errorf("dependency object has unexpected group %s", gvk.Group)
278-
}
279-
280-
var prefixType string
281-
switch gvk.Kind {
282-
case "Image":
283-
prefixType = "image"
284-
default:
285-
return "", fmt.Errorf("dependency object has unexpected kind %s", gvk.Kind)
286-
}
287-
288-
return "orc-dependency-" + prefixType + "/" + obj.GetName(), nil
289-
}
290-
291278
func IsServerTerminalError(server *infrav1alpha1.OpenStackServer) bool {
292279
if server.Status.InstanceState != nil && *server.Status.InstanceState == infrav1.InstanceStateError {
293280
return true
@@ -310,31 +297,7 @@ func (r *OpenStackServerReconciler) reconcileNormal(ctx context.Context, scope *
310297
openStackServer.SetLabels(labels)
311298
}
312299

313-
changed, dependencies, resolveDone, err := compute.ResolveServerSpec(ctx, scope, r.Client, openStackServer)
314-
315-
errs := make([]error, 0, len(dependencies)+1)
316-
errs = append(errs, err)
317-
318-
// Add a dependency label for every object we depend on
319-
// Set changed if we update any labels
320-
// Collate all errors and return in a single transaction
321-
for i := range dependencies {
322-
errs = append(errs, func() error {
323-
dependency := dependencies[i]
324-
label, err := objToDependencyLabel(dependency, r.Client.Scheme())
325-
if err != nil {
326-
return err
327-
}
328-
329-
if _, ok := labels[label]; !ok {
330-
labels[label] = ""
331-
changed = true
332-
}
333-
return nil
334-
}())
335-
}
336-
err = errors.Join(errs...)
337-
300+
changed, resolveDone, err := compute.ResolveServerSpec(ctx, scope, r.Client, openStackServer)
338301
if err != nil || !resolveDone {
339302
return ctrl.Result{}, err
340303
}

internal/controllers/image/controller.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ import (
3535
ctrlutil "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/controllers"
3636
)
3737

38+
const (
39+
Finalizer = "openstack.k-orc.cloud/image"
40+
41+
FieldOwner = "openstack.k-orc.cloud/imagecontroller"
42+
// Field owner of the object finalizer.
43+
SSAFinalizerTxn = "finalizer"
44+
// Field owner of transient status.
45+
SSAStatusTxn = "status"
46+
// Field owner of persistent id field.
47+
SSAIDTxn = "id"
48+
)
49+
50+
// ssaFieldOwner returns the field owner for a specific named SSA transaction.
51+
func ssaFieldOwner(txn string) client.FieldOwner {
52+
return client.FieldOwner(FieldOwner + "/" + txn)
53+
}
54+
3855
const (
3956
// The time to wait before reconciling again when we are expecting glance to finish some task and update status.
4057
waitForGlanceImageStatusUpdate = 15 * time.Second

0 commit comments

Comments
 (0)