mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-05 15:37:19 +00:00
added loop for namespace
Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
This commit is contained in:
parent
fa419439ec
commit
09b1592f11
2 changed files with 390 additions and 16 deletions
|
@ -42,26 +42,30 @@ func GetResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient
|
||||||
resourceTypes = append(resourceTypes, kind)
|
resourceTypes = append(resourceTypes, kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
var resourceMap map[string]map[string]*unstructured.Unstructured
|
var resourceMap map[string]map[string]map[string]*unstructured.Unstructured
|
||||||
if cluster && dClient != nil {
|
if cluster && dClient != nil {
|
||||||
resourceMap, err = getResourcesOfTypeFromCluster(resourceTypes, dClient, namespace)
|
resourceMap, err = getResourcesOfTypeFromCluster(resourceTypes, dClient, namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(resourcePaths) == 0 {
|
if len(resourcePaths) == 0 {
|
||||||
for _, rm := range resourceMap {
|
for _, rkm := range resourceMap {
|
||||||
for _, rr := range rm {
|
for _, rm := range rkm {
|
||||||
resources = append(resources, rr)
|
for _, rr := range rm {
|
||||||
|
resources = append(resources, rr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, resourcePath := range resourcePaths {
|
for _, resourcePath := range resourcePaths {
|
||||||
lenOfResource := len(resources)
|
lenOfResource := len(resources)
|
||||||
for _, rm := range resourceMap {
|
for _, rkm := range resourceMap {
|
||||||
for rn, rr := range rm {
|
for _, rm := range rkm {
|
||||||
if rn == resourcePath {
|
for rn, rr := range rm {
|
||||||
resources = append(resources, rr)
|
if rn == resourcePath {
|
||||||
continue
|
resources = append(resources, rr)
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -176,27 +180,27 @@ func GetResource(resourceBytes []byte) ([]*unstructured.Unstructured, error) {
|
||||||
return resources, nil
|
return resources, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getResourcesOfTypeFromCluster(resourceTypes []string, dClient *client.Client, namespace string) (map[string]map[string]*unstructured.Unstructured, error) {
|
func getResourcesOfTypeFromCluster(resourceTypes []string, dClient *client.Client, namespace string) (map[string]map[string]map[string]*unstructured.Unstructured, error) {
|
||||||
r := make(map[string]map[string]*unstructured.Unstructured)
|
r := make(map[string]map[string]map[string]*unstructured.Unstructured)
|
||||||
|
|
||||||
var resources []*unstructured.Unstructured
|
|
||||||
for _, kind := range resourceTypes {
|
for _, kind := range resourceTypes {
|
||||||
r[kind] = make(map[string]*unstructured.Unstructured)
|
r[kind] = make(map[string]map[string]*unstructured.Unstructured)
|
||||||
resourceList, err := dClient.ListResource("", kind, namespace, nil)
|
resourceList, err := dClient.ListResource("", kind, namespace, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// return nil, err
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
version := resourceList.GetAPIVersion()
|
version := resourceList.GetAPIVersion()
|
||||||
for _, resource := range resourceList.Items {
|
for _, resource := range resourceList.Items {
|
||||||
r[kind][resource.GetName()] = resource.DeepCopy()
|
if r[kind][resource.GetNamespace()] == nil {
|
||||||
|
r[kind][resource.GetNamespace()] = map[string]*unstructured.Unstructured{}
|
||||||
|
}
|
||||||
|
r[kind][resource.GetNamespace()][resource.GetName()] = resource.DeepCopy()
|
||||||
resource.SetGroupVersionKind(schema.GroupVersionKind{
|
resource.SetGroupVersionKind(schema.GroupVersionKind{
|
||||||
Group: "",
|
Group: "",
|
||||||
Version: version,
|
Version: version,
|
||||||
Kind: kind,
|
Kind: kind,
|
||||||
})
|
})
|
||||||
resources = append(resources, resource.DeepCopy())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return r, nil
|
return r, nil
|
||||||
|
|
Loading…
Add table
Reference in a new issue