From d40961928408590a77598432fbbd374e63f78bdf Mon Sep 17 00:00:00 2001 From: Eric Ernst Date: Mon, 1 Jul 2019 14:36:37 -0700 Subject: [PATCH] RuntimeClass-admission: fixup comment, simplify nested ifs Signed-off-by: Eric Ernst --- .../pkg/admission/runtimeclass/admission.go | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/plugin/pkg/admission/runtimeclass/admission.go b/plugin/pkg/admission/runtimeclass/admission.go index aa06f9343b5..d58f48774f1 100644 --- a/plugin/pkg/admission/runtimeclass/admission.go +++ b/plugin/pkg/admission/runtimeclass/admission.go @@ -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) { 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) { - if runtimeClass != nil { - if runtimeClass.Overhead != 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 - } + if runtimeClass == nil || 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 + return nil }