dynamic

package
v0.12.4 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: Apache-2.0 Imports: 29 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGroupEmpty        = errors.New("group must not be empty")
	ErrVersionEmpty      = errors.New("version must not be empty")
	ErrResourceEmpty     = errors.New("resource must not be empty")
	ErrInvalidCreateType = errors.New("type must be string, []byte, metav1.Object, runtime.Object, *unstructured.Unstructured, unstructured.Unstructured or map[string]interface{}")
	ErrInvalidUpdateType = ErrInvalidCreateType
	ErrInvalidApplyType  = ErrInvalidCreateType
	ErrInvalidDeleteType = ErrInvalidCreateType
	ErrInvalidGetType    = ErrInvalidCreateType
	ErrInvalidPatchType  = errors.New("patch type must be string, []byte, metav1.Object, runtime.Object, *unstructured.Unstructured, unstructured.Unstructured or map[string]interface{}")
)

Functions

This section is empty.

Types

type Handler

type Handler struct {
	Options *types.HandlerOptions
	// contains filtered or unexported fields
}

Handler is a handler that have Create()/Update()/Apply()/Path()/Delete()/Get()/ List()/Watch() method to create/update/apply/patch/delete/get/list/watch any kinds of k8s resources already registered in kubernetes API server.

when you create/update/apply/patch/delete/get/list k8s resources, you don't need to consider GroupVersionKind and GroupVersionResource of k8s resources. Provide a yaml file or map[string]interface{} data or runtime.Object to Handler, Handler will do anything that you want to do.

Note: when you delete/get/list k8s resource and the parameter passed to Delete()/Get()/List()/Watch() is a k8s resource name, you should always call WithGVK() to specify the GVK explicitly.

func New

func New(ctx context.Context, kubeconfig string, namespace string) (*Handler, error)

New creates a Handler object from kubeconfig or in-cluster config.

The kubeconfig precedence is: * kubeconfig variable passed. * KUBECONFIG environment variable pointing at a file. * $HOME/.kube/config if exists. * In-cluster config if running in cluster.

func NewOrDie

func NewOrDie(ctx context.Context, kubeconfig string, namespace string) *Handler

NewOrDie creates a Handler object. Panic if there is any error.

func (*Handler) Apply

func (h *Handler) Apply(obj interface{}) (*unstructured.Unstructured, error)

Apply applies unstructured k8s resource from type string, []byte, metav1.Object, runtime.Object, *unstructured.Unstructured, unstructured.Unstructured or map[string]interface{}.

It's not necessary to explicitly specify the GVK or GVR by calling WithGVK(), Apply() will find the GVK and GVR by RESTMapper and apply the k8s resource that defined in yaml file, json file, bytes data, map[string]interface{} or runtime.Object.

func (*Handler) ApplyFromBytes

func (h *Handler) ApplyFromBytes(data []byte) (*unstructured.Unstructured, error)

ApplyFromBytes applies unstructured k8s resource from bytes data.

func (*Handler) ApplyFromFile

func (h *Handler) ApplyFromFile(filename string) (*unstructured.Unstructured, error)

ApplyFromFile applies unstructured k8s resource from yaml or json file.

func (*Handler) ApplyFromMap

func (h *Handler) ApplyFromMap(obj map[string]interface{}) (*unstructured.Unstructured, error)

ApplyFromMap applies unstructured k8s resource from map[string]interface{}.

func (*Handler) ApplyFromObject

func (h *Handler) ApplyFromObject(obj interface{}) (*unstructured.Unstructured, error)

ApplyFromObject applies unstructured k8s resource from metav1.Object or runtime.Object.

func (*Handler) Create

func (h *Handler) Create(obj interface{}) (*unstructured.Unstructured, error)

Create creates unstructured k8s resource from type string, []byte, metav1.Object, runtime.Object, *unstructured.Unstructured, unstructured.Unstructured or map[string]interface{}.

