Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add in_sample plugin to fluentd to facilitate tests. #937

Merged
merged 12 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions apis/fluentd/v1alpha1/plugins/input/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package input

// The in_sample input plugin generates sample events. It is useful for testing, debugging, benchmarking and getting started with Fluentd.
type Sample struct {
// The tag of the event. The value is the tag assigned to the generated events.
Tag *string `json:"tag,omitempty"`
// The number of events in the event stream of each emit.
Size *int64 `json:"size,omitempty"`
// It configures how many events to generate per second.
Rate *int64 `json:"rate,omitempty"`
// If specified, each generated event has an auto-incremented key field.
AutoIncrementKey *string `json:"autoIncrementKey,omitempty"`
// The sample data to be generated. It should be either an array of JSON hashes or a single JSON hash. If it is an array of JSON hashes, the hashes in the array are cycled through in order.
Sample *string `json:"sample,omitempty"`
}
27 changes: 27 additions & 0 deletions apis/fluentd/v1alpha1/plugins/input/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Input struct {
Http *Http `json:"http,omitempty"`
// in_tail plugin
Tail *Tail `json:"tail,omitempty"`
// in_sample plugin
Sample *Sample `json:"sample,omitempty"`
}

// DeepCopyInto implements the DeepCopyInto interface.
Expand Down Expand Up @@ -78,6 +80,11 @@ func (i *Input) Params(loader plugins.SecretLoader) (*params.PluginStore, error)
return i.tailPlugin(ps, loader), nil
}

if i.Sample != nil {
ps.InsertType(string(params.SampleInputType))
return i.samplePlugin(ps, loader), nil
}

return nil, errors.New("you must define an input plugin")
}

Expand Down Expand Up @@ -329,4 +336,24 @@ func (i *Input) httpPlugin(parent *params.PluginStore, loader plugins.SecretLoad
return parent
}

func (i *Input) samplePlugin(parent *params.PluginStore, loader plugins.SecretLoader) *params.PluginStore {
sampleModel := i.Sample
if sampleModel.Tag != nil {
parent.InsertPairs("tag", fmt.Sprint(*sampleModel.Tag))
}
if sampleModel.Rate != nil {
parent.InsertPairs("rate", fmt.Sprint(*sampleModel.Rate))
}
if sampleModel.Size != nil {
parent.InsertPairs("size", fmt.Sprint(*sampleModel.Size))
}
if sampleModel.AutoIncrementKey != nil {
parent.InsertPairs("auto_increment_key", fmt.Sprint(*sampleModel.AutoIncrementKey))
}
if sampleModel.Sample != nil {
parent.InsertPairs("sample", fmt.Sprint(*sampleModel.Sample))
}
return parent
}

var _ plugins.Plugin = &Input{}
1 change: 1 addition & 0 deletions apis/fluentd/v1alpha1/plugins/params/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
HttpInputType InputType = "http"
ForwardInputType InputType = "forward"
TailInputType InputType = "tail"
SampleInputType InputType = "sample"

// Enums the supported filter types
RecordTransformerFilterType FilterType = "record_transformer"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<source>
@type sample
auto_increment_key id
rate 10
sample {"hello": "world"}
size 10
tag foo.bar
</source>
<match **>
@id main
@type label_router
<route>
@label @2d9e59757d3bfc66d93c3bc44b408922
<match>
namespaces fluent
</match>
</route>
</match>
<label @2d9e59757d3bfc66d93c3bc44b408922>
<match foo.*>
@id FluentdConfig-fluent-fluentd-config::cluster::clusteroutput::fluentd-output-stdout-0
@type stdout
</match>
</label>
22 changes: 22 additions & 0 deletions apis/fluentd/v1alpha1/tests/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ func Test_ClusterCfgInputTail(t *testing.T) {

}

