pkg/webhook
Ville Aikas f1f36a2c97
Fix subresource update logic. (#2546)
* Fix subresource update logic.

Signed-off-by: Ville Aikas <vaikas@chainguard.dev>

* Rename IsInSubResourceUpdate => GetUpdatedSubresource

Signed-off-by: Ville Aikas <vaikas@chainguard.dev>
2022-07-15 18:32:28 +00:00
..
certificates Introduce `NewContext`, deprecate `NewImplFull`. (#2222) 2021-08-21 14:00:34 -07:00
configmaps Introduce `NewContext`, deprecate `NewImplFull`. (#2222) 2021-08-21 14:00:34 -07:00
json fix a small lint warning (#2270) 2021-09-02 22:55:57 -07:00
psbinding sets mutating webhook ReinvocationPolicy to ifNeeded (#2472) 2022-06-07 15:53:37 +00:00
resourcesemantics Fix subresource update logic. (#2546) 2022-07-15 18:32:28 +00:00
testing Add support for admission webhook warnings. (#2498) 2022-05-02 21:56:57 +00:00
OWNERS Update OWNERS_ALIASES to match autogen in community (#2078) 2021-04-08 07:42:51 -07:00
README.md Update webhook/README.md to reflect current packages (#2062) 2021-03-30 00:33:20 -07:00
admission.go Drop `UserInfo` from logger tagging in webhook. (#2535) 2022-06-29 01:27:30 +00:00
admission_integration_test.go Provide the webhook infrastructure with the raw request context. (#2305) 2021-10-05 13:22:44 -07:00
context.go Streamline `webhook.New`. (#821) 2019-10-28 16:12:11 -07:00
conversion.go Provide the webhook infrastructure with the raw request context. (#2305) 2021-10-05 13:22:44 -07:00
conversion_integration_test.go Drop Client as it is only used in tests (#2203) 2021-07-30 18:05:05 -07:00
env.go add webhookname from env (#1918) 2020-11-20 07:01:52 -08:00
env_test.go Use t.Setenv instead of os.Setenv in tests (#2454) 2022-03-10 10:00:48 -08:00
helper.go Add smart handling of selectors in webhooks (#1949) 2020-12-08 23:38:41 -08:00
helper_test.go Support the webhook serving over non-TLS. (#2204) 2021-08-02 20:22:47 -07:00
stats_reporter.go Spelling (#1797) 2020-10-18 14:22:57 -07:00
stats_reporter_test.go Remove resource_name tag from webhook stats (#1464) 2020-09-21 12:09:49 -07:00
webhook.go allow overriding the default grace period of 45 seconds (#2423) 2022-02-28 11:40:09 -08:00
webhook_integration_test.go Support the webhook serving over non-TLS. (#2204) 2021-08-02 20:22:47 -07:00
webhook_test.go allow overriding the default grace period of 45 seconds (#2423) 2022-02-28 11:40:09 -08:00

README.md

Knative Webhooks

Knative provides infrastructure for authoring webhooks under knative.dev/pkg/webhook and has a few built-in helpers for certain common admission control scenarios. The built-in admission controllers are:

  1. Resource validation and defaulting (builds around apis.Validatable and apis.Defaultable under knative.dev/pkg/apis).
  2. ConfigMap validation, which builds around similar patterns from knative.dev/pkg/configmap (in particular the store concept)

To illustrate standing up the webhook, let's start with one of these built-in admission controllers and then talk about how you can write your own admission controller.

Standing up a Webhook from an Admission Controller

We provide facilities in knative.dev/pkg/injection/sharedmain to try and eliminate much of the boilerplate involved in standing up a webhook. For this example we will show how to stand up the webhook using the built-in admission controller for validating and defaulting resources.

The code to stand up such a webhook looks roughly like this:

// Create a function matching this signature to pass into sharedmain.
func NewResourceAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
	return validation.NewAdmissionController(ctx,
		// Name of the resource webhook (created via yaml)
		fmt.Sprintf("resources.webhook.%s.knative.dev", system.Namespace()),

		// The path on which to serve the webhook.
		"/resource-validation",

		// The resources to validate and default.
		map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
			// List the types to validate, this from knative.dev/sample-controller
			v1alpha1.SchemeGroupVersion.WithKind("AddressableService"): &v1alpha1.AddressableService{},
		},

		// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
		func(ctx context.Context) context.Context {
			// Here is where you would infuse the context with state
			// (e.g. attach a store with configmap data, like knative.dev/serving attaches config-defaults)
			return ctx
		},

		// Whether to disallow unknown fields when parsing the resources' JSON.
		true,
	)
}

func main() {
	// Set up a signal context with our webhook options.
	ctx := webhook.WithOptions(signals.NewContext(), webhook.Options{
		// The name of the Kubernetes service selecting over this deployment's pods.
		ServiceName: "webhook",

		// The port on which to serve.
		Port:        8443,

		// The name of the secret containing certificate data.
		SecretName:  "webhook-certs",
	})

	sharedmain.MainWithContext(ctx, "webhook",
		// The certificate controller will ensure that the named secret (above) has
		// the appropriate shape for our webhook's admission controllers.
		certificates.NewController,

		// This invokes the method defined above to instantiate the resource admission
		// controller.
		NewResourceAdmissionController,
	)
}

There is also a config map validation admission controller built in under knative.dev/pkg/webhook/configmaps.

Writing new Admission Controllers

To implement your own admission controller akin to the resource defaulting and validation controller above, you implement a knative.dev/pkg/controller.Reconciler as with any you would with any other type of controller, but the Reconciler that gets embedded in the *controller.Impl should also implement:

// AdmissionController provides the interface for different admission controllers
type AdmissionController interface {
	// Path returns the path that this particular admission controller serves on.
	Path() string

	// Admit is the callback which is invoked when an HTTPS request comes in on Path().
	Admit(context.Context, *admissionv1beta1.AdmissionRequest) *admissionv1beta1.AdmissionResponse
}

The Reconciler part is responsible for the mutating or validating webhook configuration. The AdmissionController part is responsible for guiding request dispatch (Path()) and handling admission requests (Admit()).