util

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2019 License: Apache-2.0 Imports: 39 Imported by: 0

Documentation

Overview

TODO: consider moving it to a more generic package.

Index

Constants

View Source
const (
	DefaultFederationSystemNamespace = "federation-system"
	MulticlusterPublicNamespace      = "kube-multicluster-public"
	DefaultClusterAvailableDelay     = 20 * time.Second
	DefaultClusterUnavailableDelay   = 60 * time.Second

	KubeAPIQPS              = 20.0
	KubeAPIBurst            = 30
	KubeconfigSecretDataKey = "kubeconfig"

	DefaultLeaderElectionLeaseDuration = 15 * time.Second
	DefaultLeaderElectionRenewDeadline = 10 * time.Second
	DefaultLeaderElectionRetryPeriod   = 5 * time.Second

	FederationConfigName = "federation-v2"
)
View Source
const (
	NoResyncPeriod time.Duration = 0 * time.Second

	NamespaceName = "namespaces"
	NamespaceKind = "Namespace"

	ServiceKind = "Service"

	ServiceAccountKind = "ServiceAccount"

	// Common fields
	SpecField     = "spec"
	MetadataField = "metadata"

	// ServiceAccount fields
	SecretsField = "secrets"

	// Scale types
	ReplicasField       = "replicas"
	RetainReplicasField = "retainReplicas"

	// Template fields
	TemplateField = "template"

	// Placement fields
	PlacementField       = "placement"
	ClusterNamesField    = "clusterNames"
	ClusterSelectorField = "clusterSelector"
	MatchLabelsField     = "matchLabels"

	// Override fields
	OverridesField        = "overrides"
	ClusterNameField      = "clusterName"
	ClusterOverridesField = "clusterOverrides"
	PathField             = "path"
	ValueField            = "value"
)

Providing 0 duration to an informer indicates that resync should be delayed as long as possible

View Source
const (
	OperationTypeAdd    = "add"
	OperationTypeUpdate = "update"
	OperationTypeDelete = "delete"
)

Variables

View Source
var KubeconfigGetterForSecret = func(secret *apiv1.Secret) clientcmd.KubeconfigGetter {
	return func() (*clientcmdapi.Config, error) {
		data, ok := secret.Data[KubeconfigSecretDataKey]
		if !ok {
			return nil, errors.Errorf("secret does not have data with key %s", KubeconfigSecretDataKey)
		}
		return clientcmd.Load(data)
	}
}

KubeconfigGetterForSecret gets the kubeconfig from the given secret. This is to inject a different KubeconfigGetter in tests. We don't use the standard one which calls NewInCluster in tests to avoid having to set up service accounts and mount files with secret tokens.

Functions

func BuildClusterConfig

func BuildClusterConfig(fedCluster *fedv1a1.FederatedCluster, client generic.Client, fedNamespace string, clusterNamespace string) (*restclient.Config, error)

BuildClusterConfig returns a restclient.Config that can be used to configure a client for the given FederatedCluster or an error. The client is used to access kubernetes secrets in the federation namespace and cluster-registry records in the clusterNamespace.

func ConfigMapEquivalent

func ConfigMapEquivalent(s1, s2 *api_v1.ConfigMap) bool

Checks if cluster-independent, user provided data in two given ConfigMaps are equal. If in the future the ConfigMap structure is expanded then any field that is not populated. by the api server should be included here.

func DeepCopyRelevantObjectMeta

func DeepCopyRelevantObjectMeta(obj metav1.ObjectMeta) metav1.ObjectMeta

Deep copies cluster-independent, user provided data from the given ObjectMeta struct. If in the future the ObjectMeta structure is expanded then any field that is not populated by the api server should be included here.

func GetClusterNames

func GetClusterNames(fedObject *unstructured.Unstructured) ([]string, error)

func GetUnstructured added in v0.0.3

func GetUnstructured(resource interface{}) (*unstructured.Unstructured, error)

GetUnstructured return Unstructured for any given kubernetes type

func IsClusterReady added in v0.0.2

func IsClusterReady(cluster *fedv1a1.FederatedCluster) bool

func IsPrimaryCluster

func IsPrimaryCluster(obj, clusterObj pkgruntime.Object) bool

IsPrimaryCluster checks if the caller is working with objects for the primary cluster by checking if the UIDs match for both ObjectMetas passed in. TODO (font): Need to revisit this when cluster ID is available.

func MetaAccessor

func MetaAccessor(obj pkgruntime.Object) metav1.Object

func NewGenericInformer added in v0.0.6

