Skip to content

Commit 0dfe63f

Browse files
author
Debakel Orakel
committed
Apply suggestions from review
1 parent 488f585 commit 0dfe63f

4 files changed

Lines changed: 54 additions & 29 deletions

File tree

component/main.jsonnet

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,36 +81,43 @@ local secrets = com.generateResources(
8181
}
8282
);
8383

84-
local netpols = kube.NetworkPolicy('allow-from-other-namespaces') {
85-
metadata+: {
86-
labels+: {
87-
'app.kubernetes.io/managed-by': 'commodore',
88-
'app.kubernetes.io/name': 'allow-from-other-namespaces',
84+
local netpols =
85+
local exposedComponents = com.renderArray(params.networkPolicy.exposedComponents);
86+
local allowedNamespaces = com.renderArray(params.networkPolicy.allowedNamespaces);
87+
if
88+
params.networkPolicy.enabled
89+
&& std.length(exposedComponents) > 0
90+
&& std.length(allowedNamespaces) > 0
91+
then kube.NetworkPolicy('allow-from-other-namespaces') {
92+
metadata+: {
93+
labels+: {
94+
'app.kubernetes.io/managed-by': 'commodore',
95+
'app.kubernetes.io/name': 'allow-from-other-namespaces',
96+
},
97+
namespace: params.namespace.name,
8998
},
90-
namespace: params.namespace.name,
91-
},
92-
spec: {
93-
policyTypes: [ 'Ingress' ],
94-
[if std.length(params.networkPolicy.exposedComponents) > 0 then 'podSelector']: {
95-
matchExpressions: [ {
96-
key: 'app.kubernetes.io/component',
97-
operator: 'In',
98-
values: com.renderArray(params.networkPolicy.exposedComponents),
99+
spec: {
100+
policyTypes: [ 'Ingress' ],
101+
podSelector: {
102+
matchExpressions: [ {
103+
key: 'app.kubernetes.io/component',
104+
operator: 'In',
105+
values: exposedComponents,
106+
} ],
107+
},
108+
ingress: [ {
109+
from: [ {
110+
namespaceSelector: {
111+
matchExpressions: [ {
112+
key: 'kubernetes.io/metadata.name',
113+
operator: 'In',
114+
values: allowedNamespaces,
115+
} ],
116+
},
117+
} ],
99118
} ],
100119
},
101-
ingress: [ {
102-
from: [ {
103-
namespaceSelector: {
104-
matchExpressions: [ {
105-
key: 'kubernetes.io/metadata.name',
106-
operator: 'In',
107-
values: com.renderArray(params.networkPolicy.allowedNamespaces),
108-
} ],
109-
},
110-
} ],
111-
} ],
112-
},
113-
};
120+
} else {};
114121

115122
// Define outputs below
116123
{
@@ -126,5 +133,5 @@ local netpols = kube.NetworkPolicy('allow-from-other-namespaces') {
126133
namespace: params.namespace.name,
127134
},
128135
},
129-
[if params.networkPolicy.enabled && std.length(params.networkPolicy.allowedNamespaces) > 0 then '30_network_policies']: netpols,
136+
[if std.length(netpols) > 0 then '30_network_policies']: netpols,
130137
}

docs/modules/ROOT/pages/references/parameters.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ Configure access- and secret key for the S3 storage.
594594

595595
Configure the NetworkPolicy if necessary.
596596

597+
[TIP]
598+
====
599+
The NetworkPolicy will only be deployed if it is `enabled` and has at least 1 entry in `allowedNamespaces`.
600+
597601
=== `networkPolicy.enabled`
598602
599603
[horizontal]
@@ -615,6 +619,14 @@ The networkPolicy will only be deployed if it is `enabled` and has at least 1 en
615619
616620
=== `networkPolicy.enabled`
617621
622+
[horizontal]
623+
type:: boolean
624+
default:: true
625+
626+
Enable or disable the creation of NetworkPolicies.
627+
628+
=== `networkPolicy.exposedComponents`
629+
618630
[horizontal]
619631
type:: dict
620632
default::

tests/extra-config.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ parameters:
4242
region: 'us-east-1'
4343

4444
networkPolicy:
45-
enabled: false
4645
allowedNamespaces:
4746
- vshn-grafana
4847

tests/golden/openshift/loki/loki/30_network_policies.yaml renamed to tests/golden/extra-config/loki/loki/30_network_policies.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@ spec:
1717
operator: In
1818
values:
1919
- vshn-grafana
20+
podSelector:
21+
matchExpressions:
22+
- key: app.kubernetes.io/component
23+
operator: In
24+
values:
25+
- gateway
26+
- query-frontend
2027
policyTypes:
2128
- Ingress

0 commit comments

Comments
 (0)