-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: DataPlaneKonnectExtension customization
Signed-off-by: Mattia Lavacca <[email protected]>
- Loading branch information
Showing
17 changed files
with
423 additions
and
47 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
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
117 changes: 117 additions & 0 deletions
117
controller/dataplane/dataplanekonnectextension_controller.go
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,117 @@ | ||
package dataplane | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
|
||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
ctrllog "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
operatorv1alpha1 "github.com/kong/gateway-operator/api/v1alpha1" | ||
operatorv1beta1 "github.com/kong/gateway-operator/api/v1beta1" | ||
"github.com/kong/gateway-operator/controller/pkg/ctxinjector" | ||
"github.com/kong/gateway-operator/controller/pkg/log" | ||
operatorerrors "github.com/kong/gateway-operator/internal/errors" | ||
"github.com/kong/gateway-operator/internal/utils/index" | ||
"github.com/kong/gateway-operator/pkg/consts" | ||
) | ||
|
||
// ----------------------------------------------------------------------------- | ||
// DataKonnectExtensionReconciler | ||
// ----------------------------------------------------------------------------- | ||
|
||
// DataPlaneKonnectExtensionReconciler reconciles a DataPlaneKonnectExtension object. | ||
type DataPlaneKonnectExtensionReconciler struct { | ||
client.Client | ||
ContextInjector ctxinjector.CtxInjector | ||
// DevelopmentMode indicates if the controller should run in development mode, | ||
// which causes it to e.g. perform less validations. | ||
DevelopmentMode bool | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *DataPlaneKonnectExtensionReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&operatorv1alpha1.DataPlaneKonnectExtension{}). | ||
Watches(&operatorv1beta1.DataPlane{}, handler.EnqueueRequestsFromMapFunc(r.listDataPlaneExtensionsReferenced)). | ||
Complete(r) | ||
} | ||
|
||
// listDataPlaneExtensionsReferenced returns a list of all the DataPlaneKonnectExtensions referenced by the DataPlane object. | ||
// Maximum one reference is expected. | ||
func (r *DataPlaneKonnectExtensionReconciler) listDataPlaneExtensionsReferenced(ctx context.Context, obj client.Object) []reconcile.Request { | ||
logger := ctrllog.FromContext(ctx) | ||
dataPlane, ok := obj.(*operatorv1beta1.DataPlane) | ||
if !ok { | ||
logger.Error( | ||
operatorerrors.ErrUnexpectedObject, | ||
"failed to run map funcs", | ||
"expected", "DataPlane", "found", reflect.TypeOf(obj), | ||
) | ||
return nil | ||
} | ||
|
||
if len(dataPlane.Spec.Extensions) == 0 { | ||
return nil | ||
} | ||
|
||
recs := []reconcile.Request{} | ||
for _, extension := range dataPlane.Spec.Extensions { | ||
namespace := dataPlane.Namespace | ||
if extension.Group == operatorv1alpha1.SchemeGroupVersion.Group && | ||
extension.Kind == "DataPlaneKonnectExtension" { | ||
if extension.Namespace != nil { | ||
namespace = *extension.Namespace | ||
} | ||
recs = append(recs, reconcile.Request{ | ||
NamespacedName: client.ObjectKey{ | ||
Namespace: namespace, | ||
Name: extension.Name, | ||
}, | ||
}) | ||
} | ||
} | ||
return recs | ||
} | ||
|
||
// Reconcile reconciles a DataPlaneKonnectExtension object. | ||
func (r *DataPlaneKonnectExtensionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
ctx = r.ContextInjector.InjectKeyValues(ctx) | ||
var konnectExtension operatorv1alpha1.DataPlaneKonnectExtension | ||
if err := r.Client.Get(ctx, req.NamespacedName, &konnectExtension); err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
logger := log.GetLogger(ctx, "dataplaneKonnectExtension", r.DevelopmentMode) | ||
var dataPlaneList operatorv1beta1.DataPlaneList | ||
if err := r.List(ctx, &dataPlaneList, client.MatchingFields{ | ||
index.DataPlaneKonnectExtensionIndex: konnectExtension.Namespace + "/" + konnectExtension.Name, | ||
}); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
var updated bool | ||
switch len(dataPlaneList.Items) { | ||
case 0: | ||
updated = controllerutil.RemoveFinalizer(&konnectExtension, consts.DataPlaneExtensionFinalizer) | ||
default: | ||
updated = controllerutil.AddFinalizer(&konnectExtension, consts.DataPlaneExtensionFinalizer) | ||
} | ||
if updated { | ||
if err := r.Client.Update(ctx, &konnectExtension); err != nil { | ||
if k8serrors.IsConflict(err) { | ||
return ctrl.Result{Requeue: true}, nil | ||
} | ||
return ctrl.Result{}, err | ||
} | ||
|
||
log.Info(logger, "DataPlaneKonnectExtension finalizer updated", konnectExtension) | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} |
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,113 @@ | ||
package dataplane | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/samber/lo" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/kong/gateway-operator/api/v1alpha1" | ||
"github.com/kong/gateway-operator/api/v1beta1" | ||
dputils "github.com/kong/gateway-operator/internal/utils/dataplane" | ||
"github.com/kong/gateway-operator/pkg/consts" | ||
k8sresources "github.com/kong/gateway-operator/pkg/utils/kubernetes/resources" | ||
) | ||
|
||
// applyDataPlaneKonnectExtension gets the DataPlane as argument, and in case it references a KonnectExtension, it | ||
// fetch the referenced extension and applies the necessary changes to the DataPlane spec. | ||
func applyDataPlaneKonnectExtension(ctx context.Context, cl client.Client, dataplane *v1beta1.DataPlane) error { | ||
if len(dataplane.Spec.Extensions) == 0 { | ||
return nil | ||
} | ||
|
||
for _, extensionRef := range dataplane.Spec.Extensions { | ||
if extensionRef.Group != v1alpha1.SchemeGroupVersion.Group || extensionRef.Kind != "DataPlaneKonnectExtension" { | ||
continue | ||
} | ||
namespace := dataplane.Namespace | ||
if extensionRef.Namespace != nil { | ||
namespace = *extensionRef.Namespace | ||
} | ||
|
||
konnectExt := v1alpha1.DataPlaneKonnectExtension{} | ||
if err := cl.Get(ctx, client.ObjectKey{ | ||
Namespace: namespace, | ||
Name: extensionRef.Name, | ||
}, &konnectExt); err != nil { | ||
return err | ||
} | ||
|
||
d := k8sresources.Deployment(appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: *dataplane.Spec.Deployment.PodTemplateSpec, | ||
}, | ||
}) | ||
|
||
d.WithVolume(kongInKonnectClusterCertificateVolume()) | ||
d.WithVolumeMount(kongInKonnectClusterCertificateVolumeMount(), consts.DataPlaneProxyContainerName) | ||
d.WithVolume(kongInKonnectClusterCertVolume(konnectExt.Spec.AuthConfiguration.ClusterCertificateSecretName.Name)) | ||
d.WithVolumeMount(kongInKonnectClusterVolumeMount(), consts.DataPlaneProxyContainerName) | ||
|
||
envSet := customizeKongInKonnectDefaults( | ||
dputils.KongInKonnectDefaults, | ||
*konnectExt.Spec.ControlPlaneRef.KonnectID, | ||
konnectExt.Spec.ControlPlaneRegion, | ||
konnectExt.Spec.ServerHostname) | ||
|
||
dputils.FillDataPlaneProxyContainerEnvs(nil, &d.Spec.Template, envSet) | ||
dataplane.Spec.Deployment.PodTemplateSpec = &d.Spec.Template | ||
} | ||
return nil | ||
} | ||
|
||
// customizeKongInKonnectDefaults replaces placeholders in the KongInKonnect env list with the actual values. | ||
func customizeKongInKonnectDefaults(envSet map[string]string, | ||
controlPlane, | ||
region, | ||
server string, | ||
) map[string]string { | ||
newEnvSet := make(map[string]string, len(envSet)) | ||
for k, v := range envSet { | ||
v = strings.ReplaceAll(v, "<CP-ID>", controlPlane) | ||
v = strings.ReplaceAll(v, "<REGION>", region) | ||
v = strings.ReplaceAll(v, "<SERVER>", server) | ||
newEnvSet[k] = v | ||
} | ||
return newEnvSet | ||
} | ||
|
||
func kongInKonnectClusterCertVolume(secretName string) corev1.Volume { | ||
return corev1.Volume{ | ||
Name: consts.KongClusterCertVolume, | ||
VolumeSource: corev1.VolumeSource{ | ||
Secret: &corev1.SecretVolumeSource{ | ||
SecretName: secretName, | ||
DefaultMode: lo.ToPtr(int32(420)), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func kongInKonnectClusterVolumeMount() corev1.VolumeMount { | ||
return corev1.VolumeMount{ | ||
Name: consts.KongClusterCertVolume, | ||
MountPath: consts.KongClusterCertVolumeMountPath, | ||
} | ||
} | ||
|
||
func kongInKonnectClusterCertificateVolume() corev1.Volume { | ||
return corev1.Volume{ | ||
Name: consts.ClusterCertificateVolume, | ||
} | ||
} | ||
|
||
func kongInKonnectClusterCertificateVolumeMount() corev1.VolumeMount { | ||
return corev1.VolumeMount{ | ||
Name: consts.ClusterCertificateVolume, | ||
MountPath: consts.ClusterCertificateVolumeMountPath, | ||
ReadOnly: true, | ||
} | ||
} |
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
Oops, something went wrong.