k8s.io/apiserver@v0.31.1/pkg/audit/union.go (about)

     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) bool {
    41  	success := true
    42  	for _, backend := range u.backends {
    43  		success = backend.ProcessEvents(events...) && success
    44  	}
    45  	return success
    46  }
    47  
    48  func (u union) Run(stopCh <-chan struct{}) error {
    49  	var funcs []func() error
    50  	for _, backend := range u.backends {
    51  		backend := backend
    52  		funcs = append(funcs, func() error {
    53  			return backend.Run(stopCh)
    54  		})
    55  	}
    56  	return errors.AggregateGoroutines(funcs...)
    57  }
    58  
    59  func (u union) Shutdown() {
    60  	for _, backend := range u.backends {
    61  		backend.Shutdown()
    62  	}
    63  }
    64  
    65  func (u union) String() string {
    66  	var backendStrings []string
    67  	for _, backend := range u.backends {
    68  		backendStrings = append(backendStrings, fmt.Sprintf("%s", backend))
    69  	}
    70  	return fmt.Sprintf("union[%s]", strings.Join(backendStrings, ","))
    71  }