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
- func Apply(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)
- func ApplyAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)
- func ApplyAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)
- func ApplyRunOptionsToTarget(target *RunOptions, options ...RunOption) error
- func ApplyYAML(ctx context.Context, filePath string, options ...RunOption) (kObj client.Object, err error)
- func Assert(ctx context.Context, expected client.Object, assertOptions AssertOptions, ...) (result bool, diff string, err error)
- func AssertAllYAMLs(ctx context.Context, filePaths []string, assertOptions AssertOptions, ...) (result bool, diffs []string, err error)
- func AssertEquals(ctx context.Context, expected client.Object, options ...RunOption) (result bool, diff string, err error)
- func AssertEqualsForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) (result bool, diffs []string, err error)
- func AssertEqualsForYAML(ctx context.Context, filePath string, options ...RunOption) (result bool, diff string, err error)
- func AssertIsFound(ctx context.Context, given client.Object, options ...RunOption) (result bool, diff string, err error)
- func AssertIsFoundForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) (result bool, diffs []string, err error)
- func AssertIsFoundForYAML(ctx context.Context, filePath string, options ...RunOption) (result bool, diff string, err error)
- func AssertIsNotFound(ctx context.Context, given client.Object, options ...RunOption) (result bool, diff string, err error)
- func AssertIsNotFoundForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) (result bool, diffs []string, err error)
- func AssertIsNotFoundForYAML(ctx context.Context, filePath string, options ...RunOption) (result bool, diff string, err error)
- func AssertNotEquals(ctx context.Context, given client.Object, options ...RunOption) (result bool, diff string, err error)
- func AssertYAML(ctx context.Context, filePath string, assertOptions AssertOptions, ...) (result bool, diff string, err error)
- func Create(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)
- func CreateAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)
- func CreateForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)
- func CreateForYAML(ctx context.Context, filePath string, options ...RunOption) (kObj client.Object, err error)
- func Delete(ctx context.Context, given client.Object, options ...RunOption) error
- func DeleteAll(ctx context.Context, given []client.Object, options ...RunOption) error
- func DeleteForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) error
- func DeleteForYAML(ctx context.Context, filePath string, options ...RunOption) error
- func DeleteNullInUnstructuredMap(m map[string]interface{}) (map[string]interface{}, error)
- func DeleteNullInUnstructuredSlice(m []interface{}) ([]interface{}, error)
- func DeleteWrapper(ctx context.Context, given client.Object, options ...RunOption) (_ client.Object, err error)
- func DryRun(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)
- func DryRunAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)
- func DryRunAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)
- func DryRunYAML(ctx context.Context, filePath string, options ...RunOption) (kObj client.Object, err error)
- func Get(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)
- func GetAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)
- func GetForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)
- func GetForYAML(ctx context.Context, filePath string, options ...RunOption) (client.Object, error)
- func GetKindVersionForObject(object client.Object, rscheme *runtime.Scheme) (kind string, version string, err error)
- func HasDrifted(ctx context.Context, given client.Object, options ...RunOption) (isDrift bool, drift string, err error)
- func InvokeOperationForAllObjects(ctx context.Context, operation InvokeFn, objects []client.Object, ...) ([]client.Object, error)
- func InvokeOperationForAllYAMLs(ctx context.Context, operation InvokeFn, filePaths []string, ...) ([]client.Object, error)
- func InvokeOperationForYAML(ctx context.Context, operation InvokeFn, filePath string, options ...RunOption) (kObj client.Object, err error)
- func IsEqual(observed, desired client.Object) (bool, error)
- func IsEqualOrDie(observed, desired client.Object) bool
- func IsEqualWithDiffOutput(observed, desired client.Object) (bool, string, error)
- func IsEqualWithMergeOutput(observed, desired client.Object) (bool, *unstructured.Unstructured, error)
- func IsStatusSubResourceSet(obj map[string]interface{}) (bool, error)
- func IsZero(v reflect.Value) bool
- func Noop() *noop
- func RegisterBaseRunOptions(options *RunOptions) error
- func ThreeWayLocalMerge(observed, lastApplied, desired map[string]interface{}) (map[string]interface{}, error)
- func ThreeWayLocalMergeWithTwoObjects(observed, desired map[string]interface{}) (map[string]interface{}, error)
- func ToComparableObjects(observed, desired client.Object) (observedObj, mergedObj *unstructured.Unstructured, err error)
- func Update(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)
- func UpdateAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)
- func UpdateForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)
- func UpdateForYAML(ctx context.Context, filePath string, options ...RunOption) (kObj client.Object, err error)
- func Upsert(ctx context.Context, given client.Object, options ...RunOption) (client.Object, error)
- func UpsertAll(ctx context.Context, given []client.Object, options ...RunOption) ([]client.Object, error)
- func UpsertForAllYAMLs(ctx context.Context, filePaths []string, options ...RunOption) ([]client.Object, error)
- func UpsertForYAML(ctx context.Context, filePath string, options ...RunOption) (kObj client.Object, err error)
- type ActionType
- type AssertOptions
- type AssertType
- type EntityType
- type EventuallyOptions
- type InvokeFn
- type Key
- type OperationResult
- type Registrar
- type RegistrarEntry
- type RunOption
- type RunOptions
- type Runner
- type Validator
Constants ¶
const ManagerName = "wonderland-governance-manager"
Variables ¶
This section is empty.
Functions ¶
func ApplyAllYAMLs ¶
func ApplyRunOptionsToTarget ¶
func ApplyRunOptionsToTarget(target *RunOptions, options ...RunOption) error
ApplyRunOptionsToTarget builds the target instance from the list of provided options
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 AssertEquals ¶
func AssertEqualsForAllYAMLs ¶
func AssertEqualsForYAML ¶
func AssertIsFound ¶
func AssertIsFoundForYAML ¶
func AssertIsNotFound ¶
func AssertIsNotFoundForYAML ¶
func AssertNotEquals ¶
func AssertYAML ¶
func CreateForAllYAMLs ¶
func CreateForYAML ¶
func DeleteForAllYAMLs ¶
func DeleteForYAML ¶
func DeleteNullInUnstructuredMap ¶
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 ¶
DryRun executes a ServerSideApply DryRun invocation
Note: Given object should have its metadata.managedFields set to nil
func DryRunAllYAMLs ¶
func DryRunYAML ¶
func GetForAllYAMLs ¶
func GetForYAML ¶
func GetKindVersionForObject ¶
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 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 ¶
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 ¶
IsEqualOrDie executes IsEqual with an additional task of suspending the observed thread in case of any runtime error
func IsEqualWithDiffOutput ¶
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 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 UpdateForAllYAMLs ¶
func UpdateForYAML ¶
func UpsertForAllYAMLs ¶
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 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 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" )
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 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