github.com/imran-kn/cilium-fork@v1.6.9/pkg/policy/identifier.go (about)

     1  // Copyright 2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package policy
    16  
    17  import (
    18  	"sync"
    19  
    20  	"github.com/cilium/cilium/pkg/identity"
    21  	"github.com/cilium/cilium/pkg/lock"
    22  )
    23  
    24  // Endpoint refers to any structure which has the following properties:
    25  // * a node-local ID stored as a uint16
    26  // * a security identity
    27  // * a means of incrementing its policy revision
    28  type Endpoint interface {
    29  	GetID16() uint16
    30  	RLockAlive() error
    31  	RUnlock()
    32  	GetSecurityIdentity() *identity.Identity
    33  	PolicyRevisionBumpEvent(rev uint64)
    34  }
    35  
    36  // EndpointSet is used to be able to group together a given set of Endpoints
    37  // that need to have a specific operation performed upon them (e.g., policy
    38  // revision updates).
    39  type EndpointSet struct {
    40  	mutex     lock.RWMutex
    41  	endpoints map[Endpoint]struct{}
    42  }
    43  
    44  // NewEndpointSet returns an EndpointSet with the given Endpoints map
    45  func NewEndpointSet(m map[Endpoint]struct{}) *EndpointSet {
    46  	if m != nil {
    47  		return &EndpointSet{
    48  			endpoints: m,
    49  		}
    50  	}
    51  	return &EndpointSet{
    52  		endpoints: map[Endpoint]struct{}{},
    53  	}
    54  }
    55  
    56  // ForEachGo runs epFunc asynchronously inside a go routine for each endpoint in
    57  // the EndpointSet. It signals to the provided WaitGroup when epFunc has been
    58  // executed for each endpoint.
    59  func (e *EndpointSet) ForEachGo(wg *sync.WaitGroup, epFunc func(epp Endpoint)) {
    60  	e.mutex.RLock()
    61  	defer e.mutex.RUnlock()
    62  
    63  	wg.Add(len(e.endpoints))
    64  
    65  	for ep := range e.endpoints {
    66  		go func(eppp Endpoint) {
    67  			epFunc(eppp)
    68  			wg.Done()
    69  		}(ep)
    70  	}
    71  }
    72  
    73  // Delete removes ep from the EndpointSet.
    74  func (e *EndpointSet) Delete(ep Endpoint) {
    75  	e.mutex.Lock()
    76  	delete(e.endpoints, ep)
    77  	e.mutex.Unlock()
    78  }
    79  
    80  // Insert adds ep to the EndpointSet.
    81  func (e *EndpointSet) Insert(ep Endpoint) {
    82  	e.mutex.Lock()
    83  	e.endpoints[ep] = struct{}{}
    84  	e.mutex.Unlock()
    85  }
    86  
    87  // Len returns the number of elements in the EndpointSet.
    88  func (e *EndpointSet) Len() (nElem int) {
    89  	e.mutex.RLock()
    90  	nElem = len(e.endpoints)
    91  	e.mutex.RUnlock()
    92  	return
    93  }