/
webhook.go
181 lines (151 loc) · 5.6 KB
/
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
// Copyright Red Hat
package webhook
import (
"context"
"encoding/json"
"net/http"
"strings"
"sync"
singaporev1alpha1 "github.com/stolostron/cluster-registration-operator/api/singapore/v1alpha1"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
)
const (
GROUP_SUFFIX = "singapore.open-cluster-management.io"
)
type RegisteredClusterAdmissionHook struct {
Client dynamic.ResourceInterface
ClusterRegistrarClient dynamic.ResourceInterface
KubeClient kubernetes.Interface
lock sync.RWMutex
initialized bool
}
// ValidatingResource is called by generic-admission-server on startup to register the returned REST resource through which the
// webhook is accessed by the kube apiserver.
func (a *RegisteredClusterAdmissionHook) ValidatingResource() (plural schema.GroupVersionResource, singular string) {
return schema.GroupVersionResource{
Group: "admission." + GROUP_SUFFIX,
Version: "v1alpha1",
Resource: "registeredclustervalidators",
},
"registeredclustervalidators"
}
// Validate is called by generic-admission-server when the registered REST resource above is called with an admission request.
func (a *RegisteredClusterAdmissionHook) Validate(admissionSpec *admissionv1beta1.AdmissionRequest) *admissionv1beta1.AdmissionResponse {
status := &admissionv1beta1.AdmissionResponse{}
klog.V(4).Infof("RegisteredCluster Validate %q operation for object %q, group: %s, resource: %s", admissionSpec.Operation, admissionSpec.Object, admissionSpec.Resource.Group, admissionSpec.Resource.Resource)
// only validate the request for authrealm
if !strings.HasSuffix(admissionSpec.Resource.Group, GROUP_SUFFIX) {
status.Allowed = true
return status
}
switch admissionSpec.Resource.Resource {
case "registeredclusters":
return a.ValidateRegisteredCluster(admissionSpec)
case "clusterregistrars":
return a.ValidateClusterRegistrar(admissionSpec)
}
status.Allowed = true
return status
}
func (a *RegisteredClusterAdmissionHook) ValidateRegisteredCluster(admissionSpec *admissionv1beta1.AdmissionRequest) *admissionv1beta1.AdmissionResponse {
status := &admissionv1beta1.AdmissionResponse{}
regCluster := &singaporev1alpha1.RegisteredCluster{}
err := json.Unmarshal(admissionSpec.Object.Raw, regCluster)
if err != nil {
status.Allowed = false
status.Result = &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonBadRequest,
Message: err.Error(),
}
return status
}
klog.V(4).Infof("Validate webhook for RegisteredCluster name: %s, namespace: %s", regCluster.Name, regCluster.Namespace)
switch admissionSpec.Operation {
case admissionv1beta1.Create:
klog.V(4).Info("Validate RegisteredCluster create ")
if len(regCluster.Name) > 50 {
status.Allowed = false
status.Result = &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusForbidden, Reason: metav1.StatusReasonForbidden,
Message: "RegisteredCluster name is too long (max 50 characters)",
}
return status
}
status.Allowed = true
return status
}
status.Allowed = true
return status
}
func (a *RegisteredClusterAdmissionHook) ValidateClusterRegistrar(admissionSpec *admissionv1beta1.AdmissionRequest) *admissionv1beta1.AdmissionResponse {
status := &admissionv1beta1.AdmissionResponse{}
clusterRegistrar := &singaporev1alpha1.ClusterRegistrar{}
err := json.Unmarshal(admissionSpec.Object.Raw, clusterRegistrar)
if err != nil {
status.Allowed = false
status.Result = &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonBadRequest,
Message: err.Error(),
}
return status
}
klog.V(4).Infof("Validate webhook for ClusterRegistrar name: %s", clusterRegistrar.Name)
l, err := a.ClusterRegistrarClient.List(context.TODO(), metav1.ListOptions{})
if err != nil {
status.Allowed = false
status.Result = &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonInternalError,
Message: err.Error(),
}
return status
}
if len(l.Items) > 0 {
status.Allowed = false
status.Result = &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonForbidden,
Message: "a clusterregistrar custom resource already exists",
}
return status
}
status.Allowed = true
return status
}
// Initialize is called by generic-admission-server on startup to setup initialization that webhook needs.
func (a *RegisteredClusterAdmissionHook) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
a.lock.Lock()
defer a.lock.Unlock()
klog.V(0).Infof("Initialize admission webhook for RegisteredCluster")
a.initialized = true
shallowClientConfigCopy := *kubeClientConfig
shallowClientConfigCopy.GroupVersion = &schema.GroupVersion{
Group: GROUP_SUFFIX,
Version: "v1alpha1",
}
shallowClientConfigCopy.APIPath = "/apis"
kubeClient, err := kubernetes.NewForConfig(&shallowClientConfigCopy)
if err != nil {
return err
}
a.KubeClient = kubeClient
dynamicClient, err := dynamic.NewForConfig(&shallowClientConfigCopy)
if err != nil {
return err
}
a.Client = dynamicClient.Resource(schema.GroupVersionResource{
Group: GROUP_SUFFIX,
Version: "v1alpha1",
Resource: "registeredclusters",
})
a.ClusterRegistrarClient = dynamicClient.Resource(schema.GroupVersionResource{
Group: GROUP_SUFFIX,
Version: "v1alpha1",
Resource: "clusterregistrars",
})
return nil
}