returnapierrors.NewForbidden(attributes.GetResource().GroupResource(),pod.Name,fmt.Errorf("affinity.PodAntiAffinity.RequiredDuringScheduling has TopologyKey %v but only key %v is allowed",v.TopologyKey,v1.LabelHostname))
description:"empty pod without any extended resources, expect no change in tolerations",
requestedPod:core.Pod{
Spec:core.PodSpec{},
},
expectedPod:core.Pod{
Spec:core.PodSpec{},
},
},
{
description:"pod with container without any extended resources, expect no change in tolerations",
requestedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingCPU,
},
},
},
expectedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingCPU,
},
},
},
},
{
description:"pod with init container without any extended resources, expect no change in tolerations",
requestedPod:core.Pod{
Spec:core.PodSpec{
InitContainers:[]core.Container{
containerRequestingMemory,
},
},
},
expectedPod:core.Pod{
Spec:core.PodSpec{
InitContainers:[]core.Container{
containerRequestingMemory,
},
},
},
},
{
description:"pod with container with extended resource, expect toleration to be added",
requestedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingExtendedResource1,
},
},
},
expectedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingExtendedResource1,
},
Tolerations:[]core.Toleration{
{
Key:extendedResource1,
Operator:core.TolerationOpExists,
Effect:core.TaintEffectNoSchedule,
},
},
},
},
},
{
description:"pod with init container with extended resource, expect toleration to be added",
requestedPod:core.Pod{
Spec:core.PodSpec{
InitContainers:[]core.Container{
containerRequestingExtendedResource2,
},
},
},
expectedPod:core.Pod{
Spec:core.PodSpec{
InitContainers:[]core.Container{
containerRequestingExtendedResource2,
},
Tolerations:[]core.Toleration{
{
Key:extendedResource2,
Operator:core.TolerationOpExists,
Effect:core.TaintEffectNoSchedule,
},
},
},
},
},
{
description:"pod with existing tolerations and container with extended resource, expect existing tolerations to be preserved and new toleration to be added",
requestedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingCPU,
containerRequestingExtendedResource1,
},
Tolerations:[]core.Toleration{
{
Key:"foo",
Operator:core.TolerationOpEqual,
Value:"bar",
Effect:core.TaintEffectNoSchedule,
},
},
},
},
expectedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingCPU,
containerRequestingExtendedResource1,
},
Tolerations:[]core.Toleration{
{
Key:"foo",
Operator:core.TolerationOpEqual,
Value:"bar",
Effect:core.TaintEffectNoSchedule,
},
{
Key:extendedResource1,
Operator:core.TolerationOpExists,
Effect:core.TaintEffectNoSchedule,
},
},
},
},
},
{
description:"pod with multiple extended resources, expect multiple tolerations to be added",
requestedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingMemory,
containerRequestingExtendedResource1,
},
InitContainers:[]core.Container{
containerRequestingCPU,
containerRequestingExtendedResource2,
},
},
},
expectedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingMemory,
containerRequestingExtendedResource1,
},
InitContainers:[]core.Container{
containerRequestingCPU,
containerRequestingExtendedResource2,
},
Tolerations:[]core.Toleration{
// Note the order, it's sorted by the Key
{
Key:extendedResource2,
Operator:core.TolerationOpExists,
Effect:core.TaintEffectNoSchedule,
},
{
Key:extendedResource1,
Operator:core.TolerationOpExists,
Effect:core.TaintEffectNoSchedule,
},
},
},
},
},
{
description:"pod with container requesting extended resource and existing correct toleration, expect no change in tolerations",
requestedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingCPU,
containerRequestingMemory,
containerRequestingExtendedResource1,
},
Tolerations:[]core.Toleration{
{
Key:extendedResource1,
Operator:core.TolerationOpExists,
Effect:core.TaintEffectNoSchedule,
},
},
},
},
expectedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingCPU,
containerRequestingMemory,
containerRequestingExtendedResource1,
},
Tolerations:[]core.Toleration{
{
Key:extendedResource1,
Operator:core.TolerationOpExists,
Effect:core.TaintEffectNoSchedule,
},
},
},
},
},
{
description:"pod with container requesting extended resource and existing toleration with the same key but different effect and value, expect existing tolerations to be preserved and new toleration to be added",
requestedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingCPU,
containerRequestingMemory,
containerRequestingExtendedResource1,
},
Tolerations:[]core.Toleration{
{
Key:extendedResource1,
Operator:core.TolerationOpEqual,
Value:"foo",
Effect:core.TaintEffectNoExecute,
},
},
},
},
expectedPod:core.Pod{
Spec:core.PodSpec{
Containers:[]core.Container{
containerRequestingCPU,
containerRequestingMemory,
containerRequestingExtendedResource1,
},
Tolerations:[]core.Toleration{
{
Key:extendedResource1,
Operator:core.TolerationOpEqual,
Value:"foo",
Effect:core.TaintEffectNoExecute,
},
{
Key:extendedResource1,
Operator:core.TolerationOpExists,
Effect:core.TaintEffectNoSchedule,
},
},
},
},
},
{
description:"pod with wildcard toleration and container requesting extended resource, expect existing tolerations to be preserved and new toleration to be added",
returnadmission.NewForbidden(attributes,fmt.Errorf("cannot set blockOwnerDeletion in this case because cannot find RESTMapping for APIVersion %s Kind %s: %v",ref.APIVersion,ref.Kind,err))
}
// Multiple records are returned if ref.Kind could map to multiple
// resources. User needs to have delete permission on all the
returnadmission.NewForbidden(attributes,fmt.Errorf("cannot set blockOwnerDeletion if an ownerReference refers to a resource you can't set finalizers on: %v, %v",reason,err))
klog.Warningf("failed to set admission audit annotation %s to %s: %v",key,pspName,err)
}
returnnil
}
// we didn't validate against any provider, reject the pod and give the errors for each attempt
klog.V(4).Infof("unable to validate pod %s (generate: %s) in namespace %s against any pod security policy: %v",pod.Name,pod.GenerateName,a.GetNamespace(),validationErrs)
returnadmission.NewForbidden(a,fmt.Errorf("unable to validate against any pod security policy: %v",validationErrs))
klog.Warningf("failed to set admission audit annotation %s to %s: %v",key,pspName,err)
}
returnnil
}
// we didn't validate against any provider, reject the pod and give the errors for each attempt
klog.V(4).Infof("unable to validate pod %s (generate: %s) in namespace %s against any pod security policy: %v",pod.Name,pod.GenerateName,a.GetNamespace(),validationErrs)
returnadmission.NewForbidden(a,fmt.Errorf("unable to validate against any pod security policy: %v",validationErrs))
t.Errorf("%s resulted in caps that were not expected - expected: %v, received: %v",k,v.expectedCapabilities,v.pod.Spec.Containers[0].SecurityContext.Capabilities)
t.Errorf("%s resulted in caps that were not expected - expected: %v, received: %v",k,v.expectedCapabilities,v.pod.Spec.InitContainers[0].SecurityContext.Capabilities)
t.Errorf("%s expected ReadOnlyRootFilesystem to be %t but found %#v",k,v.expectedRORFS,v.pod.Spec.Containers[0].SecurityContext.ReadOnlyRootFilesystem)
t.Errorf("%s: expected no mutation on Admit, got %s",testCaseName,diff.ObjectGoPrintSideBySide(originalPodWithoutPSPAnnotation.Spec,podWithoutPSPAnnotation.Spec))
}
}
}
if!shouldPassAdmit&&err==nil{
t.Errorf("%s: expected errors on Admit but received none",testCaseName)