object_patch

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Schemas = map[string]string{
	"v0": `
definitions:
  common:
    type: object
    properties:
      subresource:
        type: string
  create:
    required:
    - object
    properties:
      object:
        oneOf:
        - type: object
          additionalProperties: true
          minProperties: 1
        - type: string
  delete:
    type: object
    required:
    - kind
    - name
    properties:
      apiVersion:
        type: string
      kind:
        type: string
      name:
        type: string
  patch:
    type: object
    required:
    - kind
    - name
    properties:
      apiVersion:
        type: string
      kind:
        type: string
      name:
        type: string
      ignoreMissingObject:
        type: boolean

type: object
additionalProperties: false
properties:
  operation: {}
  namespace: {}
  subresource: {}
  apiVersion: {}
  kind: {}
  name: {}
  object: {}
  jsonPatch: {}
  jqFilter: {}
  mergePatch: {}
  ignoreMissingObject: {}

oneOf:
- allOf:
  - properties:
      operation:
        type: string
        enum: ["Create", "CreateOrUpdate", "CreateIfNotExists"]
  - "$ref": "#/definitions/common"
  - "$ref": "#/definitions/create"
- allOf:
  - properties:
      operation:
        type: string
        enum: ["Delete", "DeleteInBackground", "DeleteNonCascading"]
  - "$ref": "#/definitions/common"
  - "$ref": "#/definitions/delete"
- allOf:
  - oneOf:
    - required:
      - operation
      - jqFilter
      properties:
        operation:
          type: string
          enum: ["JQPatch"]
        jqFilter:
          type: string
          minimum: 1
    - required:
      - operation
      - mergePatch
      properties:
        operation:
          type: string
          enum: ["MergePatch"]
        mergePatch:
          oneOf:
          - type: object
            minProperties: 1
          - type: string
    - required:
      - operation
      - jsonPatch
      properties:
        operation:
          type: string
          enum: ["JSONPatch"]
        jsonPatch:
          oneOf:
          - type: array
            minItems: 1
            items:
            - type: object
              required: ["op", "path", "value"]
              properties:
                op:
                  type: string
                  minLength: 1
                path:
                  type: string
                  minLength: 1
                value: {}
          - type: string
  - "$ref": "#/definitions/common"
  - "$ref": "#/definitions/patch"
`,
}
View Source
var SchemasCache = map[string]*spec.Schema{}

Functions

func GetSchema

func GetSchema(name string) *spec.Schema

GetSchema returns loaded schema.

func IgnoreMissingObject

func IgnoreMissingObject() *ignoreMissingObject

IgnoreMissingObject do not return error if object exists for Patch and Filter operations.

func LoadSchema

func LoadSchema(name string) (*spec.Schema, error)

LoadSchema returns spec.Schema object loaded from yaml in Schemas map.

func WithIgnoreMissingObject

func WithIgnoreMissingObject(ignore bool) *ignoreMissingObject

func WithSubresource

func WithSubresource(s string) *subresourceHolder

WithSubresource options specifies a subresource to operate on.

Types

type CreateOption

type CreateOption interface {
	// contains filtered or unexported methods
}

func IgnoreIfExists

func IgnoreIfExists() CreateOption

IgnoreIfExists is an option for Create to not return error if object is already exists.

func UpdateIfExists

func UpdateIfExists() CreateOption

UpdateIfExists is an option for Create to update object if it already exists.

type DeleteOption

type DeleteOption interface {
	// contains filtered or unexported methods
}

func InBackground

func InBackground() DeleteOption

InBackground is a propagation option for Delete

func InForeground

func InForeground() DeleteOption

InForeground is a default propagation option for Delete

func NonCascading

func NonCascading() DeleteOption

NonCascading is a propagation option for Delete

type FilterOption

type FilterOption interface {
	// contains filtered or unexported methods
}

type KubeClient

type KubeClient interface {
	kubernetes.Interface
	Dynamic() dynamic.Interface
	GroupVersionResource(apiVersion string, kind string) (schema.GroupVersionResource, error)
}

type ObjectPatcher

type ObjectPatcher struct {
	// contains filtered or unexported fields
}

func NewObjectPatcher

func NewObjectPatcher(kubeClient KubeClient) *ObjectPatcher

func (*ObjectPatcher) ExecuteOperation

func (o *ObjectPatcher) ExecuteOperation(operation Operation) error

func (*ObjectPatcher) ExecuteOperations

func (o *ObjectPatcher) ExecuteOperations(ops []Operation) error

type Operation

type Operation interface {
	Description() string
}

Operation is a command for ObjectPatcher.

There are 4 types of operations:

- createOperation to create or update object via Create and Update API calls. Unstructured, map[string]interface{} or runtime.Object is required.

- deleteOperation to delete object via Delete API call. deletionPropagation should be set, default is Foregound.

- patchOperation to modify object via Patch API call. patchType should be set. patch can be string, []byte or map[string]interface{}

- filterOperation to modify object via Get-filter-Update process. filterFunc should be set.

