kubernetes

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: Apache-2.0 Imports: 23 Imported by: 0

README

go-kubernetes

golang utilities for interacting with the kubernetes API.

Development environment

The recommended development environment is Visual Studio Code. A dev container is provided, supporting all required tools.

How to build

Simply run go build in the repository root if you've got golang tooling installed. To build via a docker container, run just build.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ValidationOk     = ValidationResult{Ok: true}
	ValidationFailed = ValidationResult{Ok: false}
)
View Source
var KubernetesManagedFields = FieldCleaner{
	// contains filtered or unexported fields
}

Functions

func EscapeJSONPath

func EscapeJSONPath(path []string) string

EscapeJSONPath converts an array of strings (path elments) to a valid JSONPatch path, escaping special characters if needed. See https://jsonpatch.com/#json-pointer

func NewErrorResponse

func NewErrorResponse(req *admission.AdmissionRequest, message string) *admission.AdmissionResponse

func SplitPathKey

func SplitPathKey(path []string) ([]string, string)

SplitPathKey splits a path array so that the last elemnt is returned as a separate string. The path object itself will not be copied.

func StringToPath

func StringToPath(path string) []string

StringToPath generates a path array from a json path.

Types

type AdmissionRequestHook

type AdmissionRequestHook struct {
	Create ValidationFunc
	Delete ValidationFunc
	Update ValidationFunc
}

AdmissionRequestHook is a helper struct to automaticall map admission operations to functions.

func (AdmissionRequestHook) Call

Call runs the correct callback per requested operation. If an operation does not have a callback registered, an error is reported, but the request is reported as validated.

func (AdmissionRequestHook) Handle

func (h AdmissionRequestHook) Handle(ctx *gin.Context)

Handle reads an admission request, calls the corresponding hook and builds the correct response object.

type Client

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

func NewClient

func NewClient(kubeconfig string) (*Client, error)

NewClient creates a new kubernetes client for a given path to a kubeconfig file. If no file is given, an in-cluster client will be created.

func (*Client) GetNamespacedResourceHandle

func (k8s *Client) GetNamespacedResourceHandle(resource schema.GroupVersionResource, namespace string) dynamic.ResourceInterface

GetNamespacedResource creates an object to interact with a namespaced resource

func (*Client) GetObject

func (k8s *Client) GetObject(resource schema.GroupVersionResource, name, namespace string) (NamespacedObject, error)

GetObject returns a specific kubernetes object

func (*Client) ListAllObjects

func (k8s *Client) ListAllObjects(resource schema.GroupVersionResource, selector string) ([]unstructured.Unstructured, error)

ListAllObjects returns a list of objects for a given type

type FieldCleaner

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

func (FieldCleaner) Clean

func (f FieldCleaner) Clean(obj map[string]interface{}) map[string]interface{}

Remove fields from an existing object

type NamespacedObject

type NamespacedObject map[string]interface{}

func NamespacedObjectFromRaw

func NamespacedObjectFromRaw(data *runtime.RawExtension) (NamespacedObject, error)

NamespacedObjectFromUnstructured converts a raw runtime object intor a namespaced object. If the object does not have name or namespace set an error will be returned.

func NamespacedObjectFromUnstructured

func NamespacedObjectFromUnstructured(unstructuredObj unstructured.Unstructured) (NamespacedObject, error)

NamespacedObjectFromUnstructured converts an unstructured Kubernetes object into a namespaced object. If the object does not have name or namespace set an error will be returned.

func (NamespacedObject) CreateAddPatch

func (obj NamespacedObject) CreateAddPatch(path []string, value interface{}) PatchOperation

CreateAddPatch generates an add patch based.

func (NamespacedObject) CreateRemovePatch

func (obj NamespacedObject) CreateRemovePatch(path []string) PatchOperation

RemoveField generates a remove patch.

func (NamespacedObject) CreateReplacePatch

func (obj NamespacedObject) CreateReplacePatch(path []string, value interface{}) PatchOperation

PatchField generates a replace patch.

func (NamespacedObject) Delete

func (obj NamespacedObject) Delete(path []string, key string) bool

Delete will remove a given key on a given path. If an unindexed array notation is used ("[]") the first matching path will be used, which might lead to the key not being deleted. If the path is not valid because a key in the path does not exist, is no map or array, false will be returned. If the key is deleted or does not exist, true will be returned.

