Skip to content

Commit

Permalink
fix: bugs
Browse files Browse the repository at this point in the history
Signed-off-by: xu.zhu <[email protected]>
  • Loading branch information
xuzhu-591 committed Apr 8, 2024
1 parent 2c05874 commit ec08c9b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pkg/admission/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type Webhook interface {
}

func Mutating(ctx context.Context, request *Request) (*Request, error) {
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()
if request.Object == nil {
return request, nil
}
Expand All @@ -69,9 +71,11 @@ func Mutating(ctx context.Context, request *Request) (*Request, error) {
if err = loggingError(ctx, err, webhook); err != nil {
return nil, err
}
request.Object, err = jsonPatch(request.Object, response.Patch)
if err = loggingError(ctx, err, webhook); err != nil {
return nil, err
if response != nil && response.Patch != nil {
request.Object, err = jsonPatch(request.Object, response.Patch)
if err = loggingError(ctx, err, webhook); err != nil {
return nil, err
}
}
}
return request, nil
Expand Down Expand Up @@ -136,12 +140,13 @@ func Validating(ctx context.Context, request *Request) error {
func loggingError(ctx context.Context, err error, webhook Webhook) error {
if err != nil {
if webhook.IgnoreError() {
log.Errorf(ctx, "failed to admit request: %v", err)
log.Warningf(ctx, "failed to admit request: %v", err.Error())
return nil
}
log.Errorf(ctx, "failed to admit request: %v", err.Error())
return err
}
return err
return nil
}

func jsonPatch(obj interface{}, patchJSON []byte) (interface{}, error) {
Expand Down

0 comments on commit ec08c9b

Please sign in to comment.