-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice-selector-ref.yaml
66 lines (63 loc) · 1.59 KB
/
service-selector-ref.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
apiVersion: kubevious.io/v1alpha1
kind: ClusterRule
metadata:
name: service-selector-ref
spec:
summary: |
Validate Service to PodSpec label selector reference.
categories:
- k8s
- service
- pod-spec
- reference
target: |
ApiVersion('v1')
.Kind('Service')
cache: |
cache.apps = helpers.newLabelLookupDict();
for(const app of Shortcut("PodSpec")
.many())
{
cache.apps.add(app, app.config.metadata?.labels);
}
rule: |
if (!config.spec.selector) {
return;
}
const apps = cache.apps.resolveSelector(config.spec.selector)
if (apps.length === 0)
{
error(`Could not find Applications for Service with ${helpers.labelsToString(config.spec.selector)} labels`);
}
else
{
for(const app of apps)
{
const appPortMap = {};
for(const container of app?.config.spec?.containers ?? [])
{
for(const port of container.ports ?? [])
{
appPortMap[port.containerPort] = true;
if (port.name) {
appPortMap[port.name] = true;
}
}
}
for(const servicePort of config.spec?.ports ?? [])
{
if (!servicePort.port)
{
error('Service port is not set');
}
else
{
const targetPort = servicePort.targetPort ?? servicePort.port;
if (!appPortMap[targetPort])
{
warning(`Could not find exposed port ${targetPort} in ${app.name}`);
}
}
}
}
}