diff

package
v0.0.0-...-a4d2d88 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2024 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

The package provide functions that allows to compare set of Kubernetes resources using the logic equivalent to `kubectl diff`.

Index

Examples

Constants

View Source
const (
	AnnotationLastAppliedConfig = "kubectl.kubernetes.io/last-applied-configuration"
)

Variables

This section is empty.

Functions

func CreateTwoWayMergePatch

func CreateTwoWayMergePatch(orig, new, dataStruct interface{}) ([]byte, bool, error)

CreateTwoWayMergePatch is a helper to construct a two-way merge patch from objects (instead of bytes)

func GetLastAppliedConfigAnnotation

func GetLastAppliedConfigAnnotation(live *unstructured.Unstructured) (*unstructured.Unstructured, error)

func HideSecretData

HideSecretData replaces secret data values in specified target, live secrets and in last applied configuration of live secret with stars. Also preserves differences between target, live and last applied config values. E.g. if all three are equal the values would be replaced with same number of stars. If all the are different then number of stars in replacement should be different.

func Normalize

func Normalize(un *unstructured.Unstructured, opts ...Option)

func NormalizeSecret

func NormalizeSecret(un *unstructured.Unstructured, opts ...Option)

NormalizeSecret mutates the supplied object and encodes stringData to data, and converts nils to empty strings. If the object is not a secret, or is an invalid secret, then returns the same object.

Types

type DiffResult

type DiffResult struct {
	// Modified is set to true if resources are not matching
	Modified bool
	// Contains YAML representation of a live resource with applied normalizations
	NormalizedLive []byte
	// Contains "expected" YAML representation of a live resource
	PredictedLive []byte
}

Holds diffing result of two resources

func Diff

func Diff(config, live *unstructured.Unstructured, opts ...Option) (*DiffResult, error)

Diff performs a diff on two unstructured objects. If the live object happens to have a "kubectl.kubernetes.io/last-applied-configuration", then perform a three way diff.

Example
expectedResource := unstructured.Unstructured{}
if err := yaml.Unmarshal([]byte(`
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - image: nginx:1.7.9
    name: nginx
    resources:
      requests:
        cpu: 0.2
`), &expectedResource); err != nil {
	panic(err)
}

liveResource := unstructured.Unstructured{}
if err := yaml.Unmarshal([]byte(`
apiVersion: v1
kind: Pod
metadata:
  name: my-pod-123
  creationTimestamp: "2020-03-30T21:34:59Z"
  labels:
    pod-template-hash: 84bf9649fd
  name: argo-cd-cli-84bf9649fd-tm59q
  resourceVersion: "233081332"
  uid: 9a5ae31a-eed2-4f82-81fe-833799c54f99
spec:
  containers:
  - image: nginx:1.7.9
    name: nginx
    resources:
      requests:
        cpu: 0.1
`), &liveResource); err != nil {
	panic(err)
}
diff, err := Diff(&expectedResource, &liveResource, diffOptionsForTest()...)
if err != nil {
	panic(err)
}
if diff.Modified {
	fmt.Println("Resources are different")
}
Output:

func ServerSideDiff

func ServerSideDiff(config, live *unstructured.Unstructured, opts ...Option) (*DiffResult, error)

ServerSideDiff will execute a k8s server-side apply in dry-run mode with the given config. The result will be compared with given live resource to determine diff. If config or live are nil it means resource creation or deletion. In this no call will be made to kube-api and a simple diff will be returned.

func StructuredMergeDiff

func StructuredMergeDiff(config, live *unstructured.Unstructured, gvkParser *managedfields.GvkParser, manager string) (*DiffResult, error)

StructuredMergeDiff will calculate the diff using the structured-merge-diff k8s library (https://github.com/kubernetes-sigs/structured-merge-diff).

func ThreeWayDiff

func ThreeWayDiff(orig, config, live *unstructured.Unstructured) (*DiffResult, error)

ThreeWayDiff performs a diff with the understanding of how to incorporate the last-applied-configuration annotation in the diff. Inputs are assumed to be stripped of type information

func TwoWayDiff

func TwoWayDiff(config, live *unstructured.Unstructured) (*DiffResult, error)

TwoWayDiff performs a three-way diff and uses specified config as a recently applied config

type DiffResultList

type DiffResultList struct {
	Diffs    []DiffResult
	Modified bool
}

Holds result of two resources sets comparison

func DiffArray

func DiffArray(configArray, liveArray []*unstructured.Unstructured, opts ...Option) (*DiffResultList, error)

DiffArray performs a diff on a list of unstructured objects. Objects are expected to match environments

type K8sServerSideDryRunner

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

K8sServerSideDryRunner is the Kubernetes implementation of ServerSideDryRunner.

func NewK8sServerSideDryRunner

func NewK8sServerSideDryRunner(kubeApplier KubeApplier) *K8sServerSideDryRunner

NewK8sServerSideDryRunner will instantiate a new K8sServerSideDryRunner with the given kubeApplier.

func (*K8sServerSideDryRunner) Run

ServerSideApplyDryRun will invoke a kubernetes server-side apply with the given obj and the given manager in dryrun mode. Will return the predicted live state json as string.

type KubeApplier

type KubeApplier interface {
	ApplyResource(ctx context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy, force, validate, serverSideApply bool, manager string, serverSideDiff bool) (string, error)
}

type Normalizer

type Normalizer interface {
	Normalize(un *unstructured.Unstructured) error
}

Normalizer updates resource before comparing it

func GetNoopNormalizer

func GetNoopNormalizer() Normalizer

GetNoopNormalizer returns normalizer that does not apply any resource modifications

type Option

type Option func(*options)

func IgnoreAggregatedRoles

func IgnoreAggregatedRoles(ignore bool) Option

func WithGVKParser

func WithGVKParser(parser *managedfields.GvkParser) Option

func WithIgnoreMutationWebhook

func WithIgnoreMutationWebhook(mw bool) Option

func WithLogr

func WithLogr(log logr.Logger) Option

func WithManager

func WithManager(manager string) Option

func WithNormalizer

func WithNormalizer(normalizer Normalizer) Option

func WithServerSideDiff

func WithServerSideDiff(ssd bool) Option

func WithServerSideDryRunner

func WithServerSideDryRunner(ssadr ServerSideDryRunner) Option

func WithStructuredMergeDiff

func WithStructuredMergeDiff(smd bool) Option

type SMDParams

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

SMDParams defines the parameters required by the structuredMergeDiff function

type ServerSideDryRunner

type ServerSideDryRunner interface {
	Run(ctx context.Context, obj *unstructured.Unstructured, manager string) (string, error)
}

ServerSideDryRunner defines the contract to run a server-side apply in dryrun mode.

Directories

Path Synopsis
internal
fieldmanager
Package fieldmanager is a special package as its main purpose is to expose the dependencies required by structured-merge-diff library to calculate diffs when server-side apply option is enabled.
Package fieldmanager is a special package as its main purpose is to expose the dependencies required by structured-merge-diff library to calculate diffs when server-side apply option is enabled.

Jump to

Keyboard shortcuts

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