github.com/fafucoder/cilium@v1.6.11/pkg/service/id.go (about)

     1  // Copyright 2016-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 service
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/cilium/cilium/pkg/loadbalancer"
    21  	"github.com/cilium/cilium/pkg/logging/logfields"
    22  )
    23  
    24  // AcquireID acquires a service ID
    25  func AcquireID(l3n4Addr loadbalancer.L3n4Addr, baseID uint32) (*loadbalancer.L3n4AddrID, error) {
    26  	log.WithField(logfields.L3n4Addr, logfields.Repr(l3n4Addr)).Debug("Resolving service")
    27  
    28  	if enableGlobalServiceIDs {
    29  		return acquireGlobalID(l3n4Addr, baseID)
    30  	}
    31  
    32  	return serviceIDAlloc.acquireLocalID(l3n4Addr, baseID)
    33  }
    34  
    35  // RestoreID restores  previously used service ID
    36  func RestoreID(l3n4Addr loadbalancer.L3n4Addr, baseID uint32) (*loadbalancer.L3n4AddrID, error) {
    37  	log.WithField(logfields.L3n4Addr, logfields.Repr(l3n4Addr)).Debug("Restoring service")
    38  
    39  	if enableGlobalServiceIDs {
    40  		// global service IDs do not require to pass in the existing
    41  		// service ID. The global state will guarantee that the same
    42  		// service will resolve to the same service ID again.
    43  		return acquireGlobalID(l3n4Addr, 0)
    44  	}
    45  
    46  	return serviceIDAlloc.acquireLocalID(l3n4Addr, baseID)
    47  }
    48  
    49  // GetID returns the L3n4AddrID that belongs to the given id.
    50  func GetID(id uint32) (*loadbalancer.L3n4AddrID, error) {
    51  	if enableGlobalServiceIDs {
    52  		return getGlobalID(id)
    53  	}
    54  
    55  	return serviceIDAlloc.getLocalID(id)
    56  }
    57  
    58  // DeleteID deletes the L3n4AddrID belonging to the given id from the kvstore.
    59  func DeleteID(id uint32) error {
    60  	log.WithField(logfields.L3n4AddrID, id).Debug("deleting L3n4Addr by ID")
    61  
    62  	if enableGlobalServiceIDs {
    63  		return deleteGlobalID(id)
    64  	}
    65  
    66  	return serviceIDAlloc.deleteLocalID(id)
    67  }
    68  
    69  func setIDSpace(next, max uint32) error {
    70  	if enableGlobalServiceIDs {
    71  		return setGlobalIDSpace(next, max)
    72  	}
    73  
    74  	return serviceIDAlloc.setLocalIDSpace(next, max)
    75  }
    76  
    77  func getMaxServiceID() (uint32, error) {
    78  	if enableGlobalServiceIDs {
    79  		return getGlobalMaxServiceID()
    80  	}
    81  
    82  	return serviceIDAlloc.getLocalMaxID()
    83  }
    84  
    85  // AcquireBackendID acquires a new local ID for the given backend.
    86  func AcquireBackendID(l3n4Addr loadbalancer.L3n4Addr) (loadbalancer.BackendID, error) {
    87  	return restoreBackendID(l3n4Addr, 0)
    88  }
    89  
    90  // RestoreBackendID tries to restore the given local ID for the given backend.
    91  //
    92  // If ID cannot be restored (ID already taken), returns an error.
    93  func RestoreBackendID(l3n4Addr loadbalancer.L3n4Addr, id loadbalancer.BackendID) error {
    94  	newID, err := restoreBackendID(l3n4Addr, id)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	// TODO(brb) This shouldn't happen (otherwise, there is a bug in the code).
   100  	//           But maybe it makes sense to delete all svc v2 in this case.
   101  	if newID != id {
   102  		DeleteBackendID(newID)
   103  		return fmt.Errorf("restored backend ID for %+v does not match (%d != %d)",
   104  			l3n4Addr, newID, id)
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  // DeleteBackendID releases the given backend ID.
   111  // TODO(brb) maybe provide l3n4Addr as an arg for the extra safety.
   112  func DeleteBackendID(id loadbalancer.BackendID) {
   113  	backendIDAlloc.deleteLocalID(uint32(id))
   114  }
   115  
   116  // LookupBackendID looks up already allocated backend ID for the given backend
   117  // addr. If such cannot be found, returns an error.
   118  func LookupBackendID(l3n4Addr loadbalancer.L3n4Addr) (loadbalancer.BackendID, error) {
   119  	id, err := backendIDAlloc.lookupLocalID(l3n4Addr)
   120  	return loadbalancer.BackendID(id), err
   121  }
   122  
   123  func restoreBackendID(l3n4Addr loadbalancer.L3n4Addr, id loadbalancer.BackendID) (loadbalancer.BackendID, error) {
   124  	l3n4AddrID, err := backendIDAlloc.acquireLocalID(l3n4Addr, uint32(id))
   125  	if err != nil {
   126  		return 0, err
   127  	}
   128  	return loadbalancer.BackendID(l3n4AddrID.ID), nil
   129  }