github.com/fafucoder/cilium@v1.6.11/pkg/cidr/diff.go (about)

     1  // Copyright 2019 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 cidr
    16  
    17  func createIPNetMap(list []*CIDR) map[string]*CIDR {
    18  	m := map[string]*CIDR{}
    19  	for _, c := range list {
    20  		if c != nil {
    21  			m[c.String()] = c
    22  		}
    23  	}
    24  	return m
    25  }
    26  
    27  func listMissingIPNets(existing map[string]*CIDR, new []*CIDR) (missing []*CIDR) {
    28  	for _, c := range new {
    29  		if c != nil {
    30  			if _, ok := existing[c.String()]; !ok {
    31  				missing = append(missing, c)
    32  			}
    33  		}
    34  	}
    35  	return
    36  }
    37  
    38  // DiffCIDRLists compares an old and new list of CIDRs and returns the list of
    39  // removed and added CIDRs
    40  func DiffCIDRLists(old, new []*CIDR) (add, remove []*CIDR) {
    41  	add = listMissingIPNets(createIPNetMap(old), new)
    42  	remove = listMissingIPNets(createIPNetMap(new), old)
    43  	return
    44  }