Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2017 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package audit |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "strings" |
| 22 | |
| 23 | "k8s.io/apimachinery/pkg/util/errors" |
| 24 | auditinternal "k8s.io/apiserver/pkg/apis/audit" |
| 25 | ) |
| 26 | |
| 27 | // Union returns an audit Backend which logs events to a set of backends. The returned |
| 28 | // Sink implementation blocks in turn for each call to ProcessEvents. |
| 29 | func Union(backends ...Backend) Backend { |
| 30 | if len(backends) == 1 { |
| 31 | return backends[0] |
| 32 | } |
| 33 | return union{backends} |
| 34 | } |
| 35 | |
| 36 | type union struct { |
| 37 | backends []Backend |
| 38 | } |
| 39 | |
| 40 | func (u union) ProcessEvents(events ...*auditinternal.Event) { |
| 41 | for _, backend := range u.backends { |
| 42 | backend.ProcessEvents(events...) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func (u union) Run(stopCh <-chan struct{}) error { |
| 47 | var funcs []func() error |
| 48 | for _, backend := range u.backends { |
| 49 | funcs = append(funcs, func() error { |
| 50 | return backend.Run(stopCh) |
| 51 | }) |
| 52 | } |
| 53 | return errors.AggregateGoroutines(funcs...) |
| 54 | } |
| 55 | |
| 56 | func (u union) Shutdown() { |
| 57 | for _, backend := range u.backends { |
| 58 | backend.Shutdown() |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func (u union) String() string { |
| 63 | var backendStrings []string |
| 64 | for _, backend := range u.backends { |
| 65 | backendStrings = append(backendStrings, fmt.Sprintf("%s", backend)) |
| 66 | } |
| 67 | return fmt.Sprintf("union[%s]", strings.Join(backendStrings, ",")) |
| 68 | } |