Skip to content

Commit e139ead

Browse files
author
Debakel Orakel
committed
Apply suggestions from review
1 parent 126fb6d commit e139ead

4 files changed

Lines changed: 51 additions & 30 deletions

File tree

component/main.jsonnet

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

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

117124
// Define outputs below
118125
{
@@ -128,5 +135,5 @@ local netpols = kube.NetworkPolicy('allow-from-other-namespaces') {
128135
namespace: params.namespace.name,
129136
},
130137
},
131-
[if params.networkPolicy.enabled && std.length(params.networkPolicy.allowedNamespaces) > 0 then '30_network_policies']: netpols,
138+
[if std.length(netpols) > 0 then '30_network_policies']: netpols,
132139
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,14 @@ The networkPolicy will only be deployed if it is `enabled` and has at least 1 en
607607

608608
=== `networkPolicy.enabled`
609609

610+
[horizontal]
611+
type:: boolean
612+
default:: true
613+
614+
Enable or disable the creation of NetworkPolicies.
615+
616+
=== `networkPolicy.exposedComponents`
617+
610618
[horizontal]
611619
type:: dict
612620
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/openshift/openshift/30_network_policies.yaml renamed to tests/golden/extra-config/extra-config/extra-config/30_network_policies.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ metadata:
77
app.kubernetes.io/name: allow-from-other-namespaces
88
name: allow-from-other-namespaces
99
name: allow-from-other-namespaces
10-
namespace: openshift
10+
namespace: extra-config
1111
spec:
1212
ingress:
1313
- from:
@@ -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)