我目前正在为Kubernetes编写一个验证网络钩子。我在尝试将来自Kubernetes API的准入请求反序列化为Kubernetes对象时遇到问题。资源可以是以下任何一种:容器,部署,状态集,入口,服务。 UniversalDeserializer询问我类型,但是我用AdmissionRequest无法知道类型。 有没有只接受Raw对象并将其解码为有效对象的Decoder?
我尝试反序列化AdmissionRequest.Request.Object.Raw并收到该错误:
panic: Object 'Kind' is missing in '{"metadata":{"name":"gac","namespace":"all","uid":"2b38b994-9cf4-11e9-bb40-0800270c8766","generation":1,"creationTimestamp":"2019-07-02T18:06:52Z","labels":{"app":"gac","release":"nao_pode_essa"}},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"gac","release":"2"}},"template":{"metadata":{"creationTimestamp":null,"labels":{"app":"gac","release":"2"}},"spec":{"containers":'...
但是我的对象中有种。
我尝试使用对象的JSON表示反序列化它,并且它起作用了。
这是我的代码:
func main() {
review := v1beta1.AdmissionReview{}
body := `{"kind":"AdmissionReview","apiVersion":"admission.k8s.io/v1beta1","request":{"uid":"2b38c0e2-9cf4-11e9-bb40-0800270c8766","kind":{"group":"apps","version":"v1","kind":"Deployment"},"resource":{"group":"apps","version":"v1","resource":"deployments"},"namespace":"all","operation":"CREATE","userInfo":{"username":"minikube-user","groups":["system:masters","system:authenticated"]},"object":{"metadata":{"name":"gac","namespace":"all","uid":"2b38b994-9cf4-11e9-bb40-0800270c8766","generation":1,"creationTimestamp":"2019-07-02T18:06:52Z","labels":{"app":"gac","release":"nao_pode_essa"}},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"gac","release":"2"}},"template":{"metadata":{"creationTimestamp":null,"labels":{"app":"gac","release":"2"}},"spec":{"containers":[{"name":"erickkkkk","image":"erickfaustino/gac:v0.1.0","ports":[{"name":"http","containerPort":8443,"protocol":"TCP"}],"resources":{"limits":{"cpu":"1","memory":"1Gi"},"requests":{"cpu":"200m","memory":"200Mi"}},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","imagePullPolicy":"Always"}],"restartPolicy":"Always","terminationGracePeriodSeconds":30,"dnsPolicy":"ClusterFirst","securityContext":{},"schedulerName":"default-scheduler"}},"strategy":{"type":"RollingUpdate","rollingUpdate":{"maxUnavailable":"25%","maxSurge":"25%"}},"revisionHistoryLimit":10,"progressDeadlineSeconds":600},"status":{}},"oldObject":null,"dryRun":false}}`
bbyte := []byte(body)
ud := scheme.Codecs.UniversalDeserializer()
_, _, err := ud.Decode(bbyte, nil, &review)
if err != nil {
panic(err)
}
raw := review.Request.Object.Raw
lero, _, err := ud.Decode(raw, nil, nil)
if err != nil {
panic(err)
}
fmt.Println(lero)
}