Skip to content

Commit

Permalink
Merge pull request #13 from lburgazzoli/actions_improvements
Browse files Browse the repository at this point in the history
Actions improvements
  • Loading branch information
VaishnaviHire authored Oct 15, 2024
2 parents 4693c4e + fca4f75 commit f28d5c6
Show file tree
Hide file tree
Showing 29 changed files with 833 additions and 191 deletions.
4 changes: 2 additions & 2 deletions apis/components/v1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ limitations under the License.
*/

// Package v1 contains API Schema definitions for the components v1 API group
//+kubebuilder:object:generate=true
//+groupName=components.opendatahub.io
// +kubebuilder:object:generate=true
// +groupName=components.opendatahub.io
package v1

import (
Expand Down
2 changes: 1 addition & 1 deletion components/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package components

import (
"context"
"github.com/opendatahub-io/opendatahub-operator/v2/apis/components"
"os"
"path/filepath"
"strings"
Expand All @@ -14,6 +13,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/opendatahub-io/opendatahub-operator/v2/apis/components"
dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
)
Expand Down
1 change: 1 addition & 0 deletions components/dashboard/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
odhClient "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/client"
annotation "github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/trustedcabundle"
)

// CertConfigmapGeneratorReconciler holds the controller configuration.
type CertConfigmapGeneratorReconciler struct {
Client client.Client
*odhClient.Client
Scheme *runtime.Scheme
Log logr.Logger
}
Expand Down
35 changes: 0 additions & 35 deletions controllers/components/base_reconciler_actions.go

This file was deleted.

159 changes: 82 additions & 77 deletions controllers/components/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,40 @@ import (
"context"
"errors"
"fmt"
"github.com/opendatahub-io/opendatahub-operator/v2/apis/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
"k8s.io/utils/pointer"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
dscv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1"
dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions"
odhrec "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/reconciler"
odhtypes "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/types"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
k8serr "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
)

var (
DashboardInstanceName = "default-dashboard"
const (
ComponentName = "dashboard"
DashboardInstanceName = "default-dashboard"
)

var (
ComponentNameUpstream = ComponentName
PathUpstream = deploy.DefaultManifestPath + "/" + ComponentNameUpstream + "/odh"

Expand All @@ -58,34 +65,27 @@ var (
DefaultPath = ""
)

func NewDashboardReconciler(mgr ctrl.Manager) error {
r := NewBaseReconciler[*componentsv1.Dashboard](mgr, ComponentName)
func NewDashboardReconciler(ctx context.Context, mgr ctrl.Manager) error {
r, err := odhrec.NewComponentReconciler[*componentsv1.Dashboard](ctx, mgr, ComponentName)
if err != nil {
return err
}

actionCtx := logf.IntoContext(ctx, r.Log)
// Add Dashboard-specific actions
r.AddAction(&InitializeAction{BaseAction{Log: mgr.GetLogger().WithName("actions").WithName("initialize")}})
r.AddAction(&SupportDevFlagsAction{BaseAction{Log: mgr.GetLogger().WithName("actions").WithName("devFlags")}})
r.AddAction(&CleanupOAuthClientAction{BaseAction{Log: mgr.GetLogger().WithName("actions").WithName("cleanup")}})
r.AddAction(&DeployComponentAction{BaseAction{Log: mgr.GetLogger().WithName("actions").WithName("deploy")}})
r.AddAction(&UpdateStatusAction{BaseAction{Log: mgr.GetLogger().WithName("actions").WithName("update-status")}})

r.AddFinalizer(&DeleteResourcesAction{
BaseAction: BaseAction{
Log: mgr.GetLogger().WithName("finalizers").WithName("cleanup"),
},
// include only the types that must be deleted
Types: []client.Object{
&corev1.Secret{},
},
Labels: map[string]string{
"app.opendatahub.io/dashboard": "true",
},
})
r.AddAction(&InitializeAction{actions.BaseAction{Log: mgr.GetLogger().WithName("actions").WithName("initialize")}})
r.AddAction(&SupportDevFlagsAction{actions.BaseAction{Log: mgr.GetLogger().WithName("actions").WithName("devFlags")}})
r.AddAction(&CleanupOAuthClientAction{actions.BaseAction{Log: mgr.GetLogger().WithName("actions").WithName("cleanup")}})
r.AddAction(&DeployComponentAction{actions.BaseAction{Log: mgr.GetLogger().WithName("actions").WithName("deploy")}})

eh := handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
return watchDashboardResources(ctx, a)
})
r.AddAction(actions.NewUpdateStatusAction(
actionCtx,
actions.WithUpdateStatusLabel(labels.ComponentName, ComponentName),
))

eh := handler.EnqueueRequestsFromMapFunc(watchDashboardResources)

err := ctrl.NewControllerManagedBy(mgr).
err = ctrl.NewControllerManagedBy(mgr).
For(&componentsv1.Dashboard{}).
// dependants
Watches(&appsv1.Deployment{}, eh).
Expand Down Expand Up @@ -119,12 +119,7 @@ func CreateDashboardInstance(dsc *dscv1.DataScienceCluster) *componentsv1.Dashbo
Name: DashboardInstanceName,
},
Spec: componentsv1.DashboardSpec{
DSCDashboard: componentsv1.DSCDashboard{
Component: components.Component{
ManagementState: dsc.Spec.Components.Dashboard.ManagementState,
DevFlags: dsc.Spec.Components.Dashboard.DevFlags,
},
},
DSCDashboard: dsc.Spec.Components.Dashboard,
},
}
}
Expand Down Expand Up @@ -169,8 +164,7 @@ func CreateDashboardInstance(dsc *dscv1.DataScienceCluster) *componentsv1.Dashbo

