Skip to content

Commit

Permalink
fix: some bugs
Browse files Browse the repository at this point in the history
Signed-off-by: Kiloson <[email protected]>
  • Loading branch information
kilosonc committed May 7, 2024
1 parent 064d0dd commit b781db6
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions core/middleware/admission/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,49 +58,46 @@ func Middleware(skippers ...middleware.Skipper) gin.HandlerFunc {
return
}
}
if object != nil {
// fill in the request url query into admission request options
queries := c.Request.URL.Query()
options := make(map[string]interface{}, len(queries))
for k, v := range queries {
if len(v) == 1 {
options[k] = v[0]
} else {
options[k] = v
}
}
admissionRequest := &admissionwebhook.Request{
Operation: admissionmodels.Operation(attr.GetVerb()),
Resource: attr.GetResource(),
Name: attr.GetName(),
SubResource: attr.GetSubResource(),
Version: attr.GetAPIVersion(),
Object: object,
Options: options,
}
admissionRequest, err = admissionwebhook.Mutating(c, admissionRequest)
if err != nil {
response.AbortWithRPCError(c,
rpcerror.ParamError.WithErrMsg(fmt.Sprintf("admission mutating failed: %v", err)))
return
}
if err := admissionwebhook.Validating(c, admissionRequest); err != nil {
response.AbortWithRPCError(c,
rpcerror.ParamError.WithErrMsg(fmt.Sprintf("admission validating failed: %v", err)))
return
// fill in the request url query into admission request options
queries := c.Request.URL.Query()
options := make(map[string]interface{}, len(queries))
for k, v := range queries {
if len(v) == 1 {
options[k] = v[0]
} else {
options[k] = v
}
newBodyBytes, err := json.Marshal(admissionRequest.Object)
}
admissionRequest := &admissionwebhook.Request{
Operation: admissionmodels.Operation(attr.GetVerb()),
Resource: attr.GetResource(),
Name: attr.GetName(),
SubResource: attr.GetSubResource(),
Version: attr.GetAPIVersion(),
Object: object,
Options: options,
}
admissionRequest, err = admissionwebhook.Mutating(c, admissionRequest)
if err != nil {
response.AbortWithRPCError(c,
rpcerror.ParamError.WithErrMsg(fmt.Sprintf("admission mutating failed: %v", err)))
return
}
if err := admissionwebhook.Validating(c, admissionRequest); err != nil {
response.AbortWithRPCError(c,
rpcerror.ParamError.WithErrMsg(fmt.Sprintf("admission validating failed: %v", err)))
return
}
if admissionRequest.Object != nil {
bodyBytes, err = json.Marshal(admissionRequest.Object)
if err != nil {
response.AbortWithRPCError(c,
rpcerror.ParamError.WithErrMsg(fmt.Sprintf("marshal request body failed, err: %v", err)))
return
}
// restore the request body
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(newBodyBytes))
c.Next()
} else {
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
c.Next()
}
// restore the request body
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
c.Next()
}, skippers...)
}

0 comments on commit b781db6

Please sign in to comment.