1/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package validation
18
19import (
20	"fmt"
21	"strings"
22
23	apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
24	"k8s.io/apimachinery/pkg/util/validation/field"
25	apivalidation "k8s.io/kubernetes/pkg/apis/core/validation"
26	"k8s.io/kubernetes/pkg/apis/scheduling"
27	schedulingapiv1 "k8s.io/kubernetes/pkg/apis/scheduling/v1"
28)
29
30// ValidatePriorityClass tests whether required fields in the PriorityClass are
31// set correctly.
32func ValidatePriorityClass(pc *scheduling.PriorityClass) field.ErrorList {
33	allErrs := field.ErrorList{}
34	allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&pc.ObjectMeta, false, apimachineryvalidation.NameIsDNSSubdomain, field.NewPath("metadata"))...)
35	// If the priorityClass starts with a system prefix, it must be one of the
36	// predefined system priority classes.
37	if strings.HasPrefix(pc.Name, scheduling.SystemPriorityClassPrefix) {
38		if is, err := schedulingapiv1.IsKnownSystemPriorityClass(pc.Name, pc.Value, pc.GlobalDefault); !is {
39			allErrs = append(allErrs, field.Forbidden(field.NewPath("metadata", "name"), "priority class names with '"+scheduling.SystemPriorityClassPrefix+"' prefix are reserved for system use only. error: "+err.Error()))
40		}
41	} else if pc.Value > scheduling.HighestUserDefinablePriority {
42		// Non-system critical priority classes are not allowed to have a value larger than HighestUserDefinablePriority.
43		allErrs = append(allErrs, field.Forbidden(field.NewPath("value"), fmt.Sprintf("maximum allowed value of a user defined priority is %v", scheduling.HighestUserDefinablePriority)))
44	}
45	if pc.PreemptionPolicy != nil {
46		allErrs = append(allErrs, apivalidation.ValidatePreemptionPolicy(pc.PreemptionPolicy, field.NewPath("preemptionPolicy"))...)
47	}
48	return allErrs
49}
50
51// ValidatePriorityClassUpdate tests if required fields in the PriorityClass are
52// set and are valid. PriorityClass does not allow updating Name, and Value.
53func ValidatePriorityClassUpdate(pc, oldPc *scheduling.PriorityClass) field.ErrorList {
54	allErrs := apivalidation.ValidateObjectMetaUpdate(&pc.ObjectMeta, &oldPc.ObjectMeta, field.NewPath("metadata"))
55	// Name is immutable and is checked by the ObjectMeta validator.
56	if pc.Value != oldPc.Value {
57		allErrs = append(allErrs, field.Forbidden(field.NewPath("Value"), "may not be changed in an update."))
58	}
59	// PreemptionPolicy is immutable and is checked by the ObjectMeta validator.
60	allErrs = append(allErrs, apivalidation.ValidateImmutableField(pc.PreemptionPolicy, oldPc.PreemptionPolicy, field.NewPath("preemptionPolicy"))...)
61	return allErrs
62}
63