It's not necessary to explicitly specify the GVK or GVR by calling WithGVK(), Create() will find the GVK and GVR by RESTMapper and create the k8s resource that defined in yaml file, json file, bytes data, map[string]interface{} or runtime.Object.

func (*Handler) CreateFromBytes

func (h *Handler) CreateFromBytes(data []byte) (*unstructured.Unstructured, error)

CreateFromBytes creates unstructured k8s resource from bytes data.

func (*Handler) CreateFromFile

func (h *Handler) CreateFromFile(filename string) (*unstructured.Unstructured, error)

CreateFromFile creates unstructured k8s resource from yaml or json file.

func (*Handler) CreateFromMap

func (h *Handler) CreateFromMap(obj map[string]interface{}) (*unstructured.Unstructured, error)

CreateFromMap creates unstructured k8s resource from map[string]interface{}.

func (*Handler) CreateFromObject

func (h *Handler) CreateFromObject(obj interface{}) (*unstructured.Unstructured, error)

CreateFromObject creates unstructured k8s resource from metav1.Object or runtime.Object.

func (*Handler) DeepCopy

func (in *Handler) DeepCopy() *Handler

DeepCopy

func (*Handler) Delete

func (h *Handler) Delete(obj interface{}) error

Delete deletes unstructured k8s resource from type string, []byte, metav1.Object, runtime.Object, *unstructured.Unstructured, unstructured.Unstructured or map[string]interface{}.

If psssed parameter type is string, it will call DeleteByName insteard of DeleteFromFile. You should always explicitly call DeleteFromFile to delete a unstructured object from filename.

DeleteByName requires WithGVK() to explicitly specify the k8s resource's GroupVersionKind. DeleteFromFile, DeleteFromBytes and DeleteFromMap will find GVK and GVR from the provided structured or unstructured data, it's not reuqired to call WithGVK().

func (*Handler) DeleteByName

func (h *Handler) DeleteByName(name string) error

DeleteByName deletes unstructured k8s resource with given name.

func (*Handler) DeleteFromBytes

func (h *Handler) DeleteFromBytes(data []byte) error

DeleteFromBytes deletes unstructured k8s resource from bytes data.

func (*Handler) DeleteFromFile

func (h *Handler) DeleteFromFile(filename string) error

DeleteFromFile deletes unstructured k8s resource from yaml or json file.

func (*Handler) DeleteFromMap

func (h *Handler) DeleteFromMap(obj map[string]interface{}) error

DeleteFromMap deletes unstructured k8s resource from map[string]interface{}.

func (*Handler) DeleteFromObject

func (h *Handler) DeleteFromObject(obj interface{}) error

DeleteFromObject deletes unstructured k8s resource from metav1.Object or runtime.Object.

func (*Handler) DynamicClient

func (h *Handler) DynamicClient() dynamic.Interface

DynamicClient returns the underlying dynamic client used by this dynamic handler.

func (*Handler) GVK added in v0.12.2

func (h *Handler) GVK() schema.GroupVersionKind

GVK return the GroupVersionKind of the k8s object.

func (*Handler) GVR

GVR return the GroupVersionResource of the k8s object.

func (*Handler) Get

func (h *Handler) Get(obj interface{}) (*unstructured.Unstructured, error)

Get gets unstructured k8s resource from type string, []byte, metav1.Object, runtime.Object, *unstructured.Unstructured, unstructured.Unstructured or map[string]interface{}.

If psssed parameter type is string, it will call GetByName insteard of GetFromFile. You should always explicitly call GetFromFile to delete a unstructured object from filename.

GetByName requires WithGVK() to explicitly specify the k8s resource's GroupVersionKind. GetFromFile, GetFromBytes and GetFromMap will find GVK and GVR from the provided structured or unstructured data, it's not reuqired to call WithGVK().

func (*Handler) GetByName

func (h *Handler) GetByName(name string) (*unstructured.Unstructured, error)

GetByName gets unstructured k8s resource with given name.

func (*Handler) GetFromBytes

func (h *Handler) GetFromBytes(data []byte) (*unstructured.Unstructured, error)

