Skip to content

Commit 5679bd8

Browse files
PhilGraysontarokkk
andauthored
Configure default_route option from fluent-plugin-label-router (#424)
* Define a DefaultFlow on the Logging object Co-authored-by: Sándor Guba <[email protected]>
1 parent 7a11c8f commit 5679bd8

File tree

7 files changed

+1491
-12
lines changed

7 files changed

+1491
-12
lines changed

charts/logging-operator/crds/logging.banzaicloud.io_loggings.yaml

Lines changed: 701 additions & 0 deletions
Large diffs are not rendered by default.

config/crd/bases/logging.banzaicloud.io_loggings.yaml

Lines changed: 701 additions & 0 deletions
Large diffs are not rendered by default.

pkg/resources/model/system.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,18 @@ func (l *LoggingResources) CreateModel() (*types.Builder, error) {
101101
return nil, err
102102
}
103103
}
104-
if len(l.Flows) == 0 && len(l.ClusterFlows) == 0 {
104+
if l.logging.Spec.DefaultFlowSpec != nil {
105+
flow, err := l.CreateFlowFromCustomResource(l.logging.Spec.DefaultFlowSpec)
106+
if err != nil {
107+
// TODO set flow status to error?
108+
return nil, err
109+
}
110+
err = system.RegisterDefaultFlow(flow)
111+
if err != nil {
112+
return nil, err
113+
}
114+
}
115+
if len(l.Flows) == 0 && len(l.ClusterFlows) == 0 && l.logging.Spec.DefaultFlowSpec == nil {
105116
l.logger.Info("no flows found, generating empty model")
106117
}
107118
return system, nil
@@ -164,10 +175,24 @@ func GetFlowMatchFromSpec(namespace string, matches interface{}) ([]types.FlowMa
164175
return flowMatches, nil
165176
}
166177

167-
func FlowDispatcher(flowCr interface{}) (*CommonFlow, error) {
178+
func (l *LoggingResources) FlowDispatcher(flowCr interface{}) (*CommonFlow, error) {
168179
var commonFlow *CommonFlow
169180
var err error
170181
switch f := flowCr.(type) {
182+
case *v1beta1.DefaultFlowSpec:
183+
id := fmt.Sprintf("logging:%s:%s", l.logging.Namespace, l.logging.Name)
184+
flow, err := types.NewFlow([]types.FlowMatch{}, id, l.logging.Name, l.logging.Namespace)
185+
if err != nil {
186+
return nil, err
187+
}
188+
return &CommonFlow{
189+
Name: l.logging.Name,
190+
Namespace: l.logging.Namespace,
191+
Scope: "",
192+
OutputRefs: f.OutputRefs,
193+
Filters: f.Filters,
194+
Flow: flow,
195+
}, nil
171196
case v1beta1.ClusterFlow:
172197
var matches []types.FlowMatch
173198
commonFlow = &CommonFlow{
@@ -241,7 +266,7 @@ func FlowDispatcher(flowCr interface{}) (*CommonFlow, error) {
241266
}
242267

243268
func (l *LoggingResources) CreateFlowFromCustomResource(flowCr interface{}) (*types.Flow, error) {
244-
commonFlow, err := FlowDispatcher(flowCr)
269+
commonFlow, err := l.FlowDispatcher(flowCr)
245270
if err != nil {
246271
return nil, err
247272
}

pkg/sdk/api/v1beta1/logging_types.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ type _metaLoggingSpec interface{}
4141

4242
// LoggingSpec defines the desired state of Logging
4343
type LoggingSpec struct {
44-
LoggingRef string `json:"loggingRef,omitempty"`
45-
FlowConfigCheckDisabled bool `json:"flowConfigCheckDisabled,omitempty"`
46-
FlowConfigOverride string `json:"flowConfigOverride,omitempty"`
47-
FluentbitSpec *FluentbitSpec `json:"fluentbit,omitempty"`
48-
FluentdSpec *FluentdSpec `json:"fluentd,omitempty"`
49-
WatchNamespaces []string `json:"watchNamespaces,omitempty"`
50-
ControlNamespace string `json:"controlNamespace"`
44+
LoggingRef string `json:"loggingRef,omitempty"`
45+
FlowConfigCheckDisabled bool `json:"flowConfigCheckDisabled,omitempty"`
46+
FlowConfigOverride string `json:"flowConfigOverride,omitempty"`
47+
FluentbitSpec *FluentbitSpec `json:"fluentbit,omitempty"`
48+
FluentdSpec *FluentdSpec `json:"fluentd,omitempty"`
49+
DefaultFlowSpec *DefaultFlowSpec `json:"defaultFlow,omitempty"`
50+
WatchNamespaces []string `json:"watchNamespaces,omitempty"`
51+
ControlNamespace string `json:"controlNamespace"`
5152

5253
// EnableRecreateWorkloadOnImmutableFieldChange enables the operator to recreate the
5354
// fluentbit daemonset and the fluentd statefulset (and possibly other resource in the future)
@@ -83,6 +84,14 @@ type LoggingList struct {
8384
Items []Logging `json:"items"`
8485
}
8586

87+
// +kubebuilder:object:generate=true
88+
89+
// DefaultFlowSpec is a Flow for logs that did not match any other Flow
90+
type DefaultFlowSpec struct {
91+
Filters []Filter `json:"filters,omitempty"`
92+
OutputRefs []string `json:"outputRefs"`
93+
}
94+
8695
// SetDefaults fill empty attributes
8796
func (l *Logging) SetDefaults() (*Logging, error) {
8897
copy := l.DeepCopy()

pkg/sdk/api/v1beta1/zz_generated.deepcopy.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sdk/model/types/builder.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ func (s *Builder) RegisterFlow(f *Flow) error {
4242
return nil
4343
}
4444

45+
func (s *Builder) RegisterDefaultFlow(f *Flow) error {
46+
for _, e := range s.flows {
47+
if e.FlowLabel == f.FlowLabel {
48+
return errors.New("Flow already exists")
49+
}
50+
}
51+
s.flows = append(s.flows, f)
52+
s.router.Params["default_route"] = f.FlowLabel
53+
return nil
54+
}
55+
4556
func (s *Builder) Build() (*System, error) {
4657
return &System{
4758
Input: s.input,

pkg/sdk/static/gen/crds/generated.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)