Skip to content

Commit

Permalink
components: provide common interface
Browse files Browse the repository at this point in the history
Decouple components and main code of operator with ComponentHandler
interface. It allows to hanle all common operations in a unique way
and add components without changing main/dsc code. At the moment
they are initialization, controller creation and fetching some
component specific values (name, management status and corresponding
CR). For the latter the code basically copy'n'paste unfortunately.

Use init() functionality to register components only. It requires
linter silencing.

Also non-recommended approach (and linter silencing) of returning
interface is used to return component specific CR as common
client.Object to simplify the code in DSC reconciler. Otherwise one
more level of inderection is needed with passing required
functionality (closure) to the interface function.

Signed-off-by: Yauheni Kaliuta <[email protected]>
  • Loading branch information
ykaliuta committed Nov 19, 2024
1 parent acd89b8 commit eb46cb8
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 160 deletions.
24 changes: 20 additions & 4 deletions controllers/components/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@ import (

operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
dscv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
cr "github.com/opendatahub-io/opendatahub-operator/v2/pkg/componentsregistry"
odhdeploy "github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
)

func Init(platform cluster.Platform) error {
type componentHandler struct{}

func init() { //nolint: gochecknoinits
cr.Add(&componentHandler{})
}

func (s *componentHandler) GetName() string {
return componentsv1.DashboardComponentName
}

func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) operatorv1.ManagementState {
return dsc.Spec.Components.Dashboard.ManagementState
}

