Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Use pkg/webhook along with build webhook implementation #379

Merged
merged 18 commits into from
Oct 1, 2018

Conversation

shashwathi
Copy link
Contributor

Address #378

Proposed Changes

  • Do not use custom build webhook implementation
  • Implement validate and default methods for all build CRDs and use pkg/wehbook controller.

FYI
I have not added any tests in this PR. If this change does not get 👎 then I will add tests to this PR and also delete custom webhook code.

Goal of this PR is to get conversation started about this change

Lot of validation in build CRDs included calling k8s API to validate object exist like
secrets, build templates. I have not included that change here because I noticed that Serving did not have any similar validations.
My question is does build CRD want that validation still?

Thoughts or concerns @imjasonh @mattmoor @pivotal-nader-ziada ?

@knative-prow-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: shashwathi

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@shashwathi shashwathi changed the title [WIP] Use pkg/webhook instead og build webhook iimplmentation [WIP] Use pkg/webhook instead of build webhook implementation Sep 27, 2018
Copy link
Member

@imjasonh imjasonh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good so far, I think the biggest pain will be moving the full validation into the controller itself.

For now, we might be able to get away with having two webhook admission controllers, this one which does simple checks and uses knative/pkg webhook setup, and our existing one which does more thorough checks and can be phased into the real controller in a future PR.

WDYT?

pkg/apis/build/v1alpha1/build_defaults.go Show resolved Hide resolved
return err
}
return nil
// return validatePlaceholders(tmpl.TemplateSpec().Steps)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was erroneously commented out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was intentional. validatePlaceholders validates the attached template steps and template is obtained by calling kube api. This type of validation wont be in pkg/webhook. For now lets keep this is build webhook controller itself.

}
if bs.Template != nil && len(bs.Steps) > 0 {
return apis.ErrMissingField("b.spec.template").Also(apis.ErrMissingField("b.spec.steps"))
//return validationError("TemplateAndSteps", "build cannot specify both template and steps")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These return validationError lines can just be deleted.

return err
}

// Need kubeclient to fetch template
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think the current thinking is that we'll only do simple validation in the webhook, and leave it up to the controller to do full validation with resolved objects like the template, service account, secrets, etc.

I'm not sure there's a good story yet for surfacing validation errors outside of webhook checks though, we'd probably need to have some new condition type to express the Invalid state.


// ValidateSecrets for build
func (bs *BuildSpec) ValidateSecrets() *apis.FieldError {
// TODO: move this logic into set defaults
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 good call

@shashwathi shashwathi changed the title [WIP] Use pkg/webhook instead of build webhook implementation Use pkg/webhook instead of build webhook implementation Sep 28, 2018
@shashwathi
Copy link
Contributor Author

@imjasonh

I like the idea of 2 controllers for now. Updated code to split the validation between the 2 webhooks. Added tests and cleaned up bunch of common dependencies.

@shashwathi
Copy link
Contributor Author

/assign @imjasonh
/assign @pivotal-nader-ziada

@shashwathi shashwathi changed the title Use pkg/webhook instead of build webhook implementation Use pkg/webhook along with build webhook implementation Sep 28, 2018
Options: pkgoptions,
Handlers: map[schema.GroupVersionKind]pkgwebhook.GenericCRD{
v1alpha1.SchemeGroupVersion.WithKind("Build"): &v1alpha1.Build{},
v1alpha1.SchemeGroupVersion.WithKind("ClusterBuildTemplate"): &v1alpha1.ClusterBuildTemplate{},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you confirm that the common infrastructure works properly with cluster-scoped resources, since AFAIK this is the first one we're trying :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool 👍
I have added e2e test for build with template and build with cluster template as well. Going forward we will have better coverage for these features

)

// DefaultTime is 10min
const DefaultTime = 10 * time.Minute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: DefaultTimeout

package v1alpha1

// SetDefaults for build template
func (b *BuildTemplate) SetDefaults() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just put this into build_template_types.go ? This separate file doesn't add much.

reason: "long name",
tmpl: &BuildTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: "*********************************************************************************************",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings.Repeat("x", maxLength+1) is self-documenting and self-updating if we update maxLength.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed my TODO there. Good catch

}
}

