mirror of
https://github.com/intel/intel-device-plugins-for-kubernetes.git
synced 2025-06-03 03:59:37 +00:00
webhook: generate error when requesting unknown FPGA resource
This commit is contained in:
parent
f60aad70d6
commit
3814cdaf79
@ -126,7 +126,10 @@ func (p *patcher) getPatchOpsPreprogrammed(containerIdx int, container corev1.Co
|
|||||||
var ops []string
|
var ops []string
|
||||||
|
|
||||||
for resourceName, resourceQuantity := range container.Resources.Limits {
|
for resourceName, resourceQuantity := range container.Resources.Limits {
|
||||||
newName := p.translateFpgaResourceName(resourceName)
|
newName, err := p.translateFpgaResourceName(resourceName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if len(newName) > 0 {
|
if len(newName) > 0 {
|
||||||
op := fmt.Sprintf(resourceReplaceOp, containerIdx,
|
op := fmt.Sprintf(resourceReplaceOp, containerIdx,
|
||||||
"limits", rfc6901Escaper.Replace(string(resourceName)),
|
"limits", rfc6901Escaper.Replace(string(resourceName)),
|
||||||
@ -135,7 +138,10 @@ func (p *patcher) getPatchOpsPreprogrammed(containerIdx int, container corev1.Co
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for resourceName, resourceQuantity := range container.Resources.Requests {
|
for resourceName, resourceQuantity := range container.Resources.Requests {
|
||||||
newName := p.translateFpgaResourceName(resourceName)
|
newName, err := p.translateFpgaResourceName(resourceName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if len(newName) > 0 {
|
if len(newName) > 0 {
|
||||||
op := fmt.Sprintf(resourceReplaceOp, containerIdx,
|
op := fmt.Sprintf(resourceReplaceOp, containerIdx,
|
||||||
"requests", rfc6901Escaper.Replace(string(resourceName)),
|
"requests", rfc6901Escaper.Replace(string(resourceName)),
|
||||||
@ -147,15 +153,20 @@ func (p *patcher) getPatchOpsPreprogrammed(containerIdx int, container corev1.Co
|
|||||||
return ops, nil
|
return ops, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *patcher) translateFpgaResourceName(oldname corev1.ResourceName) string {
|
func (p *patcher) translateFpgaResourceName(oldname corev1.ResourceName) (string, error) {
|
||||||
|
rname := strings.ToLower(string(oldname))
|
||||||
|
if !strings.HasPrefix(rname, namespace) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
defer p.Unlock()
|
defer p.Unlock()
|
||||||
p.Lock()
|
p.Lock()
|
||||||
|
|
||||||
if newname, ok := p.resourceMap[strings.ToLower(string(oldname))]; ok {
|
if newname, ok := p.resourceMap[rname]; ok {
|
||||||
return newname
|
return newname, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return "", errors.Errorf("Unknown FPGA resource: %s", rname)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *patcher) getPatchOpsOrchestrated(containerIdx int, container corev1.Container) ([]string, error) {
|
func (p *patcher) getPatchOpsOrchestrated(containerIdx int, container corev1.Container) ([]string, error) {
|
||||||
|
@ -73,6 +73,78 @@ func TestPatcherStorageFunctions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetPatchOpsPreprogrammed(t *testing.T) {
|
||||||
|
tcases := []struct {
|
||||||
|
name string
|
||||||
|
resourceMap map[string]string
|
||||||
|
container corev1.Container
|
||||||
|
expectedErr bool
|
||||||
|
expectedOps int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Empty container",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Unknown resource in limits",
|
||||||
|
container: corev1.Container{
|
||||||
|
Resources: corev1.ResourceRequirements{
|
||||||
|
Limits: corev1.ResourceList{
|
||||||
|
"fpga.intel.com/arria10-unknown": resource.MustParse("1"),
|
||||||
|
"cpu": resource.MustParse("1"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Unknown resource in requests",
|
||||||
|
container: corev1.Container{
|
||||||
|
Resources: corev1.ResourceRequirements{
|
||||||
|
Requests: corev1.ResourceList{
|
||||||
|
"fpga.intel.com/arria10-unknown": resource.MustParse("1"),
|
||||||
|
"cpu": resource.MustParse("1"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Successful case",
|
||||||
|
container: corev1.Container{
|
||||||
|
Resources: corev1.ResourceRequirements{
|
||||||
|
Limits: corev1.ResourceList{
|
||||||
|
"fpga.intel.com/arria10-nlb0": resource.MustParse("1"),
|
||||||
|
"cpu": resource.MustParse("1"),
|
||||||
|
},
|
||||||
|
Requests: corev1.ResourceList{
|
||||||
|
"fpga.intel.com/arria10-nlb0": resource.MustParse("1"),
|
||||||
|
"cpu": resource.MustParse("1"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
resourceMap: map[string]string{
|
||||||
|
"fpga.intel.com/arria10-nlb0": rfc6901Escaper.Replace("fpga.intel.com/af-d8424dc4a4a3c413f89e433683f9040b"),
|
||||||
|
},
|
||||||
|
expectedOps: 2,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tcases {
|
||||||
|
p := &patcher{
|
||||||
|
resourceMap: tt.resourceMap,
|
||||||
|
}
|
||||||
|
ops, err := p.getPatchOpsPreprogrammed(0, tt.container)
|
||||||
|
if tt.expectedErr && err == nil {
|
||||||
|
t.Errorf("Test case '%s': no error returned", tt.name)
|
||||||
|
}
|
||||||
|
if !tt.expectedErr && err != nil {
|
||||||
|
t.Errorf("Test case '%s': unexpected error %v", tt.name, err)
|
||||||
|
}
|
||||||
|
if len(ops) != tt.expectedOps {
|
||||||
|
t.Errorf("test case '%s': expected %d ops, but got %d\n%v", tt.name, tt.expectedOps, len(ops), ops)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseResourceName(t *testing.T) {
|
func TestParseResourceName(t *testing.T) {
|
||||||
tcases := []struct {
|
tcases := []struct {
|
||||||
input string
|
input string
|
||||||
|
Loading…
Reference in New Issue
Block a user