func (s *componentHandler) Init(platform cluster.Platform) error {
mi := defaultManifestInfo(platform)

if err := odhdeploy.ApplyParams(mi.String(), imagesMap); err != nil {
Expand All @@ -23,7 +39,7 @@ func Init(platform cluster.Platform) error {
return nil
}

func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.Dashboard {
func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { //nolint:ireturn
dashboardAnnotations := make(map[string]string)

switch dsc.Spec.Components.Dashboard.ManagementState {
Expand All @@ -33,7 +49,7 @@ func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.Dashboard {
dashboardAnnotations[annotations.ManagementStateAnnotation] = "Unknown"
}

return &componentsv1.Dashboard{
return client.Object(&componentsv1.Dashboard{
TypeMeta: metav1.TypeMeta{
Kind: componentsv1.DashboardKind,
APIVersion: componentsv1.GroupVersion.String(),
Expand All @@ -45,5 +61,5 @@ func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.Dashboard {
Spec: componentsv1.DashboardSpec{
DashboardCommonSpec: dsc.Spec.Components.Dashboard.DashboardCommonSpec,
},
}
})
}
2 changes: 1 addition & 1 deletion controllers/components/dashboard/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
)

// NewComponentReconciler creates a ComponentReconciler for the Dashboard API.
func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
componentName := computeComponentName()

_, err := reconciler.ComponentReconcilerFor(mgr, componentsv1.DashboardInstanceName, &componentsv1.Dashboard{}).
Expand Down
26 changes: 20 additions & 6 deletions controllers/components/kueue/kueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (

operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
dscv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
cr "github.com/opendatahub-io/opendatahub-operator/v2/pkg/componentsregistry"
odhdeploy "github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
)
Expand All @@ -21,8 +23,21 @@ var (
DefaultPath = odhdeploy.DefaultManifestPath + "/" + ComponentName + "/rhoai" // same path for both odh and rhoai
)

// for DSC to get compoment Kueue's CR.
func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.Kueue {
type componentHandler struct{}

func init() { //nolint: gochecknoinits
cr.Add(&componentHandler{})
}

func (s *componentHandler) GetName() string {
return componentsv1.KueueComponentName
}

func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) operatorv1.ManagementState {
return dsc.Spec.Components.Kueue.ManagementState
}

func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { //nolint:ireturn
kueueAnnotations := make(map[string]string)
switch dsc.Spec.Components.Kueue.ManagementState {
case operatorv1.Managed, operatorv1.Removed:
Expand All @@ -31,7 +46,7 @@ func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.Kueue {
kueueAnnotations[annotations.ManagementStateAnnotation] = "Unknown"
}

return &componentsv1.Kueue{
return client.Object(&componentsv1.Kueue{
TypeMeta: metav1.TypeMeta{
Kind: componentsv1.KueueKind,
APIVersion: componentsv1.GroupVersion.String(),
Expand All @@ -43,11 +58,10 @@ func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.Kueue {
Spec: componentsv1.KueueSpec{
KueueCommonSpec: dsc.Spec.Components.Kueue.KueueCommonSpec,
},
}
})
}

// Init for set images.
func Init(platform cluster.Platform) error {
func (s *componentHandler) Init(platform cluster.Platform) error {
imageParamMap := map[string]string{
"odh-kueue-controller-image": "RELATED_IMAGE_ODH_KUEUE_CONTROLLER_IMAGE",
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/components/kueue/kueue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
)

func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ComponentReconcilerFor(
mgr,
componentsv1.KueueInstanceName,
Expand Down
24 changes: 20 additions & 4 deletions controllers/components/modelregistry/modelregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@ import (

operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
dscv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
cr "github.com/opendatahub-io/opendatahub-operator/v2/pkg/componentsregistry"
odhdeploy "github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
)

func Init(_ cluster.Platform) error {
type componentHandler struct{}

func init() { //nolint: gochecknoinits
cr.Add(&componentHandler{})
}

func (s *componentHandler) GetName() string {
return componentsv1.ModelRegistryComponentName
}

func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) operatorv1.ManagementState {
return dsc.Spec.Components.ModelRegistry.ManagementState
}

func (s *componentHandler) Init(_ cluster.Platform) error {
mi := baseManifestInfo(BaseManifestsSourcePath)

params := make(map[string]string)
Expand All @@ -31,7 +47,7 @@ func Init(_ cluster.Platform) error {
return nil
}

func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.ModelRegistry {
func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { //nolint:ireturn
componentAnnotations := make(map[string]string)

switch dsc.Spec.Components.ModelRegistry.ManagementState {
Expand All @@ -42,7 +58,7 @@ func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.ModelRegistry {
componentAnnotations[annotations.ManagementStateAnnotation] = "Unknown"
}

return &componentsv1.ModelRegistry{
return client.Object(&componentsv1.ModelRegistry{
TypeMeta: metav1.TypeMeta{
Kind: componentsv1.ModelRegistryKind,
APIVersion: componentsv1.GroupVersion.String(),
Expand All @@ -54,5 +70,5 @@ func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.ModelRegistry {
Spec: componentsv1.ModelRegistrySpec{
ModelRegistryCommonSpec: dsc.Spec.Components.ModelRegistry.ModelRegistryCommonSpec,
},
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
)

// NewComponentReconciler creates a ComponentReconciler for the Dashboard API.
func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ComponentReconcilerFor(
mgr,
componentsv1.ModelRegistryInstanceName,
Expand Down
26 changes: 20 additions & 6 deletions controllers/components/ray/ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (

operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
dscv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
cr "github.com/opendatahub-io/opendatahub-operator/v2/pkg/componentsregistry"
odhdeploy "github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
)
Expand All @@ -21,8 +23,21 @@ var (
DefaultPath = odhdeploy.DefaultManifestPath + "/" + ComponentName + "/openshift"
)

// for DSC to get compoment Ray's CR.
func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.Ray {
type componentHandler struct{}

func init() { //nolint: gochecknoinits
cr.Add(&componentHandler{})
}

func (s *componentHandler) GetName() string {
return componentsv1.RayComponentName
}

func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) operatorv1.ManagementState {
return dsc.Spec.Components.Ray.ManagementState
}

func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) client.Object { //nolint:ireturn
rayAnnotations := make(map[string]string)
switch dsc.Spec.Components.Ray.ManagementState {
case operatorv1.Managed, operatorv1.Removed:
Expand All @@ -31,7 +46,7 @@ func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.Ray {
rayAnnotations[annotations.ManagementStateAnnotation] = "Unknown"
}

return &componentsv1.Ray{
return client.Object(&componentsv1.Ray{
TypeMeta: metav1.TypeMeta{
Kind: componentsv1.RayKind,
APIVersion: componentsv1.GroupVersion.String(),
Expand All @@ -43,11 +58,10 @@ func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.Ray {
Spec: componentsv1.RaySpec{
RayCommonSpec: dsc.Spec.Components.Ray.RayCommonSpec,
},
}
})
}

// Init for set images.
func Init(platform cluster.Platform) error {
func (s *componentHandler) Init(platform cluster.Platform) error {
imageParamMap := map[string]string{
"odh-kuberay-operator-controller-image": "RELATED_IMAGE_ODH_KUBERAY_OPERATOR_CONTROLLER_IMAGE",
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/components/ray/ray_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
)

func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ComponentReconcilerFor(
mgr,
componentsv1.RayInstanceName,
Expand Down
26 changes: 20 additions & 6 deletions controllers/components/trainingoperator/trainingoperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (

operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
dscv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
cr "github.com/opendatahub-io/opendatahub-operator/v2/pkg/componentsregistry"
odhdeploy "github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
)
Expand All @@ -21,8 +23,21 @@ var (
DefaultPath = odhdeploy.DefaultManifestPath + "/" + ComponentName + "/rhoai"
)

// for DSC to get compoment TrainingOperator's CR.
func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.TrainingOperator {
type componentHandler struct{}

func init() { //nolint: gochecknoinits
cr.Add(&componentHandler{})
}

func (s *componentHandler) GetName() string {
return componentsv1.TrainingOperatorComponentName
}

func (s *componentHandler) GetManagementState(dsc *dscv1.DataScienceCluster) operatorv1.ManagementState {
return dsc.Spec.Components.TrainingOperator.ManagementState
}

func (s *componentHandler) NewCRObject(dsc *dscv1.DataScienceCluster) k8sclient.Object { //nolint:ireturn
trainingoperatorAnnotations := make(map[string]string)
switch dsc.Spec.Components.TrainingOperator.ManagementState {
case operatorv1.Managed, operatorv1.Removed:
Expand All @@ -31,7 +46,7 @@ func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.TrainingOperato
trainingoperatorAnnotations[annotations.ManagementStateAnnotation] = "Unknown"
}

return &componentsv1.TrainingOperator{
return k8sclient.Object(&componentsv1.TrainingOperator{
TypeMeta: metav1.TypeMeta{
Kind: componentsv1.TrainingOperatorKind,
APIVersion: componentsv1.GroupVersion.String(),
Expand All @@ -43,11 +58,10 @@ func GetComponentCR(dsc *dscv1.DataScienceCluster) *componentsv1.TrainingOperato
Spec: componentsv1.TrainingOperatorSpec{
TrainingOperatorCommonSpec: dsc.Spec.Components.TrainingOperator.TrainingOperatorCommonSpec,
},
}
})
}

// Init for set images.
func Init(platform cluster.Platform) error {
func (s *componentHandler) Init(platform cluster.Platform) error {
imageParamMap := map[string]string{
"odh-training-operator-controller-image": "RELATED_IMAGE_ODH_TRAINING_OPERATOR_IMAGE",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
)

func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ComponentReconcilerFor(
mgr,
componentsv1.TrainingOperatorInstanceName,
Expand Down
Loading

0 comments on commit eb46cb8

Please sign in to comment.