// +kubebuilder:rbac:groups="*",resources=replicasets,verbs=*

func watchDashboardResources(ctx context.Context, a client.Object) []reconcile.Request {

func watchDashboardResources(_ context.Context, a client.Object) []reconcile.Request {
if a.GetLabels()["app.opendatahub.io/dashboard"] == "true" {
return []reconcile.Request{{
NamespacedName: types.NamespacedName{Name: DashboardInstanceName},
Expand Down Expand Up @@ -245,10 +239,10 @@ func updateKustomizeVariable(ctx context.Context, cli client.Client, platform cl
// Action implementations

type InitializeAction struct {
BaseAction
actions.BaseAction
}

func (a *InitializeAction) Execute(ctx context.Context, rr *ReconciliationRequest) error {
func (a *InitializeAction) Execute(ctx context.Context, rr *odhtypes.ReconciliationRequest) error {
// Implement initialization logic
log := logf.FromContext(ctx).WithName(ComponentNameUpstream)

Expand All @@ -263,7 +257,7 @@ func (a *InitializeAction) Execute(ctx context.Context, rr *ReconciliationReques
}
DefaultPath = manifestMap[rr.Platform]

rr.Manifests = Manifests{
rr.Manifests = odhtypes.Manifests{
Paths: manifestMap,
}

Expand All @@ -275,11 +269,15 @@ func (a *InitializeAction) Execute(ctx context.Context, rr *ReconciliationReques
}

type SupportDevFlagsAction struct {
BaseAction
actions.BaseAction
}

func (a *SupportDevFlagsAction) Execute(ctx context.Context, rr *ReconciliationRequest) error {
dashboard := rr.Instance.(*componentsv1.Dashboard)
func (a *SupportDevFlagsAction) Execute(ctx context.Context, rr *odhtypes.ReconciliationRequest) error {
dashboard, ok := rr.Instance.(*componentsv1.Dashboard)
if !ok {
return fmt.Errorf("resource instance %v is not a componentsv1.Dashboard)", rr.Instance)
}

if dashboard.Spec.DevFlags == nil {
return nil
}
Expand All @@ -298,10 +296,10 @@ func (a *SupportDevFlagsAction) Execute(ctx context.Context, rr *ReconciliationR
}

type CleanupOAuthClientAction struct {
BaseAction
actions.BaseAction
}

func (a *CleanupOAuthClientAction) Execute(ctx context.Context, rr *ReconciliationRequest) error {
func (a *CleanupOAuthClientAction) Execute(ctx context.Context, rr *odhtypes.ReconciliationRequest) error {
// Remove previous oauth-client secrets
// Check if component is going from state of `Not Installed --> Installed`
// Assumption: Component is currently set to enabled
Expand Down Expand Up @@ -329,10 +327,10 @@ func (a *CleanupOAuthClientAction) Execute(ctx context.Context, rr *Reconciliati
}

type DeployComponentAction struct {
BaseAction
actions.BaseAction
}

func (a *DeployComponentAction) Execute(ctx context.Context, rr *ReconciliationRequest) error {
func (a *DeployComponentAction) Execute(ctx context.Context, rr *odhtypes.ReconciliationRequest) error {
// Implement component deployment logic
// 1. platform specific RBAC
if rr.Platform == cluster.OpenDataHub || rr.Platform == "" {
Expand All @@ -356,44 +354,51 @@ func (a *DeployComponentAction) Execute(ctx context.Context, rr *ReconciliationR
// return fmt.Errorf("failed to update params.env from %s : %w", r.entryPath, err)
// }

path := rr.Manifests.Paths[rr.Platform]
name := ComponentNameUpstream

// common: Deploy odh-dashboard manifests
// TODO: check if we can have the same component name odh-dashboard for both, or still keep rhods-dashboard for RHOAI
switch rr.Platform {
case cluster.SelfManagedRhods, cluster.ManagedRhods:
// anaconda
if err := cluster.CreateSecret(ctx, rr.Client, "anaconda-ce-access", rr.DSCI.Spec.ApplicationsNamespace); err != nil {
err := cluster.CreateSecret(
ctx,
rr.Client,
"anaconda-ce-access",
rr.DSCI.Spec.ApplicationsNamespace,
// set owner reference so it gets deleted when the Dashboard resource get deleted as well
cluster.WithOwnerReference(metav1.OwnerReference{
APIVersion: rr.Instance.GetObjectKind().GroupVersionKind().GroupVersion().String(),
Kind: rr.Instance.GetObjectKind().GroupVersionKind().Kind,
Name: rr.Instance.GetName(),
UID: rr.Instance.GetUID(),
Controller: pointer.Bool(true),
BlockOwnerDeletion: pointer.Bool(true),
}),
cluster.WithLabels(
labels.ComponentName, ComponentName,
labels.ODH.Component(name), "true",
labels.K8SCommon.PartOf, name,
),
)

if err != nil {
return fmt.Errorf("failed to create access-secret for anaconda: %w", err)
}
// Deploy RHOAI manifests
if err := deploy.DeployManifestsFromPath(ctx, rr.Client, rr.Instance, rr.Manifests.Paths[rr.Platform], rr.DSCI.Spec.ApplicationsNamespace, ComponentNameDownstream, true); err != nil {
return fmt.Errorf("failed to apply manifests from %s: %w", PathDownstream, err)
}
a.Log.Info("apply manifests done")

if err := cluster.WaitForDeploymentAvailable(ctx, rr.Client, ComponentNameDownstream, rr.DSCI.Spec.ApplicationsNamespace, 20, 3); err != nil {
return fmt.Errorf("deployment for %s is not ready to server: %w", ComponentNameDownstream, err)
}

return nil
name = ComponentNameDownstream
}

default:
// Deploy ODH manifests
if err := deploy.DeployManifestsFromPath(ctx, rr.Client, rr.Instance, rr.Manifests.Paths[cluster.OpenDataHub], rr.DSCI.Spec.ApplicationsNamespace, ComponentNameUpstream, true); err != nil {
return err
}
a.Log.Info("apply manifests done")
err = deploy.DeployManifestsFromPathWithLabels(ctx, rr.Client, rr.Instance, path, rr.DSCI.Spec.ApplicationsNamespace, name, true, map[string]string{
labels.ComponentName: ComponentName,
})

if err := cluster.WaitForDeploymentAvailable(ctx, rr.Client, ComponentNameUpstream, rr.DSCI.Spec.ApplicationsNamespace, 20, 3); err != nil {
return fmt.Errorf("deployment for %s is not ready to server: %w", ComponentNameUpstream, err)
}
if err != nil {
return fmt.Errorf("failed to apply manifests from %s: %w", name, err)
}
return nil
}

type UpdateStatusAction struct {
BaseAction
}
a.Log.Info("apply manifests done")

func (a *UpdateStatusAction) Execute(ctx context.Context, rr *ReconciliationRequest) error {
return nil
}
7 changes: 3 additions & 4 deletions controllers/components/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -31,7 +28,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
//+kubebuilder:scaffold:imports

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
Expand Down
Loading

0 comments on commit f28d5c6

Please sign in to comment.