-
Notifications
You must be signed in to change notification settings - Fork 152
/
validate_collision.go
200 lines (165 loc) · 5.84 KB
/
validate_collision.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2020-2023 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0
package ingress
import (
"context"
"fmt"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
networkingv1 "k8s.io/api/networking/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
"github.com/projectcapsule/capsule/pkg/api"
"github.com/projectcapsule/capsule/pkg/configuration"
"github.com/projectcapsule/capsule/pkg/indexer/ingress"
capsulewebhook "github.com/projectcapsule/capsule/pkg/webhook"
"github.com/projectcapsule/capsule/pkg/webhook/utils"
)
type collision struct {
configuration configuration.Configuration
}
func Collision(configuration configuration.Configuration) capsulewebhook.Handler {
return &collision{configuration: configuration}
}
func (r *collision) OnCreate(client client.Client, decoder admission.Decoder, recorder record.EventRecorder) capsulewebhook.Func {
return func(ctx context.Context, req admission.Request) *admission.Response {
return r.validate(ctx, client, req, decoder, recorder)
}
}
func (r *collision) OnUpdate(client client.Client, decoder admission.Decoder, recorder record.EventRecorder) capsulewebhook.Func {
return func(ctx context.Context, req admission.Request) *admission.Response {
return r.validate(ctx, client, req, decoder, recorder)
}
}
func (r *collision) OnDelete(client.Client, admission.Decoder, record.EventRecorder) capsulewebhook.Func {
return func(context.Context, admission.Request) *admission.Response {
return nil
}
}
func (r *collision) validate(ctx context.Context, client client.Client, req admission.Request, decoder admission.Decoder, recorder record.EventRecorder) *admission.Response {
ing, err := FromRequest(req, decoder)
if err != nil {
return utils.ErroredResponse(err)
}
var tenant *capsulev1beta2.Tenant
tenant, err = TenantFromIngress(ctx, client, ing)
if err != nil {
return utils.ErroredResponse(err)
}
if tenant == nil || tenant.Spec.IngressOptions.HostnameCollisionScope == api.HostnameCollisionScopeDisabled {
return nil
}
if err = r.validateCollision(ctx, client, ing, tenant.Spec.IngressOptions.HostnameCollisionScope); err == nil {
return nil
}
var collisionErr *ingressHostnameCollisionError
if errors.As(err, &collisionErr) {
recorder.Eventf(tenant, corev1.EventTypeWarning, "IngressHostnameCollision", "Ingress %s/%s hostname is colliding", ing.Namespace(), ing.Name())
}
response := admission.Denied(err.Error())
return &response
}
//nolint:gocognit,gocyclo,cyclop
func (r *collision) validateCollision(ctx context.Context, clt client.Client, ing Ingress, scope api.HostnameCollisionScope) error {
for hostname, paths := range ing.HostnamePathsPairs() {
for path := range paths {
var ingressObjList client.ObjectList
switch ing.(type) {
case Extension:
ingressObjList = &extensionsv1beta1.IngressList{}
case NetworkingV1:
ingressObjList = &networkingv1.IngressList{}
case NetworkingV1Beta1:
ingressObjList = &networkingv1beta1.IngressList{}
}
namespaces := sets.NewString()
//nolint:exhaustive
switch scope {
case api.HostnameCollisionScopeCluster:
tenantList := &capsulev1beta2.TenantList{}
if err := clt.List(ctx, tenantList); err != nil {
return err
}
for _, tenant := range tenantList.Items {
namespaces.Insert(tenant.Status.Namespaces...)
}
case api.HostnameCollisionScopeTenant:
selector := client.MatchingFieldsSelector{Selector: fields.OneTermEqualSelector(".status.namespaces", ing.Namespace())}
tenantList := &capsulev1beta2.TenantList{}
if err := clt.List(ctx, tenantList, selector); err != nil {
return err
}
for _, tenant := range tenantList.Items {
namespaces.Insert(tenant.Status.Namespaces...)
}
case api.HostnameCollisionScopeNamespace:
namespaces.Insert(ing.Namespace())
}
fieldSelector := fields.OneTermEqualSelector(ingress.HostPathPair, fmt.Sprintf("%s;%s", hostname, path))
if err := clt.List(ctx, ingressObjList, client.MatchingFieldsSelector{Selector: fieldSelector}); err != nil {
return err
}
ingressList := sets.NewInt()
switch list := ingressObjList.(type) {
case *extensionsv1beta1.IngressList:
for index, item := range list.Items {
if namespaces.Has(item.GetNamespace()) {
ingressList.Insert(index)
}
}
switch len(ingressList) {
case 0:
break
case 1:
if index := ingressList.List()[0]; list.Items[index].GetName() == ing.Name() && list.Items[index].GetNamespace() == ing.Namespace() {
break
}
fallthrough
default:
return NewIngressHostnameCollision(hostname)
}
case *networkingv1.IngressList:
for index, item := range list.Items {
if namespaces.Has(item.GetNamespace()) {
ingressList.Insert(index)
}
}
switch len(ingressList) {
case 0:
break
case 1:
if index := ingressList.List()[0]; list.Items[index].GetName() == ing.Name() && list.Items[index].GetNamespace() == ing.Namespace() {
break
}
fallthrough
default:
return NewIngressHostnameCollision(hostname)
}
case *networkingv1beta1.IngressList:
for index, item := range list.Items {
if namespaces.Has(item.GetNamespace()) {
ingressList.Insert(index)
}
}
switch len(ingressList) {
case 0:
break
case 1:
if index := ingressList.List()[0]; list.Items[index].GetName() == ing.Name() && list.Items[index].GetNamespace() == ing.Namespace() {
break
}
fallthrough
default:
return NewIngressHostnameCollision(hostname)
}
}
}
}
return nil
}