Skip to content

Server-Side Apply Helper for Kubernetes Custom Controller

License

Notifications You must be signed in to change notification settings

zoetrope/ssa-helper

Repository files navigation

Server-Side Apply Helper for Kubernetes Custom Controller

client-go provides ApplyConfiguration types for typesafe Server-Side Apply. This repository provides helper functions for convenient use of server-side apply in your custom controller.

DeepCopy

ApplyConfiguration types provided by client-go don't have a DeepCopy function. Especially it would be inconvenient when you embed an ApplyConfiguration type into your custom resource. This repository provides the DeepCopy functions for ApplyConfiguration types generated by deepcopy-gen.

You can call DeepCopy of an ApplyConfiguration type as follows:

dep1 := appsv1.Deployment("nginx", "default").
	WithLabels(map[string]string{"component": "nginx"}).
	WithSpec(appsv1.DeploymentSpec().
		WithReplicas(1).
		WithSelector(metav1.LabelSelector().WithMatchLabels(map[string]string{"component": "nginx"})).
		WithTemplate(corev1.PodTemplateSpec().
			WithLabels(map[string]string{"component": "nginx"}).
			WithSpec(corev1.PodSpec().
				WithContainers(corev1.Container().
					WithName("nginx").
					WithImage("nginx:latest"),
				),
			),
		),
	)

dep2 := dep1.DeepCopy()

Apply

The implementation of Server-Side Apply using Extract function (e.g. ExtractDeployment) is a bit redundant. This repository provides Apply function for ApplyConfiguration types.

dep := appsv1.Deployment("nginx", "default").
	WithLabels(map[string]string{"component": "nginx"}).
	WithSpec(appsv1.DeploymentSpec().
		WithReplicas(1).
		WithSelector(metav1.LabelSelector().WithMatchLabels(map[string]string{"component": "nginx"})).
		WithTemplate(corev1.PodTemplateSpec().
			WithLabels(map[string]string{"component": "nginx"}).
			WithSpec(corev1.PodSpec().
				WithContainers(corev1.Container().
					WithName("nginx").
					WithImage("nginx:latest"),
				),
			),
		),
	)
err := applyconfigurations.Apply(ctx, k8sClient, dep, "field-manager-name")
if err != nil {
	return err
}

OwnerReference

controller-runtime provides controllerutil.SetControllerReference. This function cannot be used for ApplyConfiguration types.

This repository provides OwnerReference function to get the owner reference of other resource for ApplyConfiguration types. You can use WithOwnerReferences function to set the owner reference to ApplyConfiguration types.

ownerRef, err := applyconfigurations.OwnerReference(owner, scheme)
if err != nil {
	return err
}

dep := appsv1.Deployment("nginx", "default").
	WithOwnerReferences(ownerRef).
	WithLabels(map[string]string{"component": "nginx"}).
	WithSpec(appsv1.DeploymentSpec().
		WithReplicas(1).
		WithSelector(metav1.LabelSelector().WithMatchLabels(map[string]string{"component": "nginx"})).
		WithTemplate(corev1.PodTemplateSpec().
			WithLabels(map[string]string{"component": "nginx"}).
			WithSpec(corev1.PodSpec().
				WithContainers(corev1.Container().
					WithName("nginx").
					WithImage("nginx:latest"),
				),
			),
		),
	)

About

Server-Side Apply Helper for Kubernetes Custom Controller

Resources

License

Stars

Watchers

Forks

Packages

No packages published