mutating

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2020 License: Apache-2.0 Imports: 13 Imported by: 14

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewWebhook

func NewWebhook(cfg WebhookConfig, mutator Mutator, ot opentracing.Tracer, 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.

func NewChain

func NewChain(logger log.Logger, mutators ...Mutator) *Chain

NewChain returns a new chain.

func (*Chain) Mutate

func (c *Chain) Mutate(ctx context.Context, obj metav1.Object) (bool, error)

Mutate will execute all the mutation chain.

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 (ChainMutatingWebhook)

chainMutatingWebhook shows how you would create a mutator chain.

package main

import (
	"context"

	corev1 "k8s.io/api/core/v1"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	"github.com/slok/kubewebhook/pkg/log"
	"github.com/slok/kubewebhook/pkg/webhook/mutating"
)

func main() {

	fakeMut := mutating.MutatorFunc(func(_ context.Context, obj metav1.Object) (bool, error) {
		return false, nil
	})

	fakeMut2 := mutating.MutatorFunc(func(_ context.Context, obj metav1.Object) (bool, error) {
		return false, nil
	})

	fakeMut3 := mutating.MutatorFunc(func(_ context.Context, obj metav1.Object) (bool, error) {
		return false, nil
	})

	// Create a mutator that is a chain of multiple mutators.
	mutChain := mutating.NewChain(log.Dummy, fakeMut, fakeMut2, fakeMut3)

	// Create webhook (usage of webhook not in this example).
	cfg := mutating.WebhookConfig{
		Name: "podWebhook",
		Obj:  &corev1.Pod{},
	}

	_, _ = mutating.NewWebhook(cfg, mutChain, nil, nil, nil)
}
Output:

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, nil)
}
Output:

Example (TracedMutatingWebhook)

TracedMutatingWebhook shows how you would create a mutating webhook that can be traced and also traces the webhook mutators with Opentracing.

package main

import (
	"context"

	opentracing "github.com/opentracing/opentracing-go"
	corev1 "k8s.io/api/core/v1"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	"github.com/slok/kubewebhook/pkg/webhook/mutating"
)

func main() {
	// Fake the tracer with a noop tracer (this should be the
	// Opentracing tracer implementation).
	tracer := &opentracing.NoopTracer{}

	fakeMut := mutating.MutatorFunc(func(_ context.Context, obj metav1.Object) (bool, error) {
		return false, nil
	})

	// This is optional, if you don't wrap the mutator with a tracing mutator
	// the trace generated by the webhook will not trace the internal chain of
	// mutators, in this case the chain is only this mutator.
	tracedFakeMut := mutating.TraceMutator(tracer, "fakeMutator", fakeMut)

	// Create webhook (usage of webhook not in this example).
	cfg := mutating.WebhookConfig{
		Name: "podWebhook",
		Obj:  &corev1.Pod{},
	}

	// Passing a valid tracer  will trace all the reviews handled by the webhook.
	_, _ = mutating.NewWebhook(cfg, tracedFakeMut, tracer, nil, nil)
}
Output:

func TraceMutator added in v0.2.0

func TraceMutator(tracer opentracing.Tracer, mutatorName string, m Mutator) Mutator

TraceMutator will wrap the mutator and trace the received mutator. for example this helper can be used to trace each of the mutators and get what parts of the mutating chain is the bottleneck.

type MutatorFunc

type MutatorFunc func(context.Context, metav1.Object) (bool, error)

MutatorFunc is a helper type to create mutators from functions.

func (MutatorFunc) Mutate

func (f MutatorFunc) Mutate(ctx context.Context, obj metav1.Object) (bool, error)

Mutate satisfies Mutator interface.

type WebhookConfig

type WebhookConfig struct {
	// Name is the name of the webhook.
	Name string
	// Object is the object of the webhook, to use multiple types on the same webhook or
	// type inference, don't set this field (will be `nil`).
	Obj metav1.Object
}

WebhookConfig is the Mutating webhook configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL