From 22f2a291cf979d540478fe0b0cbc07b096eb2e7a Mon Sep 17 00:00:00 2001 From: Matthias Bertschy Date: Thu, 29 Aug 2024 15:06:31 +0200 Subject: [PATCH] use nn.Name for generated netpol if workload-name is missing Signed-off-by: Matthias Bertschy --- .../networkpolicy/v2/networkpolicy.go | 3 +- .../file/generatednetworkpolicy_test.go | 67 ++++++++++++++++--- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/pkg/apis/softwarecomposition/networkpolicy/v2/networkpolicy.go b/pkg/apis/softwarecomposition/networkpolicy/v2/networkpolicy.go index a08598003..719d20b10 100644 --- a/pkg/apis/softwarecomposition/networkpolicy/v2/networkpolicy.go +++ b/pkg/apis/softwarecomposition/networkpolicy/v2/networkpolicy.go @@ -36,7 +36,8 @@ func GenerateNetworkPolicy(nn *softwarecomposition.NetworkNeighborhood, knownSer } name, ok := nn.Labels[helpersv1.NameMetadataKey] if !ok { - return softwarecomposition.GeneratedNetworkPolicy{}, fmt.Errorf("nn %s/%s does not have a name label", nn.Namespace, nn.Name) + logger.L().Debug("nn does not have a workload-name label, falling back to nn.Name", helpers.String("name", nn.Name), helpers.String("namespace", nn.Namespace)) + name = nn.Name } delete(nn.Labels, helpersv1.TemplateHashKey) diff --git a/pkg/registry/file/generatednetworkpolicy_test.go b/pkg/registry/file/generatednetworkpolicy_test.go index 21cc3f222..6e88abad2 100644 --- a/pkg/registry/file/generatednetworkpolicy_test.go +++ b/pkg/registry/file/generatednetworkpolicy_test.go @@ -21,11 +21,12 @@ func TestGeneratedNetworkPolicyStorage_Get(t *testing.T) { objPtr runtime.Object } tests := []struct { - name string - args args - create bool - expectedError error - want runtime.Object + name string + args args + create bool + noWorkloadName bool + expectedError error + want runtime.Object }{ { name: "no existing objects return empty list", @@ -53,7 +54,55 @@ func TestGeneratedNetworkPolicyStorage_Get(t *testing.T) { CreationTimestamp: v1.Time{}, Labels: map[string]string{ helpersv1.KindMetadataKey: "Deployment", - helpersv1.NameMetadataKey: "toto", + helpersv1.NameMetadataKey: "totowl", + }, + }, + Spec: softwarecomposition.NetworkPolicy{ + Kind: "NetworkPolicy", + APIVersion: "networking.k8s.io/v1", + ObjectMeta: v1.ObjectMeta{ + Annotations: map[string]string{ + "generated-by": "kubescape", + }, + Name: "deployment-totowl", + Namespace: "kubescape", + Labels: map[string]string{ + helpersv1.KindMetadataKey: "Deployment", + helpersv1.NameMetadataKey: "totowl", + }, + }, + Spec: softwarecomposition.NetworkPolicySpec{ + PolicyTypes: []softwarecomposition.PolicyType{ + softwarecomposition.PolicyTypeIngress, + softwarecomposition.PolicyTypeEgress, + }, + Ingress: []softwarecomposition.NetworkPolicyIngressRule{}, + Egress: []softwarecomposition.NetworkPolicyEgressRule{}, + }, + }, + PoliciesRef: []softwarecomposition.PolicyRef{}, + }, + }, + { + name: "missing workload name label", + args: args{ + key: "/spdx.softwarecomposition.kubescape.io/generatednetworkpolicies/kubescape/toto", + objPtr: &softwarecomposition.GeneratedNetworkPolicy{}, + }, + expectedError: nil, + create: true, + noWorkloadName: true, + want: &softwarecomposition.GeneratedNetworkPolicy{ + TypeMeta: v1.TypeMeta{ + Kind: "GeneratedNetworkPolicy", + APIVersion: "spdx.softwarecomposition.kubescape.io", + }, + ObjectMeta: v1.ObjectMeta{ + Name: "toto", + Namespace: "kubescape", + CreationTimestamp: v1.Time{}, + Labels: map[string]string{ + helpersv1.KindMetadataKey: "Deployment", }, }, Spec: softwarecomposition.NetworkPolicy{ @@ -67,7 +116,6 @@ func TestGeneratedNetworkPolicyStorage_Get(t *testing.T) { Namespace: "kubescape", Labels: map[string]string{ helpersv1.KindMetadataKey: "Deployment", - helpersv1.NameMetadataKey: "toto", }, }, Spec: softwarecomposition.NetworkPolicySpec{ @@ -103,10 +151,13 @@ func TestGeneratedNetworkPolicyStorage_Get(t *testing.T) { }, Labels: map[string]string{ helpersv1.KindMetadataKey: "Deployment", - helpersv1.NameMetadataKey: "toto", + helpersv1.NameMetadataKey: "totowl", }, }, } + if tt.noWorkloadName { + delete(wlObj.ObjectMeta.Labels, helpersv1.NameMetadataKey) + } err := realStorage.Create(context.TODO(), "/spdx.softwarecomposition.kubescape.io/networkneighborhoods/kubescape/toto", wlObj, nil, 0) assert.NoError(t, err) }