forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework.go
158 lines (140 loc) · 4.85 KB
/
framework.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
/*
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 converter
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/golang/glog"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
)
// convertFunc is the user defined function for any conversion. The code in this file is a
// template that can be use for any CR conversion given this function.
type convertFunc func(Object *unstructured.Unstructured, version string) (*unstructured.Unstructured, metav1.Status)
// toConversionResponse is a helper function to create an AdmissionResponse
// with an embedded error
func toConversionResponse(err error) *v1beta1.ConversionResponse {
return &v1beta1.ConversionResponse{
Result: metav1.Status{
Message: err.Error(),
Status: metav1.StatusFailure,
},
}
}
func statusErrorWithMessage(msg string, params ...string) metav1.Status {
return metav1.Status{
Message: fmt.Sprintf(msg, params),
Status: metav1.StatusFailure,
}
}
func statusSucceed() metav1.Status {
return metav1.Status{
Status: metav1.StatusSuccess,
}
}
// doConversion converts the requested object given the conversion function and returns a conversion response.
// failures will be reported as Reason in the conversion response.
func doConversion(convertRequest *v1beta1.ConversionRequest, convert convertFunc) *v1beta1.ConversionResponse {
cr := unstructured.Unstructured{}
if err := cr.UnmarshalJSON(convertRequest.Object.Raw); err != nil {
glog.Error(err)
return toConversionResponse(err)
}
var convertedCR *unstructured.Unstructured
var status metav1.Status
if convertRequest.IsList {
listCR, err := cr.ToList()
if err != nil {
glog.Error(err)
return toConversionResponse(err)
}
convertedList := listCR.DeepCopy()
for i := 0; i < len(convertedList.Items); i++ {
item, status := convert(&convertedList.Items[i], convertRequest.APIVersion)
if status.Status != metav1.StatusSuccess {
glog.Error(status.String())
return &v1beta1.ConversionResponse{
Result: status,
}
}
item.SetAPIVersion(convertRequest.APIVersion)
convertedList.Items[i] = *item
}
convertedCR = &unstructured.Unstructured{}
convertedCR.SetUnstructuredContent(convertedList.UnstructuredContent())
status = statusSucceed()
} else {
convertedCR, status = convert(&cr, convertRequest.APIVersion)
if status.Status != metav1.StatusSuccess {
glog.Error(status.String())
return &v1beta1.ConversionResponse{
Result: status,
}
}
}
convertedCR.SetAPIVersion(convertRequest.APIVersion)
return &v1beta1.ConversionResponse{
ConvertedObject: runtime.RawExtension{
Object: convertedCR,
},
Result: status,
}
}
func serve(w http.ResponseWriter, r *http.Request, convert convertFunc) {
var body []byte
if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil {
body = data
}
}
// verify the content type is accurate
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
err := fmt.Errorf("contentType=%s, expect application/json", contentType)
glog.Errorf(err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
glog.V(2).Info(fmt.Sprintf("handling request: %v", body))
convertReview := v1beta1.ConversionReview{}
deserializer := serializer.NewCodecFactory(runtime.NewScheme()).UniversalDeserializer()
if _, _, err := deserializer.Decode(body, nil, &convertReview); err != nil {
glog.Error(err)
convertReview.Response = toConversionResponse(err)
} else {
convertReview.Response = doConversion(convertReview.Request, convert)
}
glog.V(2).Info(fmt.Sprintf("sending response: %v", convertReview.Response))
convertReview.Response.UID = convertReview.Request.UID
// reset the request, it is not needed in a response.
convertReview.Request = &v1beta1.ConversionRequest{}
resp, err := json.Marshal(convertReview)
if err != nil {
glog.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := w.Write(resp); err != nil {
glog.Error(err)
return
}
}
// ServeExampleConvert servers endpoint for the example converter defined as convertExampleCRD function.
func ServeExampleConvert(w http.ResponseWriter, r *http.Request) {
serve(w, r, convertExampleCRD)
}