From 669468905a64816b2ddd2098b9959bde6f99496d Mon Sep 17 00:00:00 2001 From: Balint Molnar Date: Wed, 6 Mar 2019 12:56:43 +0100 Subject: [PATCH] Allow to add multiple configuration file to configmap --- .../logging/v1alpha1/loggingplugin_types.go | 4 ++-- pkg/controller/plugin/plugin_controller.go | 17 +++++--------- pkg/resources/plugins/configmap.go | 22 +++++++++++-------- pkg/resources/plugins/plugins.go | 7 +++--- pkg/resources/reconciler.go | 8 +++++-- pkg/resources/templates/templates.go | 12 ++-------- pkg/util/util.go | 3 +++ 7 files changed, 36 insertions(+), 37 deletions(-) diff --git a/pkg/apis/logging/v1alpha1/loggingplugin_types.go b/pkg/apis/logging/v1alpha1/loggingplugin_types.go index 94a82ec26..fa82767f8 100644 --- a/pkg/apis/logging/v1alpha1/loggingplugin_types.go +++ b/pkg/apis/logging/v1alpha1/loggingplugin_types.go @@ -62,8 +62,8 @@ type Parameter struct { // GetValue for a Parameter func (p Parameter) GetValue(namespace string, client client.Client) (string, string) { if p.ValueFrom != nil { - value, error := p.ValueFrom.GetValue(namespace, client) - if error != nil { + value, err := p.ValueFrom.GetValue(namespace, client) + if err != nil { return "", "" } return p.Name, value diff --git a/pkg/controller/plugin/plugin_controller.go b/pkg/controller/plugin/plugin_controller.go index 53eb8135e..514d913fa 100644 --- a/pkg/controller/plugin/plugin_controller.go +++ b/pkg/controller/plugin/plugin_controller.go @@ -18,12 +18,12 @@ package plugin import ( "context" + "github.com/banzaicloud/logging-operator/pkg/resources" "github.com/banzaicloud/logging-operator/pkg/resources/plugins" loggingv1alpha1 "github.com/banzaicloud/logging-operator/pkg/apis/logging/v1alpha1" - "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" @@ -89,20 +89,15 @@ func (r *ReconcilePlugin) Reconcile(request reconcile.Request) (reconcile.Result reqLogger.Info("Reconciling Plugin") // Fetch the Plugin instance - instance := &loggingv1alpha1.Plugin{} - err := r.client.Get(context.TODO(), request.NamespacedName, instance) + instanceList := &loggingv1alpha1.PluginList{} + + err := r.client.List(context.TODO(), client.InNamespace(request.Namespace), instanceList) if err != nil { - if errors.IsNotFound(err) { - // Request object not found, could have been deleted after reconcile request. - // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. - // Return and don't requeue - return reconcile.Result{}, nil - } - // Error reading the object - requeue the request. return reconcile.Result{}, err } + reconcilers := []resources.ComponentReconciler{ - plugins.New(r.client, instance), + plugins.New(r.client, instanceList, request.Namespace), } for _, rec := range reconcilers { diff --git a/pkg/resources/plugins/configmap.go b/pkg/resources/plugins/configmap.go index d19565927..ee9781966 100644 --- a/pkg/resources/plugins/configmap.go +++ b/pkg/resources/plugins/configmap.go @@ -18,6 +18,8 @@ package plugins import ( "bytes" + "text/template" + "github.com/Masterminds/sprig" loggingv1alpha1 "github.com/banzaicloud/logging-operator/pkg/apis/logging/v1alpha1" "github.com/banzaicloud/logging-operator/pkg/resources/templates" @@ -26,14 +28,12 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "text/template" ) func generateFluentdConfig(plugin *loggingv1alpha1.Plugin, client client.Client) (string, string) { var finalConfig string // Generate filters for _, filter := range plugin.Spec.Filter { - logrus.Info("Applying filter") values, err := GetDefaultValues(filter.Type) if err != nil { logrus.Infof("Error in rendering template: %s", err) @@ -92,15 +92,19 @@ func renderPlugin(plugin loggingv1alpha1.FPlugin, baseMap map[string]string, nam } func (r *Reconciler) appConfigMap() runtime.Object { - name, data := generateFluentdConfig(r.Plugin, r.Client) - if name != "" { - name = name + ".conf" + appConfigData := map[string]string{} + labels := map[string]string{} + for _, plugin := range r.PluginList.Items { + labels = util.MergeLabels(labels, plugin.Labels) + name, data := generateFluentdConfig(&plugin, r.Client) + if name != "" { + name = name + ".conf" + } + appConfigData[name] = data } return &corev1.ConfigMap{ - ObjectMeta: templates.PluginsObjectMeta(appConfigMapName, util.MergeLabels(r.Plugin.Labels, labelSelector), r.Plugin), - Data: map[string]string{ - name: data, - }, + ObjectMeta: templates.PluginsObjectMeta(appConfigMapName, util.MergeLabels(map[string]string{}, labelSelector), r.Namespace), + Data: appConfigData, } } diff --git a/pkg/resources/plugins/plugins.go b/pkg/resources/plugins/plugins.go index ea720061e..12fbd1474 100644 --- a/pkg/resources/plugins/plugins.go +++ b/pkg/resources/plugins/plugins.go @@ -39,11 +39,12 @@ type Reconciler struct { } // New creates a new FPlugin reconciler -func New(client client.Client, plugin *loggingv1alpha1.Plugin) *Reconciler { +func New(client client.Client, pluginList *loggingv1alpha1.PluginList, namespace string) *Reconciler { return &Reconciler{ PluginReconciler: resources.PluginReconciler{ - Client: client, - Plugin: plugin, + Client: client, + Namespace: namespace, + PluginList: pluginList, }, } } diff --git a/pkg/resources/reconciler.go b/pkg/resources/reconciler.go index b921141e3..87c85d500 100644 --- a/pkg/resources/reconciler.go +++ b/pkg/resources/reconciler.go @@ -25,8 +25,9 @@ import ( // PluginReconciler reconciler struct for plugin type PluginReconciler struct { - Client client.Client - Plugin *loggingv1alpha1.Plugin + Client client.Client + Namespace string + PluginList *loggingv1alpha1.PluginList } // FluentdReconciler reconciler struct for fluentd @@ -51,3 +52,6 @@ type Resource func() runtime.Object // ResourceVariation redeclaration of function with parameter and return type kubernetes Object type ResourceVariation func(t string) runtime.Object + +// ResourceWithLog redeclaration of function with logging parameter and return type kubernetes Object +type ResourceWithLog func(log logr.Logger) runtime.Object diff --git a/pkg/resources/templates/templates.go b/pkg/resources/templates/templates.go index 0fd52bba8..a4e71a3c7 100644 --- a/pkg/resources/templates/templates.go +++ b/pkg/resources/templates/templates.go @@ -22,19 +22,11 @@ import ( ) // PluginsObjectMeta creates an objectMeta for resource plugin -func PluginsObjectMeta(name string, labels map[string]string, plugin *loggingv1alpha1.Plugin) metav1.ObjectMeta { +func PluginsObjectMeta(name string, labels map[string]string, namespace string) metav1.ObjectMeta { o := metav1.ObjectMeta{ Name: name, - Namespace: plugin.Namespace, + Namespace: namespace, Labels: labels, - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: plugin.APIVersion, - Kind: plugin.Kind, - Name: plugin.Name, - UID: plugin.UID, - }, - }, } return o } diff --git a/pkg/util/util.go b/pkg/util/util.go index 9f71deeef..f69706ebe 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -18,6 +18,9 @@ package util // MergeLabels merges two map[string]string map func MergeLabels(l map[string]string, l2 map[string]string) map[string]string { + if len(l) == 0 { + l = map[string]string{} + } for lKey, lValue := range l2 { l[lKey] = lValue }