Documentation ¶
Overview ¶
Package ssa contains utilities for managing Kubernetes resources using sever-side apply. Adapted from https://github.com/stefanprodan/kustomizer/tree/v1.1.0/pkg/manager
Index ¶
- Variables
- func AnyInMetadata(object *unstructured.Unstructured, metadata map[string]string) bool
- func Equals(i, j schema.GroupKind) bool
- func FieldsToSet(f metav1.FieldsV1) (s fieldpath.Set, err error)
- func FmtObjMetadata(obj object.ObjMetadata) string
- func FmtUnstructured(obj *unstructured.Unstructured) string
- func FmtUnstructuredList(objects []*unstructured.Unstructured) string
- func IsClusterDefinition(object *unstructured.Unstructured) bool
- func IsImmutableError(err error) bool
- func IsKubernetesObject(object *unstructured.Unstructured) bool
- func IsKustomization(object *unstructured.Unstructured) bool
- func IsLessThan(i, j schema.GroupKind) bool
- func ObjectToYAML(object *unstructured.Unstructured) string
- func ObjectsToJSON(objects []*unstructured.Unstructured) (string, error)
- func ObjectsToYAML(objects []*unstructured.Unstructured) (string, error)
- func ReadObject(r io.Reader) (*unstructured.Unstructured, error)
- func ReadObjects(r io.Reader) ([]*unstructured.Unstructured, error)
- func SetNativeKindsDefaults(objects []*unstructured.Unstructured) error
- func SetToFields(s fieldpath.Set) (f metav1.FieldsV1, err error)
- type Action
- type ApplyCleanupOptions
- type ApplyOptions
- type ChangeSet
- type ChangeSetEntry
- type DeleteOptions
- type DiffOptions
- type FieldManager
- type KindOrder
- type Owner
- type ResourceManager
- func (m *ResourceManager) Apply(ctx context.Context, object *unstructured.Unstructured, opts ApplyOptions) (*ChangeSetEntry, error)
- func (m *ResourceManager) ApplyAll(ctx context.Context, objects []*unstructured.Unstructured, opts ApplyOptions) (*ChangeSet, error)
- func (m *ResourceManager) ApplyAllStaged(ctx context.Context, objects []*unstructured.Unstructured, opts ApplyOptions) (*ChangeSet, error)
- func (m *ResourceManager) Client() client.Client
- func (m *ResourceManager) Delete(ctx context.Context, object *unstructured.Unstructured, opts DeleteOptions) (*ChangeSetEntry, error)
- func (m *ResourceManager) DeleteAll(ctx context.Context, objects []*unstructured.Unstructured, opts DeleteOptions) (*ChangeSet, error)
- func (m *ResourceManager) Diff(ctx context.Context, object *unstructured.Unstructured, opts DiffOptions) (*ChangeSetEntry, *unstructured.Unstructured, *unstructured.Unstructured, error)
- func (m *ResourceManager) GetOwnerLabels(name, namespace string) map[string]string
- func (m *ResourceManager) SetOwnerLabels(objects []*unstructured.Unstructured, name, namespace string)
- func (m *ResourceManager) Wait(objects []*unstructured.Unstructured, opts WaitOptions) error
- func (m *ResourceManager) WaitForSet(set object.ObjMetadataSet, opts WaitOptions) error
- func (m *ResourceManager) WaitForTermination(objects []*unstructured.Unstructured, opts WaitOptions) error
- type SortableMetas
- type SortableUnstructureds
- type WaitOptions
Constants ¶
This section is empty.
Variables ¶
var ReconcileOrder = KindOrder{ First: []string{ "CustomResourceDefinition", "Namespace", "ClusterClass", "RuntimeClass", "PriorityClass", "StorageClass", "VolumeSnapshotClass", "IngressClass", "GatewayClass", "ResourceQuota", "ServiceAccount", "Role", "ClusterRole", "RoleBinding", "ClusterRoleBinding", "ConfigMap", "Secret", "Service", "LimitRange", "Deployment", "StatefulSet", "CronJob", "PodDisruptionBudget", }, Last: []string{ "MutatingWebhookConfiguration", "ValidatingWebhookConfiguration", }, }
ReconcileOrder holds the list of the Kubernetes native kinds that describes in which order they are reconciled.
Functions ¶
func AnyInMetadata ¶
func AnyInMetadata(object *unstructured.Unstructured, metadata map[string]string) bool
AnyInMetadata searches for the specified key-value pairs in labels and annotations, returns true if at least one key-value pair matches.
func FieldsToSet ¶
FieldsToSet creates a set paths from an input trie of fields
func FmtObjMetadata ¶
func FmtObjMetadata(obj object.ObjMetadata) string
FmtObjMetadata returns the object ID in the format <kind>/<namespace>/<name>.
func FmtUnstructured ¶
func FmtUnstructured(obj *unstructured.Unstructured) string
FmtUnstructured returns the object ID in the format <kind>/<namespace>/<name>.
func FmtUnstructuredList ¶
func FmtUnstructuredList(objects []*unstructured.Unstructured) string
FmtUnstructuredList returns a line per object in the format <kind>/<namespace>/<name>.
func IsClusterDefinition ¶
func IsClusterDefinition(object *unstructured.Unstructured) bool
IsClusterDefinition checks if the given object is a Kubernetes namespace or a custom resource definition.
func IsImmutableError ¶
IsImmutableError checks if the given error is an immutable error.
func IsKubernetesObject ¶
func IsKubernetesObject(object *unstructured.Unstructured) bool
IsKubernetesObject checks if the given object has the minimum required fields to be a Kubernetes object.
func IsKustomization ¶
func IsKustomization(object *unstructured.Unstructured) bool
IsKustomization checks if the given object is a Kustomize config.
func IsLessThan ¶
func ObjectToYAML ¶
func ObjectToYAML(object *unstructured.Unstructured) string
ObjectToYAML encodes the given Kubernetes API object to YAML.
func ObjectsToJSON ¶
func ObjectsToJSON(objects []*unstructured.Unstructured) (string, error)
ObjectsToJSON encodes the given Kubernetes API objects to a YAML multi-doc.
func ObjectsToYAML ¶
func ObjectsToYAML(objects []*unstructured.Unstructured) (string, error)
ObjectsToYAML encodes the given Kubernetes API objects to a YAML multi-doc.
func ReadObject ¶
func ReadObject(r io.Reader) (*unstructured.Unstructured, error)
ReadObject decodes a YAML or JSON document from the given reader into an unstructured Kubernetes API object.
func ReadObjects ¶
func ReadObjects(r io.Reader) ([]*unstructured.Unstructured, error)
ReadObjects decodes the YAML or JSON documents from the given reader into unstructured Kubernetes API objects. The documents which do not subscribe to the Kubernetes Object interface, are silently dropped from the result.
func SetNativeKindsDefaults ¶
func SetNativeKindsDefaults(objects []*unstructured.Unstructured) error
SetNativeKindsDefaults implements workarounds for server-side apply upstream bugs affecting Kubernetes < 1.22 ContainerPort missing default TCP proto: https://github.com/kubernetes-sigs/structured-merge-diff/issues/130 ServicePort missing default TCP proto: https://github.com/kubernetes/kubernetes/pull/98576 PodSpec resources missing int to string conversion for e.g. 'cpu: 2' secret.stringData key replacement add an extra key in the resulting data map: https://github.com/kubernetes/kubernetes/issues/108008
Types ¶
type Action ¶
type Action string
Action represents the action type performed by the reconciliation process.
type ApplyCleanupOptions ¶
type ApplyCleanupOptions struct { // Annotations defines which 'metadata.annotations' keys should be removed from in-cluster objects. Annotations []string `json:"annotations,omitempty"` // Labels defines which 'metadata.labels' keys should be removed from in-cluster objects. Labels []string `json:"labels,omitempty"` // FieldManagers defines which `metadata.managedFields` managers should be removed from in-cluster objects. FieldManagers []FieldManager `json:"fieldManagers,omitempty"` // Exclusions determines which in-cluster objects are skipped from cleanup // based on the specified key-value pairs. Exclusions map[string]string `json:"exclusions"` }
ApplyCleanupOptions defines which metadata entries are to be removed before applying objects.
type ApplyOptions ¶
type ApplyOptions struct { // Force configures the engine to recreate objects that contain immutable field changes. Force bool `json:"force"` // Exclusions determines which in-cluster objects are skipped from apply // based on the specified key-value pairs. // A nil Exclusions map means all objects are applied // regardless of their metadata labels and annotations. Exclusions map[string]string `json:"exclusions"` // WaitTimeout defines after which interval should the engine give up on waiting for // cluster scoped resources to become ready. WaitTimeout time.Duration `json:"waitTimeout"` // Cleanup defines which in-cluster metadata entries are to be removed before applying objects. Cleanup ApplyCleanupOptions `json:"cleanup"` }
ApplyOptions contains options for server-side apply requests.
func DefaultApplyOptions ¶
func DefaultApplyOptions() ApplyOptions
DefaultApplyOptions returns the default apply options where force apply is disabled.
type ChangeSet ¶
type ChangeSet struct {
Entries []ChangeSetEntry
}
ChangeSet holds the result of the reconciliation of an object collection.
func NewChangeSet ¶
func NewChangeSet() *ChangeSet
NewChangeSet returns a ChangeSet will an empty slice of entries.
func (*ChangeSet) Add ¶
func (c *ChangeSet) Add(e ChangeSetEntry)
Add appends the given entry to the end of the slice.
func (*ChangeSet) Append ¶
func (c *ChangeSet) Append(e []ChangeSetEntry)
Append adds the given ChangeSet entries to end of the slice.
func (*ChangeSet) ToObjMetadataSet ¶
func (c *ChangeSet) ToObjMetadataSet() object.ObjMetadataSet
type ChangeSetEntry ¶
type ChangeSetEntry struct { // ObjMetadata holds the unique identifier of this entry. ObjMetadata object.ObjMetadata // GroupVersion holds the API group version of this entry. GroupVersion string // Subject represents the Object ID in the format 'kind/namespace/name'. Subject string // Action represents the action type taken by the reconciler for this object. Action string }
ChangeSetEntry defines the result of an action performed on an object.
func (ChangeSetEntry) String ¶
func (e ChangeSetEntry) String() string
type DeleteOptions ¶
type DeleteOptions struct { // PropagationPolicy determined whether and how garbage collection will be // performed. PropagationPolicy metav1.DeletionPropagation // Inclusions determines which in-cluster objects are subject to deletion // based on the specified key-value pairs. // A nil Inclusions map means all objects are subject to deletion // irregardless of their metadata labels. Inclusions map[string]string // Exclusions determines which in-cluster objects are skipped from deletion // based on the specified key-value pairs. // A nil Exclusions map means all objects are subject to deletion // irregardless of their metadata labels and annotations. Exclusions map[string]string }
DeleteOptions contains options for delete requests.
func DefaultDeleteOptions ¶
func DefaultDeleteOptions() DeleteOptions
DefaultDeleteOptions returns the default delete options where the propagation policy is set to background.
type DiffOptions ¶
type DiffOptions struct { // Exclusions determines which in-cluster objects are skipped from dry-run apply // based on the specified key-value pairs. // A nil Exclusions map means all objects are applied // regardless of their metadata labels and annotations. Exclusions map[string]string `json:"exclusions"` }
DiffOptions contains options for server-side dry-run apply requests.
func DefaultDiffOptions ¶
func DefaultDiffOptions() DiffOptions
DefaultDiffOptions returns the default dry-run apply options.
type FieldManager ¶
type FieldManager struct { // Name is the name of the workflow managing fields. Name string `json:"name"` // OperationType is the type of operation performed by this manager, can be 'update' or 'apply'. OperationType metav1.ManagedFieldsOperationType `json:"operationType"` }
FieldManager identifies a workflow that's managing fields.
type Owner ¶
type Owner struct { // Field sets the field manager name for the given server-side apply patch. Field string // Group sets the owner label key prefix. Group string }
Owner contains options for setting the field manager and ownership labels group.
type ResourceManager ¶
type ResourceManager struct {
// contains filtered or unexported fields
}
ResourceManager reconciles Kubernetes resources onto the target cluster using server-side apply.
func NewResourceManager ¶
func NewResourceManager(client client.Client, poller *polling.StatusPoller, owner Owner) *ResourceManager
NewResourceManager creates a ResourceManager for the given Kubernetes client.
func (*ResourceManager) Apply ¶
func (m *ResourceManager) Apply(ctx context.Context, object *unstructured.Unstructured, opts ApplyOptions) (*ChangeSetEntry, error)
Apply performs a server-side apply of the given object if the matching in-cluster object is different or if it doesn't exist. Drift detection is performed by comparing the server-side dry-run result with the existing object. When immutable field changes are detected, the object is recreated if 'force' is set to 'true'.
func (*ResourceManager) ApplyAll ¶
func (m *ResourceManager) ApplyAll(ctx context.Context, objects []*unstructured.Unstructured, opts ApplyOptions) (*ChangeSet, error)
ApplyAll performs a server-side dry-run of the given objects, and based on the diff result, it applies the objects that are new or modified.
func (*ResourceManager) ApplyAllStaged ¶
func (m *ResourceManager) ApplyAllStaged(ctx context.Context, objects []*unstructured.Unstructured, opts ApplyOptions) (*ChangeSet, error)
ApplyAllStaged extracts the CRDs and Namespaces, applies them with ApplyAll, waits for CRDs and Namespaces to become ready, then is applies all the other objects. This function should be used when the given objects have a mix of custom resource definition and custom resources, or a mix of namespace definitions with namespaced objects.
func (*ResourceManager) Client ¶
func (m *ResourceManager) Client() client.Client
Client returns the underlying controller-runtime client.
func (*ResourceManager) Delete ¶
func (m *ResourceManager) Delete(ctx context.Context, object *unstructured.Unstructured, opts DeleteOptions) (*ChangeSetEntry, error)
Delete deletes the given object (not found errors are ignored).
func (*ResourceManager) DeleteAll ¶
func (m *ResourceManager) DeleteAll(ctx context.Context, objects []*unstructured.Unstructured, opts DeleteOptions) (*ChangeSet, error)
DeleteAll deletes the given set of objects (not found errors are ignored).
func (*ResourceManager) Diff ¶
func (m *ResourceManager) Diff(ctx context.Context, object *unstructured.Unstructured, opts DiffOptions) ( *ChangeSetEntry, *unstructured.Unstructured, *unstructured.Unstructured, error, )
Diff performs a server-side apply dry-un and returns the live and merged objects if drift is detected. If the diff contains Kubernetes Secrets, the data values are masked.
func (*ResourceManager) GetOwnerLabels ¶
func (m *ResourceManager) GetOwnerLabels(name, namespace string) map[string]string
GetOwnerLabels returns a map of labels for the specified name and namespace.
func (*ResourceManager) SetOwnerLabels ¶
func (m *ResourceManager) SetOwnerLabels(objects []*unstructured.Unstructured, name, namespace string)
SetOwnerLabels adds the ownership labels to the given objects. The ownership labels are in the format:
<owner.group>/name: <name> <owner.group>/namespace: <namespace>
func (*ResourceManager) Wait ¶
func (m *ResourceManager) Wait(objects []*unstructured.Unstructured, opts WaitOptions) error
Wait checks if the given set of objects has been fully reconciled.
func (*ResourceManager) WaitForSet ¶
func (m *ResourceManager) WaitForSet(set object.ObjMetadataSet, opts WaitOptions) error
WaitForSet checks if the given set of ObjMetadata has been fully reconciled.
func (*ResourceManager) WaitForTermination ¶
func (m *ResourceManager) WaitForTermination(objects []*unstructured.Unstructured, opts WaitOptions) error
WaitForTermination waits for the given objects to be deleted from the cluster.
type SortableMetas ¶
type SortableMetas []object.ObjMetadata
func (SortableMetas) Len ¶
func (a SortableMetas) Len() int
func (SortableMetas) Less ¶
func (a SortableMetas) Less(i, j int) bool
func (SortableMetas) Swap ¶
func (a SortableMetas) Swap(i, j int)
type SortableUnstructureds ¶
type SortableUnstructureds []*unstructured.Unstructured
func (SortableUnstructureds) Len ¶
func (a SortableUnstructureds) Len() int
func (SortableUnstructureds) Less ¶
func (a SortableUnstructureds) Less(i, j int) bool
func (SortableUnstructureds) Swap ¶
func (a SortableUnstructureds) Swap(i, j int)
type WaitOptions ¶
type WaitOptions struct { // Interval defines how often to poll the cluster for the latest state of the resources. Interval time.Duration // Timeout defines after which interval should the engine give up on waiting for resources // to become ready. Timeout time.Duration }
WaitOptions contains options for wait requests.
func DefaultWaitOptions ¶
func DefaultWaitOptions() WaitOptions
DefaultWaitOptions returns the default wait options where the poll interval is set to five seconds and the timeout to one minute.