Skip to content

Commit

Permalink
Merge pull request #141 from olivierlemasle/fix-panic-invalidreq
Browse files Browse the repository at this point in the history
Fix panic for some invalid custom metric requests
  • Loading branch information
k8s-ci-robot committed Sep 4, 2023
2 parents 7ed7110 + a528fe4 commit 1e7ed5d
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 1e7ed5d

Please sign in to comment.