Skip to content

Commit

Permalink
code optimization
Browse files Browse the repository at this point in the history
Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
  • Loading branch information
NoSkillGirl committed Jun 16, 2021
1 parent b8ada99 commit f42aff1
Showing 1 changed file with 56 additions and 59 deletions.
115 changes: 56 additions & 59 deletions pkg/kyverno/common/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 nil, err
return resources, 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("", "-")
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))
}
}
}
} else if len(resourcePaths) > 0 {
for _, resourcePath := range resourcePaths {
resourceBytes, err := getFileBytes(resourcePath)
if err != nil {

if lenOfResource >= len(resources) {
if policyReport {
log.Log.V(3).Info(fmt.Sprintf("failed to load resources: %s.", resourcePath), "error", err)
log.Log.V(3).Info(fmt.Sprintf("%s not found in cluster", resourcePath))
} else {
fmt.Printf("\n----------------------------------------------------------------------\nfailed to load resources: %s. \nerror: %s\n----------------------------------------------------------------------\n", resourcePath, err)
fmt.Printf("\n----------------------------------------------------------------------\nresource %s not found in cluster\n----------------------------------------------------------------------\n", resourcePath)
}
continue
return nil, errors.New(fmt.Sprintf("%s not found in cluster", resourcePath))
}
}
}
return resources, nil
}

getResources, err := GetResource(resourceBytes)
if err != nil {
return nil, err
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
}

for _, resource := range getResources {
resources = append(resources, resource)
}
getResources, err := GetResource(resourceBytes)
if err != nil {
return nil, err
}

for _, resource := range getResources {
resources = append(resources, resource)
}
}
return resources, nil
Expand Down Expand Up @@ -189,23 +193,16 @@ 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
}

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{
Expand Down

0 comments on commit f42aff1

Please sign in to comment.