func NewGenericInformer(config *rest.Config, namespace string, obj pkgruntime.Object, resyncPeriod time.Duration, triggerFunc func(pkgruntime.Object)) (cache.Store, cache.Controller, error)

func NewGenericInformerWithEventHandler added in v0.0.7

func NewGenericInformerWithEventHandler(config *rest.Config, namespace string, obj pkgruntime.Object, resyncPeriod time.Duration, resourceEventHandlerFuncs *cache.ResourceEventHandlerFuncs) (cache.Store, cache.Controller, error)

func NewResourceInformer

func NewResourceInformer(client ResourceClient, namespace string, triggerFunc func(pkgruntime.Object)) (cache.Store, cache.Controller)

func NewTriggerOnAllChanges

func NewTriggerOnAllChanges(triggerFunc func(pkgruntime.Object)) *cache.ResourceEventHandlerFuncs

Returns cache.ResourceEventHandlerFuncs that trigger the given function on all object changes.

func ObjFromCache added in v0.0.5

func ObjFromCache(store cache.Store, kind, key string) (*unstructured.Unstructured, error)

func ObjectMetaAndSpecEquivalent

func ObjectMetaAndSpecEquivalent(a, b runtime.Object) bool

Checks if cluster-independent, user provided data in ObjectMeta and Spec in two given top level api objects are equivalent.

func ObjectMetaEquivalent

func ObjectMetaEquivalent(a, b metav1.ObjectMeta) bool

Checks if cluster-independent, user provided data in two given ObjectMeta are equal. If in the future the ObjectMeta structure is expanded then any field that is not populated by the api server should be included here.

func ObjectMetaObjEquivalent

func ObjectMetaObjEquivalent(a, b metav1.Object) bool

Checks if cluster-independent, user provided data in two given ObjectMeta are equal. If in the future the ObjectMeta structure is expanded then any field that is not populated by the api server should be included here.

func ObjectNeedsUpdate added in v0.0.6

func ObjectNeedsUpdate(desiredObj, clusterObj *unstructured.Unstructured, recordedVersion string) bool

ObjectNeedsUpdate determines whether the 2 objects provided cluster object needs to be updated according to the desired object and the recorded version.

func ObjectVersion added in v0.0.6

func ObjectVersion(clusterObj *unstructured.Unstructured) string

ObjectVersion retrieves the field type-prefixed value used for determining currency of the given cluster object.

func PropagatedVersionStatusEquivalent

func PropagatedVersionStatusEquivalent(pvs1, pvs2 *fedv1a1.PropagatedVersionStatus) bool

PropagatedVersionStatusEquivalent returns true if both statuses are equal by comparing Template and Override version, and their ClusterVersion slices; false otherwise.

func SecretEquivalent

func SecretEquivalent(s1, s2 api_v1.Secret) bool

Checks if cluster-independent, user provided data in two given Secrets are equal. If in the future the Secret structure is expanded then any field that is not populated. by the api server should be included here.

func SetClusterNames

func SetClusterNames(fedObject *unstructured.Unstructured, clusterNames []string) error

func SetOverrides added in v0.0.4

func SetOverrides(fedObject *unstructured.Unstructured, overridesMap OverridesMap) error

SetOverrides sets the spec.overrides field of the unstructured object from the provided overrides map.

func SortClusterVersions

func SortClusterVersions(versions []fedv1a1.ClusterObjectVersion)

SortClusterVersions ASCII sorts the given cluster versions slice based on cluster name.

func StartBackoffGC

func StartBackoffGC(backoff *flowcontrol.Backoff, stopCh <-chan struct{})

func UnstructuredToInterface added in v0.0.6

func UnstructuredToInterface(rawObj *unstructured.Unstructured, obj interface{}) error

UnstructuredToInterface converts an unstructured object to the provided interface by json marshalling/unmarshalling.

Types

type ClusterLifecycleHandlerFuncs

type ClusterLifecycleHandlerFuncs struct {
	// Fired when the cluster becomes available.
	ClusterAvailable func(*fedv1a1.FederatedCluster)
	// Fired when the cluster becomes unavailable. The second arg contains data that was present
	// in the cluster before deletion.
	ClusterUnavailable func(*fedv1a1.FederatedCluster, []interface{})
}

A structure with cluster lifecycle handler functions. Cluster is available (and ClusterAvailable is fired) when it is created in federated etcd and ready. Cluster becomes unavailable (and ClusterUnavailable is fired) when it is either deleted or becomes not ready. When cluster spec (IP)is modified both ClusterAvailable and ClusterUnavailable are fired.

type ClusterOverride added in v0.0.3

