-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathingress-unique-route-rules.yaml
109 lines (99 loc) · 2.89 KB
/
ingress-unique-route-rules.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
apiVersion: kubevious.io/v1alpha1
kind: ClusterRule
metadata:
name: ingress-unique-route-rules
spec:
summary: |
Validate Ingresses to have unique routing rules.
categories:
- k8s
- ingress
- ingress-extension
- unique-route
target: |
Union(
Api('networking.k8s.io')
.Kind('Ingress'),
Api('extensions')
.Kind('Ingress')
)
globalCache: |
cache.rules = {};
cache.manifests = {};
const defaultIngressClassName = getDefaultIngressClassName();
for(const item of Union(
Api('networking.k8s.io')
.Kind('Ingress')
.allNamespaces(),
Api('extensions')
.Kind('Ingress')
.allNamespaces()
).many())
{
for(const rule of item.config.spec?.rules ?? [])
{
for(const path of rule?.http?.paths ?? [])
{
const ingressClass = item.config.spec?.ingressClassName ??
item.annotations['kubernetes.io/ingress.class'] ??
defaultIngressClassName;
register(item, {
ingressClass: ingressClass,
host: rule.host,
path: path.path,
pathType: path.pathType,
},
[
'zalando.org/skipper-predicate'
]);
}
}
}
// HELPERS
function register(item, info, annoKeys)
{
if (!cache.manifests[item.manifest.idKey]) {
cache.manifests[item.manifest.idKey] = [];
}
for(const annoKey of (annoKeys ?? []))
{
const annoVal = item.annotations[annoKey];
if (annoKey !== undefined)
{
info[annoKey] = annoVal;
}
}
const key = _.stableStringify(info);
cache.manifests[item.manifest.idKey].push(key);
if (!cache.rules[key]) {
cache.rules[key] = [];
}
cache.rules[key].push(item.manifest.idKey); // [item.manifest.idKey] = true;
}
function getDefaultIngressClassName()
{
for(const item of Api('networking.k8s.io')
.Kind('IngressClass')
.isClusterScope(true)
.many())
{
if (item.annotations["ingressclass.kubernetes.io/is-default-class"] == "true")
{
return item.name;
}
}
return "";
}
rule: |
const myRules = globalCache.manifests[item.manifest.idKey];
for(const ruleKey of myRules ?? [])
{
const others = globalCache.rules[ruleKey];
if (others.length > 1)
{
error(`Duplicate Ingress rule found. ${ruleKey}`);
}
}
dependencies:
- name: kubevious
minVersion: 1.0.49