forked from kubernetes-sigs/kubebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admissionhandler.go
172 lines (142 loc) · 5.47 KB
/
admissionhandler.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
/*
Copyright 2018 The Kubernetes 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 webhook
import (
"fmt"
"path/filepath"
"strings"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)
var _ input.File = &AddAdmissionWebhookBuilderHandler{}
// AdmissionHandler scaffolds an admission handler
type AdmissionHandler struct {
input.Input
// ResourcePackage is the package of the Resource
ResourcePackage string
// GroupDomain is the Group + "." + Domain for the Resource
GroupDomain string
// Resource is a resource in the API group
Resource *resource.Resource
Config
BuilderName string
OperationsString string
Mutate bool
}
// GetInput implements input.File
func (a *AdmissionHandler) GetInput() (input.Input, error) {
a.ResourcePackage, a.GroupDomain = getResourceInfo(coreGroups, a.Resource, a.Input)
a.Type = strings.ToLower(a.Type)
if a.Type == "mutating" {
a.Mutate = true
}
a.BuilderName = builderName(a.Config, strings.ToLower(a.Resource.Kind))
ops := make([]string, len(a.Operations))
for i, op := range a.Operations {
ops[i] = strings.Title(op)
}
a.OperationsString = strings.Join(ops, "")
if a.Path == "" {
a.Path = filepath.Join("pkg", "webhook",
fmt.Sprintf("%s_server", a.Server),
strings.ToLower(a.Resource.Kind),
a.Type,
fmt.Sprintf("%s_%s_handler.go", strings.ToLower(a.Resource.Kind), strings.Join(a.Operations, "_")))
}
a.TemplateBody = addAdmissionHandlerTemplate
return a.Input, nil
}
var addAdmissionHandlerTemplate = `{{ .Boilerplate }}
package {{ .Type }}
import (
"context"
"net/http"
{{ .Resource.Group}}{{ .Resource.Version }} "{{ .ResourcePackage }}/{{ .Resource.Group}}/{{ .Resource.Version }}"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
)
func init() {
webhookName := "{{ .BuilderName }}"
if HandlerMap[webhookName] == nil {
HandlerMap[webhookName] = []admission.Handler{}
}
HandlerMap[webhookName] = append(HandlerMap[webhookName], &{{ .Resource.Kind }}{{ .OperationsString }}Handler{})
}
// {{ .Resource.Kind }}{{ .OperationsString }}Handler handles {{ .Resource.Kind }}
type {{ .Resource.Kind }}{{ .OperationsString }}Handler struct {
// To use the client, you need to do the following:
// - uncomment it
// - import sigs.k8s.io/controller-runtime/pkg/client
// - uncomment the InjectClient method at the bottom of this file.
// Client client.Client
// Decoder decodes objects
Decoder types.Decoder
}
{{ if .Mutate }}
func (h *{{ .Resource.Kind }}{{ .OperationsString }}Handler) {{ .Type }}{{ .Resource.Kind }}Fn(ctx context.Context, obj *{{ .Resource.Group}}{{ .Resource.Version }}.{{ .Resource.Kind }}) error {
// TODO(user): implement your admission logic
return nil
}
{{ else }}
func (h *{{ .Resource.Kind }}{{ .OperationsString }}Handler) {{ .Type }}{{ .Resource.Kind }}Fn(ctx context.Context, obj *{{ .Resource.Group}}{{ .Resource.Version }}.{{ .Resource.Kind }}) (bool, string, error) {
// TODO(user): implement your admission logic
return true, "allowed to be admitted", nil
}
{{ end }}
var _ admission.Handler = &{{ .Resource.Kind }}{{ .OperationsString }}Handler{}
{{ if .Mutate }}
// Handle handles admission requests.
func (h *{{ .Resource.Kind }}{{ .OperationsString }}Handler) Handle(ctx context.Context, req types.Request) types.Response {
obj := &{{ .Resource.Group}}{{ .Resource.Version }}.{{ .Resource.Kind }}{}
err := h.Decoder.Decode(req, obj)
if err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
}
copy := obj.DeepCopy()
err = h.{{ .Type }}{{ .Resource.Kind }}Fn(ctx, copy)
if err != nil {
return admission.ErrorResponse(http.StatusInternalServerError, err)
}
return admission.PatchResponse(obj, copy)
}
{{ else }}
// Handle handles admission requests.
func (h *{{ .Resource.Kind }}{{ .OperationsString }}Handler) Handle(ctx context.Context, req types.Request) types.Response {
obj := &{{ .Resource.Group}}{{ .Resource.Version }}.{{ .Resource.Kind }}{}
err := h.Decoder.Decode(req, obj)
if err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
}
allowed, reason, err := h.{{ .Type }}{{ .Resource.Kind }}Fn(ctx, obj)
if err != nil {
return admission.ErrorResponse(http.StatusInternalServerError, err)
}
return admission.ValidationResponse(allowed, reason)
}
{{ end }}
//var _ inject.Client = &{{ .Resource.Kind }}{{ .OperationsString }}Handler{}
//
//// InjectClient injects the client into the {{ .Resource.Kind }}{{ .OperationsString }}Handler
//func (h *{{ .Resource.Kind }}{{ .OperationsString }}Handler) InjectClient(c client.Client) error {
// h.Client = c
// return nil
//}
var _ inject.Decoder = &{{ .Resource.Kind }}{{ .OperationsString }}Handler{}
// InjectDecoder injects the decoder into the {{ .Resource.Kind }}{{ .OperationsString }}Handler
func (h *{{ .Resource.Kind }}{{ .OperationsString }}Handler) InjectDecoder(d types.Decoder) error {
h.Decoder = d
return nil
}
`