Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unused network policy as well #133

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion pkg/determiner/determiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,35 @@ func (d *determiner) DetermineDeletion(ctx context.Context, info *cliresource.In

case resource.KindHorizontalPodAutoscaler:
return d.determineDeletionHorizontalPodAutoscaler(ctx, info)

case resource.KindNetworkPolicy:
return d.determineDeletionNetworkPolicy(info)
default:
return false, fmt.Errorf("unsupported kind: %s/%s", kind, info.Name)
}
}

func (d *determiner) determineDeletionNetworkPolicy(info *cliresource.Info) (bool, error) {
np, err := resource.ObjectToNetworkPolicy(info.Object)
if err != nil {
return false, err
}

// Get the selector from the Network Policy
selector, err := metav1.LabelSelectorAsSelector(&np.Spec.PodSelector)
if err != nil {
return false, fmt.Errorf("invalid label selector (%s): %w", np.Name, err)
}

// Iterate over pods to check if any match the selector
for _, pod := range d.pods {
if selector.Matches(labels.Set(pod.Labels)) {
return false, nil // Pod matches the selector, so the Network Policy is used
}
}

return true, nil // No pods match the selector, so the Network Policy is unused
}

func (d *determiner) determineDeletionPod(info *cliresource.Info) (bool, error) {
pod, err := resource.ObjectToPod(info.Object)
if err != nil {
Expand Down
106 changes: 106 additions & 0 deletions pkg/determiner/determiner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,109 @@ func Test_determiner_determineUsedSecret(t *testing.T) {
})
}
}

func Test_determiner_determineDeletionNetworkPolicy(t *testing.T) {
const (
fakeNetworkPolicy = "fake-np"
fakePodLabelKey = "app"
fakePodLabelValue = "nginx"
)

type fields struct {
pods []*corev1.Pod
}
type args struct {
info *cliresource.Info
}
tests := []struct {
name string
fields fields
args args
want bool
wantErr bool
}{
{
name: "NetworkPolicy should not be deleted when it matches a pod",
fields: fields{
pods: []*corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
fakePodLabelKey: fakePodLabelValue,
},
},
},
},
},
args: args{
info: &cliresource.Info{
Name: fakeNetworkPolicy,
Object: &networkingv1.NetworkPolicy{
TypeMeta: metav1.TypeMeta{
Kind: resource.KindNetworkPolicy,
},
Spec: networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
fakePodLabelKey: fakePodLabelValue,
},
},
},
},
},
},
want: false,
wantErr: false,
},
{
name: "NetworkPolicy should be deleted when it does not match any pod",
fields: fields{
pods: []*corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
fakePodLabelKey: "other",
},
},
},
},
},
args: args{
info: &cliresource.Info{
Name: fakeNetworkPolicy,
Object: &networkingv1.NetworkPolicy{
TypeMeta: metav1.TypeMeta{
Kind: resource.KindNetworkPolicy,
},
Spec: networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
fakePodLabelKey: fakePodLabelValue,
},
},
},
},
},
},
want: true,
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
d := &determiner{
pods: tt.fields.pods,
}
got, err := d.determineDeletionNetworkPolicy(tt.args.info)
if (err != nil) != tt.wantErr {
t.Errorf("determineDeletionNetworkPolicy() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("determineDeletionNetworkPolicy() = %v, want %v", got, tt.want)
}
})
}
}
16 changes: 16 additions & 0 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
corev1 "k8s.io/api/core/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
networkingv1 "k8s.io/api/networking/v1"
)

const (
Expand All @@ -20,6 +21,7 @@ const (
KindJob = "Job"
KindPodDisruptionBudget = "PodDisruptionBudget"
KindHorizontalPodAutoscaler = "HorizontalPodAutoscaler"
KindNetworkPolicy = "NetworkPolicy"
)

var unstructuredConverter = runtime.DefaultUnstructuredConverter
Expand Down Expand Up @@ -108,6 +110,20 @@ func ObjectToHorizontalPodAutoscaler(obj runtime.Object) (*autoscalingv1.Horizon
return &hpa, nil
}

func ObjectToNetworkPolicy(obj runtime.Object) (*networkingv1.NetworkPolicy, error) {
u, err := toUnstructured(obj)
if err != nil {
return nil, err
}

var np networkingv1.NetworkPolicy
if err := fromUnstructured(u, &np); err != nil {
return nil, err
}

return &np, nil
}

func toUnstructured(obj runtime.Object) (map[string]interface{}, error) {
return unstructuredConverter.ToUnstructured(obj)
}
Expand Down
Loading