github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/identity/reserved.go (about)

     1  // Copyright 2018 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 identity
    16  
    17  import "github.com/cilium/cilium/pkg/labels"
    18  
    19  var (
    20  	// ReservedIdentityCache that maps all reserved identities from their
    21  	// numeric identity to their corresponding identity.
    22  	ReservedIdentityCache = map[NumericIdentity]*Identity{}
    23  )
    24  
    25  // AddReservedIdentity adds the reserved numeric identity with the respective
    26  // label into the map of reserved identity cache.
    27  func AddReservedIdentity(ni NumericIdentity, lbl string) {
    28  	identity := NewIdentity(ni, labels.Labels{lbl: labels.NewLabel(lbl, "", labels.LabelSourceReserved)})
    29  	// Pre-calculate the SHA256 hash.
    30  	identity.GetLabelsSHA256()
    31  	ReservedIdentityCache[ni] = identity
    32  }
    33  
    34  // LookupReservedIdentity looks up a reserved identity by its NumericIdentity
    35  // and returns it if found. Returns nil if not found.
    36  func LookupReservedIdentity(ni NumericIdentity) *Identity {
    37  	return ReservedIdentityCache[ni]
    38  }
    39  
    40  func init() {
    41  	IterateReservedIdentities(func(lbl string, ni NumericIdentity) {
    42  		identity := NewIdentity(ni, labels.Labels{lbl: labels.NewLabel(lbl, "", labels.LabelSourceReserved)})
    43  		// Pre-calculate the SHA256 hash.
    44  		identity.GetLabelsSHA256()
    45  		ReservedIdentityCache[ni] = identity
    46  	})
    47  }