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 labels #88

Merged
merged 2 commits into from
Jan 14, 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
29 changes: 29 additions & 0 deletions pkg/apis/softwarecomposition/networkpolicy/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package networkpolicy

var ignoreLabels map[string]bool

func init() {

ignoreLabels = map[string]bool{
"app.kubernetes.io/name": false,
"app.kubernetes.io/part-of": false,
"app.kubernetes.io/component": false,
"app.kubernetes.io/instance": true,
"app.kubernetes.io/version": true,
"app.kubernetes.io/managed-by": true,
"app.kubernetes.io/created-by": true,
"app.kubernetes.io/owner": true,
"app.kubernetes.io/revision": true,
"statefulset.kubernetes.io/pod-name": true,
"scheduler.alpha.kubernetes.io/node-selector": true,
"pod-template-hash": true,
"controller-revision-hash": true,
"pod-template-generation": true,
"helm.sh/chart": true,
}
}

// IsIgnoredLabel returns true if the label is ignored
func isIgnoredLabel(label string) bool {
return ignoreLabels[label]
}
11 changes: 11 additions & 0 deletions pkg/apis/softwarecomposition/networkpolicy/networkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func generateEgressRule(neighbor softwarecomposition.NetworkNeighbor, KnownServe
policyRefs := []softwarecomposition.PolicyRef{}

if neighbor.PodSelector != nil {
removeLabels(neighbor.PodSelector.MatchLabels)
egressRule.To = append(egressRule.To, softwarecomposition.NetworkPolicyPeer{
PodSelector: neighbor.PodSelector,
})
Expand Down Expand Up @@ -348,11 +349,13 @@ func hash(s any) (string, error) {
vv := sha256.Sum256(b.Bytes())
return hex.EncodeToString(vv[:]), nil
}

func generateIngressRule(neighbor softwarecomposition.NetworkNeighbor, KnownServer []softwarecomposition.KnownServer) (softwarecomposition.NetworkPolicyIngressRule, []softwarecomposition.PolicyRef) {
ingressRule := softwarecomposition.NetworkPolicyIngressRule{}
policyRefs := []softwarecomposition.PolicyRef{}

if neighbor.PodSelector != nil {
removeLabels(neighbor.PodSelector.MatchLabels)
ingressRule.From = append(ingressRule.From, softwarecomposition.NetworkPolicyPeer{
PodSelector: neighbor.PodSelector,
})
Expand Down Expand Up @@ -437,3 +440,11 @@ func getSingleIP(ipAddress string) *softwarecomposition.IPBlock {
ipBlock := &softwarecomposition.IPBlock{CIDR: ipAddress + "/32"}
return ipBlock
}

func removeLabels(labels map[string]string) {
for key := range labels {
if isIgnoredLabel(key) {
delete(labels, key)
}
}
}
14 changes: 10 additions & 4 deletions pkg/apis/softwarecomposition/networkpolicy/networkpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func TestGenerateNetworkPolicy(t *testing.T) {
{
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"one": "1",
"one": "1",
"app.kubernetes.io/name": "nginx",
"app.kubernetes.io/revision": "1",
},
},
Ports: []softwarecomposition.NetworkPort{
Expand All @@ -50,7 +52,9 @@ func TestGenerateNetworkPolicy(t *testing.T) {
{
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"two": "2",
"two": "2",
"app.kubernetes.io/name": "nginx",
"app.kubernetes.io/revision": "1",
},
},
Ports: []softwarecomposition.NetworkPort{
Expand Down Expand Up @@ -106,7 +110,8 @@ func TestGenerateNetworkPolicy(t *testing.T) {
{
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"one": "1",
"one": "1",
"app.kubernetes.io/name": "nginx",
},
},
},
Expand All @@ -123,7 +128,8 @@ func TestGenerateNetworkPolicy(t *testing.T) {
{
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"two": "2",
"two": "2",
"app.kubernetes.io/name": "nginx",
},
},
},
Expand Down
Loading