Skip to content

Commit 0e26fd9

Browse files
committed
Refactor cyclomatic complexity for fluentbit plugins
Signed-off-by: Marco Franssen <[email protected]>
1 parent 07b9554 commit 0e26fd9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+738
-1583
lines changed

apis/fluentbit/v1alpha2/clusterfluentbitconfig_types_test.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import (
1717
. "github.com/onsi/gomega"
1818
)
1919

20-
var expected = `[Service]
20+
const (
21+
expected = `[Service]
2122
Daemon false
2223
Flush 1
2324
Grace 30
@@ -111,7 +112,8 @@ var expected = `[Service]
111112
tls On
112113
tls.verify true
113114
`
114-
var expectedYaml = `service:
115+
116+
expectedYaml = `service:
115117
daemon: false
116118
flush: 1
117119
grace: 30
@@ -206,7 +208,8 @@ pipeline:
206208
brokers: 127.0.1.1:9092
207209
topics: fluentbit-namespace
208210
`
209-
var expectedK8s = `[Service]
211+
212+
expectedK8s = `[Service]
210213
Daemon false
211214
Flush 1
212215
Grace 30
@@ -243,7 +246,7 @@ var expectedK8s = `[Service]
243246
Write_Operation update
244247
`
245248

246-
var expectedK8sYaml = `service:
249+
expectedK8sYaml = `service:
247250
daemon: false
248251
flush: 1
249252
grace: 30
@@ -280,7 +283,7 @@ pipeline:
280283
write_operation: update
281284
`
282285