func NewCreateOperation

func NewCreateOperation(obj interface{}, options ...CreateOption) Operation

func NewDeleteOperation

func NewDeleteOperation(apiVersion, kind, namespace, name string, options ...DeleteOption) Operation

func NewFilterPatchOperation

func NewFilterPatchOperation(filter func(*unstructured.Unstructured) (*unstructured.Unstructured, error), apiVersion, kind, namespace, name string, options ...FilterOption) Operation

func NewFromOperationSpec

func NewFromOperationSpec(spec OperationSpec) Operation

func NewJSONPatchOperation

func NewJSONPatchOperation(jsonPatch interface{}, apiVersion, kind, namespace, name string, options ...PatchOption) Operation

func NewMergePatchOperation

func NewMergePatchOperation(mergePatch interface{}, apiVersion, kind, namespace, name string, options ...PatchOption) Operation

func ParseOperations

func ParseOperations(specBytes []byte) ([]Operation, error)

type OperationSpec

type OperationSpec struct {
	Operation   OperationType `json:"operation" yaml:"operation"`
	ApiVersion  string        `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
	Kind        string        `json:"kind,omitempty" yaml:"kind,omitempty"`
	Namespace   string        `json:"namespace,omitempty" yaml:"namespace,omitempty"`
	Name        string        `json:"name,omitempty" yaml:"name,omitempty"`
	Subresource string        `json:"subresource,omitempty" yaml:"subresource,omitempty"`

	Object     interface{} `json:"object,omitempty" yaml:"object,omitempty"`
	JQFilter   string      `json:"jqFilter,omitempty" yaml:"jqFilter,omitempty"`
	MergePatch interface{} `json:"mergePatch,omitempty" yaml:"mergePatch,omitempty"`
	JSONPatch  interface{} `json:"jsonPatch,omitempty" yaml:"jsonPatch,omitempty"`

	IgnoreMissingObject bool `json:"ignoreMissingObject" yaml:"ignoreMissingObject"`
}

A JSON and YAML representation of the operation for shell hooks

type OperationType

type OperationType string
const (
	CreateOrUpdate    OperationType = "CreateOrUpdate"
	Create            OperationType = "Create"
	CreateIfNotExists OperationType = "CreateIfNotExists"

	Delete             OperationType = "Delete"
	DeleteInBackground OperationType = "DeleteInBackground"
	DeleteNonCascading OperationType = "DeleteNonCascading"

	JQPatch    OperationType = "JQPatch"
	MergePatch OperationType = "MergePatch"
	JSONPatch  OperationType = "JSONPatch"
)

type PatchCollector

type PatchCollector struct {
	// contains filtered or unexported fields
}

func NewPatchCollector

func NewPatchCollector() *PatchCollector

NewPatchCollector creates Operation collector to use within Go hooks.

func (*PatchCollector) Create

func (dop *PatchCollector) Create(object interface{}, options ...CreateOption)

Create or update an object.

Options:

  • WithSubresource - create a specified subresource
  • IgnoreIfExists - do not return error if the specified object exists
  • UpdateIfExists - call Update if the specified object exists

func (*PatchCollector) Delete

func (dop *PatchCollector) Delete(apiVersion, kind, namespace, name string, options ...DeleteOption)

Delete uses apiVersion, kind, namespace and name to delete object from cluster.

Options:

  • WithSubresource - delete a specified subresource
  • InForeground - remove object when all dependants are removed (default)
  • InBackground - remove object immediately, dependants remove in background
  • NonCascading - remove object, dependants become orphan

Missing object is ignored by default.

func (*PatchCollector) Filter

func (dop *PatchCollector) Filter(
	filterFunc func(*unstructured.Unstructured) (*unstructured.Unstructured, error),
	apiVersion, kind, namespace, name string, options ...FilterOption,
)

Filter retrieves a specified object, modified it with filterFunc and calls update.

Options:

  • WithSubresource — a subresource argument for Patch call.
  • IgnoreMissingObject — do not return error if the specified object is missing.

Note: do not modify and return argument in filterFunc, use FromUnstructured to instantiate a concrete type or modify after DeepCopy.

func (*PatchCollector) JSONPatch

func (dop *PatchCollector) JSONPatch(jsonPatch interface{}, apiVersion, kind, namespace, name string, options ...PatchOption)

JSONPatch applies a json patch to the specified object using API call Patch.

Options:

  • WithSubresource — a subresource argument for Patch call.
  • IgnoreMissingObject — do not return error if the specified object is missing.

func (*PatchCollector) MergePatch

func (dop *PatchCollector) MergePatch(mergePatch interface{}, apiVersion, kind, namespace, name string, options ...PatchOption)

MergePatch applies a merge patch to the specified object using API call Patch.

Options:

  • WithSubresource — a subresource argument for Patch call.
  • IgnoreMissingObject — do not return error if the specified object is missing.

func (*PatchCollector) Operations

func (dop *PatchCollector) Operations() []Operation

Operations returns all collected operations

type PatchOption

type PatchOption interface {
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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