func (NamespacedObject) Find

func (obj NamespacedObject) Find(path []string, key string, value interface{}) [][]string

Find looks for a key inside path with the given value and returns all matching paths. If nil is passed as a value, all pathes containing the key will be returned.

func (NamespacedObject) FindFirst

func (obj NamespacedObject) FindFirst(path []string, key string, value interface{}) []string

FindFirst looks for a key inside path with the given value and returns the first matching path. If nil is passed as a value, the first path with the key set will be returned.

func (NamespacedObject) FixPatchPath

func (obj NamespacedObject) FixPatchPath(path []string, value interface{}) ([]string, interface{})

func (NamespacedObject) Get

func (obj NamespacedObject) Get(path []string, key string) interface{}

Get will return an object for a given path. If the object or any part of the path does not exist, nil is returned. If an unindexed array notation is used ("[]") the first matching path is returned.

func (NamespacedObject) GetAnnotation

func (obj NamespacedObject) GetAnnotation(key string) (string, bool)

GetAnnotation will return the value of a given label. If the annotation is not set, an empty string and false is returned.

func (NamespacedObject) GetLabel

func (obj NamespacedObject) GetLabel(key string) (string, bool)

GetLabel will return the value of a given label. If the label is not set, an empty string and false is returned.

func (NamespacedObject) GetName

func (obj NamespacedObject) GetName() string

GetName will return the name of the object. The name can be a prefix if a pod is processed before it has been processed by the corresponding, e.g., ReplicaSet controller. If the name is not set, an empty string is returned.

func (NamespacedObject) GetNamespace

func (obj NamespacedObject) GetNamespace() string

GetName will return the namespace of the object. If the namespace is not set, an empty string is returned.

func (NamespacedObject) GetOwnerKind

func (obj NamespacedObject) GetOwnerKind() string

GetOwnerKind returns the resource kind of an owning resource, e.g., ReplicaSet if the pod is managed by a ReplicaSet

func (NamespacedObject) GetString

func (obj NamespacedObject) GetString(path []string, key string) (string, bool)

GetString will return a string value assigned to a given key on a given path. If the object is not a string or the path or key does not exist, false is and an empty string returned.

func (NamespacedObject) Has

func (obj NamespacedObject) Has(path []string, key string) bool

Has will return true if a key on a given path is set.

func (NamespacedObject) HasAnnotations

func (obj NamespacedObject) HasAnnotations() bool

HasAnnotations returns true if an annotation section exists

func (NamespacedObject) HasLabels

func (obj NamespacedObject) HasLabels() bool

HasLabels returns true if a labels section exists

func (NamespacedObject) Hash

func (obj NamespacedObject) Hash() (uint64, error)

Hash calculates an ordered hash of the object.

func (NamespacedObject) HashStr

func (obj NamespacedObject) HashStr() (string, error)

Hash calculates an ordered hash of the object an returns a base64 encoded string.

func (NamespacedObject) IsAnnotationNotSetTo

func (obj NamespacedObject) IsAnnotationNotSetTo(key, value string) bool

IsAnnotationNotSetTo checks if a specific annotation is not set to a given value. The comparison is done in a case insensitive way.

func (NamespacedObject) IsAnnotationSetTo

func (obj NamespacedObject) IsAnnotationSetTo(key, value string) bool

IsAnnotationSetTo checks if a specific annotation is set to a given value. The comparison is done in a case insensitive way.

func (NamespacedObject) IsLabelNotSetTo

func (obj NamespacedObject) IsLabelNotSetTo(key, value string) bool

IsLabelNotSetTo checks if a specific label is not set to a given value. The comparison is done in a case insensitive way.

func (NamespacedObject) IsLabelSetTo

func (obj NamespacedObject) IsLabelSetTo(key, value string) bool

IsLabelSetTo checks if a specific label is set to a given value. The comparison is done in a case insensitive way.

func (NamespacedObject) IsOfKind

func (obj NamespacedObject) IsOfKind(kind, apiVersion string) bool

IsOfKind returns true if the object is of the given kind and/or apiVersion. Both kind and apiVersion can be an empty string, which translates to "any"

func (NamespacedObject) RemoveManagedFields

func (obj NamespacedObject) RemoveManagedFields()

