github.com/kubernetes-incubator/kube-aws@v0.16.4/pkg/api/taint.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // Taints is a list of taints
     9  type Taints []Taint
    10  
    11  // String returns a comma-separated list of taints
    12  func (t Taints) String() string {
    13  	ts := []string{}
    14  	for _, t := range t {
    15  		ts = append(ts, t.String())
    16  	}
    17  	return strings.Join(ts, ",")
    18  }
    19  
    20  // Validate returns an error if the list of taints are invalid as a group
    21  func (t Taints) Validate() error {
    22  	keyEffects := map[string]int{}
    23  
    24  	for _, taint := range t {
    25  		if err := taint.Validate(); err != nil {
    26  			return err
    27  		}
    28  
    29  		keyEffect := taint.Key + ":" + taint.Effect
    30  		if _, ok := keyEffects[keyEffect]; ok {
    31  			return fmt.Errorf("taints must be unique by key and effect pair")
    32  		} else {
    33  			keyEffects[keyEffect] = 1
    34  		}
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  // Taint is a k8s node taint which is added to nodes which requires pods to tolerate
    41  type Taint struct {
    42  	Key    string `yaml:"key"`
    43  	Value  string `yaml:"value"`
    44  	Effect string `yaml:"effect"`
    45  }
    46  
    47  // String returns a taint represented in string
    48  func (t Taint) String() string {
    49  	return fmt.Sprintf("%s=%s:%s", t.Key, t.Value, t.Effect)
    50  }
    51  
    52  // Validate returns an error if the taint is invalid
    53  func (t Taint) Validate() error {
    54  	if len(t.Key) == 0 {
    55  		return fmt.Errorf("expected taint key to be a non-empty string")
    56  	}
    57  
    58  	if t.Effect != "NoSchedule" && t.Effect != "PreferNoSchedule" && t.Effect != "NoExecute" {
    59  		return fmt.Errorf("invalid taint effect: %s", t.Effect)
    60  	}
    61  
    62  	return nil
    63  }