-
Notifications
You must be signed in to change notification settings - Fork 346
/
log.go
161 lines (146 loc) · 4.94 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2023 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package exporter
import (
"math"
"reflect"
"slices"
"sync"
ipfixentities "github.com/vmware/go-ipfix/pkg/entities"
"github.com/vmware/go-ipfix/pkg/registry"
"k8s.io/klog/v2"
flowaggregatorconfig "antrea.io/antrea/pkg/config/flowaggregator"
"antrea.io/antrea/pkg/flowaggregator/flowlogger"
"antrea.io/antrea/pkg/flowaggregator/flowrecord"
"antrea.io/antrea/pkg/flowaggregator/options"
)
type flowFilter struct {
IngressNetworkPolicyRuleActions []uint8
EgressNetworkPolicyRuleActions []uint8
}
type LogExporter struct {
config flowaggregatorconfig.FlowLoggerConfig
filters []flowFilter
flowLogger *flowlogger.FlowLogger
stopCh chan struct{}
wg sync.WaitGroup
}
func NewLogExporter(opt *options.Options) (*LogExporter, error) {
config := opt.Config.FlowLogger
klog.InfoS("FlowLogger configuration", "path", config.Path, "maxSize", config.MaxSize, "maxBackups", config.MaxBackups, "maxAge", config.MaxAge, "compress", *config.Compress, "prettyPrint", *config.PrettyPrint)
exporter := &LogExporter{
config: config,
}
exporter.buildFilters()
return exporter, nil
}
func (e *LogExporter) buildFilters() {
ruleActionToUint8 := func(a flowaggregatorconfig.NetworkPolicyRuleAction) uint8 {
switch a {
case flowaggregatorconfig.NetworkPolicyRuleActionNone:
return registry.NetworkPolicyRuleActionNoAction
case flowaggregatorconfig.NetworkPolicyRuleActionAllow:
return registry.NetworkPolicyRuleActionAllow
case flowaggregatorconfig.NetworkPolicyRuleActionDrop:
return registry.NetworkPolicyRuleActionDrop
case flowaggregatorconfig.NetworkPolicyRuleActionReject:
return registry.NetworkPolicyRuleActionReject
default: // invalid case
return math.MaxUint8
}
}
convertFilter := func(in *flowaggregatorconfig.FlowFilter) flowFilter {
ingressNetworkPolicyRuleActions := make([]uint8, len(in.IngressNetworkPolicyRuleActions))
for idx, a := range in.IngressNetworkPolicyRuleActions {
ingressNetworkPolicyRuleActions[idx] = ruleActionToUint8(a)
}
egressNetworkPolicyRuleActions := make([]uint8, len(in.EgressNetworkPolicyRuleActions))
for idx, a := range in.EgressNetworkPolicyRuleActions {
egressNetworkPolicyRuleActions[idx] = ruleActionToUint8(a)
}
return flowFilter{
IngressNetworkPolicyRuleActions: ingressNetworkPolicyRuleActions,
EgressNetworkPolicyRuleActions: egressNetworkPolicyRuleActions,
}
}
e.filters = make([]flowFilter, 0, len(e.config.Filters))
for idx := range e.config.Filters {
e.filters = append(e.filters, convertFilter(&e.config.Filters[idx]))
}
}
func (e *LogExporter) AddRecord(record ipfixentities.Record, isRecordIPv6 bool) error {
r := flowrecord.GetFlowRecord(record)
if !e.applyFilters(r) {
klog.V(5).InfoS("Ignoring record in FlowLogger because filters do not match")
return nil
}
return e.flowLogger.WriteRecord(r, *e.config.PrettyPrint)
}
func (e *LogExporter) applyFilters(r *flowrecord.FlowRecord) bool {
if len(e.filters) == 0 {
return true
}
for idx := range e.filters {
filter := &e.filters[idx]
if len(filter.IngressNetworkPolicyRuleActions) > 0 && !slices.Contains(filter.IngressNetworkPolicyRuleActions, r.IngressNetworkPolicyRuleAction) {
continue
}
if len(filter.EgressNetworkPolicyRuleActions) > 0 && !slices.Contains(filter.EgressNetworkPolicyRuleActions, r.EgressNetworkPolicyRuleAction) {
continue
}
// both conditions match
return true
}
return false
}
func (e *LogExporter) Start() {
e.start()
}
func (e *LogExporter) Stop() {
e.stop()
}
func (e *LogExporter) start() {
e.stopCh = make(chan struct{})
e.flowLogger = flowlogger.NewFlowLogger(
e.config.Path,
// these are all valid conversions from int32 to int
int(e.config.MaxSize),
int(e.config.MaxBackups),
int(e.config.MaxAge),
*e.config.Compress,
)
e.wg.Add(1)
go func() {
defer e.wg.Done()
e.flowLogger.FlushLoop(e.stopCh)
}()
}
func (e *LogExporter) stop() {
close(e.stopCh)
e.wg.Wait()
e.flowLogger.Close()
e.flowLogger = nil
}
func (e *LogExporter) UpdateOptions(opt *options.Options) {
config := opt.Config.FlowLogger
if reflect.DeepEqual(e.config, config) {
return
}
klog.InfoS("Updating FlowLogger")
e.stop()
e.config = config
klog.InfoS("New FlowLogger configuration", "path", config.Path, "maxSize", config.MaxSize, "maxBackups", config.MaxBackups, "maxAge", config.MaxAge, "compress", *config.Compress, "prettyPrint", *config.PrettyPrint)
e.buildFilters()
e.start()
}