Skip to content

Commit

Permalink
Fix KCPRequestInfoResolver for kind: Cluster
Browse files Browse the repository at this point in the history
The cache server preprocesses the path to remove the cluster prefix
in https://github.com/kcp-dev/kcp/blob/88d6071b2aaf47bcd5e854f8209e4c3bd8035b35/pkg/cache/server/config.go#L172-L186.

When a resource of type "cluster" is created and the first cluster
prefix is dropped, the path would be a k8s request info path (/apis/...)
but will still have the `clusters` keyword in the path.

Due to this, the regex in the kcp request info resolver does not work
well.

This commit updates the resolver:

1. If a path is a k8s request info path (begins with `/api` or `/apis`),
then the path is not checked against the regex.

2. The regex is updated to preserve the forward slash in the
strippedURL.
  • Loading branch information
nikhita committed May 21, 2023
1 parent 88d6071 commit 5788e66
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 8 deletions.
29 changes: 21 additions & 8 deletions pkg/server/requestinfo/kcp_request_info_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package requestinfo
import (
"net/http"
"regexp"
"strings"

"k8s.io/apiserver/pkg/endpoints/request"
)
Expand All @@ -33,16 +34,28 @@ func NewKCPRequestInfoResolver() *KCPRequestInfoResolver {
}
}

var clustersRE = regexp.MustCompile(`/clusters/[^/]+/(.*)$`)
var clustersRE = regexp.MustCompile(`/clusters/[^/]+(/.*)$`)

func (k *KCPRequestInfoResolver) NewRequestInfo(req *http.Request) (*request.RequestInfo, error) {
matches := clustersRE.FindStringSubmatch(req.URL.Path)
if len(matches) == 2 {
// matches[0] is the leftmost pattern that matches (which includes /clusters/...) - skip over that
strippedURL := matches[1]
t := req.Clone(req.Context())
t.URL.Path = strippedURL
return k.delegate.NewRequestInfo(t)
if !containsPrefix([]string{"/apis/", "/api/"}, req.URL.Path) {
matches := clustersRE.FindStringSubmatch(req.URL.Path)
if len(matches) == 2 {
// matches[0] is the leftmost pattern that matches (which includes /clusters/...) - skip over that
strippedURL := matches[1]
t := req.Clone(req.Context())
t.URL.Path = strippedURL
return k.delegate.NewRequestInfo(t)
}
}
return k.delegate.NewRequestInfo(req)
}

// containsPrefix returns true if the specified path has a prefix that contained in given prefix Set
func containsPrefix(prefixSet []string, path string) bool {
for _, prefix := range prefixSet {
if strings.HasPrefix(path, prefix) {
return true
}
}
return false
}
115 changes: 115 additions & 0 deletions pkg/server/requestinfo/kcp_request_info_resolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Copyright 2023 The KCP Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package requestinfo

import (
"net/http"
"net/url"
"testing"

"github.com/stretchr/testify/require"
)

func TestRequestInfoResolver(t *testing.T) {
tests := map[string]struct {
path string
expectedPath string
expectedResource string
expectedSubresource string
}{
"path with /api prefix": {
path: "/api/v1/namespaces/default/pods",
expectedPath: "/api/v1/namespaces/default/pods",
expectedResource: "pods",
},
"path with /apis prefix": {
path: "/apis/wildwest.dev/v1alpha1/namespaces/default/cowboys/woody",
expectedPath: "/apis/wildwest.dev/v1alpha1/namespaces/default/cowboys/woody",
expectedResource: "cowboys",
},
"path with /apis prefix -- status subresource": {
path: "/apis/wildwest.dev/v1alpha1/namespaces/default/cowboys/woody/status",
expectedPath: "/apis/wildwest.dev/v1alpha1/namespaces/default/cowboys/woody/status",
expectedResource: "cowboys",
expectedSubresource: "status",
},
"path with /apis prefix -- clusters resource": {
path: "/apis/wildwest.dev/v1alpha1/namespaces/default/clusters/woody",
expectedPath: "/apis/wildwest.dev/v1alpha1/namespaces/default/clusters/woody",
expectedResource: "clusters",
},
"path with /apis prefix -- clusters resource, status subresource": {
path: "/apis/wildwest.dev/v1alpha1/namespaces/default/clusters/woody/status",
expectedPath: "/apis/wildwest.dev/v1alpha1/namespaces/default/clusters/woody/status",
expectedResource: "clusters",
expectedSubresource: "status",
},
"path with /clusters prefix": {
path: "/clusters/root/apis/wildwest.dev/v1alpha1/namespaces/default/cowboys/woody",
expectedPath: "/apis/wildwest.dev/v1alpha1/namespaces/default/cowboys/woody",
expectedResource: "cowboys",
},
"path with /clusters prefix -- status subresource": {
path: "/clusters/root/apis/wildwest.dev/v1alpha1/namespaces/default/cowboys/woody/status",
expectedPath: "/apis/wildwest.dev/v1alpha1/namespaces/default/cowboys/woody/status",
expectedResource: "cowboys",
expectedSubresource: "status",
},
"path with /clusters prefix -- clusters resource": {
path: "/clusters/root/apis/wildwest.dev/v1alpha1/namespaces/default/clusters/woody",
expectedPath: "/apis/wildwest.dev/v1alpha1/namespaces/default/clusters/woody",
expectedResource: "clusters",
},
"path with /clusters prefix -- clusters resource, status subresource": {
path: "/clusters/root:my-ws/apis/wildwest.dev/v1alpha1/namespaces/default/clusters/woody/status",
expectedPath: "/apis/wildwest.dev/v1alpha1/namespaces/default/clusters/woody/status",
expectedResource: "clusters",
expectedSubresource: "status",
},
"path with /services prefix": {
path: "/services/apiexport/root:my-ws/my-service/clusters/*/api/v1/configmaps",
expectedPath: "/api/v1/configmaps",
expectedResource: "configmaps",
},
"path with /services prefix -- clusters resource, status subresource": {
path: "/services/apiexport/root:my-ws/my-service/clusters/*/apis/wildwest.dev/v1alpha1/namespaces/default/clusters/woody/status",
expectedPath: "/apis/wildwest.dev/v1alpha1/namespaces/default/clusters/woody/status",
expectedResource: "clusters",
expectedSubresource: "status",
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
url, err := url.Parse(test.path)
require.NoError(t, err, "error parsing path %q to URL", test.path)

req := &http.Request{
URL: url,
Method: http.MethodGet,
}

requestInfo, err := NewKCPRequestInfoResolver().NewRequestInfo(req)
require.NoError(t, err, "unexpected error")

require.Equal(t, test.expectedPath, requestInfo.Path, "unexpected requestInfo.Path")
require.Equal(t, test.expectedResource, requestInfo.Resource, "unexpected requestInfo.Resource")
require.Equal(t, test.expectedSubresource, requestInfo.Subresource, "unexpected requestInfo.Subresource")

})
}
}

0 comments on commit 5788e66

Please sign in to comment.