/
memberclusterannounce_webhook.go
89 lines (73 loc) · 3.02 KB
/
memberclusterannounce_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
/*
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 main
import (
"context"
"fmt"
"net/http"
"k8s.io/apiserver/pkg/authentication/serviceaccount"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
multiclusterv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1"
)
//+kubebuilder:webhook:path=/validate-multicluster-crd-antrea-io-v1alpha1-memberclusterannounce,mutating=false,failurePolicy=fail,sideEffects=None,groups=multicluster.crd.antrea.io,resources=memberclusterannounces,verbs=create;update,versions=v1alpha1,name=vmemberclusterannounce.kb.io,admissionReviewVersions={v1,v1beta1}
// member cluster announce validator
type memberClusterAnnounceValidator struct {
Client client.Client
decoder *admission.Decoder
namespace string
}
// Handle handles admission requests.
func (v *memberClusterAnnounceValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
memberClusterAnnounce := &multiclusterv1alpha1.MemberClusterAnnounce{}
e := v.decoder.Decode(req, memberClusterAnnounce)
if e != nil {
klog.ErrorS(e, "Error while decoding")
return admission.Errored(http.StatusBadRequest, e)
}
ui := req.UserInfo
_, saName, err := serviceaccount.SplitUsername(ui.Username)
if err != nil {
klog.ErrorS(err, "Error getting ServiceAccount name", "request", req)
return admission.Errored(http.StatusBadRequest, err)
}
// read the ClusterSet info
clusterSetList := &multiclusterv1alpha1.ClusterSetList{}
if err := v.Client.List(context.TODO(), clusterSetList, client.InNamespace(v.namespace)); err != nil {
klog.ErrorS(err, "Error reading ClusterSet", "Namespace", v.namespace)
return admission.Errored(http.StatusPreconditionFailed, err)
}
if len(clusterSetList.Items) != 1 {
return admission.Errored(http.StatusPreconditionFailed,
fmt.Errorf("invalid ClusterSet config in the leader cluster, please contact your administrator"))
}
clusterSet := clusterSetList.Items[0]
if clusterSet.Name == memberClusterAnnounce.ClusterSetID {
for _, member := range clusterSet.Spec.Members {
if member.ClusterID == memberClusterAnnounce.ClusterID {
// validate the ServiceAccount used is correct
if member.ServiceAccount == saName {
return admission.Allowed("")
} else {
return admission.Denied("Member does not have permissions")
}
}
}
}
return admission.Denied("Unknown member")
}
func (v *memberClusterAnnounceValidator) InjectDecoder(d *admission.Decoder) error {
v.decoder = d
return nil
}