// admitPod makes sure a new pod does not set spec.Priority field. It also makes sure that the PriorityClassName exists if it is provided and resolves the pod priority from the PriorityClassName.
// Note that pod validation mechanism prevents update of a pod priority.
returnadmission.NewForbidden(a,fmt.Errorf("The integer value of priority must not be provided in pod spec. The system populates the value from the given PriorityClass name"))
returnadmission.NewForbidden(a,fmt.Errorf("the integer value of priority must not be provided in pod spec. Priority admission controller populates the value from the given PriorityClass name"))
}
varpriorityint32
iflen(pod.Spec.PriorityClassName)==0{
dpc,err:=p.findDefaultPriorityClass()
iferr!=nil{
returnfmt.Errorf("Failed to get default priority class: %v",err)
// Now that we didn't find any system priority, try resolving by user defined priority classes.
pc,err:=p.lister.Get(pod.Spec.PriorityClassName)
iferr!=nil{
returnfmt.Errorf("failed to get default priority class %s: %v",pod.Spec.PriorityClassName,err)
}
ifpc==nil{
returnadmission.NewForbidden(a,fmt.Errorf("no PriorityClass with name %v was found",pod.Spec.PriorityClassName))
}
priority=pc.Value
}
priority=pc.Value
}
pod.Spec.Priority=&priority
}
pod.Spec.Priority=&priority
returnnil
}
// admitPriorityClass ensures that the value field is not larger than the highest user definable priority. If the GlobalDefault is set, it ensures that there is no other PriorityClass whose GlobalDefault is set.
returnerrors.NewBadRequest("Resource was marked with kind Pod but was unable to be converted")
returnerrors.NewBadRequest("resource was marked with kind PriorityClass but was unable to be converted")
}
ifpc.Value>HighestUserDefinablePriority{
returnadmission.NewForbidden(a,fmt.Errorf("Maximum allowed value of a user defined priority is %v",HighestUserDefinablePriority))
returnadmission.NewForbidden(a,fmt.Errorf("maximum allowed value of a user defined priority is %v",HighestUserDefinablePriority))
}
if_,ok:=SystemPriorityClasses[pc.Name];ok{
returnadmission.NewForbidden(a,fmt.Errorf("The name of the priority class is a reserved name for system use only: %v",pc.Name))
returnadmission.NewForbidden(a,fmt.Errorf("the name of the priority class is a reserved name for system use only: %v",pc.Name))
}
// If the new PriorityClass tries to be the default priority, make sure that no other priority class is marked as default.
ifpc.GlobalDefault{
dpc,err:=p.findDefaultPriorityClass()
dpc,err:=p.getDefaultPriorityClass()
iferr!=nil{
returnfmt.Errorf("Failed to get default priority class: %v",err)
returnfmt.Errorf("failed to get default priority class: %v",err)
}
ifdpc!=nil{
// Throw an error if a second default priority class is being created, or an existing priority class is being marked as default, while another default already exists.