...

Source file src/k8s.io/kubernetes/plugin/pkg/auth/authorizer/node/intset.go

Documentation: k8s.io/kubernetes/plugin/pkg/auth/authorizer/node

     1  /*
     2  Copyright 2018 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 node
    18  
    19  // intSet maintains a map of id to refcounts
    20  type intSet struct {
    21  	// members is a map of id to refcounts
    22  	members map[int]int
    23  }
    24  
    25  func newIntSet() *intSet {
    26  	return &intSet{members: map[int]int{}}
    27  }
    28  
    29  // has returns true if the specified id has a positive refcount.
    30  // it is safe to call concurrently, but must not be called concurrently with any of the other methods.
    31  func (s *intSet) has(i int) bool {
    32  	if s == nil {
    33  		return false
    34  	}
    35  	return s.members[i] > 0
    36  }
    37  
    38  // reset removes all ids, effectively setting their refcounts to 0.
    39  // it is not thread-safe.
    40  func (s *intSet) reset() {
    41  	for k := range s.members {
    42  		delete(s.members, k)
    43  	}
    44  }
    45  
    46  // increment adds one to the refcount of the specified id.
    47  // it is not thread-safe.
    48  func (s *intSet) increment(i int) {
    49  	s.members[i]++
    50  }
    51  
    52  // decrement removes one from the refcount of the specified id,
    53  // and removes the id if the resulting refcount is <= 0.
    54  // it will not track refcounts lower than zero.
    55  // it is not thread-safe.
    56  func (s *intSet) decrement(i int) {
    57  	if s.members[i] <= 1 {
    58  		delete(s.members, i)
    59  	} else {
    60  		s.members[i]--
    61  	}
    62  }
    63  

View as plain text