GetFromBytes gets unstructured k8s resource from bytes data.

func (*Handler) GetFromFile

func (h *Handler) GetFromFile(filename string) (*unstructured.Unstructured, error)

GetFromFile gets unstructured k8s resource from yaml or json file.

func (*Handler) GetFromMap

func (h *Handler) GetFromMap(obj map[string]interface{}) (*unstructured.Unstructured, error)

GetFromMap gets unstructured k8s resource from map[string]interface{}.

func (*Handler) GetFromObject

func (h *Handler) GetFromObject(obj interface{}) (*unstructured.Unstructured, error)

GetFromObject gets unstructured k8s resource from metav1.Object or runtime.Object.

func (*Handler) InformerFactory added in v0.10.4

InformerFactory returns underlying DyanmicSharedInformerFactory which provides access to a shared informer and lister for dynamic client

func (*Handler) IsNamespaced added in v0.12.2

func (h *Handler) IsNamespaced() bool

IsNamespaced() return true if the k8s object is namespace-scoped or return false.

func (*Handler) Kind added in v0.12.2

func (h *Handler) Kind() string

Kind return the k8s object kind name.

func (*Handler) List

func (h *Handler) List() ([]*unstructured.Unstructured, error)

List list all k8s objects in the k8s cluster, it simply call `ListAll`.

func (*Handler) ListAll added in v0.7.0

func (h *Handler) ListAll() ([]*unstructured.Unstructured, error)

ListAll list all k8s objects in the k8s cluster. Calling this method requires WithGVK() to explicitly specify GVK.

func (*Handler) ListByField added in v0.7.0

func (h *Handler) ListByField(field string) ([]*unstructured.Unstructured, error)

ListByField list k8s objects by field, work like `kubectl get xxx --field-selector=xxx`. Calling this method requires WithGVK() to explicitly specify GVK.

func (*Handler) ListByLabel added in v0.7.0

func (h *Handler) ListByLabel(labels string) ([]*unstructured.Unstructured, error)

ListByLabel list k8s objects by labels. Multiple labels separated by comma(",") eg: "name=myapp,role=devops", and there is an "And" relationship between multiple labels. Calling this method requires WithGVK() to explicitly specify GVK.

func (*Handler) ListByNamespace added in v0.7.0

func (h *Handler) ListByNamespace(namespace string) ([]*unstructured.Unstructured, error)

ListByNamespace list all k8s objects in the specified namespace. It will return empty slice and error if this k8s object is cluster scope. Calling this method requires WithGVK() to explicitly specify GVK.

func (*Handler) Patch added in v0.11.5

func (h *Handler) Patch(original *unstructured.Unstructured, patch interface{}, patchOptions ...types.PatchType) (*unstructured.Unstructured, error)

Patch use the default patch type(Strategic Merge Patch) to patch typed resource or unstructured object. Supported patch types are: "StrategicMergePatchType", "MergePatchType", "JSONPatchType".

Note: if the patched object is Custom Resource, you should calling this method with types.MergePatchType, because "Strategic Merge Patch" does not work with CRD types.

It's not necessary to explicitly specify the GVK or GVR by calling WithGVK(), Patch() will find the GVK and GVR by RESTMapper and patch the k8s resource that defined in original *unstructured.Unstructured.

For further more Strategic Merge patch, see:

https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/#before-you-begin

For a comparison of JSON patch and JSON merge patch, see:

https://erosb.github.io/post/json-patch-vs-merge-patch/

func (*Handler) ResetNamespace added in v0.11.4

func (h *Handler) ResetNamespace(namespace string)

ResetNamespace

func (*Handler) Resource

func (h *Handler) Resource() string

Resource return the k8s object resource name.

func (*Handler) SetForceDelete added in v0.11.0

func (h *Handler) SetForceDelete(force bool)

SetForceDelete

func (*Handler) SetInformerFactoryNamespace added in v0.10.4

func (h *Handler) SetInformerFactoryNamespace(namespace string)

