mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 16:06:56 +00:00
* initial commit for api server lookups Signed-off-by: Jim Bugwadia <jim@nirmata.com> * initial commit for API server lookups Signed-off-by: Jim Bugwadia <jim@nirmata.com> * Enhancing dockerfiles (multi-stage) of kyverno components and adding non-root user to the docker images (#1495) * Dockerfile refactored Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com> * Adding non-root commands to docker images and enhanced the dockerfiles Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com> * changing base image to scratch Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com> * Minor typo fix Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com> * changing dockerfiles to use /etc/passwd to use non-root user' Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com> * minor typo Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com> * minor typo Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com> Signed-off-by: Jim Bugwadia <jim@nirmata.com> * revert cli image name (#1507) Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com> Signed-off-by: Jim Bugwadia <jim@nirmata.com> * Refactor resourceCache; Reduce throttling requests (background controller) (#1500) * skip sending API request for filtered resource * fix PR comment Signed-off-by: Shuting Zhao <shutting06@gmail.com> * fixes https://github.com/kyverno/kyverno/issues/1490 Signed-off-by: Shuting Zhao <shutting06@gmail.com> * fix bug - namespace is not returned properly Signed-off-by: Shuting Zhao <shutting06@gmail.com> * reduce throttling - list resource using lister * refactor resource cache * fix test Signed-off-by: Shuting Zhao <shutting06@gmail.com> * fix label selector Signed-off-by: Shuting Zhao <shutting06@gmail.com> * fix build failure Signed-off-by: Shuting Zhao <shutting06@gmail.com> Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix merge issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix unit test Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add nil check for API client Signed-off-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Raj Babu Das <mail.rajdas@gmail.com> Co-authored-by: shuting <shutting06@gmail.com>
123 lines
2.7 KiB
Go
123 lines
2.7 KiB
Go
package engine
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type APIPath struct {
|
|
Root string
|
|
Group string
|
|
Version string
|
|
ResourceType string
|
|
Name string
|
|
Namespace string
|
|
}
|
|
|
|
// NewAPIPath validates and parses an API path.
|
|
// See: https://kubernetes.io/docs/reference/using-api/api-concepts/
|
|
func NewAPIPath(path string) (*APIPath, error) {
|
|
trimmedPath := strings.Trim(path, "/ ")
|
|
paths := strings.Split(trimmedPath, "/")
|
|
|
|
if len(paths) < 3 || len(paths) > 7 {
|
|
return nil, fmt.Errorf("invalid path length %s", path)
|
|
}
|
|
|
|
if paths[0] != "api" && paths[0] != "apis" {
|
|
return nil, fmt.Errorf("urlPath must start with /api or /apis")
|
|
}
|
|
|
|
if paths[0] == "api" && paths[1] != "v1" {
|
|
return nil, fmt.Errorf("expected urlPath to start with /api/v1/")
|
|
}
|
|
|
|
if paths[0] == "api" {
|
|
if len(paths) == 3 {
|
|
return &APIPath{
|
|
Root: paths[0],
|
|
Group: paths[1],
|
|
ResourceType: paths[2],
|
|
}, nil
|
|
}
|
|
|
|
if len(paths) == 4 {
|
|
return &APIPath{
|
|
Root: paths[0],
|
|
Group: paths[1],
|
|
ResourceType: paths[2],
|
|
Name: paths[3],
|
|
}, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid /api/v1 path %s", path)
|
|
}
|
|
|
|
// /apis/GROUP/VERSION/RESOURCETYPE/
|
|
if len(paths) == 4 {
|
|
return &APIPath{
|
|
Root: paths[0],
|
|
Group: paths[1],
|
|
Version: paths[2],
|
|
ResourceType: paths[3],
|
|
}, nil
|
|
}
|
|
|
|
// /apis/GROUP/VERSION/RESOURCETYPE/NAME
|
|
if len(paths) == 5 {
|
|
return &APIPath{
|
|
Root: paths[0],
|
|
Group: paths[1],
|
|
Version: paths[2],
|
|
ResourceType: paths[3],
|
|
Name: paths[4],
|
|
}, nil
|
|
}
|
|
|
|
// /apis/GROUP/VERSION/namespaces/NAMESPACE/RESOURCETYPE
|
|
if len(paths) == 6 {
|
|
return &APIPath{
|
|
Root: paths[0],
|
|
Group: paths[1],
|
|
Version: paths[2],
|
|
Namespace: paths[4],
|
|
ResourceType: paths[5],
|
|
}, nil
|
|
}
|
|
|
|
// /apis/GROUP/VERSION/namespaces/NAMESPACE/RESOURCETYPE/NAME
|
|
if len(paths) == 7 {
|
|
return &APIPath{
|
|
Root: paths[0],
|
|
Group: paths[1],
|
|
Version: paths[2],
|
|
Namespace: paths[4],
|
|
ResourceType: paths[5],
|
|
Name: paths[6],
|
|
}, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid /apis path %s", path)
|
|
}
|
|
|
|
func (a *APIPath) String() string {
|
|
var paths []string
|
|
if a.Namespace != "" {
|
|
if a.Name == "" {
|
|
paths = []string{a.Root, a.Group, a.Version, a.ResourceType, "namespaces", a.Namespace}
|
|
} else {
|
|
paths = []string{a.Root, a.Group, a.Version, a.ResourceType, "namespaces", a.Namespace, a.Name}
|
|
}
|
|
} else {
|
|
if a.Name != "" {
|
|
paths = []string{a.Root, a.Group, a.Version, a.ResourceType, a.Name}
|
|
} else {
|
|
paths = []string{a.Root, a.Group, a.Version, a.ResourceType}
|
|
}
|
|
}
|
|
|
|
|
|
result := "/" + strings.Join(paths, "/")
|
|
result = strings.ReplaceAll(result, "//", "/")
|
|
return result
|
|
}
|