type ClusterOverride struct {
	Path  string      `json:"path"`
	Value interface{} `json:"value"`
}

type ClusterOverridesMap added in v0.0.4

type ClusterOverridesMap map[string]interface{}

Mapping of qualified path (e.g. spec.replicas) to value

type ControllerConfig added in v0.0.3

type ControllerConfig struct {
	FederationNamespaces
	KubeConfig              *restclient.Config
	ClusterAvailableDelay   time.Duration
	ClusterUnavailableDelay time.Duration
	MinimizeLatency         bool
}

ControllerConfig defines the configuration common to federation controllers.

func (*ControllerConfig) LimitedScope added in v0.0.6

func (c *ControllerConfig) LimitedScope() bool

type DelayingDeliverer

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

A structure that pushes the items to the target channel at a given time.

func NewDelayingDeliverer

func NewDelayingDeliverer() *DelayingDeliverer

func NewDelayingDelivererWithChannel

func NewDelayingDelivererWithChannel(targetChannel chan *DelayingDelivererItem) *DelayingDeliverer

func (*DelayingDeliverer) DeliverAfter

func (d *DelayingDeliverer) DeliverAfter(key string, value interface{}, delay time.Duration)

Delivers value after the given delay.

func (*DelayingDeliverer) DeliverAt

func (d *DelayingDeliverer) DeliverAt(key string, value interface{}, deliveryTime time.Time)

Delivers value at the given time.

func (*DelayingDeliverer) GetTargetChannel

func (d *DelayingDeliverer) GetTargetChannel() chan *DelayingDelivererItem

Gets target channel of the deliverer.

func (*DelayingDeliverer) Start

func (d *DelayingDeliverer) Start()

Starts the DelayingDeliverer.

func (*DelayingDeliverer) StartWithHandler

func (d *DelayingDeliverer) StartWithHandler(handler func(*DelayingDelivererItem))

Starts Delaying deliverer with a handler listening on the target channel.

func (*DelayingDeliverer) Stop

func (d *DelayingDeliverer) Stop()

Stops the DelayingDeliverer. Undelivered items are discarded.

type DelayingDelivererItem

type DelayingDelivererItem struct {
	// Key under which the value was added to deliverer.
	Key string
	// Value of the item.
	Value interface{}
	// When the item should be delivered.
	DeliveryTime time.Time
}

DelayingDelivererItem is structure delivered by DelayingDeliverer to the target channel.

type FederatedInformer

type FederatedInformer interface {
	FederationView

	// Returns a store created over all stores from target informers.
	GetTargetStore() FederatedReadOnlyStore

	// Starts all the processes.
	Start()

	// Stops all the processes inside the informer.
	Stop()
}

A structure that combines an informer running against federated api server and listening for cluster updates with multiple Kubernetes API informers (called target informers) running against federation members. Whenever a new cluster is added to the federation an informer is created for it using TargetInformerFactory. Informers are stopped when a cluster is either put offline of deleted. It is assumed that some controller keeps an eye on the cluster list and thus the clusters in ETCD are up to date.

func NewFederatedInformer

func NewFederatedInformer(
	config *ControllerConfig,
	client generic.Client,
	apiResource *metav1.APIResource,
	triggerFunc func(pkgruntime.Object),
	clusterLifecycle *ClusterLifecycleHandlerFuncs) (FederatedInformer, error)

Builds a FederatedInformer for the given federation client and factory.

type FederatedInformerForTestOnly

type FederatedInformerForTestOnly interface {
	FederatedInformer

	SetClientFactory(func(*fedv1a1.FederatedCluster) (ResourceClient, error))
}

FederatedInformer with extra method for setting fake clients.

type FederatedObject

type FederatedObject struct {
	Object      interface{}
	ClusterName string
}

An object with an origin information.

type FederatedOperation

type FederatedOperation struct {
	Type        FederatedOperationType
	ClusterName string
	Obj         pkgruntime.Object
	Key         string
}

FederatedOperation definition contains type (add/update/delete) and the object itself.

type FederatedOperationHandler

type FederatedOperationHandler func(ResourceClient, pkgruntime.Object) (string, error)

A function that executes some operation using the passed client and object.

type FederatedOperationType

type FederatedOperationType string

Type of the operation that can be executed in Federated.

type FederatedReadOnlyStore

