Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewWebhook ¶
func NewWebhook(cfg WebhookConfig, mutator Mutator, recorder metrics.Recorder, logger log.Logger) (webhook.Webhook, error)
NewWebhook is a mutating webhook and will return a webhook ready for a type of resource. It will mutate the received resources. This webhook will always allow the admission of the resource, only will deny in case of error.
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain is a chain of mutators that will execute secuentially all the mutators that have been added to it. It satisfies Mutator interface.
type Mutator ¶
type Mutator interface {
// Mutate will received a pointr to an object that can be mutated
// mutators are grouped in chains so the mutate method can return
// a stop boolean to stop executing the chain and also an error.
Mutate(context.Context, metav1.Object) (stop bool, err error)
}
Mutator knows how to mutate the received kubernetes object.
Example (PodAnnotateMutatingWebhook) ¶
PodAnnotateMutatingWebhook shows how you would create a pod mutating webhook that adds annotations to every pod received.
package main
import (
"context"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/slok/kubewebhook/pkg/webhook/mutating"
)
func main() {
// Annotations to add.
annotations := map[string]string{
"mutated": "true",
"example": "ExamplePodAnnotateMutatingWebhook",
"framework": "kubewebhook",
}
// Create our mutator that will add annotations to every pod.
pam := mutating.MutatorFunc(func(_ context.Context, obj metav1.Object) (bool, error) {
pod, ok := obj.(*corev1.Pod)
if !ok {
return false, nil
}
// Mutate our object with the required annotations.
if pod.Annotations == nil {
pod.Annotations = make(map[string]string)
}
for k, v := range annotations {
pod.Annotations[k] = v
}
return false, nil
})
// Create webhook (usage of webhook not in this example).
cfg := mutating.WebhookConfig{
Name: "podAnnotateMutatingWebhook",
Obj: &corev1.Pod{},
}
mutating.NewWebhook(cfg, pam, nil, nil)
}
type MutatorFunc ¶
MutatorFunc is a helper type to create mutators from functions.
type WebhookConfig ¶
WebhookConfig is the Mutating webhook configuration.