-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enables to get multiple specific unused resources (#57)
* Enables to get multiple specific unused resources * Add verification to indicate a list of resources
- Loading branch information
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package kor | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func retrieveNamespaceDiffs(kubeClient *kubernetes.Clientset, namespace string, resourceList []string) []ResourceDiff { | ||
var allDiffs []ResourceDiff | ||
for _, resource := range resourceList { | ||
switch resource { | ||
case "cm", "configmap": | ||
namespaceCMDiff := getUnusedCMs(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespaceCMDiff) | ||
case "svc", "service", "services": | ||
namespaceSVCDiff := getUnusedSVCs(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespaceSVCDiff) | ||
case "scrt", "secret": | ||
namespaceSecretDiff := getUnusedSecrets(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespaceSecretDiff) | ||
case "sa", "serviceaccount": | ||
namespaceSADiff := getUnusedServiceAccounts(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespaceSADiff) | ||
case "deploy", "deployments": | ||
namespaceDeploymentDiff := getUnusedDeployments(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespaceDeploymentDiff) | ||
case "sts", "statefulsets": | ||
namespaceStatefulsetDiff := getUnusedStatefulsets(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespaceStatefulsetDiff) | ||
case "role": | ||
namespaceRoleDiff := getUnusedRoles(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespaceRoleDiff) | ||
case "hpa": | ||
namespaceHpaDiff := getUnusedHpas(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespaceHpaDiff) | ||
case "pvc": | ||
namespacePvcDiff := getUnusedPvcs(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespacePvcDiff) | ||
case "ing", "ingress": | ||
namespaceIngressDiff := getUnusedIngresses(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespaceIngressDiff) | ||
case "pdb": | ||
namespacePdbDiff := getUnusedPdbs(kubeClient, namespace) | ||
allDiffs = append(allDiffs, namespacePdbDiff) | ||
} | ||
} | ||
return allDiffs | ||
} | ||
|
||
func GetUnusedMulti(includeExcludeLists IncludeExcludeLists, kubeconfig, resourceNames string) { | ||
var kubeClient *kubernetes.Clientset | ||
var namespaces []string | ||
|
||
kubeClient = GetKubeClient(kubeconfig) | ||
|
||
resourceList := strings.Split(resourceNames, ",") | ||
namespaces = SetNamespaceList(includeExcludeLists, kubeClient) | ||
|
||
for _, namespace := range namespaces { | ||
allDiffs := retrieveNamespaceDiffs(kubeClient, namespace, resourceList) | ||
output := FormatOutputAll(namespace, allDiffs) | ||
fmt.Println(output) | ||
fmt.Println() | ||
} | ||
} | ||
|
||
func GetUnusedMultiStructured(includeExcludeLists IncludeExcludeLists, kubeconfig, outputFormat, resourceNames string) (string, error) { | ||
var kubeClient *kubernetes.Clientset | ||
var namespaces []string | ||
|
||
kubeClient = GetKubeClient(kubeconfig) | ||
|
||
resourceList := strings.Split(resourceNames, ",") | ||
namespaces = SetNamespaceList(includeExcludeLists, kubeClient) | ||
|
||
// Create the JSON response object | ||
response := make(map[string]map[string][]string) | ||
|
||
for _, namespace := range namespaces { | ||
allDiffs := retrieveNamespaceDiffs(kubeClient, namespace, resourceList) | ||
// Store the unused resources for each resource type in the JSON response | ||
resourceMap := make(map[string][]string) | ||
for _, diff := range allDiffs { | ||
resourceMap[diff.resourceType] = diff.diff | ||
} | ||
response[namespace] = resourceMap | ||
} | ||
|
||
// Convert the response object to JSON | ||
jsonResponse, err := json.MarshalIndent(response, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if outputFormat == "yaml" { | ||
yamlResponse, err := yaml.JSONToYAML(jsonResponse) | ||
if err != nil { | ||
fmt.Printf("err: %v\n", err) | ||
} | ||
return string(yamlResponse), nil | ||
} else { | ||
return string(jsonResponse), nil | ||
} | ||
} |