Skip to content

Commit

Permalink
Support to watch/own object not known when the operator starts, but o…
Browse files Browse the repository at this point in the history
…nly after a component has been enabled.

Signed-off-by: Luca Burgazzoli <[email protected]>
  • Loading branch information
lburgazzoli committed Nov 20, 2024
1 parent d044d64 commit dffebab
Show file tree
Hide file tree
Showing 18 changed files with 383 additions and 140 deletions.
31 changes: 13 additions & 18 deletions bundle/manifests/opendatahub-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,6 @@ spec:
- console.openshift.io
resources:
- consolelinks
verbs:
- create
- delete
- get
- patch
- apiGroups:
- console.openshift.io
resources:
- odhquickstarts
verbs:
- create
Expand Down Expand Up @@ -624,15 +616,6 @@ spec:
- dashboard.opendatahub.io
resources:
- acceleratorprofiles
verbs:
- create
- delete
- get
- list
- patch
- apiGroups:
- dashboard.opendatahub.io
resources:
- odhapplications
- odhdocuments
verbs:
Expand Down Expand Up @@ -800,7 +783,6 @@ spec:
resources:
- servicemeshcontrolplanes
- servicemeshmemberrolls
- servicemeshmembers
- servicemeshmembers/finalizers
verbs:
- create
Expand All @@ -810,6 +792,19 @@ spec:
- update
- use
- watch
- apiGroups:
- maistra.io
resources:
- servicemeshmembers
verbs:
- create
- delete
- get
- list
- patch
- update
- use
- watch
- apiGroups:
- modelregistry.opendatahub.io
resources:
Expand Down
31 changes: 13 additions & 18 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,6 @@ rules:
- console.openshift.io
resources:
- consolelinks
verbs:
- create
- delete
- get
- patch
- apiGroups:
- console.openshift.io
resources:
- odhquickstarts
verbs:
- create
Expand Down Expand Up @@ -376,15 +368,6 @@ rules:
- dashboard.opendatahub.io
resources:
- acceleratorprofiles
verbs:
- create
- delete
- get
- list
- patch
- apiGroups:
- dashboard.opendatahub.io
resources:
- odhapplications
- odhdocuments
verbs:
Expand Down Expand Up @@ -552,7 +535,6 @@ rules:
resources:
- servicemeshcontrolplanes
- servicemeshmemberrolls
- servicemeshmembers
- servicemeshmembers/finalizers
verbs:
- create
Expand All @@ -562,6 +544,19 @@ rules:
- update
- use
- watch
- apiGroups:
- maistra.io
resources:
- servicemeshmembers
verbs:
- create
- delete
- get
- list
- patch
- update
- use
- watch
- apiGroups:
- modelregistry.opendatahub.io
resources:
Expand Down
10 changes: 8 additions & 2 deletions controllers/components/dashboard/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
Expand Down Expand Up @@ -57,10 +57,16 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
// By default, a predicated for changed generation is added by the Owns()
// method, however for deployments, we also need to retrieve status info
// hence we need a dedicated predicate to react to replicas status change
Owns(&appsv1.Deployment{}, builder.WithPredicates(resources.NewDeploymentPredicate())).
Owns(&appsv1.Deployment{}, reconciler.WithPredicates(resources.NewDeploymentPredicate())).
// operands - openshift
Owns(&routev1.Route{}).
Owns(&consolev1.ConsoleLink{}).
// Those APIs are provided by the component itself hence they should
// be watched dynamically
OwnsGVK(gvk.AcceleratorProfile, reconciler.Dynamic()).
OwnsGVK(gvk.OdhApplication, reconciler.Dynamic()).
OwnsGVK(gvk.OdhDocument, reconciler.Dynamic()).
OwnsGVK(gvk.OdhQuickStart, reconciler.Dynamic()).
// operands - watched
//
// By default the Watches functions adds:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
Expand Down Expand Up @@ -64,7 +63,7 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
Owns(&corev1.ServiceAccount{}).
Owns(&corev1.Service{}).
Owns(&monitoringv1.ServiceMonitor{}).
Owns(&appsv1.Deployment{}, builder.WithPredicates(resources.NewDeploymentPredicate())).
Owns(&appsv1.Deployment{}, reconciler.WithPredicates(resources.NewDeploymentPredicate())).
Owns(&securityv1.SecurityContextConstraints{}).
Watches(&extv1.CustomResourceDefinition{}). // call ForLabel() + new predicates
// Add datasciencepipelines-specific actions
Expand Down
3 changes: 1 addition & 2 deletions controllers/components/kueue/kueue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
Expand Down Expand Up @@ -59,7 +58,7 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
Owns(&promv1.PrometheusRule{}).
Owns(&admissionregistrationv1.MutatingWebhookConfiguration{}).
Owns(&admissionregistrationv1.ValidatingWebhookConfiguration{}).
Owns(&appsv1.Deployment{}, builder.WithPredicates(resources.NewDeploymentPredicate())).
Owns(&appsv1.Deployment{}, reconciler.WithPredicates(resources.NewDeploymentPredicate())).
Watches(&extv1.CustomResourceDefinition{}). // call ForLabel() + new predicates
// Add Kueue-specific actions
WithAction(initialize).
Expand Down
16 changes: 5 additions & 11 deletions controllers/components/modelregistry/modelregistry_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
Expand All @@ -53,10 +53,7 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
Owns(&rbacv1.ClusterRoleBinding{}).
Owns(&corev1.Service{}).
Owns(&corev1.ServiceAccount{}).
Owns(
&appsv1.Deployment{},
builder.WithPredicates(resources.NewDeploymentPredicate()),
).
Owns(&appsv1.Deployment{}, reconciler.WithPredicates(resources.NewDeploymentPredicate())).
Owns(&admissionregistrationv1.MutatingWebhookConfiguration{}).
Owns(&admissionregistrationv1.ValidatingWebhookConfiguration{}).
Watches(&corev1.Namespace{}).
Expand All @@ -66,12 +63,9 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
// in sync with the manifests, we should also create an additional watcher
Watches(&rbacv1.ClusterRole{}).
// This component adds a ServiceMeshMember resource to the registries
// namespaces that must be left even if the component is removed, hence
// we can't own.
//
// TODO: add dynamic watching for gvk.ServiceMeshMember if it make sense
// https://issues.redhat.com/browse/RHOAIENG-15170
//
// namespaces that may not be known when the controller is started, hence
// it should be watched dynamically
WatchesGVK(gvk.ServiceMeshMember, reconciler.Dynamic()).
// actions
WithAction(checkPreConditions).
WithAction(initialize).
Expand Down
3 changes: 1 addition & 2 deletions controllers/components/ray/ray_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
Expand All @@ -51,7 +50,7 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
Owns(&rbacv1.Role{}).
Owns(&rbacv1.RoleBinding{}).
Owns(&corev1.ServiceAccount{}).
Owns(&appsv1.Deployment{}, builder.WithPredicates(resources.NewDeploymentPredicate())).
Owns(&appsv1.Deployment{}, reconciler.WithPredicates(resources.NewDeploymentPredicate())).
Owns(&securityv1.SecurityContextConstraints{}).
Watches(&extv1.CustomResourceDefinition{}). // call ForLabel() + new predicates
// Add Ray-specific actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
Expand All @@ -49,7 +48,7 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
Owns(&rbacv1.ClusterRoleBinding{}).
Owns(&rbacv1.ClusterRole{}).
Owns(&corev1.ServiceAccount{}).
Owns(&appsv1.Deployment{}, builder.WithPredicates(resources.NewDeploymentPredicate())).
Owns(&appsv1.Deployment{}, reconciler.WithPredicates(resources.NewDeploymentPredicate())).
Watches(&extv1.CustomResourceDefinition{}). // call ForLabel() + new predicates
// Add TrainingOperator-specific actions
WithAction(initialize).
Expand Down
3 changes: 1 addition & 2 deletions controllers/components/trustyai/trustyai_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
Expand All @@ -50,7 +49,7 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
Owns(&rbacv1.Role{}).
Owns(&rbacv1.RoleBinding{}).
Owns(&corev1.Service{}).
Owns(&appsv1.Deployment{}, builder.WithPredicates(resources.NewDeploymentPredicate())).
Owns(&appsv1.Deployment{}, reconciler.WithPredicates(resources.NewDeploymentPredicate())).
Watches(&extv1.CustomResourceDefinition{}). // call ForLabel() + new predicates
// Add TrustyAI-specific actions
WithAction(initialize).
Expand Down
8 changes: 4 additions & 4 deletions controllers/datasciencecluster/kubebuilder_rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ package datasciencecluster

