-
Notifications
You must be signed in to change notification settings - Fork 157
/
admission_plugin.go
183 lines (158 loc) · 5.81 KB
/
admission_plugin.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 admin
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/go-kit/kit/endpoint"
"github.com/gorilla/mux"
apiv1 "k8c.io/kubermatic/v2/pkg/api/v1"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/crd/kubermatic/v1"
"k8c.io/kubermatic/v2/pkg/handler/v1/common"
"k8c.io/kubermatic/v2/pkg/provider"
k8cerrors "k8c.io/kubermatic/v2/pkg/util/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ListAdmissionPluginEndpoint returns admission plugin list
func ListAdmissionPluginEndpoint(userInfoGetter provider.UserInfoGetter, admissionPluginProvider provider.AdmissionPluginsProvider) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
userInfo, err := userInfoGetter(ctx, "")
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
admissionPluginList, err := admissionPluginProvider.List(userInfo)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
var resultList []apiv1.AdmissionPlugin
for _, plugin := range admissionPluginList {
resultList = append(resultList, convertAdmissionPlugin(plugin))
}
return resultList, nil
}
}
// GetAdmissionPluginEndpoint returns the admission plugin
func GetAdmissionPluginEndpoint(userInfoGetter provider.UserInfoGetter, admissionPluginProvider provider.AdmissionPluginsProvider) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req, ok := request.(admissionPluginReq)
if !ok {
return nil, k8cerrors.NewBadRequest("invalid request")
}
userInfo, err := userInfoGetter(ctx, "")
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
admissionPlugin, err := admissionPluginProvider.Get(userInfo, req.Name)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertAdmissionPlugin(*admissionPlugin), nil
}
}
// DeleteAdmissionPluginEndpoint deletes the admission plugin
func DeleteAdmissionPluginEndpoint(userInfoGetter provider.UserInfoGetter, admissionPluginProvider provider.AdmissionPluginsProvider) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req, ok := request.(admissionPluginReq)
if !ok {
return nil, k8cerrors.NewBadRequest("invalid request")
}
userInfo, err := userInfoGetter(ctx, "")
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
err = admissionPluginProvider.Delete(userInfo, req.Name)
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return nil, nil
}
}
// UpdateAdmissionPluginEndpoint updates the admission plugin
func UpdateAdmissionPluginEndpoint(userInfoGetter provider.UserInfoGetter, admissionPluginProvider provider.AdmissionPluginsProvider) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req, ok := request.(updateAdmissionPluginReq)
if !ok {
return nil, k8cerrors.NewBadRequest("invalid request")
}
err := req.Validate()
if err != nil {
return nil, k8cerrors.NewBadRequest(err.Error())
}
userInfo, err := userInfoGetter(ctx, "")
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
editedAdmissionPlugin, err := admissionPluginProvider.Update(userInfo, &kubermaticv1.AdmissionPlugin{
ObjectMeta: v1.ObjectMeta{Name: req.Name},
Spec: kubermaticv1.AdmissionPluginSpec{
PluginName: req.Body.Plugin,
FromVersion: req.Body.FromVersion,
},
})
if err != nil {
return nil, common.KubernetesErrorToHTTPError(err)
}
return convertAdmissionPlugin(*editedAdmissionPlugin), nil
}
}
// admissionPluginReq defines HTTP request for getAdmissionPlugin and deleteAdmissionPlugin
// swagger:parameters getAdmissionPlugin deleteAdmissionPlugin
type admissionPluginReq struct {
// in: path
// required: true
Name string `json:"name"`
}
// updateAdmissionPlugin defines HTTP request for updateAdmissionPlugin
// swagger:parameters updateAdmissionPlugin
type updateAdmissionPluginReq struct {
admissionPluginReq
// in: body
Body apiv1.AdmissionPlugin
}
// Validate validates UpdateAdmissionPluginEndpoint request
func (r updateAdmissionPluginReq) Validate() error {
if r.Name != r.Body.Name {
return fmt.Errorf("admission plugin name mismatch, you requested to update AdmissionPlugin = %s but body contains AdmissionPlugin = %s", r.Name, r.Body.Name)
}
return nil
}
func DecodeAdmissionPluginReq(c context.Context, r *http.Request) (interface{}, error) {
var req admissionPluginReq
name := mux.Vars(r)["name"]
if name == "" {
return nil, fmt.Errorf("'name' parameter is required but was not provided")
}
req.Name = name
return req, nil
}
func DecodeUpdateAdmissionPluginReq(c context.Context, r *http.Request) (interface{}, error) {
var req updateAdmissionPluginReq
pluginReq, err := DecodeAdmissionPluginReq(c, r)
if err != nil {
return nil, err
}
req.admissionPluginReq = pluginReq.(admissionPluginReq)
if err := json.NewDecoder(r.Body).Decode(&req.Body); err != nil {
return nil, err
}
return req, nil
}
func convertAdmissionPlugin(admissionPlugin kubermaticv1.AdmissionPlugin) apiv1.AdmissionPlugin {
return apiv1.AdmissionPlugin{
Name: admissionPlugin.Name,
Plugin: admissionPlugin.Spec.PluginName,
FromVersion: admissionPlugin.Spec.FromVersion,
}
}