Skip to content

Commit a528fe4

Browse files
Fix panic for some invalid custom metric requests
This refactors the handling of metrics related to namespaces, by moving the logic from the storage part to the handler. This allows the handler to perform validation of the request.
1 parent c68a98e commit a528fe4

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

pkg/apiserver/endpoints/handlers/get.go

+23
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ func ListResourceWithOptions(r cm_rest.ListerWithOptions, scope handlers.Request
4141
// For performance tracking purposes.
4242
trace := utiltrace.New("List " + req.URL.Path)
4343

44+
requestInfo, ok := request.RequestInfoFrom(req.Context())
45+
if !ok {
46+
err := errors.NewBadRequest("missing requestInfo")
47+
writeError(&scope, err, w, req)
48+
return
49+
}
50+
51+
// handle metrics describing namespaces
52+
if requestInfo.Namespace != "" && requestInfo.Resource == "metrics" {
53+
requestInfo.Subresource = requestInfo.Name
54+
requestInfo.Name = requestInfo.Namespace
55+
requestInfo.Resource = "namespaces"
56+
requestInfo.Namespace = ""
57+
requestInfo.Parts = append([]string{"namespaces", requestInfo.Name}, requestInfo.Parts[1:]...)
58+
}
59+
60+
// handle invalid requests, e.g. /namespaces/name/foo
61+
if len(requestInfo.Parts) < 3 {
62+
err := errors.NewBadRequest("invalid request path")
63+
writeError(&scope, err, w, req)
64+
return
65+
}
66+
4467
namespace, err := scope.Namer.Namespace(req)
4568
if err != nil {
4669
writeError(&scope, err, w, req)

pkg/registry/custom_metrics/reststorage.go

-9
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,6 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption
112112

113113
groupResource := schema.ParseGroupResource(resourceRaw)
114114

115-
// handle metrics describing namespaces
116-
if namespace != "" && resourceRaw == "metrics" {
117-
// namespace-describing metrics have a path of /namespaces/$NS/metrics/$metric,
118-
groupResource = schema.GroupResource{Resource: "namespaces"}
119-
metricName = name
120-
name = namespace
121-
namespace = ""
122-
}
123-
124115
var res *custom_metrics.MetricValueList
125116
var err error
126117

0 commit comments

Comments
 (0)