| 1 | /* |
| 2 | Copyright 2016 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 netscip-go gomod k8s.io/utils . `k8s.io/utils/net`/ |
| 18 | |
| 19 | import ( |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.23 fmt/" |
| 21 | "netscip-go gomod github.com/golang/go/src go1.23 net/" |
| 22 | "stringsscip-go gomod github.com/golang/go/src go1.23 strings/" |
| 23 | ) |
| 24 | |
| 25 | // IPNetSet maps string to net.IPNet. |
| 26 | type IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet# map[string]*netscip-go gomod github.com/golang/go/src go1.23 net/.IPNetscip-go gomod github.com/golang/go/src go1.23 net/IPNet# |
| 27 | |
| 28 | // ParseIPNets parses string slice to IPNetSet. |
| 29 | func ParseIPNetsscip-go gomod k8s.io/utils . `k8s.io/utils/net`/ParseIPNets().(specs ...string) (IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#, error) { |
| 30 | ipnetset := make(IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) |
| 31 | for _, spec := range specs { |
| 32 | spec = stringsscip-go gomod github.com/golang/go/src go1.23 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.23 strings/TrimSpace().(spec) |
| 33 | _, ipnet, err := ParseCIDRSloppyscip-go gomod k8s.io/utils . `k8s.io/utils/net`/ParseCIDRSloppy.(spec) |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | k := ipnet.Stringscip-go gomod github.com/golang/go/src go1.23 net/IPNet#String().() // In case of normalization |
| 38 | ipnetset[k] = ipnet |
| 39 | } |
| 40 | return ipnetset, nil |
| 41 | } |
| 42 | |
| 43 | // Insert adds items to the set. |
| 44 | func (s IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) Insertscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#Insert().(items ...*netscip-go gomod github.com/golang/go/src go1.23 net/.IPNetscip-go gomod github.com/golang/go/src go1.23 net/IPNet#) { |
| 45 | for _, item := range items { |
| 46 | s[item.Stringscip-go gomod github.com/golang/go/src go1.23 net/IPNet#String().()] = item |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Delete removes all items from the set. |
| 51 | func (s IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) Deletescip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#Delete().(items ...*netscip-go gomod github.com/golang/go/src go1.23 net/.IPNetscip-go gomod github.com/golang/go/src go1.23 net/IPNet#) { |
| 52 | for _, item := range items { |
| 53 | delete(s, item.Stringscip-go gomod github.com/golang/go/src go1.23 net/IPNet#String().()) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Has returns true if and only if item is contained in the set. |
| 58 | func (s IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) Hasscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#Has().(item *netscip-go gomod github.com/golang/go/src go1.23 net/.IPNetscip-go gomod github.com/golang/go/src go1.23 net/IPNet#) bool { |
| 59 | _, contained := s[item.Stringscip-go gomod github.com/golang/go/src go1.23 net/IPNet#String().()] |
| 60 | return contained |
| 61 | } |
| 62 | |
| 63 | // HasAll returns true if and only if all items are contained in the set. |
| 64 | func (s IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) HasAllscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#HasAll().(items ...*netscip-go gomod github.com/golang/go/src go1.23 net/.IPNetscip-go gomod github.com/golang/go/src go1.23 net/IPNet#) bool { |
| 65 | for _, item := range items { |
| 66 | if !s.Hasscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#Has().(item) { |
| 67 | return false |
| 68 | } |
| 69 | } |
| 70 | return true |
| 71 | } |
| 72 | |
| 73 | // Difference returns a set of objects that are not in s2 |
| 74 | // For example: |
| 75 | // s1 = {a1, a2, a3} |
| 76 | // s2 = {a1, a2, a4, a5} |
| 77 | // s1.Difference(s2) = {a3} |
| 78 | // s2.Difference(s1) = {a4, a5} |
| 79 | func (s IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) Differencescip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#Difference().(s2 IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet# { |
| 80 | result := make(IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) |
| 81 | for k, i := range s { |
| 82 | _, found := s2[k] |
| 83 | if found { |
| 84 | continue |
| 85 | } |
| 86 | result[k] = i |
| 87 | } |
| 88 | return result |
| 89 | } |
| 90 | |
| 91 | // StringSlice returns a []string with the String representation of each element in the set. |
| 92 | // Order is undefined. |
| 93 | func (s IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) StringSlicescip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#StringSlice().() []string { |
| 94 | a := make([]string, 0, len(s)) |
| 95 | for k := range s { |
| 96 | a = append(a, k) |
| 97 | } |
| 98 | return a |
| 99 | } |
| 100 | |
| 101 | // IsSuperset returns true if and only if s1 is a superset of s2. |
| 102 | func (s IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) IsSupersetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#IsSuperset().(s2 IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) bool { |
| 103 | for k := range s2 { |
| 104 | _, found := s[k] |
| 105 | if !found { |
| 106 | return false |
| 107 | } |
| 108 | } |
| 109 | return true |
| 110 | } |
| 111 | |
| 112 | // Equal returns true if and only if s1 is equal (as a set) to s2. |
| 113 | // Two sets are equal if their membership is identical. |
| 114 | // (In practice, this means same elements, order doesn't matter) |
| 115 | func (s IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) Equalscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#Equal().(s2 IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) bool { |
| 116 | return len(s) == len(s2) && s.IsSupersetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#IsSuperset().(s2) |
| 117 | } |
| 118 | |
| 119 | // Len returns the size of the set. |
| 120 | func (s IPNetSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#) Lenscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPNetSet#Len().() int { |
| 121 | return len(s) |
| 122 | } |
| 123 | |
| 124 | // IPSet maps string to net.IP |
| 125 | type IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet# map[string]netscip-go gomod github.com/golang/go/src go1.23 net/.IPscip-go gomod github.com/golang/go/src go1.23 net/IP# |
| 126 | |
| 127 | // ParseIPSet parses string slice to IPSet |
| 128 | func ParseIPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/ParseIPSet().(items ...string) (IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#, error) { |
| 129 | ipset := make(IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) |
| 130 | for _, item := range items { |
| 131 | ip := ParseIPSloppyscip-go gomod k8s.io/utils . `k8s.io/utils/net`/ParseIPSloppy.(stringsscip-go gomod github.com/golang/go/src go1.23 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.23 strings/TrimSpace().(item)) |
| 132 | if ip == nil { |
| 133 | return nil, fmtscip-go gomod github.com/golang/go/src go1.23 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23 fmt/Errorf().("error parsing IP %q", item) |
| 134 | } |
| 135 | |
| 136 | ipset[ip.Stringscip-go gomod github.com/golang/go/src go1.23 net/IP#String().()] = ip |
| 137 | } |
| 138 | |
| 139 | return ipset, nil |
| 140 | } |
| 141 | |
| 142 | // Insert adds items to the set. |
| 143 | func (s IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) Insertscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#Insert().(items ...netscip-go gomod github.com/golang/go/src go1.23 net/.IPscip-go gomod github.com/golang/go/src go1.23 net/IP#) { |
| 144 | for _, item := range items { |
| 145 | s[item.Stringscip-go gomod github.com/golang/go/src go1.23 net/IP#String().()] = item |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Delete removes all items from the set. |
| 150 | func (s IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) Deletescip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#Delete().(items ...netscip-go gomod github.com/golang/go/src go1.23 net/.IPscip-go gomod github.com/golang/go/src go1.23 net/IP#) { |
| 151 | for _, item := range items { |
| 152 | delete(s, item.Stringscip-go gomod github.com/golang/go/src go1.23 net/IP#String().()) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Has returns true if and only if item is contained in the set. |
| 157 | func (s IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) Hasscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#Has().(item netscip-go gomod github.com/golang/go/src go1.23 net/.IPscip-go gomod github.com/golang/go/src go1.23 net/IP#) bool { |
| 158 | _, contained := s[item.Stringscip-go gomod github.com/golang/go/src go1.23 net/IP#String().()] |
| 159 | return contained |
| 160 | } |
| 161 | |
| 162 | // HasAll returns true if and only if all items are contained in the set. |
| 163 | func (s IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) HasAllscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#HasAll().(items ...netscip-go gomod github.com/golang/go/src go1.23 net/.IPscip-go gomod github.com/golang/go/src go1.23 net/IP#) bool { |
| 164 | for _, item := range items { |
| 165 | if !s.Hasscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#Has().(item) { |
| 166 | return false |
| 167 | } |
| 168 | } |
| 169 | return true |
| 170 | } |
| 171 | |
| 172 | // Difference returns a set of objects that are not in s2 |
| 173 | // For example: |
| 174 | // s1 = {a1, a2, a3} |
| 175 | // s2 = {a1, a2, a4, a5} |
| 176 | // s1.Difference(s2) = {a3} |
| 177 | // s2.Difference(s1) = {a4, a5} |
| 178 | func (s IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) Differencescip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#Difference().(s2 IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet# { |
| 179 | result := make(IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) |
| 180 | for k, i := range s { |
| 181 | _, found := s2[k] |
| 182 | if found { |
| 183 | continue |
| 184 | } |
| 185 | result[k] = i |
| 186 | } |
| 187 | return result |
| 188 | } |
| 189 | |
| 190 | // StringSlice returns a []string with the String representation of each element in the set. |
| 191 | // Order is undefined. |
| 192 | func (s IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) StringSlicescip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#StringSlice().() []string { |
| 193 | a := make([]string, 0, len(s)) |
| 194 | for k := range s { |
| 195 | a = append(a, k) |
| 196 | } |
| 197 | return a |
| 198 | } |
| 199 | |
| 200 | // IsSuperset returns true if and only if s1 is a superset of s2. |
| 201 | func (s IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) IsSupersetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#IsSuperset().(s2 IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) bool { |
| 202 | for k := range s2 { |
| 203 | _, found := s[k] |
| 204 | if !found { |
| 205 | return false |
| 206 | } |
| 207 | } |
| 208 | return true |
| 209 | } |
| 210 | |
| 211 | // Equal returns true if and only if s1 is equal (as a set) to s2. |
| 212 | // Two sets are equal if their membership is identical. |
| 213 | // (In practice, this means same elements, order doesn't matter) |
| 214 | func (s IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) Equalscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#Equal().(s2 IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) bool { |
| 215 | return len(s) == len(s2) && s.IsSupersetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#IsSuperset().(s2) |
| 216 | } |
| 217 | |
| 218 | // Len returns the size of the set. |
| 219 | func (s IPSetscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#) Lenscip-go gomod k8s.io/utils . `k8s.io/utils/net`/IPSet#Len().() int { |
| 220 | return len(s) |
| 221 | } |