github.com/cilium/cilium@v1.16.2/operator/pkg/model/translation/helper.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package translation
     5  
     6  import (
     7  	"strings"
     8  
     9  	slim_metav1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/meta/v1"
    10  )
    11  
    12  // ParseNodeLabelSelector parses a given string representation of a label selector into a metav1.LabelSelector.
    13  // The representation is a comma-separated list of key-value pairs (key1=value1,key2=value2) that is used as MatchLabels.
    14  // Values not matching these rules are skipped.
    15  func ParseNodeLabelSelector(nodeLabelSelectorString string) *slim_metav1.LabelSelector {
    16  	if nodeLabelSelectorString == "" {
    17  		return nil
    18  	}
    19  
    20  	labels := map[string]string{}
    21  	for _, v := range strings.Split(nodeLabelSelectorString, ",") {
    22  		s := strings.Split(v, "=")
    23  		if len(s) != 2 || len(s[0]) == 0 {
    24  			continue
    25  		}
    26  		labels[s[0]] = s[1]
    27  	}
    28  
    29  	return &slim_metav1.LabelSelector{
    30  		MatchLabels: labels,
    31  	}
    32  }