github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/caas/kubernetes/provider/metadata.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package provider
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	core "k8s.io/api/core/v1"
     9  	k8slabels "k8s.io/apimachinery/pkg/labels"
    10  	"k8s.io/apimachinery/pkg/selection"
    11  )
    12  
    13  // newLabelRequirements creates a list of k8s node label requirements.
    14  // This should be called inside package init function to panic earlier
    15  // if there is a invalid requirement definition.
    16  func newLabelRequirements(rs ...requirementParams) k8slabels.Selector {
    17  	s := k8slabels.NewSelector()
    18  	for _, r := range rs {
    19  		l, err := k8slabels.NewRequirement(r.key, r.operator, r.strValues)
    20  		if err != nil {
    21  			// this panic only happens if the compiled code is wrong.
    22  			panic(errors.Annotatef(err, "incorrect requirement config %v", r))
    23  		}
    24  		s = s.Add(*l)
    25  	}
    26  	return s
    27  }
    28  
    29  // requirementParams defines parameters used to create a k8s label requirement.
    30  type requirementParams struct {
    31  	key       string
    32  	operator  selection.Operator
    33  	strValues []string
    34  }
    35  
    36  func getCloudProviderFromNodeMeta(node core.Node) string {
    37  	for k, checker := range k8sCloudCheckers {
    38  		if checker.Matches(k8slabels.Set(node.GetLabels())) {
    39  			return k
    40  		}
    41  	}
    42  	return ""
    43  }