Skip to content

Commit

Permalink
Skip transform for creating when adding approval operator if skipCrea…
Browse files Browse the repository at this point in the history
…teCheck (#344)

Transform for creating eventing will add wrong operator in some
case for example automate approval for stageRun.

So we skip the tranfrom for create operation base on skipCreateCheck.

Signed-off-by: yuzhipeng <[email protected]>

Signed-off-by: yuzhipeng <[email protected]>
  • Loading branch information
yuzp1996 authored Dec 30, 2022
1 parent 27848a1 commit 9a2f023
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 52 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions webhook/admission/transform_approval_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import (
// ChecksGetter gets the checks from the runtime object
type ChecksGetter interface {
GetChecks(runtime.Object) []*metav1alpha1.Check
// SkipCreateCheck represent whether should skip checking for create request
// Whether or not skip creating check mainly depends on the developer's permission
// If the developer can create approval in resource directly then we should not skip
// checking and vice versa.
SkipCreateCheck() bool
}

// PairOfOldNewCheck is a pair of old and new check
Expand All @@ -45,9 +50,15 @@ func WithApprovalOperator(getter ChecksGetter) TransformFunc {
if req.Operation != admissionv1.Create && req.Operation != admissionv1.Update {
return
}

if req.Operation == admissionv1.Create && getter.SkipCreateCheck() {
return
}

log := logging.FromContext(ctx)
newChecks := getter.GetChecks(runtimeObj)
var oldChecks []*metav1alpha1.Check

if req.Operation == admissionv1.Create {
// If it is a the create operation, the base is nil.
oldChecks = make([]*metav1alpha1.Check, len(newChecks))
Expand All @@ -56,6 +67,7 @@ func WithApprovalOperator(getter ChecksGetter) TransformFunc {
oldObject, _ := base.(runtime.Object)
oldChecks = getter.GetChecks(oldObject)
}

if len(oldChecks) != len(newChecks) {
log.Warnw("unable to add approval operator, length mismatch", "req", req, "old", oldChecks, "new", newChecks)
return
Expand Down

0 comments on commit 9a2f023

Please sign in to comment.