Ejemplo n.º 1
0
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 || len(parts[1]) == 0 {
				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
}
Ejemplo n.º 2
0
// SlaveAttributesToLabels converts slave attributes into string key/value labels
func SlaveAttributesToLabels(attrs []*mesos.Attribute) map[string]string {
	l := map[string]string{}
	for _, a := range attrs {
		if a == nil {
			continue
		}

		var v string
		k := labelPrefix + a.GetName()

		switch a.GetType() {
		case mesos.Value_TEXT:
			v = a.GetText().GetValue()
		case mesos.Value_SCALAR:
			v = strconv.FormatFloat(a.GetScalar().GetValue(), 'G', -1, 64)
		}

		if !validation.IsQualifiedName(k) {
			log.V(3).Infof("ignoring invalid node label name %q", k)
			continue
		}

		if !validation.IsValidLabelValue(v) {
			log.V(3).Infof("ignoring invalid node label %s value: %q", k, v)
			continue
		}

		l[k] = v
	}
	return l
}
Ejemplo n.º 3
0
func isValidLabelValue(label string) bool {
	errs := validation.IsValidLabelValue(label)
	if len(errs) > 0 {
		return false
	}
	return true
}
Ejemplo n.º 4
0
func validateLabelValue(v string) error {
	if !validation.IsValidLabelValue(v) {
		//FIXME: this is not the right regex!
		return fmt.Errorf("invalid label value: %s", qualifiedNameErrorMsg)
	}
	return nil
}
Ejemplo n.º 5
0
// 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
}
Ejemplo n.º 6
0
// 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
}
Ejemplo n.º 7
0
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
}
Ejemplo n.º 8
0
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
}
Ejemplo n.º 9
0
func validateLabelValue(v string) error {
	if len(kvalidation.IsValidLabelValue(v)) != 0 {
		return field.Invalid(field.NewPath("label value"), v, qualifiedNameErrorMsg)
	}
	return nil
}
Ejemplo n.º 10
0
func validateLabelValue(v string) error {
	if !kvalidation.IsValidLabelValue(v) {
		return fielderrors.NewFieldInvalid("label value", v, qualifiedNameErrorMsg)
	}
	return nil
}
Ejemplo n.º 11
0
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
}
Ejemplo n.º 12
0
func validateLabelValue(v string) error {
	if !validation.IsValidLabelValue(v) {
		return fmt.Errorf("invalid label value: %s", labelValueErrorMsg)
	}
	return nil
}