SetInformerFactoryNamespace limit the scope of informer list-and-watch k8s resource. informer list-and-watch all namespace k8s resource by default.

func (*Handler) SetInformerFactoryResyncPeriod added in v0.10.4

func (h *Handler) SetInformerFactoryResyncPeriod(resyncPeriod time.Duration)

SetInformerFactoryResyncPeriod will set informer resync period.

func (*Handler) SetInformerFactoryTweakListOptions added in v0.10.4

func (h *Handler) SetInformerFactoryTweakListOptions(tweakListOptions dynamicinformer.TweakListOptionsFunc)

SetInformerFactoryTweakListOptions sets a custom filter on all listers of the configured SharedInformerFactory.

func (*Handler) SetLimit added in v0.11.0

func (h *Handler) SetLimit(limit int64)

SetLimit

func (*Handler) SetPropagationPolicy added in v0.11.0

func (h *Handler) SetPropagationPolicy(policy string)

SetPropagationPolicy will set the PropagationPolicy. If we delete job or/and cronjob, we should always set the PropagationPolicy to DeletePropagationBackground to delete all pods managed by that job or/and cronjob. Default to "DeletePropagationBackground" to job and/or cronjob.

func (*Handler) SetTimeout added in v0.11.0

func (h *Handler) SetTimeout(timeout int64)

SetTimeout

func (*Handler) Update

func (h *Handler) Update(obj interface{}) (*unstructured.Unstructured, error)

Update updates unstructured k8s resource from type string, []byte, metav1.Object, runtime.Object, *unstructured.Unstructured, unstructured.Unstructured or map[string]interface{}.

It's not necessary to explicitly specify the GVK or GVR by calling WithGVK(), Update() will find the GVK and GVR by RESTMapper and update the k8s resource that defined in yaml file, json file, bytes data, map[string]interface{} or runtime.Object.

func (*Handler) UpdateFromBytes

func (h *Handler) UpdateFromBytes(data []byte) (*unstructured.Unstructured, error)

UpdateFromBytes updates unstructured k8s resource from bytes data.

func (*Handler) UpdateFromFile

func (h *Handler) UpdateFromFile(filename string) (*unstructured.Unstructured, error)

UpdateFromFile updates unstructured k8s resource from yaml or json file.

func (*Handler) UpdateFromMap

func (h *Handler) UpdateFromMap(obj map[string]interface{}) (*unstructured.Unstructured, error)

UpdateFromMap updates unstructured k8s resource from map[string]interface{}.

func (*Handler) UpdateFromObject

func (h *Handler) UpdateFromObject(obj interface{}) (*unstructured.Unstructured, error)

UpdateFromObject updates unstructured k8s resource from metav1.Object or runtime.Object.

func (*Handler) Version

func (h *Handler) Version() string

Version return the k8s object version.

func (*Handler) Watch added in v0.11.0

func (h *Handler) Watch(addFunc, modifyFunc, deleteFunc func(obj interface{})) error

Watch watch all k8s resource with the specified kind. You should always specify the GroupVersionKind with WithGVK() method.

Object as the parameter of addFunc, modifyFunc, deleteFunc:

  • If Event.Type is Added or Modified: the new state of the object.
  • If Event.Type is Deleted: the state of the object immediately before deletion.
  • If Event.Type is Bookmark: the object (instance of a type being watched) where only ResourceVersion field is set. On successful restart of watch from a bookmark resourceVersion, client is guaranteed to not get repeat event nor miss any events.
  • If Event.Type is Error: *api.Status is recommended; other types may make sense depending on context.

func (*Handler) WatchByField added in v0.11.0

func (h *Handler) WatchByField(field string, addFunc, modifyFunc, deleteFunc func(obj interface{})) error

WatchByField watch a single or multiple k8s resources with specified Kind and selected by the field. You should always specify the GroupVersionKind with WithGVK() method.

