@@ -2,7 +2,7 @@ package vector
2
2
3
3
import (
4
4
"fmt"
5
- "strings "
5
+ "sort "
6
6
7
7
logging "github.com/openshift/cluster-logging-operator/apis/logging/v1"
8
8
"github.com/openshift/cluster-logging-operator/internal/generator"
@@ -11,24 +11,39 @@ import (
11
11
)
12
12
13
13
const (
14
- IsInfraContainer = `starts_with!(.kubernetes.pod_namespace,"kube") || starts_with!(.kubernetes.pod_namespace,"openshift") || .kubernetes.pod_namespace == "default"`
15
- IsNamespaceLog = `(.kubernetes.pod_namespace == "%s")`
14
+ NsKube = "kube"
15
+ NsOpenshift = "openshift"
16
+ NsDefault = "default"
17
+
18
+ K8sPodNamespace = ".kubernetes.pod_namespace"
19
+ K8sLabelKeyExpr = ".kubernetes.pod_labels.%s"
20
+
21
+ InputContainerLogs = "container_logs"
22
+ InputJournalLogs = "journal_logs"
23
+ RouteApplicationLogs = "route_application_logs"
16
24
17
25
SrcPassThrough = "."
18
26
)
19
27
20
28
var (
21
- AddLogTypeApp = fmt .Sprintf (".log_type = %q" , logging .InputNameApplication )
22
- AddLogTypeInfra = fmt .Sprintf (".log_type = %q" , logging .InputNameInfrastructure )
23
- AddLogTypeAudit = fmt .Sprintf (".log_type = %q" , logging .InputNameAudit )
24
- InfraContainerLogsExpr = fmt .Sprintf (`'%s'` , IsInfraContainer )
25
- AppContainerLogsExpr = fmt .Sprintf (`'!(%s)'` , IsInfraContainer )
26
- InputContainerLogs = "container_logs"
27
- InputJournalLogs = "journal_logs"
28
- RouteApplicationLogs = "route_application_logs"
29
+ InfraContainerLogs = OR (
30
+ StartWith (K8sPodNamespace , NsKube ),
31
+ StartWith (K8sPodNamespace , NsOpenshift ),
32
+ Eq (K8sPodNamespace , NsDefault ))
33
+ AppContainerLogs = Neg (Paren (InfraContainerLogs ))
34
+
35
+ AddLogTypeApp = fmt .Sprintf (".log_type = %q" , logging .InputNameApplication )
36
+ AddLogTypeInfra = fmt .Sprintf (".log_type = %q" , logging .InputNameInfrastructure )
37
+ AddLogTypeAudit = fmt .Sprintf (".log_type = %q" , logging .InputNameAudit )
29
38
30
- OR = func (nsExpr ... string ) string {
31
- return fmt .Sprintf ("'%s'" , strings .Join (nsExpr , " || " ))
39
+ MatchNS = func (ns string ) string {
40
+ return Eq (K8sPodNamespace , ns )
41
+ }
42
+ K8sLabelKey = func (k string ) string {
43
+ return fmt .Sprintf (K8sLabelKeyExpr , k )
44
+ }
45
+ MatchLabel = func (k , v string ) string {
46
+ return Eq (K8sLabelKey (k ), v )
32
47
}
33
48
)
34
49
@@ -45,19 +60,18 @@ func SourcesToInputs(spec *logging.ClusterLogForwarderSpec, o generator.Options)
45
60
Routes : map [string ]string {},
46
61
}
47
62
if types .Has (logging .InputNameApplication ) {
48
- r .Routes ["app" ] = AppContainerLogsExpr
63
+ r .Routes ["app" ] = Quote ( AppContainerLogs )
49
64
}
50
65
if types .Has (logging .InputNameInfrastructure ) {
51
- r .Routes ["infra" ] = InfraContainerLogsExpr
66
+ r .Routes ["infra" ] = Quote ( InfraContainerLogs )
52
67
}
53
- //TODO Add handling of user-defined inputs
54
68
el = append (el , r )
55
69
}
56
70
57
71
if types .Has (logging .InputNameApplication ) {
58
72
r := Remap {
59
73
Desc : `Rename log stream to "application"` ,
60
- ComponentID : "application" ,
74
+ ComponentID : logging . InputNameApplication ,
61
75
Inputs : helpers .MakeInputs ("route_container_logs.app" ),
62
76
VRL : AddLogTypeApp ,
63
77
}
@@ -66,7 +80,7 @@ func SourcesToInputs(spec *logging.ClusterLogForwarderSpec, o generator.Options)
66
80
if types .Has (logging .InputNameInfrastructure ) {
67
81
r := Remap {
68
82
Desc : `Rename log stream to "infrastructure"` ,
69
- ComponentID : "infrastructure" ,
83
+ ComponentID : logging . InputNameInfrastructure ,
70
84
Inputs : helpers .MakeInputs ("route_container_logs.infra" , InputJournalLogs ),
71
85
VRL : AddLogTypeInfra ,
72
86
}
@@ -75,30 +89,43 @@ func SourcesToInputs(spec *logging.ClusterLogForwarderSpec, o generator.Options)
75
89
if types .Has (logging .InputNameAudit ) {
76
90
r := Remap {
77
91
Desc : `Rename log stream to "audit"` ,
78
- ComponentID : "audit" ,
92
+ ComponentID : logging . InputNameAudit ,
79
93
Inputs : helpers .MakeInputs ("host_audit_logs" , "k8s_audit_logs" , "openshift_audit_logs" ),
80
94
VRL : AddLogTypeAudit ,
81
95
}
82
96
el = append (el , r )
83
97
}
84
- //TODO add labels based routing
85
98
userDefined := spec .InputMap ()
86
99
for _ , pipeline := range spec .Pipelines {
87
100
for _ , inRef := range pipeline .InputRefs {
88
101
if input , ok := userDefined [inRef ]; ok {
89
102
// user defined input
90
103
if input .Application != nil {
91
104
app := input .Application
105
+ matchNS := []string {}
92
106
if len (app .Namespaces ) != 0 {
93
- matchNS := []string {}
94
107
for _ , ns := range app .Namespaces {
95
- matchNS = append (matchNS , fmt . Sprintf ( IsNamespaceLog , ns ))
108
+ matchNS = append (matchNS , MatchNS ( ns ))
96
109
}
110
+ }
111
+ matchLabels := []string {}
112
+ if app .Selector != nil && len (app .Selector .MatchLabels ) != 0 {
113
+ labels := app .Selector .MatchLabels
114
+ keys := make ([]string , 0 , len (labels ))
115
+ for k := range labels {
116
+ keys = append (keys , k )
117
+ }
118
+ sort .Strings (keys )
119
+ for _ , k := range keys {
120
+ matchLabels = append (matchLabels , MatchLabel (k , labels [k ]))
121
+ }
122
+ }
123
+ if len (matchNS ) != 0 || len (matchLabels ) != 0 {
97
124
el = append (el , Route {
98
125
ComponentID : RouteApplicationLogs ,
99
- Inputs : helpers .MakeInputs ("application" ),
126
+ Inputs : helpers .MakeInputs (logging . InputNameApplication ),
100
127
Routes : map [string ]string {
101
- input .Name : OR (matchNS ... ),
128
+ input .Name : Quote ( AND ( OR (matchNS ... ), AND ( matchLabels ... )) ),
102
129
},
103
130
})
104
131
}
0 commit comments