Skip to content

Commit

Permalink
feat: add inspect target resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Rookie0031 committed Oct 13, 2024
1 parent 56fc312 commit 3b1c6c3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
58 changes: 57 additions & 1 deletion source/cmd/inspect_ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,62 @@ func inspectNamespace(clientset *kubernetes.Clientset, namespace string) (bool,
}
resourceCounts["Ingresses"] = len(ingresses.Items)

// Check for ServiceAccounts
serviceAccounts, err := clientset.CoreV1().ServiceAccounts(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, resourceCounts, err
}
resourceCounts["ServiceAccounts"] = len(serviceAccounts.Items)

// Check for Roles
roles, err := clientset.RbacV1().Roles(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, resourceCounts, err
}
resourceCounts["Roles"] = len(roles.Items)

// Check for RoleBindings
roleBindings, err := clientset.RbacV1().RoleBindings(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, resourceCounts, err
}
resourceCounts["RoleBindings"] = len(roleBindings.Items)

// Check for NetworkPolicies
networkPolicies, err := clientset.NetworkingV1().NetworkPolicies(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, resourceCounts, err
}
resourceCounts["NetworkPolicies"] = len(networkPolicies.Items)

// Check for ResourceQuotas
resourceQuotas, err := clientset.CoreV1().ResourceQuotas(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, resourceCounts, err
}
resourceCounts["ResourceQuotas"] = len(resourceQuotas.Items)

// Check for LimitRanges
limitRanges, err := clientset.CoreV1().LimitRanges(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, resourceCounts, err
}
resourceCounts["LimitRanges"] = len(limitRanges.Items)

// Check for HorizontalPodAutoscalers
hpas, err := clientset.AutoscalingV2().HorizontalPodAutoscalers(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, resourceCounts, err
}
resourceCounts["HorizontalPodAutoscalers"] = len(hpas.Items)

// Check for PodDisruptionBudgets
pdbs, err := clientset.PolicyV1().PodDisruptionBudgets(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, resourceCounts, err
}
resourceCounts["PodDisruptionBudgets"] = len(pdbs.Items)

// If no Pods and Services are found, the namespace is considered empty of primary resources
return resourceCounts["Pods"] == 0 && resourceCounts["Services"] == 0, resourceCounts, nil
return resourceCounts["Pods"] == 0 && resourceCounts["Services"] == 0 && resourceCounts["ConfigMap"] == 0, resourceCounts, nil
}
Binary file added source/kubectl-ns-inspect
Binary file not shown.
2 changes: 1 addition & 1 deletion source/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package main

import "kubectl-ns-inspect/kubectl-ns-inspect/cmd"
import "kubectl-ns-inspect/source/cmd"

func main() {
cmd.Execute()
Expand Down

0 comments on commit 3b1c6c3

Please sign in to comment.