RemoveManagedFields removes managed fields from an object. See KubernetesManagedFields and FieldCleaner.

func (NamespacedObject) Set

func (obj NamespacedObject) Set(path []string, key string, value interface{}) bool

Set will set a value for a given key on a given path. The path will be created if not existing. Missing arrays in the path will be created but existing arrays will never be extended. If any part of the path is not a map[string]interface{} or a slice of the former, or the value cannot be set for any other reason, the function will return false.

func (NamespacedObject) SetAnnotation

func (obj NamespacedObject) SetAnnotation(key, value string)

SetAnnotation will set an annotation on the object. It will create the annotations section if it does not exist.

func (NamespacedObject) SetName

func (obj NamespacedObject) SetName(value string)

SetName will set the name of the object.

func (NamespacedObject) SetNamespace

func (obj NamespacedObject) SetNamespace(value string)

SetName will set the namespace of the object.

type ParsedAdmissionRequest

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

func NewParsedAdmissionRequest

func NewParsedAdmissionRequest(gvr schema.GroupVersionResource, name, namespace string, new, old NamespacedObject) ParsedAdmissionRequest

NewParsedAdmissionRequest creates a new ParsedAdmissionRequest from a given resources. This can be used to simulate AdmissionRequests.

func ParseRequest

ParseRequest converts an kubernetes AdmissionRequest into a parsed request.

func (*ParsedAdmissionRequest) GetExistingObject

func (p *ParsedAdmissionRequest) GetExistingObject() (NamespacedObject, error)

GetExistingObject returns the object existing on the cluster. This object is only available on Delete and Update requests.

func (*ParsedAdmissionRequest) GetGroupVersionResource

func (p *ParsedAdmissionRequest) GetGroupVersionResource() schema.GroupVersionResource

GetGroupVersionResource returns the GroupVersionResource assigned to this request.

func (*ParsedAdmissionRequest) GetIncomingJSON

func (p *ParsedAdmissionRequest) GetIncomingJSON() []byte

Returns the incoming object raw json string

func (*ParsedAdmissionRequest) GetIncomingObject

func (p *ParsedAdmissionRequest) GetIncomingObject() (NamespacedObject, error)

GetIncomingObject returns the object to be placed on the cluster. This object is only available on Create and Update requests.

func (*ParsedAdmissionRequest) GetName

func (p *ParsedAdmissionRequest) GetName() string

GetName returns the name assigned to the admission request. This should be equal to GetNewObject().GetName()

func (*ParsedAdmissionRequest) GetNamespace

func (p *ParsedAdmissionRequest) GetNamespace() string

GetNamespace returns the namespace assigned to the admission request.

type PatchOperation

type PatchOperation struct {
	Op    string      `json:"op"`
	Path  string      `json:"path"`
	From  string      `json:"from,omitempty"`
	Value interface{} `json:"value,omitempty"`
}

PatchOperation is an operation of a JSON patch https://tools.ietf.org/html/rfc6902. This is required to report changes back through an admissionreview response.

func NewPatchOperationAdd

func NewPatchOperationAdd(path string, value interface{}) PatchOperation

NewPatchOperationAdd returns an "add" JSON patch operation.

func NewPatchOperationCopy

func NewPatchOperationCopy(from, path string) PatchOperation

NewPatchOperationCopy returns a "copy" JSON patch operation.

func NewPatchOperationMove

func NewPatchOperationMove(from, path string) PatchOperation

NewPatchOperationMove returns a "move" JSON patch operation.

func NewPatchOperationRemove

func NewPatchOperationRemove(path string) PatchOperation

NewPatchOperationRemove returns a "remove" JSON patch operation.

func NewPatchOperationReplace

func NewPatchOperationReplace(path string, value interface{}) PatchOperation

NewPatchOperationReplace returns a "replace" JSON patch operation.

type ValidationFunc

type ValidationFunc func(req ParsedAdmissionRequest) ValidationResult

ValidationFunc callback function prototype for hooks

type ValidationResult

type ValidationResult struct {
	// Ok holds the result of the validation
	Ok bool
	// Message can give additional context on the result
	Message string
	// Patches may hold modifications to be done on the validated object
	Patches []PatchOperation
}

Result of a ValidationFunc.

func (ValidationResult) ToResponse

Jump to

Keyboard shortcuts

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