diff

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2020 License: Apache-2.0 Imports: 19 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

This section is empty.

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 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 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 added in v0.2.1

type Option func(*options)

func IgnoreAggregatedRoles added in v0.2.1

func IgnoreAggregatedRoles(ignore bool) Option

func WithLogr added in v0.2.1

func WithLogr(log logr.Logger) Option

func WithNormalizer added in v0.2.1

func WithNormalizer(normalizer Normalizer) Option

Jump to

Keyboard shortcuts

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