Skip to content

Commit

Permalink
Add migration logic for odhdashboardconfig
Browse files Browse the repository at this point in the history
Signed-off-by: AjayJagan <ajaganat@redhat.com>

rh-pre-commit.version: 2.3.2
rh-pre-commit.check-secrets: ENABLED
  • Loading branch information
AjayJagan committed Mar 5, 2025
1 parent 3fb2d67 commit 28077d3
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 deletions.
2 changes: 2 additions & 0 deletions controllers/components/dashboard/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
deploy.WithCache(),
)).
WithAction(deployments.NewAction()).
//TODO: to be removed: https://issues.redhat.com/browse/RHOAIENG-21080
WithAction(patchOdhDashboardConfig).
WithAction(updateStatus).
// must be the final action
WithAction(gc.NewAction(
Expand Down
43 changes: 43 additions & 0 deletions controllers/components/dashboard/dashboard_controller_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
routev1 "github.com/openshift/api/route/v1"
corev1 "k8s.io/api/core/v1"

Check failure on line 10 in controllers/components/dashboard/dashboard_controller_actions.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gci)
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

componentApi "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1alpha1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
Expand All @@ -19,6 +21,7 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/resources"
k8serr "k8s.io/apimachinery/pkg/api/errors"

Check failure on line 24 in controllers/components/dashboard/dashboard_controller_actions.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gci)
)

func initialize(ctx context.Context, rr *odhtypes.ReconciliationRequest) error {
Expand Down Expand Up @@ -101,6 +104,46 @@ func configureDependencies(_ context.Context, rr *odhtypes.ReconciliationRequest
return nil
}

// TODO: to be removed: https://issues.redhat.com/browse/RHOAIENG-21080
func patchOdhDashboardConfig(ctx context.Context, rr *odhtypes.ReconciliationRequest) error {
log := logf.FromContext(ctx)
dashboardConfig := &unstructured.Unstructured{}
dashboardConfig.SetGroupVersionKind(gvk.OdhDashboardConfig)
err := rr.Client.Get(ctx, client.ObjectKey{
Name: "odh-dashboard-config",
Namespace: rr.DSCI.Spec.ApplicationsNamespace,
}, dashboardConfig)
if err != nil {
if k8serr.IsNotFound(err) {
log.Info("ODH Dashboard Config not found, skipping patch", "namespace", rr.DSCI.Spec.ApplicationsNamespace)
return nil
}
return fmt.Errorf("failed to get ODH Dashboard Config instance: %w", err)
}
patch := dashboardConfig.DeepCopy()
updates := map[string][]any{
"notebookSizes": getNotebookSizesData(),
"modelServerSizes": getModelServerSizeData(),
}

updated, err := updateSpecFields(patch, updates)
if err != nil {
return err
}

if !updated {
log.Info("No changes needed, skipping patch")
return nil
}

if err := rr.Client.Patch(ctx, patch, client.MergeFrom(dashboardConfig)); err != nil {
return fmt.Errorf("failed to patch dashboard config: %w", err)
}

log.Info("Patched odhdashboardconfig successfully")
return nil
}

func updateStatus(ctx context.Context, rr *odhtypes.ReconciliationRequest) error {
d, ok := rr.Instance.(*componentApi.Dashboard)
if !ok {
Expand Down
145 changes: 145 additions & 0 deletions controllers/components/dashboard/dashboard_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/opendatahub-io/opendatahub-operator/v2/apis/common"
Expand Down Expand Up @@ -101,3 +102,147 @@ func computeComponentName() string {
func GetAdminGroup() string {
return adminGroups[cluster.GetRelease().Name]
}

// TODO: to be removed: https://issues.redhat.com/browse/RHOAIENG-21080
func updateSpecFields(obj *unstructured.Unstructured, updates map[string][]any) (bool, error) {
updated := false

for field, newData := range updates {
existingField, exists, err := unstructured.NestedSlice(obj.Object, "spec", field)
if err != nil {
return false, fmt.Errorf("failed to get field '%s': %w", field, err)
}

if !exists || len(existingField) == 0 {
if err := unstructured.SetNestedSlice(obj.Object, newData, "spec", field); err != nil {
return false, fmt.Errorf("failed to set field '%s': %w", field, err)
}
updated = true
}
}

return updated, nil
}

// TODO: to be removed: https://issues.redhat.com/browse/RHOAIENG-21080
func getNotebookSizesData() []any {
return []any{
map[string]any{
"name": "Small",
"resources": map[string]any{
"requests": map[string]any{
"cpu": "1",
"memory": "4Gi",
},
"limits": map[string]any{
"cpu": "3",
"memory": "8Gi",
},
},
},
map[string]any{
"name": "Small",
"resources": map[string]any{
"requests": map[string]any{
"cpu": "1",
"memory": "8Gi",
},
"limits": map[string]any{
"cpu": "2",
"memory": "8Gi",
},
},
},
map[string]any{
"name": "Medium",
"resources": map[string]any{
"requests": map[string]any{
"cpu": "3",
"memory": "24Gi",
},
"limits": map[string]any{
"cpu": "6",
"memory": "24Gi",
},
},
},
map[string]any{
"name": "Large",
"resources": map[string]any{
"requests": map[string]any{
"cpu": "7",
"memory": "56Gi",
},
"limits": map[string]any{
"cpu": "14",
"memory": "56Gi",
},
},
},
map[string]any{
"name": "X Large",
"resources": map[string]any{
"requests": map[string]any{
"cpu": "15",
"memory": "120Gi",
},
"limits": map[string]any{
"cpu": "30",
"memory": "120Gi",
},
},
},
}
}

// TODO: to be removed: https://issues.redhat.com/browse/RHOAIENG-21080
func getModelServerSizeData() []any {
return []any{
map[string]any{
"name": "Small",
"resources": map[string]any{
"requests": map[string]any{
"cpu": "1",
"memory": "4Gi",
},
"limits": map[string]any{
"cpu": "2",
"memory": "8Gi",
},
},
},
map[string]any{
"name": "Medium",
"resources": map[string]any{
"requests": map[string]any{
"cpu": "4",
"memory": "8Gi",
},
"limits": map[string]any{
"cpu": "8",
"memory": "10Gi",
},
},
},
map[string]any{
"name": "Large",
"resources": map[string]any{
"requests": map[string]any{
"cpu": "6",
"memory": "16Gi",
},
"limits": map[string]any{
"cpu": "10",
"memory": "20Gi",
},
},
},
map[string]any{
"name": "Custom",
"resources": map[string]any{
"requests": map[string]any{},
"limits": map[string]any{},
},
},
}
}

0 comments on commit 28077d3

Please sign in to comment.