Object as the parameter of addFunc, modifyFunc, deleteFunc:

  • If Event.Type is Added or Modified: the new state of the object.
  • If Event.Type is Deleted: the state of the object immediately before deletion.
  • If Event.Type is Bookmark: the object (instance of a type being watched) where only ResourceVersion field is set. On successful restart of watch from a bookmark resourceVersion, client is guaranteed to not get repeat event nor miss any events.
  • If Event.Type is Error: *api.Status is recommended; other types may make sense depending on context.

func (*Handler) WatchByLabel added in v0.11.0

func (h *Handler) WatchByLabel(labels string, addFunc, modifyFunc, deleteFunc func(obj interface{})) error

WatchByLabel watch a single or multiple k8s resource with the specified Kind and selected by the labels. Multiple labels are separated by ",", label key and value conjunctaed by "=". You should always specify the GroupVersionKind with WithGVK() method.

Object as the parameter of addFunc, modifyFunc, deleteFunc:

  • If Event.Type is Added or Modified: the new state of the object.
  • If Event.Type is Deleted: the state of the object immediately before deletion.
  • If Event.Type is Bookmark: the object (instance of a type being watched) where only ResourceVersion field is set. On successful restart of watch from a bookmark resourceVersion, client is guaranteed to not get repeat event nor miss any events.
  • If Event.Type is Error: *api.Status is recommended; other types may make sense depending on context.

func (*Handler) WatchByName added in v0.11.0

func (h *Handler) WatchByName(name string, addFunc, modifyFunc, deleteFunc func(obj interface{})) error

WatchByName watch a single k8s resource with the specified Kind. You should always specify the GroupVersionKind with WithGVK() method.

Object as the parameter of addFunc, modifyFunc, deleteFunc:

  • If Event.Type is Added or Modified: the new state of the object.
  • If Event.Type is Deleted: the state of the object immediately before deletion.
  • If Event.Type is Bookmark: the object (instance of a type being watched) where only ResourceVersion field is set. On successful restart of watch from a bookmark resourceVersion, client is guaranteed to not get repeat event nor miss any events.
  • If Event.Type is Error: *api.Status is recommended; other types may make sense depending on context.

func (*Handler) WatchByNamespace added in v0.11.0

func (h *Handler) WatchByNamespace(namespace string, addFunc, modifyFunc, deleteFunc func(obj interface{})) error

WatchByNamespace watch all k8s resource with the specified kind in the specified namespace. You should always specify the GroupVersionKind with WithGVK() method.

Object as the parameter of addFunc, modifyFunc, deleteFunc:

  • If Event.Type is Added or Modified: the new state of the object.
  • If Event.Type is Deleted: the state of the object immediately before deletion.
  • If Event.Type is Bookmark: the object (instance of a type being watched) where only ResourceVersion field is set. On successful restart of watch from a bookmark resourceVersion, client is guaranteed to not get repeat event nor miss any events.
  • If Event.Type is Error: *api.Status is recommended; other types may make sense depending on context.

func (*Handler) WithDryRun added in v0.11.0

func (h *Handler) WithDryRun() *Handler

WithDryRun deep copies a new handler and prints the create/update/apply/delete operations, without sending it to apiserver.

func (*Handler) WithGVK added in v0.11.0

func (h *Handler) WithGVK(gvk schema.GroupVersionKind) *Handler

WithGVK returns the same handler but with provided group, version and resource.

func (*Handler) WithNamespace added in v0.7.0

func (h *Handler) WithNamespace(namespace string) *Handler

WithNamespace returns the same handler but with provided namespace. If the k8s resource is namespace scope, it will create/delete/update/apply k8s resource in the new namespace. If the k8s resource is cluster scope, it will ignore the namespace.

But the namespace defined in yaml file have higher precedence than namespace specified here.

If no namespace is defined in yaml file and no namespace is specified using WithNamespace() method, then the namespace default to metav1.NamespaceDefault("default").

namespace precedence: * namespace defined in yaml file or json file. * namespace specified by WithNamespace() method. * namespace specified in dynamic.New() or dynamic.NewOrDie() funciton. * namespace will be ignored if k8s resource is cluster scope.

Jump to

Keyboard shortcuts

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