From ab5b462d17dd7c43aabce700e971d5465779eea1 Mon Sep 17 00:00:00 2001 From: Avesh Agarwal Date: Wed, 22 Mar 2017 00:28:00 -0400 Subject: [PATCH] Add node affinity, pod affinity and pod antiaffinity validation for alpha annotations. --- pkg/api/helpers.go | 21 +++++++++++++++++++++ pkg/api/validation/validation.go | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pkg/api/helpers.go b/pkg/api/helpers.go index 4705e11fed5..943a9ea9ae3 100644 --- a/pkg/api/helpers.go +++ b/pkg/api/helpers.go @@ -18,6 +18,7 @@ package api import ( "crypto/md5" + "encoding/json" "fmt" "reflect" "strings" @@ -463,6 +464,11 @@ const ( // an object (e.g. secret, config map) before fetching it again from apiserver. // This annotation can be attached to node. ObjectTTLAnnotationKey string = "node.alpha.kubernetes.io/ttl" + + // AffinityAnnotationKey represents the key of affinity data (json serialized) + // in the Annotations of a Pod. + // TODO: remove when alpha support for affinity is removed + AffinityAnnotationKey string = "scheduler.alpha.kubernetes.io/affinity" ) // AddOrUpdateTolerationInPod tries to add a toleration to the pod's toleration list. @@ -596,6 +602,21 @@ func PodAnnotationsFromSysctls(sysctls []Sysctl) string { return strings.Join(kvs, ",") } +// GetAffinityFromPodAnnotations gets the json serialized affinity data from Pod.Annotations +// and converts it to the Affinity type in api. +// TODO: remove when alpha support for affinity is removed +func GetAffinityFromPodAnnotations(annotations map[string]string) (*Affinity, error) { + if len(annotations) > 0 && annotations[AffinityAnnotationKey] != "" { + var affinity Affinity + err := json.Unmarshal([]byte(annotations[AffinityAnnotationKey]), &affinity) + if err != nil { + return nil, err + } + return &affinity, nil + } + return nil, nil +} + // GetPersistentVolumeClass returns StorageClassName. func GetPersistentVolumeClass(volume *PersistentVolume) string { // Use beta annotation first diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index 1dd8845200b..1e360749ab9 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -105,6 +105,10 @@ func ValidateDNS1123Subdomain(value string, fldPath *field.Path) field.ErrorList func ValidatePodSpecificAnnotations(annotations map[string]string, spec *api.PodSpec, fldPath *field.Path) field.ErrorList { allErrs := field.ErrorList{} + if annotations[api.AffinityAnnotationKey] != "" { + allErrs = append(allErrs, ValidateAffinityInPodAnnotations(annotations, fldPath)...) + } + // TODO: remove these after we EOL the annotations. if hostname, exists := annotations[utilpod.PodHostnameAnnotation]; exists { allErrs = append(allErrs, ValidateDNS1123Label(hostname, fldPath.Key(utilpod.PodHostnameAnnotation))...) @@ -136,6 +140,23 @@ func ValidatePodSpecificAnnotations(annotations map[string]string, spec *api.Pod return allErrs } +// ValidateAffinityInPodAnnotations tests that the serialized Affinity in Pod.Annotations has valid data +func ValidateAffinityInPodAnnotations(annotations map[string]string, fldPath *field.Path) field.ErrorList { + allErrs := field.ErrorList{} + + affinity, err := api.GetAffinityFromPodAnnotations(annotations) + if err != nil { + allErrs = append(allErrs, field.Invalid(fldPath, api.AffinityAnnotationKey, err.Error())) + return allErrs + } + if affinity == nil { + return allErrs + } + + allErrs = append(allErrs, validateAffinity(affinity, fldPath.Child("affinity"))...) + return allErrs +} + func ValidatePodSpecificAnnotationUpdates(newPod, oldPod *api.Pod, fldPath *field.Path) field.ErrorList { allErrs := field.ErrorList{} newAnnotations := newPod.Annotations