/* Only for RHODS */
// +kubebuilder:rbac:groups="user.openshift.io",resources=users,verbs=list;watch;patch;delete;get
// +kubebuilder:rbac:groups="console.openshift.io",resources=consolelinks,verbs=create;get;patch;delete
// +kubebuilder:rbac:groups="user.openshift.io",resources=groups,verbs=get;create;list;watch;patch;delete
// +kubebuilder:rbac:groups="console.openshift.io",resources=consolelinks,verbs=create;get;patch;list;delete;watch

// Ray
// +kubebuilder:rbac:groups=components.opendatahub.io,resources=rays,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -138,9 +139,7 @@ package datasciencecluster
// +kubebuilder:rbac:groups="console.openshift.io",resources=odhquickstarts,verbs=create;get;patch;list;delete;watch
// +kubebuilder:rbac:groups="dashboard.opendatahub.io",resources=odhdocuments,verbs=create;get;patch;list;delete;watch
// +kubebuilder:rbac:groups="dashboard.opendatahub.io",resources=odhapplications,verbs=create;get;patch;list;delete;watch
// +kubebuilder:rbac:groups="dashboard.opendatahub.io",resources=acceleratorprofiles,verbs=create;get;patch;list;delete
// +kubebuilder:rbac:groups="user.openshift.io",resources=groups,verbs=get;create;list;watch;patch;delete
// +kubebuilder:rbac:groups="console.openshift.io",resources=consolelinks,verbs=create;get;patch;delete
// +kubebuilder:rbac:groups="dashboard.opendatahub.io",resources=acceleratorprofiles,verbs=create;get;patch;list;delete;watch

