Skip to content

Commit

Permalink
refactor: map false minikube unused resources (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
cecobask committed Apr 30, 2024
1 parent 8adabd8 commit 8822778
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 14 deletions.
67 changes: 67 additions & 0 deletions pkg/kor/clusterroles.go
Expand Up @@ -17,6 +17,69 @@ import (
"github.com/yonahd/kor/pkg/filters"
)

var exceptionClusterRoles = []ExceptionResource{
{
ResourceName: "admin",
Namespace: "",
},
{
ResourceName: "edit",
Namespace: "",
},
{
ResourceName: "system:aggregate-to-admin",
Namespace: "",
},
{
ResourceName: "system:aggregate-to-edit",
Namespace: "",
},
{
ResourceName: "system:aggregate-to-view",
Namespace: "",
},
{
ResourceName: "system:auth-delegator",
Namespace: "",
},
{
ResourceName: "system:certificates.k8s.io:kube-apiserver-client-approver",
Namespace: "",
},
{
ResourceName: "system:certificates.k8s.io:kube-apiserver-client-kubelet-approver",
Namespace: "",
},
{
ResourceName: "system:certificates.k8s.io:kubelet-serving-approver",
Namespace: "",
},
{
ResourceName: "system:certificates.k8s.io:legacy-unknown-approver",
Namespace: "",
},
{
ResourceName: "system:heapster",
Namespace: "",
},
{
ResourceName: "system:kube-aggregator",
Namespace: "",
},
{
ResourceName: "system:kubelet-api-admin",
Namespace: "",
},
{
ResourceName: "system:node-problem-detector",
Namespace: "",
},
{
ResourceName: "view",
Namespace: "",
},
}

func retrieveUsedClusterRoles(clientset kubernetes.Interface, filterOpts *filters.Options) ([]string, error) {

//Get a list of all namespaces
Expand Down Expand Up @@ -133,6 +196,10 @@ func retrieveClusterRoleNames(clientset kubernetes.Interface, filterOpts *filter
continue
}

if isResourceException(clusterRole.Name, "", exceptionClusterRoles) {
continue
}

names = append(names, clusterRole.Name)
}
return names, unusedClusterRoles, nil
Expand Down
46 changes: 41 additions & 5 deletions pkg/kor/configmaps.go
Expand Up @@ -14,9 +14,39 @@ import (
"github.com/yonahd/kor/pkg/filters"
)

var exceptionconfigmaps = []ExceptionResource{
{ResourceName: "aws-auth", Namespace: "kube-system"},
{ResourceName: "kube-root-ca.crt", Namespace: "*"},
var exceptionConfigMaps = []ExceptionResource{
{
ResourceName: "aws-auth",
Namespace: "kube-system",
},
{
ResourceName: "kube-root-ca.crt",
Namespace: "*",
},
{
ResourceName: "extension-apiserver-authentication",
Namespace: "kube-system",
},
{
ResourceName: "kube-apiserver-legacy-service-account-token-tracking",
Namespace: "kube-system",
},
{
ResourceName: "kubeadm-config",
Namespace: "kube-system",
},
{
ResourceName: "kubelet-config",
Namespace: "kube-system",
},
{
ResourceName: "kubernetes-dashboard-settings",
Namespace: "kubernetes-dashboard",
},
{
ResourceName: "cluster-info",
Namespace: "kube-public",
},
}

func retrieveUsedCM(clientset kubernetes.Interface, namespace string) ([]string, []string, []string, []string, []string, error) {
Expand Down Expand Up @@ -75,7 +105,7 @@ func retrieveUsedCM(clientset kubernetes.Interface, namespace string) ([]string,
}
}

for _, resource := range exceptionconfigmaps {
for _, resource := range exceptionConfigMaps {
if resource.Namespace == namespace || resource.Namespace == "*" {
volumesCM = append(volumesCM, resource.ResourceName)
}
Expand Down Expand Up @@ -126,7 +156,13 @@ func processNamespaceCM(clientset kubernetes.Interface, namespace string, filter
}

var usedConfigMaps []string
slicesToAppend := [][]string{volumesCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM}
slicesToAppend := [][]string{
volumesCM,
envCM,
envFromCM,
envFromContainerCM,
envFromInitContainerCM,
}

for _, slice := range slicesToAppend {
usedConfigMaps = append(usedConfigMaps, slice...)
Expand Down
53 changes: 47 additions & 6 deletions pkg/kor/kor.go
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"sort"
"strings"

"github.com/olekukonko/tablewriter"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
Expand Down Expand Up @@ -123,10 +124,16 @@ func FormatOutput(namespace string, resources []string, resourceType string, opt

var buf bytes.Buffer
table := tablewriter.NewWriter(&buf)
table.SetHeader([]string{"#", "Resource Name"})
table.SetHeader([]string{
"#",
"Resource Name",
})

for i, name := range resources {
table.Append([]string{fmt.Sprintf("%d", i+1), name})
table.Append([]string{
fmt.Sprintf("%d", i+1),
name,
})
}

table.Render()
Expand All @@ -138,7 +145,11 @@ func FormatOutputFromMap(namespace string, allDiffs map[string][]string, opts Op
i := 0
var buf bytes.Buffer
table := tablewriter.NewWriter(&buf)
table.SetHeader([]string{"#", "Resource Type", "Resource Name"})
table.SetHeader([]string{
"#",
"Resource Type",
"Resource Name",
})

// TODO parse resourceType, diff

Expand All @@ -150,7 +161,11 @@ func FormatOutputFromMap(namespace string, allDiffs map[string][]string, opts Op

allEmpty = false
for _, val := range diff {
row := []string{fmt.Sprintf("%d", i+1), resourceType, val}
row := []string{
fmt.Sprintf("%d", i+1),
resourceType,
val,
}
table.Append(row)
i += 1
}
Expand All @@ -173,7 +188,11 @@ func FormatOutputAll(namespace string, allDiffs []ResourceDiff, opts Opts) strin
i := 0
var buf bytes.Buffer
table := tablewriter.NewWriter(&buf)
table.SetHeader([]string{"#", "Resource Type", "Resource Name"})
table.SetHeader([]string{
"#",
"Resource Type",
"Resource Name",
})

// TODO parse resourceType, diff

Expand All @@ -185,7 +204,11 @@ func FormatOutputAll(namespace string, allDiffs []ResourceDiff, opts Opts) strin

allEmpty = false
for _, val := range data.diff {
row := []string{fmt.Sprintf("%d", i+1), data.resourceType, val}
row := []string{
fmt.Sprintf("%d", i+1),
data.resourceType,
val,
}
table.Append(row)
i += 1
}
Expand Down Expand Up @@ -245,3 +268,21 @@ func unusedResourceFormatter(outputFormat string, outputBuffer bytes.Buffer, opt
}
return string(jsonResponse), nil
}

func isResourceException(resourceName, namespace string, exceptions []ExceptionResource) bool {
var match bool
for _, e := range exceptions {
if e.ResourceName == resourceName && e.Namespace == namespace {
match = true
break
}
if strings.HasSuffix(e.ResourceName, "*") {
resourceNameMatched := strings.HasPrefix(resourceName, strings.TrimSuffix(e.ResourceName, "*"))
if resourceNameMatched && e.Namespace == namespace {
match = true
break
}
}
}
return match
}
30 changes: 28 additions & 2 deletions pkg/kor/secrets.go
Expand Up @@ -21,6 +21,25 @@ var exceptionSecretTypes = []string{
`kubernetes.io/service-account-token`,
}

var exceptionSecrets = []ExceptionResource{
{
ResourceName: "kubernetes-dashboard-certs",
Namespace: "kubernetes-dashboard",
},
{
ResourceName: "kubernetes-dashboard-csrf",
Namespace: "kubernetes-dashboard",
},
{
ResourceName: "kubernetes-dashboard-key-holder",
Namespace: "kubernetes-dashboard",
},
{
ResourceName: "bootstrap-token-*",
Namespace: "kube-system",
},
}

func retrieveIngressTLS(clientset kubernetes.Interface, namespace string) ([]string, error) {
secretNames := make([]string, 0)
ingressList, err := clientset.NetworkingV1().Ingresses(namespace).List(context.TODO(), metav1.ListOptions{})
Expand Down Expand Up @@ -121,7 +140,7 @@ func retrieveSecretNames(clientset kubernetes.Interface, namespace string, filte
continue
}

if !slices.Contains(exceptionSecretTypes, string(secret.Type)) {
if !slices.Contains(exceptionSecretTypes, string(secret.Type)) && !isResourceException(secret.Name, namespace, exceptionSecrets) {
names = append(names, secret.Name)
}
}
Expand All @@ -147,7 +166,14 @@ func processNamespaceSecret(clientset kubernetes.Interface, namespace string, fi
}

var usedSecrets []string
slicesToAppend := [][]string{envSecrets, envSecrets2, volumeSecrets, pullSecrets, tlsSecrets, initContainerEnvSecrets}
slicesToAppend := [][]string{
envSecrets,
envSecrets2,
volumeSecrets,
pullSecrets,
tlsSecrets,
initContainerEnvSecrets,
}

for _, slice := range slicesToAppend {
usedSecrets = append(usedSecrets, slice...)
Expand Down
11 changes: 10 additions & 1 deletion pkg/kor/services.go
Expand Up @@ -13,6 +13,13 @@ import (
"github.com/yonahd/kor/pkg/filters"
)

var exceptionServices = []ExceptionResource{
{
ResourceName: "k8s.io-minikube-hostpath",
Namespace: "kube-system",
},
}

func ProcessNamespaceServices(clientset kubernetes.Interface, namespace string, filterOpts *filters.Options) ([]string, error) {
endpointsList, err := clientset.CoreV1().Endpoints(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: filterOpts.IncludeLabels})
if err != nil {
Expand All @@ -25,7 +32,9 @@ func ProcessNamespaceServices(clientset kubernetes.Interface, namespace string,
if pass, _ := filter.Run(filterOpts); pass {
continue
}

if isResourceException(endpoints.Name, namespace, exceptionServices) {
continue
}
if endpoints.Labels["kor/used"] == "false" {
endpointsWithoutSubsets = append(endpointsWithoutSubsets, endpoints.Name)
continue
Expand Down
13 changes: 13 additions & 0 deletions pkg/kor/storageclasses.go
Expand Up @@ -14,6 +14,13 @@ import (
"github.com/yonahd/kor/pkg/filters"
)

var exceptionStorageClasses = []ExceptionResource{
{
ResourceName: "standard",
Namespace: "",
},
}

func retrieveUsedStorageClasses(clientset kubernetes.Interface) ([]string, error) {
pvs, err := clientset.CoreV1().PersistentVolumes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
Expand Down Expand Up @@ -73,6 +80,12 @@ func processStorageClasses(clientset kubernetes.Interface, filterOpts *filters.O
return nil, err
}

for i, name := range storageClassNames {
if isResourceException(name, "", exceptionStorageClasses) {
storageClassNames = append(storageClassNames[:i], storageClassNames[i+1:]...)
}
}

diff := CalculateResourceDifference(usedStorageClasses, storageClassNames)
diff = append(diff, unusedStorageClassNames...)
return diff, nil
Expand Down

0 comments on commit 8822778

Please sign in to comment.