mutating

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

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.

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

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 string
	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