type FederatedReadOnlyStore interface {
	// Returns all items in the store.
	List() ([]FederatedObject, error)

	// Returns all items from a cluster.
	ListFromCluster(clusterName string) ([]interface{}, error)

	// GetKeyFor returns the key under which the item would be put in the store.
	GetKeyFor(item interface{}) string

	// GetByKey returns the item stored under the given key in the specified cluster (if exist).
	GetByKey(clusterName string, key string) (interface{}, bool, error)

	// Returns the items stored under the given key in all clusters.
	GetFromAllClusters(key string) ([]FederatedObject, error)

	// Checks whether stores for all clusters form the lists (and only these) are there and
	// are synced. This is only a basic check whether the data inside of the store is usable.
	// It is not a full synchronization/locking mechanism it only tries to ensure that out-of-sync
	// issues occur less often.	All users of the interface should assume
	// that there may be significant delays in content updates of all kinds and write their
	// code that it doesn't break if something is slightly out-of-sync.
	ClustersSynced(clusters []*fedv1a1.FederatedCluster) bool
}

FederatedReadOnlyStore is an overlay over multiple stores created in federated clusters.

type FederatedResource added in v0.0.3

type FederatedResource struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	ClusterStatus []ResourceClusterStatus `json:"clusterStatus,omitempty"`
}

FederatedResource is a generic representation of a federated type

type FederatedUpdater

type FederatedUpdater interface {
	// Executes the given set of operations.
	Update([]FederatedOperation) (map[string]string, []error)
}

A helper that executes the given set of updates on federation, in parallel.

func NewFederatedUpdater

func NewFederatedUpdater(federation FederationView, kind string, timeout time.Duration, recorder record.EventRecorder, add, update, del FederatedOperationHandler) FederatedUpdater

type FederationNamespaces added in v0.0.3

type FederationNamespaces struct {
	FederationNamespace string
	ClusterNamespace    string
	TargetNamespace     string
}

FederationNamespaces defines the namespace configuration shared by most federation controllers.

type FederationView

type FederationView interface {
	// GetClientForCluster returns a client for the cluster, if present.
	GetClientForCluster(clusterName string) (ResourceClient, error)

	// GetUnreadyClusters returns a list of all clusters that are not ready yet.
	GetUnreadyClusters() ([]*fedv1a1.FederatedCluster, error)

	// GetReadyClusters returns all clusters for which the sub-informers are run.
	GetReadyClusters() ([]*fedv1a1.FederatedCluster, error)

	// GetReadyCluster returns the cluster with the given name, if found.
	GetReadyCluster(name string) (*fedv1a1.FederatedCluster, bool, error)

	// ClustersSynced returns true if the view is synced (for the first time).
	ClustersSynced() bool
}

An interface to access federation members and clients.

type GenericOverride added in v0.0.5

type GenericOverride struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec *GenericOverrideSpec `json:"spec,omitempty"`
}

type GenericOverrideItem added in v0.0.5

type GenericOverrideItem struct {
	ClusterName      string            `json:"clusterName"`
	ClusterOverrides []ClusterOverride `json:"clusterOverrides,omitempty"`
}

type GenericOverrideSpec added in v0.0.5

type GenericOverrideSpec struct {
	Overrides []GenericOverrideItem `json:"overrides,omitempty"`
}

type GenericPlacement added in v0.0.4

type GenericPlacement struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec GenericPlacementSpec `json:"spec,omitempty"`
}

type GenericPlacementFields added in v0.0.6

type GenericPlacementFields struct {
	ClusterNames    []string              `json:"clusterNames,omitempty"`
	ClusterSelector *metav1.LabelSelector `json:"clusterSelector,omitempty"`
}

type GenericPlacementSpec added in v0.0.4

type GenericPlacementSpec struct {
	Placement GenericPlacementFields `json:"placement,omitempty"`
}

TODO(marun) Consider removing this intermediate field. It is only used for grouping.

type LeaderElectionConfiguration added in v0.0.7

type LeaderElectionConfiguration struct {
	// leaseDuration is the duration that non-leader candidates will wait
	// after observing a leadership renewal until attempting to acquire
	// leadership of a led but unrenewed leader slot. This is effectively the
	// maximum duration that a leader can be stopped before it is replaced
	// by another candidate. This is only applicable if leader election is
	// enabled.
	LeaseDuration time.Duration
	// renewDeadline is the interval between attempts by the acting master to
	// renew a leadership slot before it stops leading. This must be less
	// than or equal to the lease duration. This is only applicable if leader
	// election is enabled.
	RenewDeadline time.Duration
	// retryPeriod is the duration the clients should wait between attempting
	// acquisition and renewal of a leadership. This is only applicable if
	// leader election is enabled.
	RetryPeriod time.Duration
	// resourceLock indicates the resource object type that will be used to lock
	// during leader election cycles.
	ResourceLock string
}