func Test_ClusterCfgInputSample(t *testing.T) {
g := NewGomegaWithT(t)
sl := plugins.NewSecretLoader(nil, Fluentd.Namespace, logr.Logger{})

psr := fluentdv1alpha1.NewGlobalPluginResources("main")
psr.CombineGlobalInputsPlugins(sl, FluentdInputSample.Spec.GlobalInputs)

clustercfgRouter, err := psr.BuildCfgRouter(&FluentdConfig1)
g.Expect(err).NotTo(HaveOccurred())
clusterOutputs := []fluentdv1alpha1.ClusterOutput{FluentdClusterOutputTag}
clustercfgResources, _ := psr.PatchAndFilterClusterLevelResources(sl, FluentdConfig1.GetCfgId(), []fluentdv1alpha1.ClusterFilter{}, clusterOutputs)
err = psr.WithCfgResources(*clustercfgRouter.Label, clustercfgResources)
g.Expect(err).NotTo(HaveOccurred())

for i := 0; i < maxRuntimes; i++ {
config, errs := psr.RenderMainConfig(false)
// fmt.Println(config)
g.Expect(errs).NotTo(HaveOccurred())
g.Expect(string(getExpectedCfg("./expected/fluentd-global-cfg-input-sample.cfg"))).To(Equal(config))
}
}

