sigs.k8s.io/external-dns@v0.14.1/endpoint/target_filter.go (about)

     1  /*
     2  Copyright 2017 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 endpoint
    18  
    19  import (
    20  	"net"
    21  	"strings"
    22  
    23  	log "github.com/sirupsen/logrus"
    24  )
    25  
    26  // TargetFilterInterface defines the interface to select matching targets for a specific provider or runtime
    27  type TargetFilterInterface interface {
    28  	Match(target string) bool
    29  }
    30  
    31  // TargetNetFilter holds a lists of valid target names
    32  type TargetNetFilter struct {
    33  	// FilterNets define what targets to match
    34  	FilterNets []*net.IPNet
    35  	// excludeNets define what targets not to match
    36  	excludeNets []*net.IPNet
    37  }
    38  
    39  // prepareTargetFilters provides consistent trimming for filters/exclude params
    40  func prepareTargetFilters(filters []string) []*net.IPNet {
    41  	fs := make([]*net.IPNet, 0)
    42  
    43  	for _, filter := range filters {
    44  		filter = strings.TrimSpace(filter)
    45  
    46  		_, filterNet, err := net.ParseCIDR(filter)
    47  		if err != nil {
    48  			log.Errorf("Invalid target net filter: %s", filter)
    49  
    50  			continue
    51  		}
    52  
    53  		fs = append(fs, filterNet)
    54  	}
    55  	return fs
    56  }
    57  
    58  // NewTargetNetFilterWithExclusions returns a new TargetNetFilter, given a list of matches and exclusions
    59  func NewTargetNetFilterWithExclusions(targetFilterNets []string, excludeNets []string) TargetNetFilter {
    60  	return TargetNetFilter{FilterNets: prepareTargetFilters(targetFilterNets), excludeNets: prepareTargetFilters(excludeNets)}
    61  }
    62  
    63  // Match checks whether a target can be found in the TargetNetFilter.
    64  func (tf TargetNetFilter) Match(target string) bool {
    65  	return matchTargetNetFilter(tf.FilterNets, target, true) && !matchTargetNetFilter(tf.excludeNets, target, false)
    66  }
    67  
    68  // matchTargetNetFilter determines if any `filters` match `target`.
    69  // If no `filters` are provided, behavior depends on `emptyval`
    70  // (empty `tf.filters` matches everything, while empty `tf.exclude` excludes nothing)
    71  func matchTargetNetFilter(filters []*net.IPNet, target string, emptyval bool) bool {
    72  	if len(filters) == 0 {
    73  		return emptyval
    74  	}
    75  
    76  	for _, filter := range filters {
    77  		ip := net.ParseIP(target)
    78  
    79  		if filter.Contains(ip) {
    80  			return true
    81  		}
    82  	}
    83  
    84  	return false
    85  }