Skip to content

Commit

Permalink
refactor: PDB label selection
Browse files Browse the repository at this point in the history
  • Loading branch information
doronkg committed Oct 20, 2024
1 parent 673c21e commit 306dfab
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
10 changes: 10 additions & 0 deletions pkg/kor/create_test_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func CreateTestDeployment(namespace, name string, replicas int32, labels map[str
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Template: corev1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: labels,
},
},
},
}
}
Expand All @@ -40,6 +45,11 @@ func CreateTestStatefulSet(namespace, name string, replicas int32, labels map[st
},
Spec: appsv1.StatefulSetSpec{
Replicas: &replicas,
Template: corev1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: labels,
},
},
},
}
}
Expand Down
50 changes: 37 additions & 13 deletions pkg/kor/pdbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
labels "k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"

Expand Down Expand Up @@ -52,36 +53,59 @@ func processNamespacePdbs(clientset kubernetes.Interface, namespace string, filt
}

selector := pdb.Spec.Selector
if selector == nil {
if selector == nil || len(selector.MatchLabels) == 0 {
reason := "Pdb has no selector"
unusedPdbs = append(unusedPdbs, ResourceInfo{Name: pdb.Name, Reason: reason})
continue
}
if len(selector.MatchLabels) == 0 {
reason := "Pdb has no selector"
unusedPdbs = append(unusedPdbs, ResourceInfo{Name: pdb.Name, Reason: reason})
continue
}
deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: metav1.FormatLabelSelector(selector),
})

labelSelector, err := metav1.LabelSelectorAsSelector(selector)
if err != nil {
return nil, err
}
statefulSets, err := clientset.AppsV1().StatefulSets(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: metav1.FormatLabelSelector(selector),
})

isPdbUsed, err := hasMatchedResources(clientset, namespace, filterOpts, labelSelector)
if err != nil {
return nil, err
}
if len(deployments.Items) == 0 && len(statefulSets.Items) == 0 {

if !isPdbUsed {
reason := "Pdb is not referencing any deployments or statefulsets"
unusedPdbs = append(unusedPdbs, ResourceInfo{Name: pdb.Name, Reason: reason})
}
}

return unusedPdbs, nil
}

func hasMatchedResources(clientset kubernetes.Interface, namespace string, filterOpts *filters.Options, labelSelector labels.Selector) (bool, error) {
deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}

for _, deployment := range deployments.Items {
deploymentLabels := labels.Set(deployment.Spec.Template.Labels)
if labelSelector.Matches(deploymentLabels) {
return true, nil
}
}

statefulSets, err := clientset.AppsV1().StatefulSets(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}

for _, statefulSet := range statefulSets.Items {
statefulSetLabels := labels.Set(statefulSet.Spec.Template.Labels)
if labelSelector.Matches(statefulSetLabels) {
return true, nil
}
}

return false, nil
}

func GetUnusedPdbs(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts common.Opts) (string, error) {
resources := make(map[string]map[string][]ResourceInfo)
for _, namespace := range filterOpts.Namespaces(clientset) {
Expand Down

0 comments on commit 306dfab

Please sign in to comment.