Skip to content

Commit

Permalink
move to one-off logic
Browse files Browse the repository at this point in the history
rh-pre-commit.version: 2.3.2
rh-pre-commit.check-secrets: ENABLED
  • Loading branch information
AjayJagan committed Mar 6, 2025
1 parent 7c3be1d commit 18b3133
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 195 deletions.
2 changes: 0 additions & 2 deletions controllers/components/dashboard/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ 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
52 changes: 5 additions & 47 deletions controllers/components/dashboard/dashboard_controller_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"fmt"
"strings"

routev1 "github.com/openshift/api/route/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

componentApi "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1alpha1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
Expand All @@ -14,13 +19,6 @@ 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"
routev1 "github.com/openshift/api/route/v1"
corev1 "k8s.io/api/core/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
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"
)

func initialize(ctx context.Context, rr *odhtypes.ReconciliationRequest) error {
Expand Down Expand Up @@ -103,46 +101,6 @@ 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
148 changes: 2 additions & 146 deletions controllers/components/dashboard/dashboard_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"context"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/opendatahub-io/opendatahub-operator/v2/apis/common"
componentApi "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1alpha1"
dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/controllers/status"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
odhtypes "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/types"
odhdeploy "github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand Down Expand Up @@ -101,147 +101,3 @@ 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{},
},
},
}
}
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,20 @@ func main() { //nolint:funlen,maintidx,gocyclo
os.Exit(1)
}
}

// TODO: to be removed: https://issues.redhat.com/browse/RHOAIENG-21080
var patchODCFunc manager.RunnableFunc = func(ctx context.Context) error {
if err := upgrade.PatchOdhDashboardConfig(ctx, setupClient); err != nil {
setupLog.Error(err, "Unable to patch the odhdashboardconfig")
}
return err
}

err = mgr.Add(patchODCFunc)
if err != nil {
setupLog.Error(err, "Error patching odhdashboardconfig")
}

// Cleanup resources from previous v2 releases
var cleanExistingResourceFunc manager.RunnableFunc = func(ctx context.Context) error {
if err = upgrade.CleanupExistingResource(ctx, setupClient, platform, oldReleaseVersion); err != nil {
Expand Down
Loading

0 comments on commit 18b3133

Please sign in to comment.