From 9bcba88d0acf7af0b74ee4e2623213966bdaeaf7 Mon Sep 17 00:00:00 2001 From: dd di cesare Date: Tue, 31 May 2022 20:58:57 +0200 Subject: [PATCH] [controller] Propagating Limitador's env vars * limitador namespace * limitador service name * limitador service grpc port * Using keyed values --- controllers/kuadrant_controller.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/controllers/kuadrant_controller.go b/controllers/kuadrant_controller.go index 1184c5d40..3ef523bae 100644 --- a/controllers/kuadrant_controller.go +++ b/controllers/kuadrant_controller.go @@ -21,13 +21,13 @@ import ( "encoding/json" "errors" "fmt" - "github.com/go-logr/logr" authorinov1beta1 "github.com/kuadrant/authorino-operator/api/v1beta1" limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1" istioapiv1alpha1 "istio.io/api/operator/v1alpha1" iopv1alpha1 "istio.io/istio/operator/pkg/apis/istio/v1alpha1" appsv1 "k8s.io/api/apps/v1" + v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -43,8 +43,15 @@ import ( ) const ( - kuadrantFinalizer = "kuadrant.kuadrant.io/finalizer" - extAuthorizerName = "kuadrant-authorization" + kuadrantFinalizer = "kuadrant.kuadrant.io/finalizer" + extAuthorizerName = "kuadrant-authorization" + envLimitadorServiceName = "LIMITADOR_SERVICE_NAME" + envLimitadorServiceGrpcPort = "LIMITADOR_SERVICE_GRPC_PORT" +) + +var ( + limitadorServiceName = common.FetchEnv(envLimitadorServiceName, "limitador") + limitadorServiceGrpcPort = common.FetchEnv(envLimitadorServiceGrpcPort, "8081") ) // KuadrantReconciler reconciles a Kuadrant object @@ -386,9 +393,19 @@ func (r *KuadrantReconciler) createOnlyInKuadrantNSCb(ctx context.Context, kObj return err } + switch obj := k8sObj.(type) { + case *appsv1.Deployment: // If it's a Deployment obj, it adds the required env vars + obj.Spec.Template.Spec.Containers[0].Env = []v1.EnvVar{ + {Name: "LIMITADOR_NAMESPACE", Value: kObj.Namespace, ValueFrom: &v1.EnvVarSource{}}, + {Name: envLimitadorServiceName, Value: limitadorServiceName, ValueFrom: &v1.EnvVarSource{}}, + {Name: envLimitadorServiceGrpcPort, Value: limitadorServiceGrpcPort, ValueFrom: &v1.EnvVarSource{}}, + } + err = r.Client().Create(ctx, obj) + default: + err = r.Client().Create(ctx, k8sObj) + } k8sObjKind := k8sObj.DeepCopyObject().GetObjectKind() - err = r.Client().Create(ctx, k8sObj) logger.V(1).Info("create resource", "GKV", k8sObjKind.GroupVersionKind(), "name", k8sObj.GetName(), "error", err) if err != nil { if apierrors.IsAlreadyExists(err) {