object_patch

package
v1.4.15-slog Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: Apache-2.0 Imports: 29 Imported by: 38

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
      ignoreHookError:
        type: boolean

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

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 IgnoreHookError added in v1.4.0

func IgnoreHookError() *ignoreHookError

IgnoreHookError allows applying patches for a Status subresource even if the hook fails

func IgnoreMissingObject added in v1.0.2

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 WithIgnoreHookError added in v1.4.0

func WithIgnoreHookError(ignoreError bool) *ignoreHookError

func WithIgnoreMissingObject added in v1.0.2

func WithIgnoreMissingObject(ignore bool) *ignoreMissingObject

func WithSubresource added in v1.0.2

func WithSubresource(s string) *subresourceHolder

WithSubresource options specifies a subresource to operate on.

Types

type CreateOption added in v1.0.2

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

func IgnoreIfExists added in v1.0.2

func IgnoreIfExists() CreateOption

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

func UpdateIfExists added in v1.0.2

func UpdateIfExists() CreateOption

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

type DeleteOption added in v1.0.2

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

func InBackground added in v1.0.2

func InBackground() DeleteOption

InBackground is a propagation option for Delete

func InForeground added in v1.0.2

func InForeground() DeleteOption

InForeground is a default propagation option for Delete

func NonCascading added in v1.0.2

func NonCascading() DeleteOption

NonCascading is a propagation option for Delete

type FilterOption added in v1.0.2

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

type KubeClient added in v1.0.2

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, logger *unilogger.Logger) *ObjectPatcher

func (*ObjectPatcher) ExecuteOperation added in v1.0.2

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

func (*ObjectPatcher) ExecuteOperations added in v1.0.2

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

type Operation added in v1.0.2

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 GetPatchStatusOperationsOnHookError added in v1.4.0

func GetPatchStatusOperationsOnHookError(operations []Operation) []Operation

GetPatchStatusOperationsOnHookError returns list of Patch/Filter operations eligible for execution on Hook Error

func NewCreateOperation added in v1.0.2

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

func NewDeleteOperation added in v1.0.2

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

func NewFilterPatchOperation added in v1.0.2

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

func NewFromOperationSpec added in v1.0.2

func NewFromOperationSpec(spec OperationSpec) Operation

func NewJSONPatchOperation added in v1.0.2

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

func NewMergePatchOperation added in v1.0.2

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

func ParseOperations added in v1.0.2

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"`
	IgnoreHookError     bool `json:"ignoreHookError" yaml:"ignoreHookError"`
}

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 added in v1.0.2

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

func NewPatchCollector added in v1.0.2

func NewPatchCollector() *PatchCollector

NewPatchCollector creates Operation collector to use within Go hooks.

func (*PatchCollector) Create added in v1.0.2

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 added in v1.0.2

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 added in v1.0.2

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.
  • IgnoreHookError — allows applying patches for a Status subresource even if the hook fails

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

func (*PatchCollector) JSONPatch added in v1.0.2

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.
  • IgnoreHookError — allows applying patches for a Status subresource even if the hook fails

func (*PatchCollector) MergePatch added in v1.0.2

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.
  • IgnoreHookError — allows applying patches for a Status subresource even if the hook fails

func (*PatchCollector) Operations added in v1.0.2

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

Operations returns all collected operations

type PatchOption added in v1.0.2

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