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

use nn.Name for generated netpol if workload-name is missing #142

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
67 changes: 59 additions & 8 deletions pkg/registry/file/generatednetworkpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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)
}
Expand Down
Loading