Skip to content

Commit

Permalink
Merge pull request opendatahub-io#489 from bartoszmajsak/reconcile_si…
Browse files Browse the repository at this point in the history
…mplification

chore: simplifies reconcile functions
  • Loading branch information
zdtsw authored Sep 13, 2023
2 parents be646e1 + e58115e commit b6e0587
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 109 deletions.
9 changes: 5 additions & 4 deletions components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ can be found [here](https://github.com/opendatahub-io/opendatahub-operator/tree/

- Define struct that includes a shared struct `Component` with common fields.
- Implement [interface](https://github.com/opendatahub-io/opendatahub-operator/blob/main/components/component.go#L15) methods according to your component

```go
type ComponentInterface interface {
ReconcileComponent(owner metav1.Object, client client.Client, scheme *runtime.Scheme,
enabled bool, namespace string, manifestsUri string) error
GetComponentName() string
SetImageParamsMap(imageMap map[string]string) map[string]string
ReconcileComponent(cli client.Client, owner metav1.Object, DSCISpec *dsci.DSCInitializationSpec) error
GetComponentName() string
GetManagementState() operatorv1.ManagementState
SetImageParamsMap(imageMap map[string]string) map[string]string
}
```
### Add reconcile and Events
Expand Down
30 changes: 13 additions & 17 deletions components/codeflare/codeflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package codeflare

import (
"fmt"
operatorv1 "github.com/openshift/api/operator/v1"

"context"
dsci "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
imagev1 "github.com/openshift/api/image/v1"
operatorv1 "github.com/openshift/api/operator/v1"
codeflarev1alpha1 "github.com/project-codeflare/codeflare-operator/api/codeflare/v1alpha1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -30,20 +29,20 @@ type CodeFlare struct {
components.Component `json:""`
}

func (d *CodeFlare) SetImageParamsMap(imageMap map[string]string) map[string]string {
func (c *CodeFlare) SetImageParamsMap(imageMap map[string]string) map[string]string {
imageParamMap = imageMap
return imageParamMap
}

func (d *CodeFlare) GetComponentName() string {
func (c *CodeFlare) GetComponentName() string {
return ComponentName
}

// Verifies that CodeFlare implements ComponentInterface
var _ components.ComponentInterface = (*CodeFlare)(nil)

func (c *CodeFlare) ReconcileComponent(owner metav1.Object, cli client.Client, scheme *runtime.Scheme, managementState operatorv1.ManagementState, dscispec *dsci.DSCInitializationSpec) error {
enabled := managementState == operatorv1.Managed
func (d *CodeFlare) ReconcileComponent(cli client.Client, owner metav1.Object, dscispec *dsci.DSCInitializationSpec) error {
enabled := d.GetManagementState() == operatorv1.Managed

if enabled {
// check if the CodeFlare operator is installed
Expand All @@ -57,15 +56,12 @@ func (c *CodeFlare) ReconcileComponent(owner metav1.Object, cli client.Client, s
if platform == deploy.SelfManagedRhods || platform == deploy.ManagedRhods {
dependentOperator = RHCodeflareOperator
}
found, err := deploy.OperatorExists(cli, dependentOperator)

if !found {
if err != nil {
return err
} else {
return fmt.Errorf("operator %s not found. Please install the operator before enabling %s component",
dependentOperator, ComponentName)
}
if found, err := deploy.OperatorExists(cli, dependentOperator); err != nil {
return err
} else if !found {
return fmt.Errorf("operator %s not found. Please install the operator before enabling %s component",
dependentOperator, ComponentName)
}

// Update image parameters only when we do not have customized manifests set
Expand All @@ -77,7 +73,7 @@ func (c *CodeFlare) ReconcileComponent(owner metav1.Object, cli client.Client, s
}

// Special handling to delete MCAD InstaScale ImageStream resources
if managementState == operatorv1.Removed {
if d.GetManagementState() == operatorv1.Removed {
// Fetch the MCAD resource based on the request
mcad := &codeflarev1alpha1.MCAD{}
err := cli.Get(context.TODO(), client.ObjectKey{
Expand Down Expand Up @@ -116,10 +112,10 @@ func (c *CodeFlare) ReconcileComponent(owner metav1.Object, cli client.Client, s
}

// Deploy Codeflare
err := deploy.DeployManifestsFromPath(owner, cli, ComponentName,
err := deploy.DeployManifestsFromPath(owner, cli, d.GetComponentName(),
CodeflarePath,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)

return err

Expand Down
9 changes: 6 additions & 3 deletions components/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
dsci "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -22,9 +21,13 @@ type Component struct {
// Add any other common fields across components below
}

func (c *Component) GetManagementState() operatorv1.ManagementState {
return c.ManagementState
}

type ComponentInterface interface {
ReconcileComponent(owner metav1.Object, client client.Client, scheme *runtime.Scheme,
managementState operatorv1.ManagementState, DSCISpec *dsci.DSCInitializationSpec) error
ReconcileComponent(cli client.Client, owner metav1.Object, DSCISpec *dsci.DSCInitializationSpec) error
GetComponentName() string
GetManagementState() operatorv1.ManagementState
SetImageParamsMap(imageMap map[string]string) map[string]string
}
27 changes: 13 additions & 14 deletions components/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dashboard

import (
"fmt"
operatorv1 "github.com/openshift/api/operator/v1"

"context"
"strings"
Expand All @@ -11,10 +12,8 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/common"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
operatorv1 "github.com/openshift/api/operator/v1"
routev1 "github.com/openshift/api/route/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -53,16 +52,16 @@ func (d *Dashboard) GetComponentName() string {
// Verifies that Dashboard implements ComponentInterface
var _ components.ComponentInterface = (*Dashboard)(nil)

func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, scheme *runtime.Scheme, managementState operatorv1.ManagementState, dscispec *dsci.DSCInitializationSpec) error {
enabled := managementState == operatorv1.Managed
func (d *Dashboard) ReconcileComponent(cli client.Client, owner metav1.Object, dscispec *dsci.DSCInitializationSpec) error {

// TODO: Add any additional tasks if required when reconciling component

platform, err := deploy.GetPlatform(cli)
if err != nil {
return err
}

// Update Default rolebinding
enabled := d.GetManagementState() == operatorv1.Managed
if enabled {
if platform == deploy.OpenDataHub || platform == "" {
err := common.UpdatePodSecurityRolebinding(cli, []string{"odh-dashboard"}, dscispec.ApplicationsNamespace)
Expand Down Expand Up @@ -100,7 +99,7 @@ func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, s
err = deploy.DeployManifestsFromPath(owner, cli, ComponentNameSupported,
PathODHDashboardConfig,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)
if err != nil {
return fmt.Errorf("failed to set dashboard config from %s: %v", PathODHDashboardConfig, err)
}
Expand All @@ -109,7 +108,7 @@ func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, s
err = deploy.DeployManifestsFromPath(owner, cli, ComponentNameSupported,
PathOVMS,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)
if err != nil {
return fmt.Errorf("failed to set dashboard OVMS from %s: %v", PathOVMS, err)
}
Expand All @@ -122,7 +121,7 @@ func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, s
err = deploy.DeployManifestsFromPath(owner, cli, ComponentNameSupported,
PathAnaconda,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)
if err != nil {
return fmt.Errorf("failed to deploy anaconda resources from %s: %v", PathAnaconda, err)
}
Expand All @@ -141,7 +140,7 @@ func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, s
err = deploy.DeployManifestsFromPath(owner, cli, ComponentName,
Path,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)
if err != nil {
return err
}
Expand All @@ -150,7 +149,7 @@ func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, s
err = deploy.DeployManifestsFromPath(owner, cli, ComponentNameSupported,
PathSupported,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)
if err != nil {
return err
}
Expand All @@ -162,7 +161,7 @@ func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, s
err = deploy.DeployManifestsFromPath(owner, cli, ComponentNameSupported,
PathISVSM,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)
if err != nil {
return fmt.Errorf("failed to set dashboard ISV from %s: %v", PathISVSM, err)
}
Expand All @@ -184,7 +183,7 @@ func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, s
err = deploy.DeployManifestsFromPath(owner, cli, ComponentNameSupported,
PathConsoleLink,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)
if err != nil {
return fmt.Errorf("failed to set dashboard consolelink from %s: %v", PathConsoleLink, err)
}
Expand All @@ -193,7 +192,7 @@ func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, s
err = deploy.DeployManifestsFromPath(owner, cli, ComponentNameSupported,
PathISVAddOn,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)
if err != nil {
return fmt.Errorf("failed to set dashboard ISV from %s: %v", PathISVAddOn, err)
}
Expand All @@ -215,7 +214,7 @@ func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, s
err = deploy.DeployManifestsFromPath(owner, cli, ComponentNameSupported,
PathConsoleLink,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)
if err != nil {
return fmt.Errorf("failed to set dashboard consolelink from %s", PathConsoleLink)
}
Expand Down
10 changes: 5 additions & 5 deletions components/datasciencepipelines/datasciencepipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -41,8 +40,8 @@ func (d *DataSciencePipelines) GetComponentName() string {
// Verifies that Dashboard implements ComponentInterface
var _ components.ComponentInterface = (*DataSciencePipelines)(nil)

func (d *DataSciencePipelines) ReconcileComponent(owner metav1.Object, cli client.Client, scheme *runtime.Scheme, managementState operatorv1.ManagementState, dscispec *dsci.DSCInitializationSpec) error {
enabled := managementState == operatorv1.Managed
func (d *DataSciencePipelines) ReconcileComponent(cli client.Client, owner metav1.Object, dscispec *dsci.DSCInitializationSpec) error {
enabled := d.GetManagementState() == operatorv1.Managed

if enabled {
// check if the dependent operator installed is done in dashboard
Expand All @@ -54,10 +53,11 @@ func (d *DataSciencePipelines) ReconcileComponent(owner metav1.Object, cli clien
}
}
}
err := deploy.DeployManifestsFromPath(owner, cli, ComponentName,

err := deploy.DeployManifestsFromPath(owner, cli, d.GetComponentName(),
Path,
dscispec.ApplicationsNamespace,
scheme, enabled)
cli.Scheme(), enabled)
return err
}

Expand Down
44 changes: 19 additions & 25 deletions components/kserve/kserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package kserve

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

dsci "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/common"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
operatorv1 "github.com/openshift/api/operator/v1"
)

const (
Expand All @@ -35,41 +34,36 @@ type Kserve struct {
components.Component `json:""`
}

func (d *Kserve) SetImageParamsMap(imageMap map[string]string) map[string]string {
func (k *Kserve) SetImageParamsMap(imageMap map[string]string) map[string]string {
imageParamMap = imageMap
return imageParamMap
}

func (d *Kserve) GetComponentName() string {
func (k *Kserve) GetComponentName() string {
return ComponentName
}

// Verifies that Kserve implements ComponentInterface
var _ components.ComponentInterface = (*Kserve)(nil)

func (k *Kserve) ReconcileComponent(owner metav1.Object, cli client.Client, scheme *runtime.Scheme, managementState operatorv1.ManagementState, dscispec *dsci.DSCInitializationSpec) error {
enabled := managementState == operatorv1.Managed
func (k *Kserve) ReconcileComponent(cli client.Client, owner metav1.Object, dscispec *dsci.DSCInitializationSpec) error {
enabled := k.GetManagementState() == operatorv1.Managed

if enabled {
// check on dependent operators
found, err := deploy.OperatorExists(cli, ServiceMeshOperator)
if !found {
if err != nil {
return err
} else {
return fmt.Errorf("operator %s not found. Please install the operator before enabling %s component",
ServiceMeshOperator, ComponentName)
}
if found, err := deploy.OperatorExists(cli, ServiceMeshOperator); err != nil {
return err
} else if !found {
return fmt.Errorf("operator %s not found. Please install the operator before enabling %s component",
ServiceMeshOperator, ComponentName)
}

// check on dependent operators might be in multiple namespaces
found, err = deploy.OperatorExists(cli, ServerlessOperator)
if !found {
if err != nil {
return err
} else {
return fmt.Errorf("operator %s not found. Please install the operator before enabling %s component",
ServerlessOperator, ComponentName)
}
if found, err := deploy.OperatorExists(cli, ServerlessOperator); err != nil {
return err
} else if !found {
return fmt.Errorf("operator %s not found. Please install the operator before enabling %s component",
ServerlessOperator, ComponentName)
}

// Update image parameters only when we do not have customized manifests set
Expand All @@ -83,7 +77,7 @@ func (k *Kserve) ReconcileComponent(owner metav1.Object, cli client.Client, sche
if err := deploy.DeployManifestsFromPath(owner, cli, ComponentName,
Path,
dscispec.ApplicationsNamespace,
scheme, enabled); err != nil {
cli.Scheme(), enabled); err != nil {
return err
}

Expand All @@ -100,10 +94,10 @@ func (k *Kserve) ReconcileComponent(owner metav1.Object, cli client.Client, sche
}
}
}
if err := deploy.DeployManifestsFromPath(owner, cli, ComponentName,
if err := deploy.DeployManifestsFromPath(owner, cli, k.GetComponentName(),
DependentPath,
dscispec.ApplicationsNamespace,
scheme, enabled); err != nil {
cli.Scheme(), enabled); err != nil {
return err
}

Expand Down
Loading

0 comments on commit b6e0587

Please sign in to comment.