1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-04-08 10:04:25 +00:00

code optimization

Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
This commit is contained in:
NoSkillGirl 2021-06-16 07:56:08 +05:30
parent b8ada99d50
commit f42aff1a42

View file

@ -42,71 +42,75 @@ func GetResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient
resourceTypes = append(resourceTypes, kind)
}
// var resourceMap map[string]map[string]map[string]*unstructured.Unstructured
var resourceMap map[string]*unstructured.Unstructured
if cluster && dClient != nil {
resourceMap, err = getResourcesOfTypeFromCluster(resourceTypes, dClient, namespace)
resources, err = whenClusterIsTrue(resourceTypes, dClient, namespace, resourcePaths, policyReport)
if err != nil {
return resources, err
}
fmt.Println("resources: ", resources)
} else if len(resourcePaths) > 0 {
resources, err = whenClusterIsFalse(resourcePaths, policyReport)
if err != nil {
return resources, err
}
}
return resources, err
}
func whenClusterIsTrue(resourceTypes []string, dClient *client.Client, namespace string, resourcePaths []string, policyReport bool) ([]*unstructured.Unstructured, error) {
resources := make([]*unstructured.Unstructured, 0)
resourceMap, err := getResourcesOfTypeFromCluster(resourceTypes, dClient, namespace)
if err != nil {
return nil, err
}
if len(resourcePaths) == 0 {
for _, rr := range resourceMap {
resources = append(resources, rr)
}
} else {
for _, resourcePath := range resourcePaths {
lenOfResource := len(resources)
for rn, rr := range resourceMap {
s := strings.Split(rn, "-")
if s[2] == resourcePath {
resources = append(resources, rr)
}
}
if lenOfResource >= len(resources) {
if policyReport {
log.Log.V(3).Info(fmt.Sprintf("%s not found in cluster", resourcePath))
} else {
fmt.Printf("\n----------------------------------------------------------------------\nresource %s not found in cluster\n----------------------------------------------------------------------\n", resourcePath)
}
return nil, errors.New(fmt.Sprintf("%s not found in cluster", resourcePath))
}
}
}
return resources, nil
}
func whenClusterIsFalse(resourcePaths []string, policyReport bool) ([]*unstructured.Unstructured, error) {
resources := make([]*unstructured.Unstructured, 0)
for _, resourcePath := range resourcePaths {
resourceBytes, err := getFileBytes(resourcePath)
if err != nil {
if policyReport {
log.Log.V(3).Info(fmt.Sprintf("failed to load resources: %s.", resourcePath), "error", err)
} else {
fmt.Printf("\n----------------------------------------------------------------------\nfailed to load resources: %s. \nerror: %s\n----------------------------------------------------------------------\n", resourcePath, err)
}
continue
}
getResources, err := GetResource(resourceBytes)
if err != nil {
return nil, err
}
if len(resourcePaths) == 0 {
// for _, rkm := range resourceMap {
// for _, rm := range rkm {
// for _, rr := range rm {
// resources = append(resources, rr)
// }
// }
// }
for _, rr := range resourceMap {
resources = append(resources, rr)
}
} else {
for _, resourcePath := range resourcePaths {
lenOfResource := len(resources)
// for _, rkm := range resourceMap {
// for _, rm := range rkm {
// for rn, rr := range rm {
// if rn == resourcePath {
// resources = append(resources, rr)
// continue
// }
// }
// }
// }
for _, rr := range resourceMap {
s := strings.Split("", "-")
resources = append(resources, rr)
}
if lenOfResource >= len(resources) {
if policyReport {
log.Log.V(3).Info(fmt.Sprintf("%s not found in cluster", resourcePath))
} else {
fmt.Printf("\n----------------------------------------------------------------------\nresource %s not found in cluster\n----------------------------------------------------------------------\n", resourcePath)
}
return nil, errors.New(fmt.Sprintf("%s not found in cluster", resourcePath))
}
}
}
} else if len(resourcePaths) > 0 {
for _, resourcePath := range resourcePaths {
resourceBytes, err := getFileBytes(resourcePath)
if err != nil {
if policyReport {
log.Log.V(3).Info(fmt.Sprintf("failed to load resources: %s.", resourcePath), "error", err)
} else {
fmt.Printf("\n----------------------------------------------------------------------\nfailed to load resources: %s. \nerror: %s\n----------------------------------------------------------------------\n", resourcePath, err)
}
continue
}
getResources, err := GetResource(resourceBytes)
if err != nil {
return nil, err
}
for _, resource := range getResources {
resources = append(resources, resource)
}
for _, resource := range getResources {
resources = append(resources, resource)
}
}
return resources, nil
@ -189,11 +193,9 @@ func GetResource(resourceBytes []byte) ([]*unstructured.Unstructured, error) {
}
func getResourcesOfTypeFromCluster(resourceTypes []string, dClient *client.Client, namespace string) (map[string]*unstructured.Unstructured, error) {
// r := make(map[string]map[string]map[string]*unstructured.Unstructured)
r := make(map[string]*unstructured.Unstructured)
for _, kind := range resourceTypes {
// r[kind] = make(map[string]map[string]*unstructured.Unstructured)
resourceList, err := dClient.ListResource("", kind, namespace, nil)
if err != nil {
continue
@ -201,11 +203,6 @@ func getResourcesOfTypeFromCluster(resourceTypes []string, dClient *client.Clien
version := resourceList.GetAPIVersion()
for _, resource := range resourceList.Items {
// if r[kind][resource.GetNamespace()] == nil {
// r[kind][resource.GetNamespace()] = map[string]*unstructured.Unstructured{}
// }
// r[kind][resource.GetNamespace()][resource.GetName()] = resource.DeepCopy()
key := kind + "-" + resource.GetNamespace() + "-" + resource.GetName()
r[key] = resource.DeepCopy()
resource.SetGroupVersionKind(schema.GroupVersionKind{