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

Add "controllerutil" helpers for applyconfigurations (SSA) #2674

Open
erikgb opened this issue Feb 4, 2024 · 4 comments
Open

Add "controllerutil" helpers for applyconfigurations (SSA) #2674

erikgb opened this issue Feb 4, 2024 · 4 comments
Labels
kind/feature Categorizes issue or PR as related to a new feature. kind/support Categorizes issue or PR as a support question.

Comments

@erikgb
Copy link
Contributor

erikgb commented Feb 4, 2024

We are making extensive use of the helpers defined in https://github.com/kubernetes-sigs/controller-runtime/blob/main/pkg/controller/controllerutil/controllerutil.go, and while migrating our controllers to SSA we had to create our own modified copies based on corev1/metav1 applyconfigurations.

I think it could make sense adding a SSA-flavoured edition of these simple (but useful) helpers to controller-runtime. And I would be happy to submit a PR if you agree. The most challenging is probably to decide the package name/location. WDYT @alvaroaleman @vincepri @sbueringer?

@troy0820
Copy link
Member

troy0820 commented Feb 5, 2024

/kind support feature

@k8s-ci-robot k8s-ci-robot added kind/support Categorizes issue or PR as a support question. kind/feature Categorizes issue or PR as related to a new feature. labels Feb 5, 2024
@troy0820
Copy link
Member

troy0820 commented Mar 8, 2024

The tricky part here is the applyconfigurations that are not available for custom resources. (Should we deal with controller-tools to have that be created when you scaffold kubebuilder?)

Do we only plan to support this for corev1 and the like? I think server side apply was looking to be done on the client.Client interface as well. Doing this in controllerutils may help migrate that to the interface. I'm interested in this as well.

@erikgb
Copy link
Contributor Author

erikgb commented Mar 9, 2024

I just want to add the same helper as we have for the normal types. Here is an example from the modified helper we are using now:

import (
	"fmt"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	metav1ac "k8s.io/client-go/applyconfigurations/meta/v1"
	"k8s.io/utils/ptr"
	"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)

// SetControllerReference sets owner as a Controller OwnerReference on controlled.
// This is used for garbage collection of the controlled object and for
// reconciling the owner object on changes to controlled (with a Watch + EnqueueRequestForOwner).
func SetControllerReference(owner metav1.Object, controlled *metav1ac.ObjectMetaApplyConfiguration, scheme *runtime.Scheme) error {
	// Validate the owner.
	ro, ok := owner.(runtime.Object)
	if !ok {
		return fmt.Errorf("%T is not a runtime.Object, cannot call SetControllerReference", owner)
	}
	if err := validateOwner(owner, controlled); err != nil {
		return err
	}

	gvk, err := apiutil.GVKForObject(ro, scheme)
	if err != nil {
		return err
	}
	controlled.WithOwnerReferences(
		metav1ac.OwnerReference().
			WithAPIVersion(gvk.GroupVersion().String()).
			WithKind(gvk.Kind).
			WithName(owner.GetName()).
			WithUID(owner.GetUID()).
			WithBlockOwnerDeletion(true).
			WithController(true),
	)
	return nil
}

@erikgb
Copy link
Contributor Author

erikgb commented Mar 9, 2024

This might be possible to solve with generics also. I can look into a generics solution, as it will probably make the code more compact with less duplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature. kind/support Categorizes issue or PR as a support question.
Projects
None yet
Development

No branches or pull requests

3 participants