|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/go-logr/logr" |
| 8 | + catalogd "github.com/operator-framework/catalogd/pkg/apis/core/v1beta1" |
| 9 | + operatorv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" |
| 10 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 17 | +) |
| 18 | + |
| 19 | +// Predicate for reconciling operators when available (i.e. ready) catalogsources on cluster change |
| 20 | +type catalogReadyTransitionPredicate struct { |
| 21 | + predicate.Funcs |
| 22 | + catalogReady map[string]bool |
| 23 | +} |
| 24 | + |
| 25 | +func newCatalogReadyTransitionPredicate() *catalogReadyTransitionPredicate { |
| 26 | + return &catalogReadyTransitionPredicate{ |
| 27 | + catalogReady: map[string]bool{}, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func (c *catalogReadyTransitionPredicate) Create(e event.CreateEvent) bool { |
| 32 | + fmt.Println("CreateEvent CatalogSource", e.Object.GetName()) |
| 33 | + catalogReady, err := isCatalogReady(e.Object) |
| 34 | + if err != nil { |
| 35 | + fmt.Println(err) |
| 36 | + return false |
| 37 | + } |
| 38 | + c.catalogReady[e.Object.GetName()] = catalogReady |
| 39 | + return catalogReady |
| 40 | +} |
| 41 | + |
| 42 | +func (c *catalogReadyTransitionPredicate) Update(e event.UpdateEvent) bool { |
| 43 | + fmt.Println("UpdateEvent CatalogSource", e.ObjectOld.GetName(), e.ObjectNew.GetName()) |
| 44 | + oldCatalogReady, err := isCatalogReady(e.ObjectOld) |
| 45 | + if err != nil { |
| 46 | + fmt.Println(err) |
| 47 | + return false |
| 48 | + } |
| 49 | + |
| 50 | + newCatalogReady, err := isCatalogReady(e.ObjectNew) |
| 51 | + if err != nil { |
| 52 | + fmt.Println(err) |
| 53 | + return false |
| 54 | + } |
| 55 | + |
| 56 | + c.catalogReady[e.ObjectNew.GetName()] = newCatalogReady |
| 57 | + // TODO: determine if ready -> non-ready transition triggers reconcile with stale catalog contents |
| 58 | + return oldCatalogReady != newCatalogReady |
| 59 | +} |
| 60 | + |
| 61 | +func (c *catalogReadyTransitionPredicate) Delete(e event.DeleteEvent) bool { |
| 62 | + fmt.Println("DeleteEvent CatalogSource", e.Object.GetName()) |
| 63 | + delete(c.catalogReady, e.Object.GetName()) |
| 64 | + return true |
| 65 | +} |
| 66 | + |
| 67 | +func (c *catalogReadyTransitionPredicate) Generic(e event.GenericEvent) bool { |
| 68 | + fmt.Println("GenericEvent CatalogSource", e.Object.GetName()) |
| 69 | + catalogReady, err := isCatalogReady(e.Object) |
| 70 | + if err != nil { |
| 71 | + fmt.Println(err) |
| 72 | + return false |
| 73 | + } |
| 74 | + predicateState := c.catalogReady[e.Object.GetName()] != catalogReady |
| 75 | + c.catalogReady[e.Object.GetName()] = catalogReady |
| 76 | + return predicateState |
| 77 | +} |
| 78 | + |
| 79 | +func isCatalogReady(o client.Object) (bool, error) { |
| 80 | + catalog, ok := o.(*catalogd.CatalogSource) |
| 81 | + if !ok { |
| 82 | + return false, fmt.Errorf("wrong object type: not a catalogsource: %+v", o) |
| 83 | + } |
| 84 | + if len(catalog.Status.Conditions) > 0 { |
| 85 | + for _, cond := range catalog.Status.Conditions { |
| 86 | + if cond.Type == catalogd.TypeReady && cond.Status == v1.ConditionTrue { |
| 87 | + return true, nil |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return false, nil |
| 92 | +} |
| 93 | + |
| 94 | +// Generate reconcile requests for all operators affected by a catalog change |
| 95 | +func operatorRequestsForCatalog(ctx context.Context, c client.Client, logger logr.Logger) handler.MapFunc { |
| 96 | + return func(object client.Object) []reconcile.Request { |
| 97 | + // no way of associating an operator to a catalog so create reconcile requests for everything |
| 98 | + operators := operatorv1alpha1.OperatorList{} |
| 99 | + err := c.List(ctx, &operators) |
| 100 | + if err != nil { |
| 101 | + logger.Error(err, "unable to enqueue operators for catalog reconcile") |
| 102 | + return nil |
| 103 | + } |
| 104 | + var requests []reconcile.Request |
| 105 | + for _, op := range operators.Items { |
| 106 | + requests = append(requests, reconcile.Request{ |
| 107 | + NamespacedName: types.NamespacedName{ |
| 108 | + Namespace: op.GetNamespace(), |
| 109 | + Name: op.GetName(), |
| 110 | + }, |
| 111 | + }) |
| 112 | + } |
| 113 | + return requests |
| 114 | + } |
| 115 | +} |
0 commit comments