webhook: generate error when requesting unknown FPGA resource

This commit is contained in:
Dmitry Rozhkov 2018-08-20 12:08:05 +03:00
parent f60aad70d6
commit 3814cdaf79
2 changed files with 89 additions and 6 deletions

View File

@ -126,7 +126,10 @@ func (p *patcher) getPatchOpsPreprogrammed(containerIdx int, container corev1.Co
var ops []string
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 {
op := fmt.Sprintf(resourceReplaceOp, containerIdx,
"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 {
newName := p.translateFpgaResourceName(resourceName)
newName, err := p.translateFpgaResourceName(resourceName)
if err != nil {
return nil, err
}
if len(newName) > 0 {
op := fmt.Sprintf(resourceReplaceOp, containerIdx,
"requests", rfc6901Escaper.Replace(string(resourceName)),
@ -147,15 +153,20 @@ func (p *patcher) getPatchOpsPreprogrammed(containerIdx int, container corev1.Co
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()
p.Lock()
if newname, ok := p.resourceMap[strings.ToLower(string(oldname))]; ok {
return newname
if newname, ok := p.resourceMap[rname]; ok {
return newname, nil
}
return ""
return "", errors.Errorf("Unknown FPGA resource: %s", rname)
}
func (p *patcher) getPatchOpsOrchestrated(containerIdx int, container corev1.Container) ([]string, error) {

View File

@ -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) {
tcases := []struct {
input string