From 0667f07601f6ce16a5821b3e41b2d15170360786 Mon Sep 17 00:00:00 2001 From: Toby Pinfold <28760528+TobyPinfold@users.noreply.github.com> Date: Thu, 12 Dec 2024 11:57:46 +0000 Subject: [PATCH] Add overrides within provider deployment construction --- controllers/pipelines/provider_controller.go | 53 +++++++++----------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/controllers/pipelines/provider_controller.go b/controllers/pipelines/provider_controller.go index 891711bdf..770acf2d3 100644 --- a/controllers/pipelines/provider_controller.go +++ b/controllers/pipelines/provider_controller.go @@ -18,7 +18,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" ) -// ProviderReconciler reconciles a Provider object type ProviderReconciler struct { StateHandler[*pipelinesv1.Provider] ResourceReconciler[*pipelinesv1.Provider] @@ -60,7 +59,7 @@ func (r *ProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c } if providerServiceDeployment == nil { - providerServiceDeployment = constructProviderServiceDeployment(providerServiceName, req.Namespace, provider) + providerServiceDeployment = constructProviderDeployment(providerServiceName, req.Namespace, provider, r.Config) if err := r.EC.Client.Create(ctx, providerServiceDeployment); err != nil { logger.Error(err, "unable to create provider service deployment") return ctrl.Result{}, err @@ -103,41 +102,39 @@ func (r *ProviderReconciler) fetchProviderServiceDeployment(ctx context.Context, return deployment, nil } -func constructProviderServiceDeployment(name, namespace string, provider *pipelinesv1.Provider) *appsv1.Deployment { - var replicas int32 = 1 +func replaceImageForContainersMarkedAsProviderService(containers []v1.Container, image string) []v1.Container { + for _, a := range containers { + if a.Name == "provider-service" { + a.Image = image + } + } + return containers +} + +func constructProviderDeployment(name, namespace string, provider *pipelinesv1.Provider, config config.KfpControllerConfigSpec) *appsv1.Deployment { + + replicas := int32(config.DefaultProviderValues.Replicas) + + podTemplate := config.DefaultProviderValues.PodTemplateSpec + podTemplate.Spec.Containers = replaceImageForContainersMarkedAsProviderService(podTemplate.Spec.Containers, provider.Spec.Image) + + const providerLabelKey = "provider" + podTemplate.ObjectMeta.Labels[providerLabelKey] = provider.GetName() + labels := config.DefaultProviderValues.Labels + labels[providerLabelKey] = provider.GetName() + return &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: namespace, - Labels: map[string]string{ - "app": "provider-service", - "provider": provider.GetName(), - }, + Labels: labels, }, Spec: appsv1.DeploymentSpec{ Selector: &metav1.LabelSelector{ - MatchLabels: map[string]string{ - "app": "provider-service", - "provider": provider.GetName(), - }, + MatchLabels: labels, }, Replicas: &replicas, - Template: v1.PodTemplateSpec{ - ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{ - "app": "provider-service", - "provider": provider.GetName(), - }, - }, - Spec: v1.PodSpec{ - Containers: []v1.Container{ - { - Name: "provider-service", - Image: provider.Spec.Image, - }, - }, - }, - }, + Template: podTemplate, }, } }