mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
refactor: webhook exclusion and unit tests (#4528)
Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
parent
1cacd0173d
commit
8e33532b38
4 changed files with 88 additions and 19 deletions
|
@ -102,7 +102,7 @@ func (h *handlers) Validate(logger logr.Logger, request *admissionv1.AdmissionRe
|
|||
if request.Operation == admissionv1.Delete {
|
||||
h.handleDelete(logger, request)
|
||||
}
|
||||
if excludeKyvernoResources(request.Kind.Kind) {
|
||||
if webhookutils.ExcludeKyvernoResources(request.Kind.Kind) {
|
||||
return admissionutils.ResponseSuccess()
|
||||
}
|
||||
kind := request.Kind.Kind
|
||||
|
@ -161,7 +161,7 @@ func (h *handlers) Validate(logger logr.Logger, request *admissionv1.AdmissionRe
|
|||
}
|
||||
|
||||
func (h *handlers) Mutate(logger logr.Logger, request *admissionv1.AdmissionRequest) *admissionv1.AdmissionResponse {
|
||||
if excludeKyvernoResources(request.Kind.Kind) {
|
||||
if webhookutils.ExcludeKyvernoResources(request.Kind.Kind) {
|
||||
return admissionutils.ResponseSuccess()
|
||||
}
|
||||
if request.Operation == admissionv1.Delete {
|
||||
|
|
|
@ -28,23 +28,6 @@ type updateRequestResponse struct {
|
|||
err error
|
||||
}
|
||||
|
||||
func excludeKyvernoResources(kind string) bool {
|
||||
switch kind {
|
||||
case "ClusterPolicyReport":
|
||||
return true
|
||||
case "PolicyReport":
|
||||
return true
|
||||
case "ReportChangeRequest":
|
||||
return true
|
||||
case "GenerateRequest":
|
||||
return true
|
||||
case "ClusterReportChangeRequest":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func errorResponse(logger logr.Logger, err error, message string) *admissionv1.AdmissionResponse {
|
||||
logger.Error(err, message)
|
||||
return admissionutils.ResponseFailure(message + ": " + err.Error())
|
||||
|
|
18
pkg/webhooks/utils/exclude.go
Normal file
18
pkg/webhooks/utils/exclude.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package utils
|
||||
|
||||
func ExcludeKyvernoResources(kind string) bool {
|
||||
switch kind {
|
||||
case "ClusterPolicyReport":
|
||||
return true
|
||||
case "PolicyReport":
|
||||
return true
|
||||
case "ReportChangeRequest":
|
||||
return true
|
||||
case "GenerateRequest":
|
||||
return true
|
||||
case "ClusterReportChangeRequest":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
68
pkg/webhooks/utils/exclude_test.go
Normal file
68
pkg/webhooks/utils/exclude_test.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestExcludeKyvernoResources(t *testing.T) {
|
||||
type args struct {
|
||||
kind string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{{
|
||||
name: "Policy",
|
||||
args: args{"Policy"},
|
||||
want: false,
|
||||
}, {
|
||||
name: "ClusterPolicy",
|
||||
args: args{"ClusterPolicy"},
|
||||
want: false,
|
||||
}, {
|
||||
name: "ClusterPolicyReport",
|
||||
args: args{"ClusterPolicyReport"},
|
||||
want: true,
|
||||
}, {
|
||||
name: "PolicyReport",
|
||||
args: args{"PolicyReport"},
|
||||
want: true,
|
||||
}, {
|
||||
name: "ReportChangeRequest",
|
||||
args: args{"ReportChangeRequest"},
|
||||
want: true,
|
||||
}, {
|
||||
name: "GenerateRequest",
|
||||
args: args{"GenerateRequest"},
|
||||
want: true,
|
||||
}, {
|
||||
name: "ClusterReportChangeRequest",
|
||||
args: args{"ClusterReportChangeRequest"},
|
||||
want: true,
|
||||
}, {
|
||||
name: "Pod",
|
||||
args: args{"Pod"},
|
||||
want: false,
|
||||
}, {
|
||||
name: "Job",
|
||||
args: args{"Job"},
|
||||
want: false,
|
||||
}, {
|
||||
name: "Deployment",
|
||||
args: args{"Deployment"},
|
||||
want: false,
|
||||
}, {
|
||||
name: "empty",
|
||||
args: args{""},
|
||||
want: false,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ExcludeKyvernoResources(tt.args.kind)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue