Skip to content

Commit

Permalink
Use IETF-6901 encoding for resource patches
Browse files Browse the repository at this point in the history
Extended resources must be qualified by domain (e.g. example.com/dongle).
The special '/' must be properly escaped the patch to be properly applied.
  • Loading branch information
domenicbozzuto committed Apr 11, 2024
1 parent 23788cb commit 472c584
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource/pod/recommendation"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"

"github.com/go-openapi/jsonpointer"
)

const (
Expand Down Expand Up @@ -105,9 +107,11 @@ func appendPatchesAndAnnotations(patches []resource_admission.PatchRecord, annot
}

func getAddResourceRequirementValuePatch(i int, kind string, resource core.ResourceName, quantity resource.Quantity) resource_admission.PatchRecord {
// Patch fields which can contain '/' characters (e.g. domain-qualified extended resources) must conform
// to IETF-6901 and escape those characters
return resource_admission.PatchRecord{
Op: "add",
Path: fmt.Sprintf("/spec/containers/%d/resources/%s/%s", i, kind, resource),
Path: fmt.Sprintf("/spec/containers/%d/resources/%s/%s", i, kind, jsonpointer.Escape(resource.String())),
Value: quantity.String()}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/test"
vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"

"github.com/go-openapi/jsonpointer"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -248,6 +249,33 @@ func TestClalculatePatches_ResourceUpdates(t *testing.T) {
addAnnotationRequest([][]string{{cpu}}, limit),
},
},
{
name: "patch for extended resource properly escaped",
pod: &core.Pod{
Spec: core.PodSpec{
Containers: []core.Container{{
Resources: core.ResourceRequirements{
Requests: core.ResourceList{
"example.com/dongle": resource.MustParse("0"),
},
},
}},
},
},
namespace: "default",
recommendResources: []vpa_api_util.ContainerResources{
{
Requests: core.ResourceList{
"example.com/dongle": resource.MustParse("1"),
},
},
},
recommendAnnotations: vpa_api_util.ContainerToAnnotationsMap{},
expectPatches: []resource_admission.PatchRecord{
addResourceRequestPatch(0, jsonpointer.Escape("example.com/dongle"), "1"),
addAnnotationRequest([][]string{{"example.com/dongle"}}, request),
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 472c584

Please sign in to comment.