LeaderElectionConfiguration defines the configuration of leader election clients for controller that can run with leader election enabled.

type OverridesMap added in v0.0.4

type OverridesMap map[string]ClusterOverridesMap

Mapping of clusterName to overrides for the cluster

func GetOverrides added in v0.0.4

func GetOverrides(rawObj *unstructured.Unstructured) (OverridesMap, error)

GetOverrides returns a map of overrides populated from the given unstructured object.

func (OverridesMap) ToUnstructuredSlice added in v0.0.4

func (m OverridesMap) ToUnstructuredSlice() []interface{}

ToUnstructuredSlice converts the map of overrides to a slice of interfaces that can be set in an unstructured object.

type PlacementDirective added in v0.0.4

type PlacementDirective struct {
	ClusterNames    []string
	ClusterSelector labels.Selector
}

func GetPlacementDirective added in v0.0.4

func GetPlacementDirective(resource *unstructured.Unstructured) (*PlacementDirective, error)

type QualifiedName

type QualifiedName struct {
	Namespace string
	Name      string
}

func NewQualifiedName

func NewQualifiedName(obj pkgruntime.Object) QualifiedName

func (QualifiedName) String

func (n QualifiedName) String() string

String returns the general purpose string representation

type ReconcileFunc added in v0.0.2

type ReconcileFunc func(qualifiedName QualifiedName) ReconciliationStatus

type ReconcileWorker added in v0.0.2

type ReconcileWorker interface {
	Enqueue(qualifiedName QualifiedName)
	EnqueueForClusterSync(qualifiedName QualifiedName)
	EnqueueForError(qualifiedName QualifiedName)
	EnqueueForRetry(qualifiedName QualifiedName)
	EnqueueObject(obj pkgruntime.Object)
	EnqueueWithDelay(qualifiedName QualifiedName, delay time.Duration)
	Run(stopChan <-chan struct{})
	SetDelay(retryDelay, clusterSyncDelay time.Duration)
}

func NewReconcileWorker added in v0.0.2

func NewReconcileWorker(reconcile ReconcileFunc, timing WorkerTiming) ReconcileWorker

type ReconciliationStatus

type ReconciliationStatus int
const (
	StatusAllOK ReconciliationStatus = iota
	StatusNeedsRecheck
	StatusError
	StatusNotSynced
)

type ResourceClient

type ResourceClient interface {
	Resources(namespace string) dynamic.ResourceInterface
	Kind() string
}

func NewResourceClient

func NewResourceClient(config *rest.Config, apiResource *metav1.APIResource) (ResourceClient, error)

type ResourceClusterStatus added in v0.0.3

type ResourceClusterStatus struct {
	ClusterName string                 `json:"clusterName,omitempty"`
	Status      map[string]interface{} `json:"status,omitempty"`
}

ResourceClusterStatus defines the status of federated resource within a cluster

type SafeMap added in v0.0.7

type SafeMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewSafeMap added in v0.0.7

func NewSafeMap() *SafeMap

func (*SafeMap) Delete added in v0.0.7

func (s *SafeMap) Delete(key string)

func (*SafeMap) DeleteAll added in v0.0.8

func (s *SafeMap) DeleteAll()

func (*SafeMap) Get added in v0.0.7

func (s *SafeMap) Get(key string) (interface{}, bool)

func (*SafeMap) GetAll added in v0.0.7

func (s *SafeMap) GetAll() []interface{}

func (*SafeMap) Size added in v0.0.8

func (s *SafeMap) Size() int

func (*SafeMap) Store added in v0.0.7

func (s *SafeMap) Store(key string, value interface{})

type TargetInformerFactory

type TargetInformerFactory func(*fedv1a1.FederatedCluster, ResourceClient) (cache.Store, cache.Controller)

A function that should be used to create an informer on the target object. Store should use cache.DeletionHandlingMetaNamespaceKeyFunc as a keying function.

type WorkerTiming added in v0.0.2

type WorkerTiming struct {
	Interval         time.Duration
	RetryDelay       time.Duration
	ClusterSyncDelay time.Duration
	InitialBackoff   time.Duration
	MaxBackoff       time.Duration
}

Directories

Path Synopsis
Package to help federation controllers to delete federated resources from underlying clusters when the resource is deleted from federation control plane.
Package to help federation controllers to delete federated resources from underlying clusters when the resource is deleted from federation control plane.
Helper functions for manipulating finalizers.
Helper functions for manipulating finalizers.

Jump to

Keyboard shortcuts

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