Skip to content

Commit 9ee8575

Browse files
committed
Use getters for retrieving network policy labels
1 parent 194c470 commit 9ee8575

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

utils/utils.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func createNetworkPolicyEgressRule(appName string, namespace string, config comm
350350
if config != nil {
351351
rule.To = append(rule.To,
352352
// Add peer to allow traffic to other pods belonging to the app
353-
createNetworkPolicyPeer(appName, namespace, config, config.GetToNamespaceLabels, config.GetToLabels),
353+
createNetworkPolicyPeer(appName, namespace, config, getNetworkPolicyEgressLabelGetters),
354354
)
355355
}
356356
return rule
@@ -456,14 +456,9 @@ func createOpenShiftNetworkPolicyIngressRule(appName string, namespace string, i
456456
)
457457
}
458458

459-
if config != nil {
460-
rule.From = append(rule.From,
461-
// Add peer to allow traffic from other pods belonging to the app
462-
createNetworkPolicyPeer(appName, namespace, config, config.GetFromNamespaceLabels, config.GetFromLabels),
463-
)
464-
}
465-
// default to allow traffic from OpenShift monitoring
466459
rule.From = append(rule.From,
460+
// Add peer to allow traffic from other pods belonging to the app
461+
createNetworkPolicyPeer(appName, namespace, config, getNetworkPolicyIngressLabelGetters),
467462
// Add peer to allow traffic from OpenShift monitoring
468463
networkingv1.NetworkPolicyPeer{
469464
NamespaceSelector: &metav1.LabelSelector{
@@ -484,7 +479,7 @@ func createKubernetesNetworkPolicyIngressRule(appName string, namespace string,
484479

485480
rule := networkingv1.NetworkPolicyIngressRule{}
486481
rule.From = []networkingv1.NetworkPolicyPeer{
487-
createNetworkPolicyPeer(appName, namespace, config, config.GetFromNamespaceLabels, config.GetFromLabels),
482+
createNetworkPolicyPeer(appName, namespace, config, getNetworkPolicyIngressLabelGetters),
488483
}
489484
return rule
490485
}
@@ -497,12 +492,12 @@ func createAllowAllNetworkPolicyIngressRule() networkingv1.NetworkPolicyIngressR
497492
}
498493
}
499494

500-
func createNetworkPolicyPeer(appName string, namespace string, networkPolicy common.BaseComponentNetworkPolicy, getNamespaceLabels func() map[string]string, getLabels func() map[string]string) networkingv1.NetworkPolicyPeer {
495+
func createNetworkPolicyPeer(appName string, namespace string, networkPolicy common.BaseComponentNetworkPolicy, getNetworkPolicyLabelGetters func(common.BaseComponentNetworkPolicy) (func() map[string]string, func() map[string]string)) networkingv1.NetworkPolicyPeer {
501496
peer := networkingv1.NetworkPolicyPeer{
502497
NamespaceSelector: &metav1.LabelSelector{},
503498
PodSelector: &metav1.LabelSelector{},
504499
}
505-
500+
getNamespaceLabels, getLabels := getNetworkPolicyLabelGetters(networkPolicy)
506501
if networkPolicy == nil || getNamespaceLabels() == nil {
507502
peer.NamespaceSelector.MatchLabels = map[string]string{
508503
"kubernetes.io/metadata.name": namespace,
@@ -559,6 +554,30 @@ func customizeNetworkPolicyPorts(ingress *networkingv1.NetworkPolicyIngressRule,
559554
}
560555
}
561556

557+
func getNetworkPolicyIngressLabelGetters(config common.BaseComponentNetworkPolicy) (func() map[string]string, func() map[string]string) {
558+
var getNamespaceLabels, getLabels func() map[string]string
559+
if config != nil {
560+
getNamespaceLabels = config.GetFromNamespaceLabels
561+
getLabels = config.GetFromLabels
562+
} else {
563+
getNamespaceLabels = nil
564+
getLabels = nil
565+
}
566+
return getNamespaceLabels, getLabels
567+
}
568+
569+
func getNetworkPolicyEgressLabelGetters(config common.BaseComponentNetworkPolicy) (func() map[string]string, func() map[string]string) {
570+
var getNamespaceLabels, getLabels func() map[string]string
571+
if config != nil {
572+
getNamespaceLabels = config.GetToNamespaceLabels
573+
getLabels = config.GetToLabels
574+
} else {
575+
getNamespaceLabels = nil
576+
getLabels = nil
577+
}
578+
return getNamespaceLabels, getLabels
579+
}
580+
562581
// returns true if policy is contained within the policyTypes array, false otherwise
563582
func policyTypesContains(policyTypes []networkingv1.PolicyType, policy networkingv1.PolicyType) bool {
564583
for _, currentPolicy := range policyTypes {

0 commit comments

Comments
 (0)