-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathingress-service-ref.yaml
111 lines (110 loc) · 3.1 KB
/
ingress-service-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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
apiVersion: kubevious.io/v1alpha1
kind: ClusterRule
metadata:
name: ingress-service-ref
spec:
summary: |
Validate Ingress to Service reference.
categories:
- k8s
- ingress
- service
- reference
target: |
Api('networking.k8s.io')
.Kind('Ingress')
rule: |
if (config.spec?.defaultBackend) {
validateBackend(config.spec?.defaultBackend, 'default backend');
}
for(const [ruleIndex, rule] of (config.spec?.rules ?? []).entries())
{
if (rule.http)
{
for(const [pathIndex, path] of (rule.http.paths ?? []).entries())
{
if (path.backend)
{
validateBackend(path.backend, `rule [${ruleIndex}], path [${pathIndex}]`);
}
}
}
}
function validateBackend(backend, backendName)
{
if (!backend.resource)
{
if (!backend.service)
{
error(`service is not set in ${backendName}`);
}
else
{
const serviceName = backend.service.name;
const serviceItem = Api('')
.Kind('Service')
.name(serviceName)
.single();
if (!serviceItem)
{
error(`Could not find Service ${serviceName} referenced in ${backendName}`);
}
else
{
const servicePort = backend.service.port;
if (servicePort)
{
if (servicePort.name)
{
if (servicePort.number)
{
error(`Using both port name and number at the same time in ${backendName}`);
}
else
{
const ports = {};
for(const port of serviceItem.config.spec?.ports ?? [])
{
ports[port.name] = true;
}
if (!ports[servicePort.name])
{
error(`Could not find Port ${servicePort.name} in Service ${serviceName} referenced in ${backendName}`);
}
}
}
else
{
if (servicePort.number)
{
const ports = {};
for(const port of serviceItem.config.spec?.ports ?? [])
{
ports[port.port] = true;
}
if (!ports[servicePort.number])
{
error(`Could not find Port ${servicePort.number} in Service ${serviceName} referenced in ${backendName}`);
}
}
else
{
error(`Should have port name or number set in ${backendName}`);
}
}
}
else
{
error(`port is not set in ${backendName}`);
}
}
}
}
else
{
if (backend.service)
{
error(`Should only use resource or service in ${backendName}`);
}
}
}