patcher

package
v0.0.0-...-d390798 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2018 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package patcher is heavily inspired by the work done in the Kubernetes kubectl package (https://github.com/kubernetes/kubernetes/tree/master/pkg/kubectl) It has been altered to make it easier to use from an extension point of view.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateApplyAnnotation

func CreateApplyAnnotation(name string, info *resource.Info, codec runtime.Encoder) error

CreateApplyAnnotation gets the modified configuration of the object, without embedding it again, and then sets it on the object as the annotation.

func GetModifiedConfiguration

func GetModifiedConfiguration(name string, info *resource.Info, annotate bool, codec runtime.Encoder) ([]byte, error)

GetModifiedConfiguration retrieves the modified configuration of the object. If annotate is true, it embeds the result as an annotation in the modified configuration. If an object was read from the command input, it will use that version of the object. Otherwise, it will use the version from the server.

func GetOriginalConfiguration

func GetOriginalConfiguration(name string, mapping *meta.RESTMapping, obj runtime.Object) ([]byte, error)

GetOriginalConfiguration retrieves the original configuration of the object from the annotation, or nil if no annotation was found.

func IsEmptyPatch

func IsEmptyPatch(patch []byte) bool

IsEmptyPatch looks at the contents of a patch to see wether or not it is an empty patch and could thus potentially be skipped. JSONMergePatch doesn't always cleanly merge, so we need to set up a set of rules we can ignore.

func SetOriginalConfiguration

func SetOriginalConfiguration(name string, info *resource.Info, original []byte) error

SetOriginalConfiguration sets the original configuration of the object as the annotation on the object for later use in computing a three way patch.

Types

type Config

type Config struct {
	// AllowCreate specifies wether or not we should be able to create the
	// object or not. If this is disabled, when an object does not exist on the
	// server and a patch is requested, Kubekit will return an error.
	// Disabling this could be useful to ensure the `OnUpdate` CRD Handler
	// function only performs updates, not creates.
	// This does not count towards the `Force` option, where we'll delete and
	// re-create an object if there is an error on updating it.
	// Defaults to `true`
	AllowCreate bool

	// AllowUpdate specifies wether or not we should be able to perform updates.
	// If this is disabled and, when an object already exists on the server and
	// a patch is sent, Kubekit will return an error.
	// Disabling this could be useful to ensure the `OnCreate` CRD Handler
	// function only performs create actions.
	// Defaults to `true`
	AllowUpdate bool

	// DeleteFirst enforces us to delete the resource on the server first before
	// trying to patch it. This enforces creating a new resource.
	// This option is provided to enable replacing specific resources like
	// PodDisruptionBudget. These resources can't be updated and need to be
	// recreated to reconfigure.
	// Defaults to `false`
	DeleteFirst bool

	// Force allows Kubekit to delete and re-create the object when there is an
	// error applying the patch.
	// This can come in handy for objects that don't allow updating, like
	// PodDisruptionBudget.
	// Defaults to `false`
	Force bool

	// Validation enables the schema validation before sending it off to the
	// server.
	// Defaults to `false`
	Validation bool

	// Retries resembles the amount of retries we'll execute when we encounter
	// an error applying a patch.
	// Defaults to `5`
	Retries int
	// contains filtered or unexported fields
}

Config represents a set of options that can be passed into an Apply action.

func NewConfig

func NewConfig(opts ...OptionFunc) *Config

NewConfig creates a new configuration. Any options passed in will overwrite the defaults.

func NewFromConfig

func NewFromConfig(c *Config, opts ...OptionFunc) *Config

NewFromConfig creates a new configuration based off of the given configuration and options. The given options will overwrite the specified config.

func (*Config) DeepCopy

func (c *Config) DeepCopy() *Config

DeepCopy copies the entire config object to a new struct.

type Factory

type Factory interface {
	Object() (meta.RESTMapper, runtime.ObjectTyper)
	Validator(validate bool) (validation.Schema, error)
	NewBuilder() *resource.Builder
	ClientSet() (internalclientset.Interface, error)
	JSONEncoder() runtime.Encoder
	Decoder(bool) runtime.Decoder
	OpenAPISchema() (openapi.Resources, error)
}

Factory represents a slimmed down version of the kubectl cmdutil Factory. It is recommended to use this factory to inject into the patcher, but you can provide your own implementation as well.

type OptionFunc

type OptionFunc func(c *Config)

OptionFunc represents a function that can be used to set options for the Apply command.

func DisableCreate

func DisableCreate() OptionFunc

DisableCreate disables creating objects. They can only be updated.

func DisableUpdate

func DisableUpdate() OptionFunc

DisableUpdate disables updating objects. They can only be created.

func DisableValidation

func DisableValidation() OptionFunc

DisableValidation disables the schema validation.

func WithDeleteFirst

func WithDeleteFirst() OptionFunc

WithDeleteFirst will enforce deleting the resource on the server first before attempting to update it. This option is provided to enable replacing specific resources like PodDisruptionBudget. These resources can't be updated and need to be recreated to reconfigure.

func WithForce

func WithForce() OptionFunc

WithForce Delete and re-create the specified object when there is an error and we've retried several times. This can come in handy for objects that don't allow updating, like PodDisruptionBudget.

func WithRetries

func WithRetries(i int) OptionFunc

WithRetries sets the amount of retries we should execute when encountering an error before backing off.

type Patcher

type Patcher struct {
	Factory
	// contains filtered or unexported fields
}

Patcher represents the PatcherObject which is responsible for applying changes to a resource upstream.

func New

func New(name string, f Factory, opts ...OptionFunc) *Patcher

New sets up a new Patcher which can perform a SimpleApply and an Apply. The optional options provided will act as defaults for these operations. Any options given in the specific methods will take president over the defaults.

func (*Patcher) Apply

func (p *Patcher) Apply(obj runtime.Object, opts ...OptionFunc) ([]byte, error)

Apply will take an object and calculate a patch for it to apply to the server. When an object hasn't been created before, the object will be created unless otherwise specified. By using apply, Kubekit will annotate the resource on the server to keep track of applied changes so it can perform a three-way merge.

func (*Patcher) Delete

func (p *Patcher) Delete(obj runtime.Object, opts ...OptionFunc) error

Delete will delete the object from the cluster. It will try and do it gracefully, if that's not possible, it will force deletion.

func (*Patcher) Get

func (p *Patcher) Get(obj interface{}, namespace, name string) error

Get fetches the data for a given object in a given namespace with the given name and loads it into the given object.

type Result

type Result []*resource.Info

Result provides convenience methods for comparing collections of Infos.

func NewResult

func NewResult(cfg *Config, factory Factory, obj runtime.Object) (Result, error)

NewResult creats a new Result set based on the givven mapping and configuration.

func (Result) Visit

func (r Result) Visit(fn resource.VisitorFunc) error

Visit implements resource.Visitor.

Jump to

Keyboard shortcuts

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