283-
var expectedK8sYamlWithClusterFilterOutput = `service:
286+
expectedK8sYamlWithClusterFilterOutput = `service:
284287
daemon: false
285288
flush: 1
286289
grace: 30
@@ -327,7 +330,7 @@ pipeline:
327330
write_operation: update
328331
`
329332

330-
var expectedParsers = `[PARSER]
333+
expectedParsers = `[PARSER]
331334
Name clusterparser0
332335
Format json
333336
Time_Key time
@@ -346,7 +349,7 @@ var expectedParsers = `[PARSER]
346349
Types status:integer size:integer
347350
`
348351

349-
var expectedMultilineParsers = `[MULTILINE_PARSER]
352+
expectedMultilineParsers = `[MULTILINE_PARSER]
350353
Name clustermultilineparser0
351354
Type regex
352355
Parser go
@@ -365,6 +368,12 @@ var expectedMultilineParsers = `[MULTILINE_PARSER]
365368
Rule "cont" "/^\s+at.*/" "cont"
366369
`
367370

371+
FOOBAR = "foo:bar"
372+
APP_NAME = "app_name"
373+
APP_ID = "9780495d9db3"
374+
TESTING = "testing"
375+
)
376+
368377
var labels = map[string]string{
369378
"label0": "lv0",
370379
"label1": "lv1",
@@ -510,10 +519,10 @@ func Test_FluentBitConfig_RenderMainConfig(t *testing.T) {
510519

511520
headers := map[string]string{}
512521

513-
headers["Authorization"] = "foo:bar"
514-
headers["X-Log-Header-App-Name"] = "app_name"
515-
headers["X-Log-Header-0"] = "testing"
516-
headers["X-Log-Header-App-ID"] = "9780495d9db3"
522+
headers["Authorization"] = FOOBAR
523+
headers["X-Log-Header-App-Name"] = APP_NAME
524+
headers["X-Log-Header-0"] = TESTING
525+
headers["X-Log-Header-App-ID"] = APP_ID
517526

518527
httpOutput := ClusterOutput{
519528
TypeMeta: metav1.TypeMeta{
@@ -618,6 +627,7 @@ func Test_FluentBitConfig_RenderMainConfig(t *testing.T) {
618627
i++
619628
}
620629
}
630+
621631
func Test_FluentBitConfig_RenderMainConfigYaml(t *testing.T) {
622632
g := NewGomegaWithT(t)
623633

@@ -742,10 +752,10 @@ func Test_FluentBitConfig_RenderMainConfigYaml(t *testing.T) {
742752

743753
headers := map[string]string{}
744754

745-
headers["Authorization"] = "foo:bar"
746-
headers["X-Log-Header-App-Name"] = "app_name"
747-
headers["X-Log-Header-0"] = "testing"
748-
headers["X-Log-Header-App-ID"] = "9780495d9db3"
755+
headers["Authorization"] = FOOBAR
756+
headers["X-Log-Header-App-Name"] = APP_NAME
757+
headers["X-Log-Header-0"] = TESTING
758+
headers["X-Log-Header-App-ID"] = APP_ID
749759

750760
httpOutput := ClusterOutput{
751761
TypeMeta: metav1.TypeMeta{

apis/fluentbit/v1alpha2/clusteroutput_types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestClusterOutputList_Load(t *testing.T) {
166166
headers["Authorization"] = "foo:bar"
167167
headers["X-Log-Header-App-Name"] = "app_name"
168168
headers["X-Log-Header-0"] = "testing"
169-
headers["X-Log-Header-App-ID"] = "9780495d9db3"
169+
headers["X-Log-Header-App-ID"] = APP_ID
170170

171171
httpOutput := ClusterOutput{
172172
TypeMeta: metav1.TypeMeta{
@@ -306,7 +306,7 @@ func TestClusterOutputList_Load_As_Yaml(t *testing.T) {
306306
headers["Authorization"] = "foo:bar"
307307
headers["X-Log-Header-App-Name"] = "app_name"
308308
headers["X-Log-Header-0"] = "testing"
309-
headers["X-Log-Header-App-ID"] = "9780495d9db3"
309+
headers["X-Log-Header-App-ID"] = APP_ID
310310

311311
httpOutput := ClusterOutput{
312312
TypeMeta: metav1.TypeMeta{

apis/fluentbit/v1alpha2/plugins/common_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ type CommonParams struct {
1313
// Alias for the plugin
1414
Alias string `json:"alias,omitempty"`
1515
// RetryLimit describes how many times fluent-bit should retry to send data to a specific output. If set to false fluent-bit will try indefinitely. If set to any integer N>0 it will try at most N+1 times. Leading zeros are not allowed (values such as 007, 0150, 01 do not work). If this property is not defined fluent-bit will use the default value: 1.
16-
// nolint:misspell
1716
// +kubebuilder:validation:Pattern="^(((f|F)alse)|(no_limits)|(no_retries)|([1-9]+[0-9]*))$"
1817
RetryLimit string `json:"retryLimit,omitempty"`
1918
}

apis/fluentbit/v1alpha2/plugins/filter/kubernetes_types.go

Lines changed: 39 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -119,114 +119,45 @@ func (k *Kubernetes) Params(_ plugins.SecretLoader) (*params.KVs, error) {
119119
if err != nil {
120120
return kvs, err
121121
}
122-
if k.BufferSize != "" {
123-
kvs.Insert("Buffer_Size", k.BufferSize)
124-
}
125-
if k.KubeURL != "" {
126-
kvs.Insert("Kube_URL", k.KubeURL)
127-
}
128-
if k.KubeCAFile != "" {
129-
kvs.Insert("Kube_CA_File", k.KubeCAFile)
130-
}
131-
if k.KubeCAPath != "" {
132-
kvs.Insert("Kube_CA_Path", k.KubeCAPath)
133-
}
134-
if k.KubeTokenFile != "" {
135-
kvs.Insert("Kube_Token_File", k.KubeTokenFile)
136-
}
137-
if k.KubeTagPrefix != "" {
138-
kvs.Insert("Kube_Tag_Prefix", k.KubeTagPrefix)
139-
}
140-
if k.MergeLog != nil {
141-
kvs.Insert("Merge_Log", fmt.Sprint(*k.MergeLog))
142-
}
143-
if k.MergeLogKey != "" {
144-
kvs.Insert("Merge_Log_Key", k.MergeLogKey)
145-
}
146-
if k.MergeLogTrim != nil {
147-
kvs.Insert("Merge_Log_Trim", fmt.Sprint(*k.MergeLogTrim))
148-
}
149-
if k.MergeParser != "" {
150-
kvs.Insert("Merge_Parser", k.MergeParser)
151-
}
152-
if k.KeepLog != nil {
153-
kvs.Insert("Keep_Log", fmt.Sprint(*k.KeepLog))
154-
}
155-
if k.TLSDebug != nil {
156-
kvs.Insert("tls.debug", fmt.Sprint(*k.TLSDebug))
157-
}
158-
if k.TLSVerify != nil {
159-
kvs.Insert("tls.verify", fmt.Sprint(*k.TLSVerify))
160-
}
161-
if k.UseJournal != nil {
162-
kvs.Insert("Use_Journal", fmt.Sprint(*k.UseJournal))
163-
}
164-
if k.CacheUseDockerId != nil {
165-
kvs.Insert("Cache_Use_Docker_Id", fmt.Sprint(*k.CacheUseDockerId))
166-
}
167-
if k.RegexParser != "" {
168-
kvs.Insert("Regex_Parser", k.RegexParser)
169-
}
170-
if k.K8SLoggingParser != nil {
171-
kvs.Insert("K8S-Logging.Parser", fmt.Sprint(*k.K8SLoggingParser))
172-
}
173-
if k.K8SLoggingExclude != nil {
174-
kvs.Insert("K8S-Logging.Exclude", fmt.Sprint(*k.K8SLoggingExclude))
175-
}
176-
if k.Labels != nil {
177-
kvs.Insert("Labels", fmt.Sprint(*k.Labels))
178-
}
179-
if k.Annotations != nil {
180-
kvs.Insert("Annotations", fmt.Sprint(*k.Annotations))
181-
}
182-
if k.KubeMetaPreloadCacheDir != "" {
183-
kvs.Insert("Kube_meta_preload_cache_dir", k.KubeMetaPreloadCacheDir)
184-
}
185-
if k.DummyMeta != nil {
186-
kvs.Insert("Dummy_Meta", fmt.Sprint(*k.DummyMeta))
187-
}
188-
if k.DNSRetries != nil {
189-
kvs.Insert("DNS_Retries", fmt.Sprint(*k.DNSRetries))
190-
}
191-
if k.DNSWaitTime != nil {
192-
kvs.Insert("DNS_Wait_Time", fmt.Sprint(*k.DNSWaitTime))
193-
}
194-
if k.UseKubelet != nil {
195-
kvs.Insert("Use_Kubelet", fmt.Sprint(*k.UseKubelet))
196-
}
197-
if k.KubeletPort != nil {
198-
kvs.Insert("Kubelet_Port", fmt.Sprint(*k.KubeletPort))
199-
}
200-
if k.KubeletHost != "" {
201-
kvs.Insert("Kubelet_Host", k.KubeletHost)
202-
}
203-
if k.KubeMetaCacheTTL != "" {
204-
kvs.Insert("Kube_Meta_Cache_TTL", k.KubeMetaCacheTTL)
205-
}
206-
if k.KubeTokenTTL != "" {
207-
kvs.Insert("Kube_Token_TTL", k.KubeTokenTTL)
208-
}
209-
if k.KubeTokenCommand != "" {
210-
kvs.Insert("Kube_Token_Command", fmt.Sprint(k.KubeTokenCommand))
211-
}
212-
if k.KubeMetaNamespaceCacheTTL != nil {
213-
kvs.Insert("Kube_Meta_Namespace_Cache_TTL", fmt.Sprint(*k.KubeMetaNamespaceCacheTTL))
214-
}
215-
if k.NamespaceLabels != nil {
216-
kvs.Insert("Namespace_Labels", fmt.Sprint(*k.NamespaceLabels))
217-
}
218-
if k.NamespaceAnnotations != nil {
219-
kvs.Insert("Namespace_Annotations", fmt.Sprint(*k.NamespaceAnnotations))
220-
}
221-
if k.NamespaceMetadataOnly != nil {
222-
kvs.Insert("Namespace_Metadata_Only", fmt.Sprint(*k.NamespaceMetadataOnly))
223-
}
224-
if k.OwnerReferences != nil {
225-
kvs.Insert("Owner_References", fmt.Sprint(*k.OwnerReferences))
226-
}
227-
if k.UseTagForMeta != nil {
228-
kvs.Insert("Use_Tag_For_Meta", fmt.Sprint(*k.UseTagForMeta))
229-
}
122+
123+
plugins.InsertKVString(kvs, "Buffer_Size", k.BufferSize)
124+
plugins.InsertKVString(kvs, "Kube_URL", k.KubeURL)
125+
plugins.InsertKVString(kvs, "Kube_CA_File", k.KubeCAFile)
126+
plugins.InsertKVString(kvs, "Kube_CA_Path", k.KubeCAPath)
127+
plugins.InsertKVString(kvs, "Kube_Token_File", k.KubeTokenFile)
128+
plugins.InsertKVString(kvs, "Kube_Tag_Prefix", k.KubeTagPrefix)
129+
plugins.InsertKVString(kvs, "Merge_Log_Key", k.MergeLogKey)
130+
plugins.InsertKVString(kvs, "Merge_Parser", k.MergeParser)
131+
plugins.InsertKVString(kvs, "Regex_Parser", k.RegexParser)
132+
plugins.InsertKVString(kvs, "Kube_meta_preload_cache_dir", k.KubeMetaPreloadCacheDir)
133+
plugins.InsertKVString(kvs, "Kubelet_Host", k.KubeletHost)
134+
plugins.InsertKVString(kvs, "Kube_Meta_Cache_TTL", k.KubeMetaCacheTTL)
135+
plugins.InsertKVString(kvs, "Kube_Token_TTL", k.KubeTokenTTL)
136+
plugins.InsertKVString(kvs, "Kube_Token_Command", k.KubeTokenCommand)
137+
138+
plugins.InsertKVField(kvs, "Merge_Log", k.MergeLog)
139+
plugins.InsertKVField(kvs, "Merge_Log_Trim", k.MergeLogTrim)
140+
plugins.InsertKVField(kvs, "Keep_Log", k.KeepLog)
141+
plugins.InsertKVField(kvs, "tls.debug", k.TLSDebug)
142+
plugins.InsertKVField(kvs, "tls.verify", k.TLSVerify)
143+
plugins.InsertKVField(kvs, "Use_Journal", k.UseJournal)
144+
plugins.InsertKVField(kvs, "Cache_Use_Docker_Id", k.CacheUseDockerId)
145+
plugins.InsertKVField(kvs, "K8S-Logging.Parser", k.K8SLoggingParser)
146+
plugins.InsertKVField(kvs, "K8S-Logging.Exclude", k.K8SLoggingExclude)
147+
plugins.InsertKVField(kvs, "Labels", k.Labels)
148+
plugins.InsertKVField(kvs, "Annotations", k.Annotations)
149+
plugins.InsertKVField(kvs, "Dummy_Meta", k.DummyMeta)
150+
plugins.InsertKVField(kvs, "DNS_Retries", k.DNSRetries)
151+
plugins.InsertKVField(kvs, "DNS_Wait_Time", k.DNSWaitTime)
152+
plugins.InsertKVField(kvs, "Use_Kubelet", k.UseKubelet)
153+
plugins.InsertKVField(kvs, "Kubelet_Port", k.KubeletPort)
154+
plugins.InsertKVField(kvs, "Kube_Meta_Namespace_Cache_TTL", k.KubeMetaNamespaceCacheTTL)
155+
plugins.InsertKVField(kvs, "Namespace_Labels", k.NamespaceLabels)
156+
plugins.InsertKVField(kvs, "Namespace_Annotations", k.NamespaceAnnotations)
157+
plugins.InsertKVField(kvs, "Namespace_Metadata_Only", k.NamespaceMetadataOnly)
158+
plugins.InsertKVField(kvs, "Owner_References", k.OwnerReferences)
159+
plugins.InsertKVField(kvs, "Use_Tag_For_Meta", k.UseTagForMeta)
160+
230161
return kvs, nil
231162
}
232163

apis/fluentbit/v1alpha2/plugins/input/collectd.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package input
22

33
import (
4-
"fmt"
5-
64
"github.com/fluent/fluent-operator/v3/apis/fluentbit/v1alpha2/plugins"
75
"github.com/fluent/fluent-operator/v3/apis/fluentbit/v1alpha2/plugins/params"
86
)
@@ -29,14 +27,11 @@ func (*Collectd) Name() string {
2927
// implement Section() method
3028
func (c *Collectd) Params(_ plugins.SecretLoader) (*params.KVs, error) {
3129
kvs := params.NewKVs()
32-
if c.Listen != "" {
33-
kvs.Insert("Listen", c.Listen)
34-
}
35-
if c.Port != nil {
36-
kvs.Insert("Port", fmt.Sprint(*c.Port))
37-
}
38-
if c.TypesDB != "" {
39-
kvs.Insert("TypesDB", c.TypesDB)
40-
}
30+
31+
plugins.InsertKVString(kvs, "Listen", c.Listen)
32+
plugins.InsertKVString(kvs, "TypesDB", c.TypesDB)
33+
34+
plugins.InsertKVField(kvs, "Port", c.Port)
35+
4136
return kvs, nil
4237
}

apis/fluentbit/v1alpha2/plugins/input/dummy_types.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package input
22

33
import (
4-
"fmt"
5-
64
"github.com/fluent/fluent-operator/v3/apis/fluentbit/v1alpha2/plugins"
75
"github.com/fluent/fluent-operator/v3/apis/fluentbit/v1alpha2/plugins/params"
86
)
@@ -30,17 +28,12 @@ func (*Dummy) Name() string {
3028
// implement Section() method
3129
func (d *Dummy) Params(_ plugins.SecretLoader) (*params.KVs, error) {
3230
kvs := params.NewKVs()
33-
if d.Tag != "" {
34-
kvs.Insert("Tag", d.Tag)
35-
}
36-
if d.Dummy != "" {
37-
kvs.Insert("Dummy", d.Dummy)
38-
}
39-
if d.Rate != nil {
40-
kvs.Insert("Rate", fmt.Sprint(*d.Rate))
41-
}
42-
if d.Samples != nil {
43-
kvs.Insert("Samples", fmt.Sprint(*d.Samples))
44-
}
31+
32+
plugins.InsertKVString(kvs, "Tag", d.Tag)
33+
plugins.InsertKVString(kvs, "Dummy", d.Dummy)
34+
35+
plugins.InsertKVField(kvs, "Rate", d.Rate)
36+
plugins.InsertKVField(kvs, "Samples", d.Samples)
37+
4538
return kvs, nil
4639
}

0 commit comments

Comments
 (0)