Documentation
¶
Overview ¶
Package agentgroup lets a single agent instance (drift-detection-manager, sveltos-agent running in the management cluster) serve a group of clusters, instead of one instance per cluster.
A group is identified by a ConfigMap, created in the namespace where the agents run, whose data lists the clusters (members) the group's agent must serve. The agent Deployment of the group carries the same name as its ConfigMap. The component creating the Deployment (the controller that owns the agent) is the only writer of the ConfigMap: it uses AssignCluster to add a cluster to a group and RemoveCluster to take it out again.
A group only contains clusters of one pool: same component, same cluster namespace, same shard. A pool may have many groups; clusters are placed in the lowest-numbered group that still has room (sticky first-fit). Clusters never move between groups.
AssignCluster and RemoveCluster keep no state in memory. An Assigner wraps them with a write-through cache of the groups and serializes the decisions of a pool, which is what a controller reconciling several clusters in parallel should use. Whichever is used, concurrent writers (several processes, a person editing a ConfigMap) are handled with what the API server already guarantees:
- group names are deterministic (component, pool hash, index), so two writers creating the same group collide with AlreadyExists;
- membership changes are Updates carrying the resourceVersion that was read, so a concurrent change makes the write fail with a conflict and the operation is retried;
- a group is deleted with a resourceVersion precondition, so a group that just received a new member is never deleted.
Index ¶
Constants ¶
const ( // ComponentLabel identifies the component (for instance drift-detection) a group belongs to. ComponentLabel = "agentgroup.projectsveltos.io/component" // ClusterNamespaceLabel is the namespace of all the clusters in the group. ClusterNamespaceLabel = "agentgroup.projectsveltos.io/cluster-namespace" // ShardLabel is set on groups of a sharded pool. Its value is a hash of the shard key // (a shard key is free text, so it cannot be used as a label value as is). ShardLabel = "agentgroup.projectsveltos.io/shard" )
Variables ¶
This section is empty.
Functions ¶
func RemoveCluster ¶
func RemoveCluster(ctx context.Context, reader client.Reader, writer client.Client, namespace, component, clusterNamespace, clusterName string, clusterType libsveltosv1beta1.ClusterType, ) (emptied []string, err error)
RemoveCluster takes the cluster out of every group of the component that lists it. Cluster namespace is enough to find the groups: the shard of a cluster being deleted is not reliable.
It returns the names of the groups that were left without members, and so were deleted (the ConfigMap only: the caller must delete the agent Deployment with the same name). A group that receives a new member while its last one is being removed is not deleted.
It is idempotent: removing a cluster that is not in any group does nothing.
Types ¶
type AssignResult ¶
type AssignResult struct {
// GroupName is the name of the group (ConfigMap and agent Deployment) serving the cluster.
GroupName string
// Created is true when the group did not exist and was created by this call. The caller
// is expected to create the agent Deployment.
Created bool
}
AssignResult describes where a cluster was placed.
func AssignCluster ¶
func AssignCluster(ctx context.Context, reader client.Reader, writer client.Client, namespace string, pool Pool, member Member, capacity int) (*AssignResult, error)
AssignCluster makes sure member is part of a group of pool and returns that group.
The member is placed in the lowest-numbered group with fewer than capacity members. When all groups are full, a new one is created. A member already in a group stays where it is, whatever capacity is: lowering the capacity never moves clusters.
reader must not be served from a cache: a stale list could place the same cluster in two groups. namespace is where the agents (and the ConfigMaps of the groups) live.
type Assigner ¶
type Assigner struct {
// contains filtered or unexported fields
}
Assigner places clusters in groups and takes them out again, like AssignCluster and RemoveCluster, keeping the groups in memory.
The ConfigMaps of the groups remain the source of truth, read by the agents. The Assigner is a write-through cache of them:
- the groups of a pool are read from the management cluster the first time they are needed, which is also how they are rebuilt when the process restarts;
- a cluster already in a group is answered from memory, without calling the API server;
- decisions about a pool are taken one at a time, so they never see a stale view of it;
- every write still carries the resourceVersion that was read, so a change made by someone else (another process, a person) makes the write fail with a conflict. The groups are then read again and the operation retried;
- the groups are read again after any change made here and after resyncPeriod, which also picks up ConfigMaps changed or deleted by someone else.
It assumes one process makes the placement decisions for a given component and cluster namespace (as the controller owning the agent does). reader should not be served from a cache. An Assigner is safe for concurrent use.
func NewAssigner ¶
NewAssigner returns an Assigner for the groups (ConfigMaps) living in namespace, which is where the agents run.
func (*Assigner) AssignCluster ¶
func (a *Assigner) AssignCluster(ctx context.Context, pool Pool, member Member, capacity int, ) (*AssignResult, error)
AssignCluster makes sure member is part of a group of pool and returns that group. See the package level AssignCluster for how the group is chosen.
func (*Assigner) RemoveCluster ¶
func (a *Assigner) RemoveCluster(ctx context.Context, component, clusterNamespace, clusterName string, clusterType libsveltosv1beta1.ClusterType) (emptied []string, err error)
RemoveCluster takes the cluster out of every group of the component that lists it, and returns the names of the groups that were left without members and so were deleted (the ConfigMap only: the caller must delete the agent Deployment with the same name). See the package level RemoveCluster.
A cluster that is not in any group (as far as is known) does not make any API call.
type Group ¶
type Group struct {
// Name is the name of the ConfigMap and of the agent Deployment of the group.
Name string
// ClusterNamespace is the namespace of all members.
ClusterNamespace string
// Members are the clusters served by the group.
Members []Member
// contains filtered or unexported fields
}
Group is a group, as read from the management cluster.
func ListGroups ¶
func ListGroups(ctx context.Context, reader client.Reader, namespace, component, clusterNamespace string, ) ([]Group, error)
ListGroups returns all the groups of a component, in the given namespace, for the given cluster namespace. An empty clusterNamespace returns the groups for all cluster namespaces. Use a reader that does not go through a cache when the result decides what to write.
type Member ¶
type Member struct {
// Name is the name of the cluster. The namespace is the one of the group.
Name string `json:"name"`
// ClusterType is Capi or Sveltos.
ClusterType libsveltosv1beta1.ClusterType `json:"clusterType"`
// WatchNamespaces, when not empty, restricts the namespaced resources the agent watches
// in this cluster (namespace-scoped watch mode).
WatchNamespaces []string `json:"watchNamespaces,omitempty"`
}
Member is a cluster served by a group.
type Pool ¶
type Pool struct {
// Component is the agent the groups are for (for instance drift-detection).
Component string
// ClusterNamespace is the namespace of the clusters.
ClusterNamespace string
// Shard is the shard key of the clusters. Empty when the cluster is not sharded.
Shard string
}
Pool identifies the set of groups a cluster can be placed in.