// ModelRegistry
// +kubebuilder:rbac:groups=components.opendatahub.io,resources=modelregistries,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -149,6 +148,7 @@ package datasciencecluster
// +kubebuilder:rbac:groups=modelregistry.opendatahub.io,resources=modelregistries,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=modelregistry.opendatahub.io,resources=modelregistries/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=modelregistry.opendatahub.io,resources=modelregistries/finalizers,verbs=update;get
// +kubebuilder:rbac:groups=maistra.io,resources=servicemeshmembers,verbs=get;list;watch;create;update;patch;delete

// Kueue
// +kubebuilder:rbac:groups=components.opendatahub.io,resources=kueues,verbs=get;list;watch;create;update;patch;delete
Expand Down
6 changes: 6 additions & 0 deletions pkg/cluster/gvk/gvk.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ var (
Kind: "OdhDocument",
}

AcceleratorProfile = schema.GroupVersionKind{
Group: "dashboard.opendatahub.io",
Version: "v1",
Kind: "AcceleratorProfile",
}

OdhQuickStart = schema.GroupVersionKind{
Group: "console.openshift.io",
Version: "v1",
Expand Down
10 changes: 4 additions & 6 deletions pkg/controller/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,23 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
)

func ToOwner() handler.EventHandler {
func LabelToName(label string) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
objLabels := a.GetLabels()
if len(objLabels) == 0 {
return []reconcile.Request{}
}

partOf := objLabels[labels.ComponentPartOf]
if partOf == "" {
name := objLabels[label]
if name == "" {
return []reconcile.Request{}
}

return []reconcile.Request{{
NamespacedName: types.NamespacedName{
Name: partOf,
Name: name,
},
}}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/reconciler/component_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (r *ComponentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

// Execute actions
for _, action := range r.Actions {
l.V(3).Info("Executing action", "action", action)
l.Info("Executing action", "action", action)

actx := log.IntoContext(
ctx,
Expand Down
Loading

0 comments on commit dffebab

Please sign in to comment.