-
Notifications
You must be signed in to change notification settings - Fork 346
/
validate.go
98 lines (88 loc) · 3.01 KB
/
validate.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
// Copyright 2021 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package externalippool
import (
"encoding/json"
"fmt"
admv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
crdv1alpha2 "antrea.io/antrea/pkg/apis/crd/v1alpha2"
)
func (c *ExternalIPPoolController) ValidateExternalIPPool(review *admv1.AdmissionReview) *admv1.AdmissionResponse {
var result *metav1.Status
var msg string
allowed := true
klog.V(2).Info("Validating ExternalIPPool", "request", review.Request)
var newObj, oldObj crdv1alpha2.ExternalIPPool
if review.Request.Object.Raw != nil {
if err := json.Unmarshal(review.Request.Object.Raw, &newObj); err != nil {
klog.ErrorS(err, "Error de-serializing current ExternalIPPool")
return newAdmissionResponseForErr(err)
}
}
if review.Request.OldObject.Raw != nil {
if err := json.Unmarshal(review.Request.OldObject.Raw, &oldObj); err != nil {
klog.ErrorS(err, "Error de-serializing old ExternalIPPool")
return newAdmissionResponseForErr(err)
}
}
switch review.Request.Operation {
case admv1.Create:
// This shouldn't happen with the webhook configuration we include in the Antrea YAML manifests.
klog.V(2).Info("Validating CREATE request for ExternalIPPool")
// Always allow CREATE request.
case admv1.Update:
klog.V(2).Info("Validating UPDATE request for ExternalIPPool")
oldIPRangeSet := getIPRangeSet(oldObj.Spec.IPRanges)
newIPRangeSet := getIPRangeSet(newObj.Spec.IPRanges)
deletedIPRanges := oldIPRangeSet.Difference(newIPRangeSet)
if deletedIPRanges.Len() > 0 {
allowed = false
msg = fmt.Sprintf("existing IPRanges %v cannot be deleted", deletedIPRanges.List())
}
case admv1.Delete:
// This shouldn't happen with the webhook configuration we include in the Antrea YAML manifests.
klog.V(2).Info("Validating DELETE request for ExternalIPPool")
// Always allow DELETE request.
}
if msg != "" {
result = &metav1.Status{
Message: msg,
}
}
return &admv1.AdmissionResponse{
Allowed: allowed,
Result: result,
}
}
func getIPRangeSet(ipRanges []crdv1alpha2.IPRange) sets.String {
set := sets.NewString()
for _, ipRange := range ipRanges {
ipRangeStr := ipRange.CIDR
if ipRangeStr == "" {
ipRangeStr = fmt.Sprintf("%s-%s", ipRange.Start, ipRange.End)
}
set.Insert(ipRangeStr)
}
return set
}
func newAdmissionResponseForErr(err error) *admv1.AdmissionResponse {
return &admv1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
}