func Test_ClusterCfgOutput2ES(t *testing.T) {
g := NewGomegaWithT(t)
sl := plugins.NewSecretLoader(nil, Fluentd.Namespace, logr.Logger{})
Expand Down
24 changes: 24 additions & 0 deletions apis/fluentd/v1alpha1/tests/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@ spec:
config.fluentd.fluent.io/enabled: "true"
`

FluentdInputSample fluentdv1alpha1.Fluentd
FluentdInputSampleRaw = `
apiVersion: fluentd.fluent.io/v1alpha1
kind: Fluentd
metadata:
name: fluentd
namespace: fluent
labels:
app.kubernetes.io/name: fluentd
spec:
globalInputs:
- sample:
sample: '{"hello": "world"}'
tag: "foo.bar"
rate: 10
size: 10
autoIncrementKey: "id"
replicas: 1
image: kubesphere/fluentd:v1.15.3
fluentdCfgSelector:
matchLabels:
config.fluentd.fluent.io/enabled: "true"
`
FluentdInputTail fluentdv1alpha1.Fluentd
FluentdInputTailRaw = `
apiVersion: fluentd.fluent.io/v1alpha1
Expand Down Expand Up @@ -532,6 +555,7 @@ func init() {
func() {
ParseIntoObject(FluentdRaw, &Fluentd)
ParseIntoObject(FluentdInputTailRaw, &FluentdInputTail)
ParseIntoObject(FluentdInputSampleRaw, &FluentdInputSample)
ParseIntoObject(FluentdClusterOutputTagRaw, &FluentdClusterOutputTag)
ParseIntoObject(FluentdClusterFluentdConfig1Raw, &FluentdClusterFluentdConfig1)
ParseIntoObject(FluentdClusterFluentdConfig2Raw, &FluentdClusterFluentdConfig2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,34 @@ spec:
description: The @log_level parameter specifies the plugin-specific
logging level
type: string
sample:
description: in_sample plugin
properties:
autoIncrementKey:
description: If specified, each generated event has an auto-incremented
key field.
type: string
rate:
description: It configures how many events to generate per
second.
format: int64
type: integer
sample:
description: The sample data to be generated. It should
be either an array of JSON hashes or a single JSON hash.
If it is an array of JSON hashes, the hashes in the array
are cycled through in order.
type: string
size:
description: The number of events in the event stream of
each emit.
format: int64
type: integer
tag:
description: The tag of the event. The value is the tag
assigned to the generated events.
type: string
type: object
tail:
description: in_tail plugin
properties:
Expand Down
28 changes: 28 additions & 0 deletions config/crd/bases/fluentd.fluent.io_fluentds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,34 @@ spec:
description: The @log_level parameter specifies the plugin-specific
logging level
type: string
sample:
description: in_sample plugin
properties:
autoIncrementKey:
description: If specified, each generated event has an auto-incremented
key field.
type: string
rate:
description: It configures how many events to generate per
second.
format: int64
type: integer
sample:
description: The sample data to be generated. It should
be either an array of JSON hashes or a single JSON hash.
If it is an array of JSON hashes, the hashes in the array
are cycled through in order.
type: string
size:
description: The number of events in the event stream of
each emit.
format: int64
type: integer
tag:
description: The tag of the event. The value is the tag
assigned to the generated events.
type: string
type: object
tail:
description: in_tail plugin
properties:
Expand Down
12 changes: 12 additions & 0 deletions docs/plugins/fluentd/input/sample.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Sample

The in_sample input plugin generates sample events. It is useful for testing, debugging, benchmarking and getting started with Fluentd.


| Field | Description | Scheme |
| ----- | ----------- | ------ |
| tag | The tag of the event. The value is the tag assigned to the generated events. | *string |
| size | The number of events in the event stream of each emit. | *int64 |
| rate | It configures how many events to generate per second. | *int64 |
| autoIncrementKey | If specified, each generated event has an auto-incremented key field. | *string |
| sample | The sample data to be generated. It should be either an array of JSON hashes or a single JSON hash. If it is an array of JSON hashes, the hashes in the array are cycled through in order. | *string |
1 change: 1 addition & 0 deletions docs/plugins/fluentd/input/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Input defines all available input plugins and their parameters
| forward | in_forward plugin | *Forward |
| http | in_http plugin | *Http |
| tail | in_tail plugin | *Tail |
| sample | in_sample plugin | *Sample |
28 changes: 28 additions & 0 deletions manifests/setup/fluent-operator-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20956,6 +20956,34 @@ spec:
description: The @log_level parameter specifies the plugin-specific
logging level
type: string
sample:
description: in_sample plugin
properties:
autoIncrementKey:
description: If specified, each generated event has an auto-incremented
key field.
type: string
rate:
description: It configures how many events to generate per
second.
format: int64
type: integer
sample:
description: The sample data to be generated. It should
be either an array of JSON hashes or a single JSON hash.
If it is an array of JSON hashes, the hashes in the array
are cycled through in order.
type: string
size:
description: The number of events in the event stream of
each emit.
format: int64
type: integer
tag:
description: The tag of the event. The value is the tag
assigned to the generated events.
type: string
type: object
tail:
description: in_tail plugin
properties:
Expand Down
28 changes: 28 additions & 0 deletions manifests/setup/setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20956,6 +20956,34 @@ spec:
description: The @log_level parameter specifies the plugin-specific
logging level
type: string
sample:
description: in_sample plugin
properties:
autoIncrementKey:
description: If specified, each generated event has an auto-incremented
key field.
type: string
rate:
description: It configures how many events to generate per
second.
format: int64
type: integer
sample:
description: The sample data to be generated. It should
be either an array of JSON hashes or a single JSON hash.
If it is an array of JSON hashes, the hashes in the array
are cycled through in order.
type: string
size:
description: The number of events in the event stream of
each emit.
format: int64
type: integer
tag:
description: The tag of the event. The value is the tag
assigned to the generated events.
type: string
type: object
tail:
description: in_tail plugin
properties:
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/collector-statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func MakefbStatefulset(co fluentbitv1alpha2.Collector) *appsv1.StatefulSet {
if co.Spec.Ports != nil {
statefulset.Spec.Template.Spec.Containers[0].Ports = append(statefulset.Spec.Template.Spec.Containers[0].Ports, co.Spec.Ports...)
}

// Mount Secrets
for _, secret := range co.Spec.Secrets {
statefulset.Spec.Template.Spec.Volumes = append(statefulset.Spec.Template.Spec.Volumes, corev1.Volume{
Expand Down
Loading