/
admission-webhook.go
99 lines (86 loc) · 3.37 KB
/
admission-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
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 etcd
import (
commtypes "github.com/Tencent/bk-bcs/bcs-common/common/types"
"github.com/Tencent/bk-bcs/bcs-mesos/kubebkbcsv2/apis/bkbcs/v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (store *managerStore) CheckAdmissionWebhookExist(admission *commtypes.AdmissionWebhookConfiguration) (string, bool) {
admission.NameSpace = commtypes.DefaultAdmissionNamespace
client := store.BkbcsClient.AdmissionWebhookConfigurations(admission.NameSpace)
obj, err := client.Get(admission.Name, metav1.GetOptions{})
if err == nil {
return obj.ResourceVersion, true
}
return "", false
}
func (store *managerStore) SaveAdmissionWebhook(admission *commtypes.AdmissionWebhookConfiguration) error {
admission.NameSpace = commtypes.DefaultAdmissionNamespace
err := store.checkNamespace(admission.NameSpace)
if err != nil {
return err
}
client := store.BkbcsClient.AdmissionWebhookConfigurations(admission.NameSpace)
v2Admission := &v2.AdmissionWebhookConfiguration{
TypeMeta: metav1.TypeMeta{
Kind: CrdAdmissionWebhookConfiguration,
APIVersion: ApiversionV2,
},
ObjectMeta: metav1.ObjectMeta{
Name: admission.Name,
Namespace: admission.NameSpace,
Labels: admission.Labels,
Annotations: admission.Annotations,
},
Spec: v2.AdmissionWebhookConfigurationSpec{
AdmissionWebhookConfiguration: *admission,
},
}
rv, exist := store.CheckAdmissionWebhookExist(admission)
if exist {
v2Admission.ResourceVersion = rv
_, err = client.Update(v2Admission)
} else {
_, err = client.Create(v2Admission)
}
return err
}
func (store *managerStore) FetchAdmissionWebhook(ns, name string) (*commtypes.AdmissionWebhookConfiguration, error) {
ns = commtypes.DefaultAdmissionNamespace
client := store.BkbcsClient.AdmissionWebhookConfigurations(ns)
v2Admission, err := client.Get(name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return &v2Admission.Spec.AdmissionWebhookConfiguration, nil
}
func (store *managerStore) DeleteAdmissionWebhook(ns, name string) error {
ns = commtypes.DefaultAdmissionNamespace
client := store.BkbcsClient.AdmissionWebhookConfigurations(ns)
err := client.Delete(name, &metav1.DeleteOptions{})
return err
}
func (store *managerStore) FetchAllAdmissionWebhooks() ([]*commtypes.AdmissionWebhookConfiguration, error) {
client := store.BkbcsClient.AdmissionWebhookConfigurations("")
v2Admissions, err := client.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
admissions := make([]*commtypes.AdmissionWebhookConfiguration, 0, len(v2Admissions.Items))
for _, v2 := range v2Admissions.Items {
obj := v2.Spec.AdmissionWebhookConfiguration
admissions = append(admissions, &obj)
}
return admissions, nil
}