Merge pull request #79621 from egernst/admission-fixups

RuntimeClass-admission: fixup comment, simplify nested ifs
This commit is contained in:
Kubernetes Prow Robot 2019-07-11 05:36:55 -07:00 committed by GitHub
commit d11eb67c02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -127,7 +127,7 @@ func NewRuntimeClass() *RuntimeClass {
} }
} }
// admissionAction handles Admit and Validate phases of admission, switching based on the admissionPhase parameter // prepareObjects returns pod and runtimeClass types from the given admission attributes
func (r *RuntimeClass) prepareObjects(attributes admission.Attributes) (pod *api.Pod, runtimeClass *v1beta1.RuntimeClass, err error) { func (r *RuntimeClass) prepareObjects(attributes admission.Attributes) (pod *api.Pod, runtimeClass *v1beta1.RuntimeClass, err error) {
pod, ok := attributes.GetObject().(*api.Pod) pod, ok := attributes.GetObject().(*api.Pod)
@ -160,25 +160,24 @@ func (r *RuntimeClass) getRuntimeClass(pod *api.Pod, runtimeClassName *string) (
func setOverhead(a admission.Attributes, pod *api.Pod, runtimeClass *v1beta1.RuntimeClass) (err error) { func setOverhead(a admission.Attributes, pod *api.Pod, runtimeClass *v1beta1.RuntimeClass) (err error) {
if runtimeClass != nil { if runtimeClass == nil || runtimeClass.Overhead == nil {
if runtimeClass.Overhead != nil { return nil
// convert to internal type and assign to pod's Overhead
nodeOverhead := &node.Overhead{}
err := nodev1beta1.Convert_v1beta1_Overhead_To_node_Overhead(runtimeClass.Overhead, nodeOverhead, nil)
if err != nil {
return err
}
// reject pod if Overhead is already set that differs from what is defined in RuntimeClass
if pod.Spec.Overhead != nil && !apiequality.Semantic.DeepEqual(nodeOverhead.PodFixed, pod.Spec.Overhead) {
return admission.NewForbidden(a, fmt.Errorf("pod rejected: Pod's Overhead doesn't match RuntimeClass's defined Overhead"))
}
pod.Spec.Overhead = nodeOverhead.PodFixed
}
} }
// convert to internal type and assign to pod's Overhead
nodeOverhead := &node.Overhead{}
err = nodev1beta1.Convert_v1beta1_Overhead_To_node_Overhead(runtimeClass.Overhead, nodeOverhead, nil)
if err != nil {
return err
}
// reject pod if Overhead is already set that differs from what is defined in RuntimeClass
if pod.Spec.Overhead != nil && !apiequality.Semantic.DeepEqual(nodeOverhead.PodFixed, pod.Spec.Overhead) {
return admission.NewForbidden(a, fmt.Errorf("pod rejected: Pod's Overhead doesn't match RuntimeClass's defined Overhead"))
}
pod.Spec.Overhead = nodeOverhead.PodFixed
return nil return nil
} }