if len(name) > 63 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract this to a const?

seen := map[string]struct{}{}
for _, p := range params {
if _, ok := seen[p.Name]; ok {
return apis.ErrMultipleOneOf("ParamName")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think MultipleOneOf is the correct error, I think that error is intended to communicate "you can specify X or Y, but not both"

https://godoc.org/github.com/knative/pkg/apis#ErrMultipleOneOf

(ErrMissingOneOf is "you must specify X or Y, but not neither")

Maybe ErrInvalidKeyName? https://godoc.org/github.com/knative/pkg/apis#ErrInvalidKeyName

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah pick the error with closest match I guess. I am okay with ErrInvalidKeyName

@@ -0,0 +1,111 @@
package v1alpha1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in filename: cluster_build_template_validation_test.go

reason string // if "", expect success.
}{{
desc: "Single named step",
tmpl: &ClusterBuildTemplate{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can condense both of these test tables into one, and avoid duplication:

func TestValidateBuildTemplate(t *testing.T) {
  for _, c := range []struct {
    desc string
    spec BuildTemplateSpec
    reason string
  } {{
   ...
  }} {
    t.Run("namespaced-"+c.desc, func(t *testing.T) {
      tmpl := &BuildTemplate{Spec: c.spec},
      tmpl.Validate()
      ...
    })
    t.Run("cluster-namespaced-"+c.desc, func(t *testing.T) {
      ctmpl := &ClusterBuildTemplate{Spec: c.spec},
      ctmpl.Validate()
      ...
    })
  }
}

@knative-metrics-robot
Copy link

The following is the coverage report on pkg/.
Say /test pull-knative-build-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/build/v1alpha1/build_defaults.go Do not exist 100.0%
pkg/apis/build/v1alpha1/build_template_types.go 33.3% 66.7% 33.3
pkg/apis/build/v1alpha1/build_template_validation.go Do not exist 94.4%
pkg/apis/build/v1alpha1/build_types.go 77.8% 88.9% 11.1
pkg/apis/build/v1alpha1/build_validation.go Do not exist 96.6%
pkg/apis/build/v1alpha1/cluster_build_template_default.go Do not exist 0.0%
pkg/apis/build/v1alpha1/cluster_build_template_validation.go Do not exist 100.0%
pkg/webhook/build.go 89.6% 89.7% 0.1
pkg/webhook/template_common.go 71.4% 100.0% 28.6
pkg/webhook/webhook.go 25.6% 23.4% -2.2

Copy link
Member

@imjasonh imjasonh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@knative-prow-robot knative-prow-robot merged commit 96c3637 into knative:master Oct 1, 2018
@shashwathi shashwathi deleted the wref branch October 1, 2018 20:14
vdemeester pushed a commit to vdemeester/knative-build that referenced this pull request Apr 3, 2019
* Add validation & defaut for btmpl

* add validation for cluster build template

* Add validation and defaults for build

* Use pkg webhook

* compiles

* Add webhook package

* Move tests

* Add tests

* webhook test

* cleanup unused code

* Revert previous change

* Add e2e tests for build with template and cluster template

* Fix typo

* Fix test typo

* Increase coverage

* Address comments

* Add tests

* Rename file to generic name
vdemeester pushed a commit to vdemeester/knative-build that referenced this pull request Apr 3, 2019
* Add validation & defaut for btmpl

* add validation for cluster build template

* Add validation and defaults for build

* Use pkg webhook

* compiles

* Add webhook package

* Move tests

* Add tests

* webhook test

* cleanup unused code

* Revert previous change

* Add e2e tests for build with template and cluster template

* Fix typo

* Fix test typo

* Increase coverage

* Address comments

* Add tests

* Rename file to generic name
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants