k8s

package
v0.0.0-...-4d57693 Latest Latest
Warning

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

Go to latest
Published: May 12, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package k8s provides the building blocks that can be used to execute Kubernetes API call and in turn result in building higher order implementations of health checks, policy checks, integration testing, end to end testing, etc.

These are the references which were studied while implementing this package

- https://github.com/kubernetes/community/blob/master/contributors/devel/sig-testing/e2e-tests.md - https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/conformance-tests.md - https://docs.google.com/document/d/11JKqcnUOrw5Lk98f_ylJXBXyxWSW1z3CZu27OLX1CbM/ - E2E Test Framework 2 Design - https://docs.google.com/document/d/1ZtN58kU8SKmDDkxeBKxR9Un76eqhszCVcZuqhs-fLIU/ - K8sClient K8s library - https://cluster-api.sigs.k8s.io/developer/e2e.html - https://www.eficode.com/blog/testing-kubernetes-deployments-within-ci-pipelines - https://blog.mayadata.io/testing-kubernetes-operators - https://rancher.com/blog/2020/kubernetes-security-vulnerabilities - https://github.com/stakater/tronador - On demand dynamic test environments on K8s - https://d2iq.com/blog/running-kind-inside-a-kubernetes-cluster-for-continuous-integration - https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/ - https://github.com/yannh/kubeconform - https://github.com/chaos-mesh/chaos-mesh - https://kodfabrik.com/journal/a-good-makefile-for-go - https://github.com/banzaicloud/k8s-objectmatcher/tree/master/tests - https://github.com/linkerd/linkerd2/tree/main/pkg/healthcheck

Index

Constants

View Source
const ManagerName = "wonderland-governance-manager"

Variables

This section is empty.

Functions

func Apply

func Apply(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)

func ApplyAll

func ApplyAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)

func ApplyAllYAMLs

func ApplyAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)

func ApplyRunOptionsToTarget

func ApplyRunOptionsToTarget(target *RunOptions, options ...RunOption) error

ApplyRunOptionsToTarget builds the target instance from the list of provided options

func ApplyYAML

func ApplyYAML(ctx context.Context, filePath string, options ...RunOption) (kObj client.Object, err error)

func Assert

func Assert(ctx context.Context, expected client.Object, assertOptions AssertOptions, options ...RunOption) (result bool, diff string, err error)

Assert returns true if assertion matches the expectation

Note: The return value `diff` provides the difference between the actual vs desired states

func AssertAllYAMLs

func AssertAllYAMLs(ctx context.Context, filePaths []string, assertOptions AssertOptions, options ...RunOption) (result bool, diffs []string, err error)

func AssertEquals

func AssertEquals(ctx context.Context, expected client.Object, options ...RunOption) (result bool, diff string, err error)

func AssertEqualsForAllYAMLs

func AssertEqualsForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) (result bool, diffs []string, err error)

func AssertEqualsForYAML

func AssertEqualsForYAML(ctx context.Context, filePath string, options ...RunOption) (result bool, diff string, err error)

func AssertIsFound

func AssertIsFound(ctx context.Context, given client.Object, options ...RunOption) (result bool, diff string, err error)

func AssertIsFoundForAllYAMLs

func AssertIsFoundForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) (result bool, diffs []string, err error)

func AssertIsFoundForYAML

func AssertIsFoundForYAML(ctx context.Context, filePath string, options ...RunOption) (result bool, diff string, err error)

func AssertIsNotFound

func AssertIsNotFound(ctx context.Context, given client.Object, options ...RunOption) (result bool, diff string, err error)

func AssertIsNotFoundForAllYAMLs

func AssertIsNotFoundForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) (result bool, diffs []string, err error)

func AssertIsNotFoundForYAML

func AssertIsNotFoundForYAML(ctx context.Context, filePath string, options ...RunOption) (result bool, diff string, err error)

func AssertNotEquals

func AssertNotEquals(ctx context.Context, given client.Object, options ...RunOption) (result bool, diff string, err error)

func AssertYAML

func AssertYAML(ctx context.Context, filePath string, assertOptions AssertOptions, options ...RunOption) (result bool, diff string, err error)

func Create

func Create(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)

func CreateAll

func CreateAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)

func CreateForAllYAMLs

func CreateForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)

func CreateForYAML

func CreateForYAML(ctx context.Context, filePath string, options ...RunOption) (kObj client.Object, err error)

func Delete

func Delete(ctx context.Context, given client.Object, options ...RunOption) error

func DeleteAll

func DeleteAll(ctx context.Context, given []client.Object, options ...RunOption) error

func DeleteForAllYAMLs

func DeleteForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) error

func DeleteForYAML

func DeleteForYAML(ctx context.Context, filePath string, options ...RunOption) error

func DeleteNullInUnstructuredMap

func DeleteNullInUnstructuredMap(m map[string]interface{}) (map[string]interface{}, error)

DeleteNullInUnstructuredMap removes the key value pairs for those value(s) that represent a nil. It also removes the key: value when value of string type is empty i.e "".

Note: This supports Kubernetes compatible unstructured types only

func DeleteNullInUnstructuredSlice

func DeleteNullInUnstructuredSlice(m []interface{}) ([]interface{}, error)

DeleteNullInUnstructuredSlice removes the key value pairs for those value(s) that represent a nil.

Note: This supports Kubernetes compatible unstructured types only

func DeleteWrapper

func DeleteWrapper(ctx context.Context, given client.Object, options ...RunOption) (_ client.Object, err error)

DeleteWrapper invokes delete operation & ensures its signature matches with other invocations like Get, Create, Update, Apply & DryRun.

func DryRun

func DryRun(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)

DryRun executes a ServerSideApply DryRun invocation

Note: Given object should have its metadata.managedFields set to nil

func DryRunAll

func DryRunAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)

func DryRunAllYAMLs

func DryRunAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)

func DryRunYAML

func DryRunYAML(ctx context.Context, filePath string, options ...RunOption) (kObj client.Object, err error)

func Get

func Get(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)

func GetAll

func GetAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)

func GetForAllYAMLs

func GetForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)

func GetForYAML

func GetForYAML(ctx context.Context, filePath string, options ...RunOption) (client.Object, error)

func GetKindVersionForObject

func GetKindVersionForObject(object client.Object, rscheme *runtime.Scheme) (kind string, version string, err error)

func HasDrifted

func HasDrifted(ctx context.Context, given client.Object, options ...RunOption) (isDrift bool, drift string, err error)

HasDrifted returns true if given object differs from the object observed in the cluster

Note: - Object states comparison is a server side implementation i.e. Kubernetes APIs are invoked to determine the comparison result

func InvokeOperationForAllObjects

func InvokeOperationForAllObjects(ctx context.Context, operation InvokeFn, objects []client.Object, options ...RunOption) ([]client.Object, error)

func InvokeOperationForAllYAMLs

func InvokeOperationForAllYAMLs(ctx context.Context, operation InvokeFn, filePaths []string, options ...RunOption) ([]client.Object, error)

InvokeOperationForAllYAMLs executes the passed function against the provided file paths

func InvokeOperationForYAML

func InvokeOperationForYAML(ctx context.Context, operation InvokeFn, filePath string, options ...RunOption) (kObj client.Object, err error)

InvokeOperationForYAML executes the passed function against the provided file path

func IsEqual

func IsEqual(observed, desired client.Object) (bool, error)

IsEqual matches any Kubernetes resource for equality. A match is found if desired object's fields matches the corresponding fields of observed object. Desired object's field values may be an exact match or may be a subset of corresponding values found in observed object.

Note: - Comparison is done on the basis of fields present in the desired object - Comparison is purely a client side implementation i.e. Kubernetes APIs are not involved in the process

func IsEqualOrDie

func IsEqualOrDie(observed, desired client.Object) bool

IsEqualOrDie executes IsEqual with an additional task of suspending the observed thread in case of any runtime error

func IsEqualWithDiffOutput

func IsEqualWithDiffOutput(observed, desired client.Object) (bool, string, error)

IsEqualWithDiffOutput matches any Kubernetes resource for equality. A match is found if desired object's fields matches the corresponding fields of observed object. Desired object's field values may be an exact match or may be a subset of corresponding values found in observed object.

Note: - Comparison is done on the basis of fields present in the desired object - Comparison is purely a client side implementation i.e. Kubernetes APIs are not involved in the process - Diff response is formatted as -observed +merged

func IsEqualWithMergeOutput

func IsEqualWithMergeOutput(observed, desired client.Object) (bool, *unstructured.Unstructured, error)

IsEqualWithMergeOutput matches any Kubernetes resource for equality. A match is found if desired object's fields matches the corresponding fields of observed object. Desired object's field values may be an exact match or may be a subset of corresponding values found in observed object.

Note: - Comparison is done on the basis of fields present in the desired object - Comparison is purely a client side implementation i.e. Kubernetes APIs are not involved in the process

func IsStatusSubResourceSet

func IsStatusSubResourceSet(obj map[string]interface{}) (bool, error)

func IsZero

func IsZero(v reflect.Value) bool

func Noop

func Noop() *noop

func RegisterBaseRunOptions

func RegisterBaseRunOptions(options *RunOptions) error

RegisterBaseRunOptions is used to set default or common run options once instead of specifying them repeatedly across each function invocations

func ThreeWayLocalMerge

func ThreeWayLocalMerge(observed, lastApplied, desired map[string]interface{}) (map[string]interface{}, error)

ThreeWayLocalMerge represents a three-way client side merge

func ThreeWayLocalMergeWithTwoObjects

func ThreeWayLocalMergeWithTwoObjects(observed, desired map[string]interface{}) (map[string]interface{}, error)

ThreeWayLocalMergeWithTwoObjects represents a three-way client side merge

func ToComparableObjects

func ToComparableObjects(observed, desired client.Object) (observedObj, mergedObj *unstructured.Unstructured, err error)

ToComparableObjects merges the provided desired state with the provided observed state to form a merged state. As the function name suggests, this is useful before running DeepEqual check.

Note: - Merge is done on the basis of fields present in the desired object - Merge is purely a client side implementation i.e. Kubernetes APIs are not involved in the process - Merged state differs from the observed state if the desired state is not a subset of the observed state. - Merged state takes care of Kubernetes read only system fields by copying them from the observed state into the merged state

func Update

func Update(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)

func UpdateAll

func UpdateAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)

func UpdateForAllYAMLs

func UpdateForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)

func UpdateForYAML

func UpdateForYAML(ctx context.Context, filePath string, options ...RunOption) (kObj client.Object, err error)

func Upsert

func Upsert(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)

func UpsertAll

func UpsertAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)

func UpsertForAllYAMLs

func UpsertForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)

func UpsertForYAML

func UpsertForYAML(ctx context.Context, filePath string, options ...RunOption) (kObj client.Object, err error)

Types

type ActionType

type ActionType string

ActionType defines the ActionType performed in the step

const (
	// ActionTypeCreate defines a Kubernetes resource create operation
	ActionTypeCreate ActionType = "Create"

	// ActionTypeCreateOrMerge defines a Kubernetes resource create or
	// merge operation
	ActionTypeCreateOrMerge ActionType = "CreateOrMerge"

	// ActionTypeGet defines a Kubernetes resource get operation
	ActionTypeGet ActionType = "Get"

	// ActionTypeDelete defines a Kubernetes resource delete operation
	ActionTypeDelete ActionType = "Delete"

	// ActionTypeUpdate defines a Kubernetes resource update operation
	ActionTypeUpdate ActionType = "Update"
)

type AssertOptions

type AssertOptions struct {
	AssertType     AssertType
	CustomAssertFn func(actual, expected client.Object) (result bool, diff string, err error)
}

type AssertType

type AssertType string

AssertType defines the assertion performed in the step

const (
	// AssertTypeIsEquals defines Equals assertion
	AssertTypeIsEquals AssertType = "Equals"

	// AssertTypeIsNotEquals defines NotEquals assertion
	AssertTypeIsNotEquals AssertType = "NotEquals"

	// AssertTypeIsFound defines IsFound assertion
	AssertTypeIsFound AssertType = "IsFound"

	// AssertTypeIsNotFound defines IsNotFound assertion
	AssertTypeIsNotFound AssertType = "IsNotFound"

	// AssertTypeIsNoop defines a no operation assertion
	//
	// This is the default assertion type when assert
	// is not set
	AssertTypeIsNoop AssertType = "Noop"

	// AssertTypeIsCustom defines a custom assertion
	AssertTypeIsCustom AssertType = "Custom"
)

type EntityType

type EntityType string

EntityType defines the type of Entity

const (
	EntityTypeGarbageCollector EntityType = "gc"
)

type EventuallyOptions

type EventuallyOptions struct {
	RetryInterval    time.Duration
	RetryTimeout     time.Duration
	RetryOnErrorOnly bool
	RetryOnErrorType error
}

type InvokeFn

type InvokeFn func(ctx context.Context, object client.Object, options ...RunOption) (client.Object, error)

type Key

type Key string

Key is used to look up any entity. It is advisable to make name human-readable.

type OperationResult

type OperationResult string

OperationResult is the action result of a CreateOrUpdate call

credit: https://github.com/kubernetes-sigs/controller-runtime/tree/master/pkg/controller/controllerutil

const (
	// OperationResultNone implies that the resource was not changed
	OperationResultNone OperationResult = "unchanged"

	// OperationResultCreated implies that a new resource got created
	OperationResultCreated OperationResult = "created"

	// OperationResultUpdatedResourceOnly implies that an existing resource got updated
	OperationResultUpdatedResourceOnly OperationResult = "updated-resource-only"

	// OperationResultUpdatedResourceAndStatus implies an existing resource as well as its status got updated
	OperationResultUpdatedResourceAndStatus OperationResult = "updated-resource-and-status"
)

func UpsertVerbose

func UpsertVerbose(ctx context.Context, given client.Object, options ...RunOption) (client.Object, OperationResult, error)

type Registrar

type Registrar interface {
	// Get fetches the Runner instance corresponding to
	// the provided key
	Get(key Key) Runner

	// GetKeys fetches all Keys found in the registry
	GetKeys() []Key

	// GetRunners fetches all Runner instances found in
	// the registry
	GetRunners() []Runner

	// Type of entities that this registrar is supposed
	// to store
	Type() EntityType

	// Register the provided Runner
	Register(s Runner) error

	// IsRegistered returns true if the provided key
	// was Store earlier
	IsRegistered(key Key) bool
}

Registrar exposes the contract(s) to store & retrieve Job

type RegistrarEntry

type RegistrarEntry interface {
	// Key of the entry that uniquely identifies name
	// amongst the entries present in the registrar
	Key() Key

	// Type of the entry. Entry's type should match
	// registrar's type.
	Type() EntityType
}

RegistrarEntry defines those entries that can be stored in a registrar

type RunOption

type RunOption interface {
	// ApplyTo sets the provided RunOption instance
	ApplyTo(RunOption) error
}

type RunOptions

type RunOptions struct {
	Client    client.Client
	Clientset *kubernetes.Clientset
	Scheme    *runtime.Scheme

	// Desired state field(s) with null or empty value(s) are considered
	// as valid during Upsert operation
	AcceptNullFieldValuesDuringUpsert *bool

	// SetFinalizersToNullDuringUpsert when true will set the target's
	// finalizers to nil during Upsert operation
	SetFinalizersToNullDuringUpsert *bool
}

RunOptions defines standard runtime options for a Runner

func FromRunOptions

func FromRunOptions(options ...RunOption) (*RunOptions, error)

FromRunOptions creates a new options instance assembled from the provided list of options

func (*RunOptions) ApplyTo

func (o *RunOptions) ApplyTo(target RunOption) error

ApplyTo applies properties from the method receiver to the provided target instance

type Runner

type Runner interface {
	Run(ctx context.Context, opts ...RunOption) error
}

Runner exposes contract(s) to run an entity

type Validator

type Validator interface {
	Validate() error
}

Validator exposes the contract to validate a Runner

Jump to

Keyboard shortcuts

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