-
Notifications
You must be signed in to change notification settings - Fork 8
/
cluster_webhook.go
301 lines (260 loc) · 8.66 KB
/
cluster_webhook.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* MIT License
*
* Copyright (c) since 2021, flomesh.io Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// Package cluster contains webhook logic for the Cluster resource
package cluster
import (
"fmt"
"net"
"net/http"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
admissionregv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/kubernetes"
flomeshadmission "github.com/flomesh-io/fsm/pkg/admission"
clusterv1alpha1 "github.com/flomesh-io/fsm/pkg/apis/multicluster/v1alpha1"
"github.com/flomesh-io/fsm/pkg/configurator"
"github.com/flomesh-io/fsm/pkg/constants"
"github.com/flomesh-io/fsm/pkg/webhook"
)
type register struct {
*webhook.RegisterConfig
}
// NewRegister creates a new cluster webhook register
func NewRegister(cfg *webhook.RegisterConfig) webhook.Register {
return ®ister{
RegisterConfig: cfg,
}
}
// GetWebhooks returns the webhooks of the cluster resource
func (r *register) GetWebhooks() ([]admissionregv1.MutatingWebhook, []admissionregv1.ValidatingWebhook) {
rule := flomeshadmission.NewRule(
[]admissionregv1.OperationType{admissionregv1.Create, admissionregv1.Update},
[]string{"flomesh.io"},
[]string{"v1alpha1"},
[]string{"clusters"},
)
return []admissionregv1.MutatingWebhook{flomeshadmission.NewMutatingWebhook(
"mcluster.kb.flomesh.io",
r.WebhookSvcNs,
r.WebhookSvcName,
constants.ClusterMutatingWebhookPath,
r.CaBundle,
nil,
nil,
admissionregv1.Fail,
[]admissionregv1.RuleWithOperations{rule},
)}, []admissionregv1.ValidatingWebhook{flomeshadmission.NewValidatingWebhook(
"vcluster.kb.flomesh.io",
r.WebhookSvcNs,
r.WebhookSvcName,
constants.ClusterValidatingWebhookPath,
r.CaBundle,
nil,
nil,
admissionregv1.Fail,
[]admissionregv1.RuleWithOperations{rule},
)}
}
// GetHandlers returns the webhook handlers of the cluster resource
func (r *register) GetHandlers() map[string]http.Handler {
return map[string]http.Handler{
constants.ClusterMutatingWebhookPath: webhook.DefaultingWebhookFor(newDefaulter(r.KubeClient, r.Config)),
constants.ClusterValidatingWebhookPath: webhook.ValidatingWebhookFor(newValidator(r.KubeClient)),
}
}
type defaulter struct {
kubeClient kubernetes.Interface
cfg configurator.Configurator
}
func newDefaulter(kubeClient kubernetes.Interface, cfg configurator.Configurator) *defaulter {
return &defaulter{
kubeClient: kubeClient,
cfg: cfg,
}
}
func (w *defaulter) RuntimeObject() runtime.Object {
return &clusterv1alpha1.Cluster{}
}
func (w *defaulter) SetDefaults(obj interface{}) {
c, ok := obj.(*clusterv1alpha1.Cluster)
if !ok {
return
}
log.Debug().Msgf("Default Webhook, name=%s", c.Name)
log.Debug().Msgf("Before setting default values, spec=%v", c.Spec)
//meshConfig := w.configStore.MeshConfig.GetConfig()
//
//if meshConfig == nil {
// return
//}
// for InCluster connector, it's name is always 'local'
//if c.Spec.IsInCluster {
// c.Name = "local"
//}
//if c.Labels == nil {
// c.Labels = make(map[string]string)
//}
//
//if c.Spec.IsInCluster {
// c.Labels[constants.MultiClustersConnectorMode] = "local"
//} else {
// c.Labels[constants.MultiClustersConnectorMode] = "remote"
//}
log.Debug().Msgf("After setting default values, spec=%v", c.Spec)
}
type validator struct {
kubeClient kubernetes.Interface
}
// RuntimeObject returns the runtime object of the validator
func (w *validator) RuntimeObject() runtime.Object {
return &clusterv1alpha1.Cluster{}
}
// ValidateCreate validates the creation of the cluster resource
func (w *validator) ValidateCreate(obj interface{}) error {
//cluster, ok := obj.(*clusterv1alpha1.Cluster)
//if !ok {
// return nil
//}
//if cluster.Spec.IsInCluster {
// // There can be ONLY ONE Cluster of InCluster mode
// clusterList, err := w.k8sAPI.FlomeshClient.
// ClusterV1alpha1().
// Clusters().
// List(context.TODO(), metav1.ListOptions{})
// if err != nil {
// log.Error().Msgf("Failed to list Clusters, %v", err)
// return err
// }
//
// numOfInCluster := 0
// for _, c := range clusterList.Items {
// if c.Spec.IsInCluster {
// numOfInCluster++
// }
// }
// if numOfInCluster >= 1 {
// errMsg := fmt.Sprintf("there're %d InCluster resources, should ONLY have exact ONE", numOfInCluster)
// log.Error().Msgf(errMsg)
// return errors.New(errMsg)
// }
//}
return doValidation(obj)
}
// ValidateUpdate validates the update of the cluster resource
func (w *validator) ValidateUpdate(_, obj interface{}) error {
//oldCluster, ok := oldObj.(*clusterv1alpha1.Cluster)
//if !ok {
// return nil
//}
//
//cluster, ok := obj.(*clusterv1alpha1.Cluster)
//if !ok {
// return nil
//}
//if oldCluster.Spec.IsInCluster != cluster.Spec.IsInCluster {
// return errors.New("cannot update an immutable field: spec.IsInCluster")
//}
return doValidation(obj)
}
// ValidateDelete validates the deletion of the cluster resource
func (w *validator) ValidateDelete(_ interface{}) error {
return nil
}
func newValidator(kubeClient kubernetes.Interface) *validator {
return &validator{
kubeClient: kubeClient,
}
}
func doValidation(obj interface{}) error {
c, ok := obj.(*clusterv1alpha1.Cluster)
if !ok {
return nil
}
//if c.Labels == nil || c.Labels[constants.MultiClustersConnectorMode] == "" {
// return fmt.Errorf("missing required label 'multicluster.flomesh.io/connector-mode'")
//}
//connectorMode := c.Labels[constants.MultiClustersConnectorMode]
//switch connectorMode {
//case "local", "remote":
// log.Debug().Msgf("multicluster.flomesh.io/connector-mode=%s", connectorMode)
//default:
// return fmt.Errorf("invalid value %q for label multicluster.flomesh.io/connector-mode, must be either 'local' or 'remote'", connectorMode)
//}
//if c.Spec.IsInCluster {
// if connectorMode == "remote" {
// return fmt.Errorf("label and spec doesn't match: multicluster.flomesh.io/connector-mode=remote, spec.IsInCluster=true")
// }
//
// return nil
//} else {
// if connectorMode == "local" {
// return fmt.Errorf("label and spec doesn't match: multicluster.flomesh.io/connector-mode=local, spec.IsInCluster=false")
// }
host := c.Spec.GatewayHost
if host == "" {
return errors.New("GatewayHost is required in OutCluster mode")
}
if c.Spec.Kubeconfig == "" {
return fmt.Errorf("kubeconfig must be set in OutCluster mode")
}
//if c.Name == "local" {
// return errors.New("Cluster Name 'local' is reserved for InCluster Mode ONLY, please change the cluster name")
//}
isDNSName := false
if ipErrs := validation.IsValidIPv4Address(field.NewPath(""), host); len(ipErrs) > 0 {
// Not IPv4 address
log.Warn().Msgf("%q is NOT a valid IPv4 address: %v", host, ipErrs)
if dnsErrs := validation.IsDNS1123Subdomain(host); len(dnsErrs) > 0 {
// Not valid DNS domain name
return fmt.Errorf("invalid DNS name %q: %v", host, dnsErrs)
}
// is DNS name
isDNSName = true
}
var gwIPv4 net.IP
if isDNSName {
ipAddr, err := net.ResolveIPAddr("ip4", host)
if err != nil {
return fmt.Errorf("%q cannot be resolved to IP", host)
}
log.Debug().Msgf("%q is resolved to IP: %s", host, ipAddr.IP)
gwIPv4 = ipAddr.IP.To4()
} else {
gwIPv4 = net.ParseIP(host).To4()
}
if gwIPv4 == nil {
return fmt.Errorf("%q cannot be resolved to a IPv4 address", host)
}
if gwIPv4 != nil && (gwIPv4.IsLoopback() || gwIPv4.IsUnspecified()) {
return fmt.Errorf("gateway Host %s is resolved to Loopback IP or Unspecified", host)
}
port := int(c.Spec.GatewayPort)
if errs := validation.IsValidPortNum(port); len(errs) > 0 {
return fmt.Errorf("invalid port number %d: %v", c.Spec.GatewayPort, errs)
}
return nil
}