Skip to content

Commit

Permalink
Fix panic for some invalid custom metric requests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
olivierlemasle committed Jul 27, 2023
1 parent c68a98e commit a528fe4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
23 changes: 23 additions & 0 deletions pkg/apiserver/endpoints/handlers/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ func ListResourceWithOptions(r cm_rest.ListerWithOptions, scope handlers.Request
// For performance tracking purposes.
trace := utiltrace.New("List " + req.URL.Path)

requestInfo, ok := request.RequestInfoFrom(req.Context())
if !ok {
err := errors.NewBadRequest("missing requestInfo")
writeError(&scope, err, w, req)
return
}

// handle metrics describing namespaces
if requestInfo.Namespace != "" && requestInfo.Resource == "metrics" {
requestInfo.Subresource = requestInfo.Name
requestInfo.Name = requestInfo.Namespace
requestInfo.Resource = "namespaces"
requestInfo.Namespace = ""
requestInfo.Parts = append([]string{"namespaces", requestInfo.Name}, requestInfo.Parts[1:]...)
}

// handle invalid requests, e.g. /namespaces/name/foo
if len(requestInfo.Parts) < 3 {
err := errors.NewBadRequest("invalid request path")
writeError(&scope, err, w, req)
return
}

namespace, err := scope.Namer.Namespace(req)
if err != nil {
writeError(&scope, err, w, req)
Expand Down
9 changes: 0 additions & 9 deletions pkg/registry/custom_metrics/reststorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,6 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption

groupResource := schema.ParseGroupResource(resourceRaw)

// handle metrics describing namespaces
if namespace != "" && resourceRaw == "metrics" {
// namespace-describing metrics have a path of /namespaces/$NS/metrics/$metric,
groupResource = schema.GroupResource{Resource: "namespaces"}
metricName = name
name = namespace
namespace = ""
}

var res *custom_metrics.MetricValueList
var err error

Expand Down

0 comments on commit a528fe4

Please sign in to comment.