Skip to content

Commit

Permalink
check req kind in webhook handle func
Browse files Browse the repository at this point in the history
  • Loading branch information
JessicaGreben committed Jan 28, 2019
1 parent 8dcef36 commit 31897ee
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions pkg/validator/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,27 @@ var _ admission.Handler = &Validator{}

// Handle for Validator to run validation checks.
func (v *Validator) Handle(ctx context.Context, req types.Request) types.Response {
deploy := appsv1.Deployment{}
err := v.decoder.Decode(req, &deploy)
if err == nil {
results := ValidateDeploys(v.Config, &deploy, Results{})
allowed, reason := results.Format()
return admission.ValidationResponse(allowed, reason)
}
var err error
var allowed bool
var reason string
var results Results

pod := corev1.Pod{}
err = v.decoder.Decode(req, &pod)
if err == nil {
results := ValidatePods(v.Config, &pod.Spec, Results{})
allowed, reason := results.Format()
return admission.ValidationResponse(allowed, reason)
switch req.AdmissionRequest.Kind.Kind {
case "Deployment":
deploy := appsv1.Deployment{}
err = v.decoder.Decode(req, &deploy)
results = ValidateDeploys(v.Config, &deploy, Results{})
case "Pod":
pod := corev1.Pod{}
err = v.decoder.Decode(req, &pod)
results = ValidatePods(v.Config, &pod.Spec, Results{})
}
if err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
}
return admission.ErrorResponse(http.StatusBadRequest, err)

allowed, reason = results.Format()
return admission.ValidationResponse(allowed, reason)
}

// ValidateDeploys validates that each deployment conforms to the Fairwinds config.
Expand Down

0 comments on commit 31897ee

Please sign in to comment.