Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): Add migration logic for odhdashboardconfig #1709

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
176 changes: 176 additions & 0 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,179 @@

return nil
}

// TODO: to be removed: https://issues.redhat.com/browse/RHOAIENG-21080
func PatchOdhDashboardConfig(ctx context.Context, cli client.Client) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this function should also check the current and previous version to determine if the patch should be applied ? something like https://github.com/opendatahub-io/opendatahub-operator/blob/main/pkg/upgrade/upgrade.go#L503

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep it can be done, although I may need some guidance on which versions should be patched
@Gkrumbach07 ^

log := logf.FromContext(ctx)

var dashboardConfigs unstructured.UnstructuredList
dashboardConfigs.SetGroupVersionKind(gvk.OdhDashboardConfig)

if err := cli.List(ctx, &dashboardConfigs); err != nil {
log.Error(err, "Failed to list odhdashboardconfig CRs")
return fmt.Errorf("error listing odhdashboardconfig CRs: %w", err)
}

Check warning on line 629 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L620-L629

Added lines #L620 - L629 were not covered by tests

for _, cr := range dashboardConfigs.Items {
if !cluster.IsNotReservedNamespace(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: cr.GetNamespace()}}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this required ? my understanding is that there is only one OdhDashboardConfig per cluster so I thinnk it should be patched, regardless of the namespace ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed the logic to handle the singleton case

continue

Check warning on line 633 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L631-L633

Added lines #L631 - L633 were not covered by tests
}

log.Info("Found CR, applying patch", "namespace", cr.GetNamespace(), "name", cr.GetName())

patch := cr.DeepCopy()
updates := map[string][]any{
Copy link
Contributor

@lburgazzoli lburgazzoli Mar 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nip: since the updates map is always the same, can't this be just a var ? not sure I see the benefits of having the values returned by functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var would do fine 😄

"notebookSizes": getNotebookSizesData(),
"modelServerSizes": getModelServerSizeData(),
}

updated, err := updateSpecFields(patch, updates)
if err != nil {
return fmt.Errorf("failed to update odhdashboardconfig spec fields: %w", err)
}

Check warning on line 647 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L636-L647

Added lines #L636 - L647 were not covered by tests

if !updated {
log.Info("No changes needed, skipping patch", "namespace", cr.GetNamespace(), "name", cr.GetName())
continue

Check warning on line 651 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L649-L651

Added lines #L649 - L651 were not covered by tests
}

if err := cli.Patch(ctx, patch, client.MergeFrom(&cr)); err != nil {
return fmt.Errorf("failed to patch CR %s in namespace %s: %w", cr.GetName(), cr.GetNamespace(), err)
}

Check warning on line 656 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L654-L656

Added lines #L654 - L656 were not covered by tests

log.Info("Patched odhdashboardconfig successfully", "namespace", cr.GetNamespace(), "name", cr.GetName())

Check warning on line 658 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L658

Added line #L658 was not covered by tests
}

return nil

Check warning on line 661 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L661

Added line #L661 was not covered by tests
}

// 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)
}

Check warning on line 672 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L665-L672

Added lines #L665 - L672 were not covered by tests

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

Check warning on line 678 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L674-L678

Added lines #L674 - L678 were not covered by tests
}
}

return updated, nil

Check warning on line 682 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L682

Added line #L682 was not covered by tests
}

// 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": "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",
},
},
},
}

Check warning on line 740 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L686-L740

Added lines #L686 - L740 were not covered by tests
}

// 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{},
},
},
}

Check warning on line 792 in pkg/upgrade/upgrade.go

View check run for this annotation

Codecov / codecov/patch

pkg/upgrade/upgrade.go#L744-L792

Added lines #L744 - L792 were not covered by tests
}