Skip to content

Commit

Permalink
Optimize type in CRD
Browse files Browse the repository at this point in the history
Signed-off-by: zhu733756 <zhu733756@kubesphere.io>
  • Loading branch information
zhu733756 committed Feb 7, 2022
1 parent c60d911 commit d70f6f5
Show file tree
Hide file tree
Showing 21 changed files with 278 additions and 484 deletions.
82 changes: 19 additions & 63 deletions apis/fluentd/v1alpha1/helper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1alpha1

import (
"fmt"
"os"
"testing"

Expand All @@ -15,7 +16,6 @@ import (

var (
forwardId = "forward-001"
forwardType = "forward"
forwardLogLevel = "info"
forwardLabel = "forward-test"

Expand All @@ -27,9 +27,8 @@ var (

inputs = []input.Input{
{
InputCommon: &input.InputCommon{
InputCommon: input.InputCommon{
Id: &forwardId,
Type: &forwardType,
LogLevel: &forwardLogLevel,
Label: &forwardLabel,
},
Expand All @@ -44,11 +43,6 @@ var (
},
}

recordTransformerFilterType = "record_transformer"
grepFilterType = "grep"
parserFilterType = "parser"
stdoutFilterType = "stdout"

recordKey1 = "avg"
recordValue1 = `${record["total"] / record["count"]}`
recordKey2 = "message"
Expand Down Expand Up @@ -80,20 +74,14 @@ var (
},
}

forwardOutputType = "forward"

httpOutputType = "http"

kafkaOutputType = "kafka2"

bufferId = "common_buffer"
bufferType = "file"
bufferPath = "/fluentd/buffer/"
bufferTag = "buffertag.*"
bufferTotalLimitSize = "5GB"

buffer = common.Buffer{
BufferCommon: &common.BufferCommon{
BufferCommon: common.BufferCommon{
Id: &bufferId,
Type: &bufferType,
},
Expand All @@ -115,7 +103,7 @@ var (
}

serversDiscovery = common.ServiceDiscovery{
Type: &sdType,
SDCommon: common.SDCommon{Type: &sdType},
FileServiceDiscovery: &common.FileServiceDiscovery{
Path: &sdPath,
},
Expand Down Expand Up @@ -339,7 +327,7 @@ func Test_RenderMainConfig(t *testing.T) {
i := 0
for i < 5 {
config, errs := psr.RenderMainConfig()
// fmt.Println(config)
fmt.Println(config)
g.Expect(errs).NotTo(HaveOccurred())
g.Expect(string(expected)).To(Equal(config))

Expand All @@ -352,17 +340,11 @@ func createFilterSpecs() (FilterSpec, FilterSpec, FilterSpec) {
filterSpec1 := FilterSpec{
Filters: []filter.Filter{
{
FilterCommon: &filter.FilterCommon{
Type: &recordTransformerFilterType,
},
RecordTransformer: &filter.RecordTransformer{
Records: records,
},
},
{
FilterCommon: &filter.FilterCommon{
Type: &grepFilterType,
},
Grep: &filter.Grep{
Regexps: []*filter.Regexp{
{
Expand Down Expand Up @@ -392,20 +374,16 @@ func createFilterSpecs() (FilterSpec, FilterSpec, FilterSpec) {
filterSpec2 := FilterSpec{
Filters: []filter.Filter{
{
FilterCommon: &filter.FilterCommon{
Type: &recordTransformerFilterType,
},
RecordTransformer: &filter.RecordTransformer{
Records: records,
},
},
{
FilterCommon: &filter.FilterCommon{
Type: &stdoutFilterType,
},
Stdout: &filter.Stdout{
Format: &common.Format{
Type: &formatType,
FormatCommon: common.FormatCommon{
Type: &formatType,
},
},
},
},
Expand All @@ -415,28 +393,24 @@ func createFilterSpecs() (FilterSpec, FilterSpec, FilterSpec) {
filterSpec3 := FilterSpec{
Filters: []filter.Filter{
{
FilterCommon: &filter.FilterCommon{
Type: &parserFilterType,
},
Parser: &filter.Parser{
Parse: &common.Parse{
ParseCommon: &common.ParseCommon{
ParseCommon: common.ParseCommon{
Type: &regexpParser,
},
Time: &common.Time{
Time: common.Time{
TimeFormat: &timeFormat,
},
Expression: &regexpExpression,
},
},
},
{
FilterCommon: &filter.FilterCommon{
Type: &stdoutFilterType,
},
Stdout: &filter.Stdout{
Format: &common.Format{
Type: &formatType,
FormatCommon: common.FormatCommon{
Type: &formatType,
},
},
},
},
Expand All @@ -450,10 +424,7 @@ func createOutputSpecs() (OutputSpec, OutputSpec, OutputSpec) {
outputSpec1 := OutputSpec{
Outputs: []output.Output{
{
OutputCommon: &output.OutputCommon{
Type: &forwardOutputType,
},
Match: &common.Match{
Match: common.Match{
Buffer: &buffer,
},
Forward: &output.Forward{
Expand All @@ -462,10 +433,7 @@ func createOutputSpecs() (OutputSpec, OutputSpec, OutputSpec) {
},
},
{
OutputCommon: &output.OutputCommon{
Type: &kafkaOutputType,
},
Match: &common.Match{
Match: common.Match{
Buffer: &buffer,
},
Kafka: &output.Kafka2{
Expand All @@ -478,10 +446,7 @@ func createOutputSpecs() (OutputSpec, OutputSpec, OutputSpec) {
outputSpec2 := OutputSpec{
Outputs: []output.Output{
{
OutputCommon: &output.OutputCommon{
Type: &forwardOutputType,
},
Match: &common.Match{
Match: common.Match{
Buffer: &buffer,
},
Forward: &output.Forward{
Expand All @@ -490,10 +455,7 @@ func createOutputSpecs() (OutputSpec, OutputSpec, OutputSpec) {
},
},
{
OutputCommon: &output.OutputCommon{
Type: &httpOutputType,
},
Match: &common.Match{
Match: common.Match{
Buffer: &buffer,
},
Http: &output.Http{
Expand All @@ -508,10 +470,7 @@ func createOutputSpecs() (OutputSpec, OutputSpec, OutputSpec) {
outputSpec3 := OutputSpec{
Outputs: []output.Output{
{
OutputCommon: &output.OutputCommon{
Type: &httpOutputType,
},
Match: &common.Match{
Match: common.Match{
Buffer: &buffer,
},
Http: &output.Http{
Expand All @@ -521,10 +480,7 @@ func createOutputSpecs() (OutputSpec, OutputSpec, OutputSpec) {
},
},
{
OutputCommon: &output.OutputCommon{
Type: &kafkaOutputType,
},
Match: &common.Match{
Match: common.Match{
Buffer: &buffer,
},
Kafka: &output.Kafka2{
Expand Down
20 changes: 9 additions & 11 deletions apis/fluentd/v1alpha1/plugins/common/buffer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type BufferCommon struct {
}

type Buffer struct {
*BufferCommon `json:",inline,omitempty"`
BufferCommon `json:",inline,omitempty"`
// The file buffer plugin
*FileBuffer `json:",inline,omitempty"`
// The file_single buffer plugin
Expand Down Expand Up @@ -139,16 +139,14 @@ func (b *Buffer) Name() string {

func (b *Buffer) Params(_ plugins.SecretLoader) (*params.PluginStore, error) {
ps := params.NewPluginStore(b.Name())
if b.BufferCommon != nil {
if b.BufferCommon.Id != nil {
ps.InsertPairs("@id", *b.Id)
}
if b.BufferCommon.Type != nil {
ps.InsertPairs("@type", *b.Type)
}
if b.BufferCommon.LogLevel != nil {
ps.InsertPairs("@log_level", *b.LogLevel)
}
if b.Id != nil {
ps.InsertPairs("@id", fmt.Sprint(*b.Id))
}
if b.Type != nil {
ps.InsertType(fmt.Sprint(*b.Type))
}
if b.LogLevel != nil {
ps.InsertPairs("@log_level", fmt.Sprint(*b.LogLevel))
}

if b.FileBuffer != nil && b.FileBuffer.PathSuffix != nil {
Expand Down
79 changes: 40 additions & 39 deletions apis/fluentd/v1alpha1/plugins/common/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Time struct {
// The inject section can be under <match> or <filter> section.
type Inject struct {
// Time section
*Time `json:"inline,omitempty"`
Time `json:"inline,omitempty"`

// The field name to inject hostname
HostnameKey *string `json:"hostnameKey,omitempty"`
Expand Down Expand Up @@ -117,7 +117,7 @@ type Auth struct {

// The server plug-in
type Server struct {
*CommonFields `json:",inline"`
CommonFields `json:",inline"`

// Host defines the IP address or host name of the server.
Host *string `json:"host,omitempty"`
Expand All @@ -137,13 +137,7 @@ type Server struct {
Weight *string `json:"weight,omitempty"`
}

// Fluentd has a pluggable system called Service Discovery that lets the user extend and reuse custom output service discovery.
type ServiceDiscovery struct {
// The server section of this plugin
Server *Server `json:"server,omitempty"`
*FileServiceDiscovery `json:",inline,omitempty"`
*SrvServiceDiscovery `json:",inline,omitempty"`

type SDCommon struct {
// The @id parameter specifies a unique name for the configuration.
Id *string `json:"id,omitempty"`
// The @type parameter specifies the type of the plugin.
Expand All @@ -153,6 +147,15 @@ type ServiceDiscovery struct {
LogLevel *string `json:"logLevel,omitempty"`
}

// Fluentd has a pluggable system called Service Discovery that lets the user extend and reuse custom output service discovery.
type ServiceDiscovery struct {
SDCommon `json:",inline,omitempty"`
// The server section of this plugin
Server *Server `json:"server,omitempty"`
*FileServiceDiscovery `json:",inline,omitempty"`
*SrvServiceDiscovery `json:",inline,omitempty"`
}

type FileServiceDiscovery struct {
// The path of the target list. Default is '/etc/fluent/sd.yaml'
Path *string `json:"path,omitempty"`
Expand Down Expand Up @@ -181,26 +184,26 @@ func (j *Inject) Name() string {

func (j *Inject) Params(_ plugins.SecretLoader) (*params.PluginStore, error) {
ps := params.NewPluginStore(j.Name())
if j.Time != nil {
if j.Time.TimeType != nil {
ps.InsertPairs("time_type", fmt.Sprint(*j.Time.TimeType))
}
if j.Time.TimeFormat != nil {
ps.InsertPairs("time_type", fmt.Sprint(*j.Time.TimeFormat))
}
if j.Time.Localtime != nil {
ps.InsertPairs("localtime", fmt.Sprint(*j.Time.Localtime))
}
if j.Time.UTC != nil {
ps.InsertPairs("utc", fmt.Sprint(*j.Time.UTC))
}
if j.Time.Timezone != nil {
ps.InsertPairs("timezone", fmt.Sprint(*j.Time.Timezone))
}
if j.Time.TimeFormatFallbacks != nil {
ps.InsertPairs("time_format_fallbacks", fmt.Sprint(*j.Time.TimeFormatFallbacks))
}

if j.TimeType != nil {
ps.InsertPairs("time_type", fmt.Sprint(*j.TimeType))
}
if j.TimeFormat != nil {
ps.InsertPairs("time_type", fmt.Sprint(*j.TimeFormat))
}
if j.Localtime != nil {
ps.InsertPairs("localtime", fmt.Sprint(*j.Localtime))
}
if j.UTC != nil {
ps.InsertPairs("utc", fmt.Sprint(*j.UTC))
}
if j.Timezone != nil {
ps.InsertPairs("timezone", fmt.Sprint(*j.Timezone))
}
if j.TimeFormatFallbacks != nil {
ps.InsertPairs("time_format_fallbacks", fmt.Sprint(*j.TimeFormatFallbacks))
}

if j.HostnameKey != nil {
ps.InsertPairs("hostname_key", fmt.Sprint(*j.HostnameKey))
}
Expand Down Expand Up @@ -348,16 +351,14 @@ func (s *Server) Name() string {
func (s *Server) Params(_ plugins.SecretLoader) (*params.PluginStore, error) {
ps := params.NewPluginStore(s.Name())

if s.CommonFields != nil {
if s.CommonFields.Id != nil {
ps.InsertPairs("@id", fmt.Sprint(*s.CommonFields.Id))
}
if s.CommonFields.Type != nil {
ps.InsertPairs("@type", fmt.Sprint(*s.CommonFields.Type))
}
if s.CommonFields.LogLevel != nil {
ps.InsertPairs("@log_level", fmt.Sprint(*s.CommonFields.LogLevel))
}
if s.Id != nil {
ps.InsertPairs("@id", fmt.Sprint(*s.Id))
}
if s.Type != nil {
ps.InsertType(fmt.Sprint(*s.Type))
}
if s.LogLevel != nil {
ps.InsertPairs("@log_level", fmt.Sprint(*s.LogLevel))
}

if s.Host != nil {
Expand Down Expand Up @@ -399,7 +400,7 @@ func (sd *ServiceDiscovery) Params(loader plugins.SecretLoader) (*params.PluginS
ps.InsertPairs("@id", fmt.Sprint(*sd.Id))
}

ps.InsertPairs("@type", fmt.Sprint(*sd.Type))
ps.InsertType(fmt.Sprint(*sd.Type))

if sd.LogLevel != nil {
ps.InsertPairs("@log_level", fmt.Sprint(*sd.LogLevel))
Expand Down

0 comments on commit d70f6f5

Please sign in to comment.