istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tools/bug-report/pkg/util/match/match.go (about)

     1  // Copyright Istio Authors
     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 match
    16  
    17  import (
    18  	"path/filepath"
    19  	"strings"
    20  
    21  	"istio.io/istio/pkg/log"
    22  )
    23  
    24  func MatchesMap(selection, cluster map[string]string) bool {
    25  	if len(selection) == 0 {
    26  		return true
    27  	}
    28  	if len(cluster) == 0 {
    29  		return false
    30  	}
    31  
    32  	for ks, vs := range selection {
    33  		vc, ok := cluster[ks]
    34  		if !ok {
    35  			return false
    36  		}
    37  		if !MatchesGlob(vc, vs) {
    38  			return false
    39  		}
    40  	}
    41  	return true
    42  }
    43  
    44  func MatchesGlobs(matchString string, patterns []string) bool {
    45  	if len(patterns) == 0 {
    46  		return true
    47  	}
    48  	if len(patterns) == 1 {
    49  		p := strings.TrimSpace(patterns[0])
    50  		if p == "" || p == "*" {
    51  			return true
    52  		}
    53  	}
    54  
    55  	for _, p := range patterns {
    56  		if MatchesGlob(matchString, p) {
    57  			return true
    58  		}
    59  	}
    60  	return false
    61  }
    62  
    63  func MatchesGlob(matchString, pattern string) bool {
    64  	match, err := filepath.Match(pattern, matchString)
    65  	if err != nil {
    66  		// Shouldn't be here as prior validation is assumed.
    67  		log.Errorf("Unexpected filepath error for %s match %s: %s", pattern, matchString, err)
    68  		return false
    69  	}
    70  	return match
    71  }