forked from opendatahub-io/opendatahub-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove DSC ownership from deployed components resources (opendatahub-…
…io#1437) * Remove DSC ownership from deployed components resources Component owner resources must be reowned from DSC to the component specific CR * Fix findings * unit tests * Fix linter * Fix e2e
- Loading branch information
1 parent
f9a2b9b
commit 6d836db
Showing
12 changed files
with
461 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package deploy | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk" | ||
) | ||
|
||
func isLegacyOwnerRef(or metav1.OwnerReference) bool { | ||
switch { | ||
case or.APIVersion == gvk.DataScienceCluster.GroupVersion().String() && or.Kind == gvk.DataScienceCluster.Kind: | ||
return true | ||
case or.APIVersion == gvk.DSCInitialization.GroupVersion().String() && or.Kind == gvk.DSCInitialization.Kind: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
// removeOwnerReferences removes all owner references from a Kubernetes object that match the provided predicate. | ||
// | ||
// This function iterates through the OwnerReferences of the given object, filters out those that satisfy | ||
// the predicate, and updates the object in the cluster using the provided client. | ||
// | ||
// Parameters: | ||
// - ctx: The context for the request, which can carry deadlines, cancellation signals, and other request-scoped values. | ||
// - cli: A controller-runtime client used to update the Kubernetes object. | ||
// - obj: The Kubernetes object whose OwnerReferences are to be filtered. It must implement client.Object. | ||
// - predicate: A function that takes an OwnerReference and returns true if the reference should be removed. | ||
// | ||
// Returns: | ||
// - An error if the update operation fails, otherwise nil. | ||
func removeOwnerReferences( | ||
ctx context.Context, | ||
cli client.Client, | ||
obj client.Object, | ||
predicate func(reference metav1.OwnerReference) bool, | ||
) error { | ||
oldRefs := obj.GetOwnerReferences() | ||
if len(oldRefs) == 0 { | ||
return nil | ||
} | ||
|
||
newRefs := oldRefs[:0] | ||
for _, ref := range oldRefs { | ||
if !predicate(ref) { | ||
newRefs = append(newRefs, ref) | ||
} | ||
} | ||
|
||
if len(newRefs) == len(oldRefs) { | ||
return nil | ||
} | ||
|
||
obj.SetOwnerReferences(newRefs) | ||
|
||
// Update the object in the cluster | ||
if err := cli.Update(ctx, obj); err != nil { | ||
return fmt.Errorf( | ||
"failed to remove owner references from object %s/%s with gvk %s: %w", | ||
obj.GetNamespace(), | ||
obj.GetName(), | ||
obj.GetObjectKind().GroupVersionKind(), | ||
err, | ||
) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.