1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-04-16 01:07:14 +00:00

536 apply on cluster now supports significantly more resource types

This commit is contained in:
shravan 2020-01-25 22:38:54 +05:30
parent 78edfd2f7d
commit 94f8721a6e
5 changed files with 53 additions and 22 deletions

View file

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"k8s.io/apimachinery/pkg/runtime/schema"
@ -27,25 +26,6 @@ import (
"k8s.io/client-go/kubernetes/scheme"
)
var kindToListApi = map[string]string{
"resourcequota": "/api/v1/resourcequotas",
"serviceaccount": "/api/v1/serviceaccounts",
"limitrange": "/api/v1/limitranges",
"replicationcontroller": "/api/v1/replicationcontrollers",
"persistentvolume": "/api/v1/persistentvolumes",
"event": "/api/v1/events",
"persistentvolumeclaim": "/api/v1/persistentvolumeclaims",
"podtemplate": "/api/v1/podtemplates",
"componentstatus": "/api/v1/componentstatuses",
"secret": "/api/v1/secrets",
"service": "/api/v1/services",
"namespace": "/api/v1/namespaces",
"node": "/api/v1/nodes",
"endpoint": "/api/v1/endpoints",
"pod": "/api/v1/pods",
"configmap": "/api/v1/configmaps",
}
func Command() *cobra.Command {
var resourcePath, kubeConfig string
@ -152,7 +132,12 @@ func getResourcesOfTypeFromCluster(resourceTypes []string, kubeConfig string) ([
}
for _, kind := range resourceTypes {
listObjectRaw, err := dClient.RESTClient().Get().RequestURI(kindToListApi[strings.ToLower(kind)]).Do().Raw()
endpoint, err := getListEndpointForKind(kind)
if err != nil {
return nil, err
}
listObjectRaw, err := dClient.RESTClient().Get().RequestURI(endpoint).Do().Raw()
if err != nil {
return nil, err
}

View file

@ -0,0 +1,37 @@
package apply
import (
"fmt"
"strings"
"github.com/nirmata/kyverno/pkg/openapi"
)
func getListEndpointForKind(kind string) (string, error) {
definitionName := openapi.GetDefinitionNameFromKind(kind)
definitionNameWithoutPrefix := strings.Replace(definitionName, "io.k8s.", "", -1)
parts := strings.Split(definitionNameWithoutPrefix, ".")
definitionPrefix := strings.Join(parts[:len(parts)-1], ".")
defPrefixToApiPrefix := map[string]string{
"api.core.v1": "/api/v1",
"api.apps.v1": "/apis/apps/v1",
"api.batch.v1": "/apis/batch/v1",
"api.admissionregistration.v1": "/apis/admissionregistration.k8s.io/v1",
"kube-aggregator.pkg.apis.apiregistration.v1": "/apis/apiregistration.k8s.io/v1",
"apiextensions-apiserver.pkg.apis.apiextensions.v1": "/apis/apiextensions.k8s.io/v1",
"api.autoscaling.v1": "/apis/autoscaling/v1/",
"api.storage.v1": "/apis/storage.k8s.io/v1",
"api.coordination.v1": "/apis/coordination.k8s.io/v1",
"api.scheduling.v1": "/apis/scheduling.k8s.io/v1",
"api.rbac.v1": "/apis/rbac.authorization.k8s.io/v1",
}
if defPrefixToApiPrefix[definitionPrefix] == "" {
return "", fmt.Errorf("Unsupported resource type %v", kind)
}
return defPrefixToApiPrefix[definitionPrefix] + "/" + strings.ToLower(kind) + "s", nil
}

View file

@ -5,7 +5,7 @@ import (
"fmt"
"io/ioutil"
policyvalidate "github.com/nirmata/kyverno/pkg/engine/policy"
policyvalidate "github.com/nirmata/kyverno/pkg/policy"
v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
"github.com/spf13/cobra"

5
pkg/openapi/helper.go Normal file
View file

@ -0,0 +1,5 @@
package openapi
func GetDefinitionNameFromKind(kind string) string {
return openApiGlobalState.kindToDefinitionName[kind]
}

View file

@ -157,6 +157,10 @@ func setValidationGlobalState() error {
openApiGlobalState.kindToDefinitionName[path[len(path)-1]] = definition.GetName()
}
for _, path := range openApiGlobalState.document.GetPaths().GetPath() {
path.GetName()
}
openApiGlobalState.models, err = proto.NewOpenAPIData(openApiGlobalState.document)
if err != nil {
return err