Skip to content

Commit 160036c

Browse files
baluchickenahma
authored andcommitted
Allow to add multiple configuration file to configmap (#66)
1 parent fdc3fa2 commit 160036c

File tree

7 files changed

+36
-37
lines changed

7 files changed

+36
-37
lines changed

pkg/apis/logging/v1alpha1/loggingplugin_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ type Parameter struct {
6262
// GetValue for a Parameter
6363
func (p Parameter) GetValue(namespace string, client client.Client) (string, string) {
6464
if p.ValueFrom != nil {
65-
value, error := p.ValueFrom.GetValue(namespace, client)
66-
if error != nil {
65+
value, err := p.ValueFrom.GetValue(namespace, client)
66+
if err != nil {
6767
return "", ""
6868
}
6969
return p.Name, value

pkg/controller/plugin/plugin_controller.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ package plugin
1818

1919
import (
2020
"context"
21+
2122
"github.com/banzaicloud/logging-operator/pkg/resources"
2223
"github.com/banzaicloud/logging-operator/pkg/resources/plugins"
2324

2425
loggingv1alpha1 "github.com/banzaicloud/logging-operator/pkg/apis/logging/v1alpha1"
2526

26-
"k8s.io/apimachinery/pkg/api/errors"
2727
"k8s.io/apimachinery/pkg/runtime"
2828
"sigs.k8s.io/controller-runtime/pkg/client"
2929
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -89,20 +89,15 @@ func (r *ReconcilePlugin) Reconcile(request reconcile.Request) (reconcile.Result
8989
reqLogger.Info("Reconciling Plugin")
9090

9191
// Fetch the Plugin instance
92-
instance := &loggingv1alpha1.Plugin{}
93-
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
92+
instanceList := &loggingv1alpha1.PluginList{}
93+
94+
err := r.client.List(context.TODO(), client.InNamespace(request.Namespace), instanceList)
9495
if err != nil {
95-
if errors.IsNotFound(err) {
96-
// Request object not found, could have been deleted after reconcile request.
97-
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
98-
// Return and don't requeue
99-
return reconcile.Result{}, nil
100-
}
101-
// Error reading the object - requeue the request.
10296
return reconcile.Result{}, err
10397
}
98+
10499
reconcilers := []resources.ComponentReconciler{
105-
plugins.New(r.client, instance),
100+
plugins.New(r.client, instanceList, request.Namespace),
106101
}
107102

108103
for _, rec := range reconcilers {

pkg/resources/plugins/configmap.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package plugins
1818

1919
import (
2020
"bytes"
21+
"text/template"
22+
2123
"github.com/Masterminds/sprig"
2224
loggingv1alpha1 "github.com/banzaicloud/logging-operator/pkg/apis/logging/v1alpha1"
2325
"github.com/banzaicloud/logging-operator/pkg/resources/templates"
@@ -26,14 +28,12 @@ import (
2628
corev1 "k8s.io/api/core/v1"
2729
"k8s.io/apimachinery/pkg/runtime"
2830
"sigs.k8s.io/controller-runtime/pkg/client"
29-
"text/template"
3031
)
3132

3233
func generateFluentdConfig(plugin *loggingv1alpha1.Plugin, client client.Client) (string, string) {
3334
var finalConfig string
3435
// Generate filters
3536
for _, filter := range plugin.Spec.Filter {
36-
logrus.Info("Applying filter")
3737
values, err := GetDefaultValues(filter.Type)
3838
if err != nil {
3939
logrus.Infof("Error in rendering template: %s", err)
@@ -92,15 +92,19 @@ func renderPlugin(plugin loggingv1alpha1.FPlugin, baseMap map[string]string, nam
9292
}
9393

9494
func (r *Reconciler) appConfigMap() runtime.Object {
95-
name, data := generateFluentdConfig(r.Plugin, r.Client)
96-
if name != "" {
97-
name = name + ".conf"
95+
appConfigData := map[string]string{}
96+
labels := map[string]string{}
97+
for _, plugin := range r.PluginList.Items {
98+
labels = util.MergeLabels(labels, plugin.Labels)
99+
name, data := generateFluentdConfig(&plugin, r.Client)
100+
if name != "" {
101+
name = name + ".conf"
102+
}
103+
appConfigData[name] = data
98104
}
99105

100106
return &corev1.ConfigMap{
101-
ObjectMeta: templates.PluginsObjectMeta(appConfigMapName, util.MergeLabels(r.Plugin.Labels, labelSelector), r.Plugin),
102-
Data: map[string]string{
103-
name: data,
104-
},
107+
ObjectMeta: templates.PluginsObjectMeta(appConfigMapName, util.MergeLabels(map[string]string{}, labelSelector), r.Namespace),
108+
Data: appConfigData,
105109
}
106110
}

pkg/resources/plugins/plugins.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ type Reconciler struct {
3939
}
4040

4141
// New creates a new FPlugin reconciler
42-
func New(client client.Client, plugin *loggingv1alpha1.Plugin) *Reconciler {
42+
func New(client client.Client, pluginList *loggingv1alpha1.PluginList, namespace string) *Reconciler {
4343
return &Reconciler{
4444
PluginReconciler: resources.PluginReconciler{
45-
Client: client,
46-
Plugin: plugin,
45+
Client: client,
46+
Namespace: namespace,
47+
PluginList: pluginList,
4748
},
4849
}
4950
}

pkg/resources/reconciler.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import (
2525

2626
// PluginReconciler reconciler struct for plugin
2727
type PluginReconciler struct {
28-
Client client.Client
29-
Plugin *loggingv1alpha1.Plugin
28+
Client client.Client
29+
Namespace string
30+
PluginList *loggingv1alpha1.PluginList
3031
}
3132

3233
// FluentdReconciler reconciler struct for fluentd
@@ -51,3 +52,6 @@ type Resource func() runtime.Object
5152

5253
// ResourceVariation redeclaration of function with parameter and return type kubernetes Object
5354
type ResourceVariation func(t string) runtime.Object
55+
56+
// ResourceWithLog redeclaration of function with logging parameter and return type kubernetes Object
57+
type ResourceWithLog func(log logr.Logger) runtime.Object

pkg/resources/templates/templates.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,11 @@ import (
2222
)
2323

2424
// PluginsObjectMeta creates an objectMeta for resource plugin
25-
func PluginsObjectMeta(name string, labels map[string]string, plugin *loggingv1alpha1.Plugin) metav1.ObjectMeta {
25+
func PluginsObjectMeta(name string, labels map[string]string, namespace string) metav1.ObjectMeta {
2626
o := metav1.ObjectMeta{
2727
Name: name,
28-
Namespace: plugin.Namespace,
28+
Namespace: namespace,
2929
Labels: labels,
30-
OwnerReferences: []metav1.OwnerReference{
31-
{
32-
APIVersion: plugin.APIVersion,
33-
Kind: plugin.Kind,
34-
Name: plugin.Name,
35-
UID: plugin.UID,
36-
},
37-
},
3830
}
3931
return o
4032
}

pkg/util/util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package util
1818

1919
// MergeLabels merges two map[string]string map
2020
func MergeLabels(l map[string]string, l2 map[string]string) map[string]string {
21+
if len(l) == 0 {
22+
l = map[string]string{}
23+
}
2124
for lKey, lValue := range l2 {
2225
l[lKey] = lValue
2326
}

0 commit comments

Comments
 (0)