github.com/fafucoder/cilium@v1.6.11/pkg/ipam/types.go (about) 1 // Copyright 2016-2020 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 ipam 16 17 import ( 18 "net" 19 20 "github.com/cilium/cilium/pkg/datapath" 21 "github.com/cilium/cilium/pkg/lock" 22 23 "github.com/davecgh/go-spew/spew" 24 ) 25 26 // AllocationResult is the result of an allocation 27 type AllocationResult struct { 28 // IP is the allocated IP 29 IP net.IP 30 31 // CIDRs is a list of all CIDRs to which the IP has direct access to. 32 // This is primarily useful if the IP has been allocated out of a VPC 33 // subnet range and the VPC provides routing to a set of CIDRs in which 34 // the IP is routable. 35 CIDRs []string 36 37 // Master is the MAC address of the master interface. This is useful 38 // when the IP is a secondary address of an interface which is 39 // represented on the node as a Linux device and all routing of the IP 40 // must occur through that master interface. 41 Master string 42 43 // GatewayIP is the IP of the gateway which must be used for this IP. 44 // If the allocated IP is derived from a VPC, then the gateway 45 // represented the gateway of the VPC or VPC subnet. 46 GatewayIP string 47 48 // ExpirationUUID is the UUID of the expiration timer. This field is 49 // only set if AllocateNextWithExpiration is used. 50 ExpirationUUID string 51 } 52 53 // Allocator is the interface for an IP allocator implementation 54 type Allocator interface { 55 // Allocate allocates a specific IP or fails 56 Allocate(ip net.IP, owner string) (*AllocationResult, error) 57 58 // Release releases a previously allocated IP or fails 59 Release(ip net.IP) error 60 61 // AllocateNext allocates the next available IP or fails if no more IPs 62 // are available 63 AllocateNext(owner string) (*AllocationResult, error) 64 65 // Dump returns a map of all allocated IPs with the IP represented as 66 // key in the map. Dump must also provide a status one-liner to 67 // represent the overall status, e.g. number of IPs allocated and 68 // overall health information if available. 69 Dump() (map[string]string, string) 70 } 71 72 // IPNetWithOwner is a structure containing a net.IPNet struct with the owner 73 // of that IP Network. 74 type IPNetWithOwner struct { 75 ipNet net.IPNet 76 owner string 77 } 78 79 // IPBlacklist is a structure used to store information related to blacklisted 80 // IPs and IPNetworks. 81 type IPBlacklist struct { 82 // A hashmap containing IP and the corresponding owners. 83 ips map[string]string 84 85 // A list of IPNetwork with owners, for blacklisting subnets. 86 ipNets []*IPNetWithOwner 87 } 88 89 // Config is the IPAM configuration used for a particular IPAM type. 90 type IPAM struct { 91 nodeAddressing datapath.NodeAddressing 92 config Configuration 93 94 IPv6Allocator Allocator 95 IPv4Allocator Allocator 96 97 // owner maps an IP to the owner 98 owner map[string]string 99 100 // expirationTimers is a map of all expiration timers. Each entry 101 // represents a IP allocation which is protected by an expiration 102 // timer. 103 expirationTimers map[string]string 104 105 // mutex covers access to all members of this struct 106 allocatorMutex lock.RWMutex 107 108 blacklist IPBlacklist 109 } 110 111 // DebugStatus implements debug.StatusObject to provide debug status collection 112 // ability 113 func (ipam *IPAM) DebugStatus() string { 114 if ipam == nil { 115 return "<nil>" 116 } 117 118 ipam.allocatorMutex.RLock() 119 str := spew.Sdump(ipam) 120 ipam.allocatorMutex.RUnlock() 121 return str 122 }