func parseLabels(spec []string) (map[string]string, []string, error) { labels := map[string]string{} var remove []string for _, labelSpec := range spec { if strings.Index(labelSpec, "=") != -1 { parts := strings.Split(labelSpec, "=") if len(parts) != 2 { return nil, nil, fmt.Errorf("invalid label spec: %v", labelSpec) } if errs := validation.IsValidLabelValue(parts[1]); len(errs) != 0 { return nil, nil, fmt.Errorf("invalid label value: %q: %s", labelSpec, strings.Join(errs, ";")) } labels[parts[0]] = parts[1] } else if strings.HasSuffix(labelSpec, "-") { remove = append(remove, labelSpec[:len(labelSpec)-1]) } else { return nil, nil, fmt.Errorf("unknown label spec: %v", labelSpec) } } for _, removeLabel := range remove { if _, found := labels[removeLabel]; found { return nil, nil, fmt.Errorf("can not both modify and remove a label in the same command") } } return labels, remove, nil }
// ParseTaint parses a taint from a string. Taint must be off the format '<key>=<value>:<effect>'. func ParseTaint(st string) (v1.Taint, error) { var taint v1.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 := v1.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 != v1.TaintEffectNoSchedule && effect != v1.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 }
// ValidateLabels validates that a set of labels are correctly defined. func ValidateLabels(labels map[string]string, fldPath *field.Path) field.ErrorList { allErrs := field.ErrorList{} for k, v := range labels { allErrs = append(allErrs, ValidateLabelName(k, fldPath)...) for _, msg := range validation.IsValidLabelValue(v) { allErrs = append(allErrs, field.Invalid(fldPath, v, msg)) } } return allErrs }
func validateLabelValue(v string) error { if errs := validation.IsValidLabelValue(v); len(errs) != 0 { return fmt.Errorf("invalid label value: %q: %s", v, strings.Join(errs, "; ")) } return nil }