...

Source file src/k8s.io/kubernetes/test/integration/scheduler_perf/label_selector.go

Documentation: k8s.io/kubernetes/test/integration/scheduler_perf

     1  /*
     2  Copyright 2023 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 benchmark
    18  
    19  import "strings"
    20  
    21  // enabled checks a a label filter that works as in GitHub:
    22  // - empty string means enabled
    23  // - individual labels are comma-separated
    24  // - [+]<label> means the workload must have that label
    25  // - -<label> means the workload must not have that label
    26  func enabled(labelFilter string, labels ...string) bool {
    27  	for _, label := range strings.Split(labelFilter, ",") {
    28  		if label == "" {
    29  			continue
    30  		}
    31  		mustHaveLabel := label[0] != '-'
    32  		if label[0] == '-' || label[0] == '+' {
    33  			label = label[1:]
    34  		}
    35  		haveLabel := containsStr(labels, label)
    36  		if haveLabel != mustHaveLabel {
    37  			return false
    38  		}
    39  	}
    40  	return true
    41  }
    42  
    43  func containsStr(hay []string, needle string) bool {
    44  	for _, item := range hay {
    45  		if item == needle {
    46  			return true
    47  		}
    48  	}
    49  	return false
    50  }
    51  

View as plain text