func parseTaints(spec []string) ([]api.Taint, []api.Taint, error) { var taints, taintsToRemove []api.Taint uniqueTaints := map[api.TaintEffect]sets.String{} for _, taintSpec := range spec { if strings.Index(taintSpec, "=") != -1 && strings.Index(taintSpec, ":") != -1 { parts := strings.Split(taintSpec, "=") if len(parts) != 2 || len(parts[1]) == 0 || len(validation.IsQualifiedName(parts[0])) > 0 { return nil, nil, fmt.Errorf("invalid taint spec: %v", taintSpec) } parts2 := strings.Split(parts[1], ":") errs := validation.IsValidLabelValue(parts2[0]) if len(parts2) != 2 || len(errs) != 0 { return nil, nil, fmt.Errorf("invalid taint spec: %v, %s", taintSpec, strings.Join(errs, "; ")) } if parts2[1] != string(api.TaintEffectNoSchedule) && parts2[1] != string(api.TaintEffectPreferNoSchedule) { return nil, nil, fmt.Errorf("invalid taint spec: %v, unsupported taint effect", taintSpec) } effect := api.TaintEffect(parts2[1]) newTaint := api.Taint{ Key: parts[0], Value: parts2[0], Effect: effect, } // validate if taint is unique by <key, effect> if len(uniqueTaints[newTaint.Effect]) > 0 && uniqueTaints[newTaint.Effect].Has(newTaint.Key) { return nil, nil, fmt.Errorf("duplicated taints with the same key and effect: %v", newTaint) } // add taint to existingTaints for uniqueness check if len(uniqueTaints[newTaint.Effect]) == 0 { uniqueTaints[newTaint.Effect] = sets.String{} } uniqueTaints[newTaint.Effect].Insert(newTaint.Key) taints = append(taints, newTaint) } else if strings.HasSuffix(taintSpec, "-") { taintKey := taintSpec[:len(taintSpec)-1] var effect api.TaintEffect if strings.Index(taintKey, ":") != -1 { parts := strings.Split(taintKey, ":") taintKey = parts[0] effect = api.TaintEffect(parts[1]) } taintsToRemove = append(taintsToRemove, api.Taint{Key: taintKey, Effect: effect}) } else { return nil, nil, fmt.Errorf("unknown taint spec: %v", taintSpec) } } return taints, taintsToRemove, nil }
// ParseTaint parses a taint from a string. Taint must be off the format '<key>=<value>:<effect>'. func ParseTaint(st string) (api.Taint, error) { var taint api.Taint parts := strings.Split(st, "=") if len(parts) != 2 || len(parts[1]) == 0 || len(validation.IsQualifiedName(parts[0])) > 0 { return taint, fmt.Errorf("invalid taint spec: %v", st) } parts2 := strings.Split(parts[1], ":") effect := api.TaintEffect(parts2[1]) errs := validation.IsValidLabelValue(parts2[0]) if len(parts2) != 2 || len(errs) != 0 { return taint, fmt.Errorf("invalid taint spec: %v, %s", st, strings.Join(errs, "; ")) } if effect != api.TaintEffectNoSchedule && effect != api.TaintEffectPreferNoSchedule { return taint, fmt.Errorf("invalid taint spec: %v, unsupported taint effect", st) } taint.Key = parts[0] taint.Value = parts2[0] taint.Effect = effect return taint, nil }
func (t taintsVar) Set(s string) error { sts := strings.Split(s, ",") var taints []api.Taint for _, st := range sts { taint, err := ParseTaint(st) if err != nil { return err } taints = append(taints, api.Taint{Key: taint.Key, Value: taint.Value, Effect: api.TaintEffect(taint.Effect)}) } *t.ptr = taints return nil }
func parseTaints(spec []string) ([]api.Taint, []string, error) { var taints []api.Taint var remove []string for _, taintSpec := range spec { if strings.Index(taintSpec, "=") != -1 && strings.Index(taintSpec, ":") != -1 { parts := strings.Split(taintSpec, "=") if len(parts) != 2 || len(parts[1]) == 0 || len(validation.IsQualifiedName(parts[0])) > 0 { return nil, nil, fmt.Errorf("invalid taint spec: %v", taintSpec) } parts2 := strings.Split(parts[1], ":") errs := validation.IsValidLabelValue(parts2[0]) if len(parts2) != 2 || len(errs) != 0 { return nil, nil, fmt.Errorf("invalid taint spec: %v, %s", taintSpec, strings.Join(errs, "; ")) } if parts2[1] != string(api.TaintEffectNoSchedule) && parts2[1] != string(api.TaintEffectPreferNoSchedule) { return nil, nil, fmt.Errorf("invalid taint spec: %v, unsupported taint effect", taintSpec) } effect := api.TaintEffect(parts2[1]) newTaint := api.Taint{ Key: parts[0], Value: parts2[0], Effect: effect, } taints = append(taints, newTaint) } else if strings.HasSuffix(taintSpec, "-") { remove = append(remove, taintSpec[:len(taintSpec)-1]) } else { return nil, nil, fmt.Errorf("unknown taint spec: %v", taintSpec) } } return taints, remove, nil }