util

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: Apache-2.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// RevisionAnnotation is the revision annotation of a deployment's replica sets which records its rollout sequence
	RevisionAnnotation = "deployment.kubernetes.io/revision"
	// RevisionHistoryAnnotation maintains the history of all old revisions that a replica set has served for a deployment.
	RevisionHistoryAnnotation = "deployment.kubernetes.io/revision-history"
	// ReplicasAnnotation is the desired replicas for a deployment recorded as an annotation
	// in its replica sets. Helps in separating scaling events from the rollout process and for
	// determining if the new replica set for a deployment is really saturated.
	ReplicasAnnotation = "deployment.kubernetes.io/desired-replicas"
	// MaxReplicasAnnotation is the maximum replicas a deployment can have at a given point, which
	// is deployment.spec.replicas + maxSurge. Used by the underlying replica sets to estimate their
	// proportions in case the deployment has surge replicas.
	MaxReplicasAnnotation = "deployment.kubernetes.io/max-replicas"

	// ReplicaSetUpdatedReason is added in a deployment when one of its replica sets is updated as part
	// of the rollout process.
	ReplicaSetUpdatedReason = "ReplicaSetUpdated"
	// FailedRSCreateReason is added in a deployment when it cannot create a new replica set.
	FailedRSCreateReason = "ReplicaSetCreateError"
	// NewReplicaSetReason is added in a deployment when it creates a new replica set.
	NewReplicaSetReason = "NewReplicaSetCreated"
	// FoundNewRSReason is added in a deployment when it adopts an existing replica set.
	FoundNewRSReason = "FoundNewReplicaSet"
	// NewRSAvailableReason is added in a deployment when its newest replica set is made available
	// ie. the number of new pods that have passed readiness checks and run for at least minReadySeconds
	// is at least the minimum available pods that need to run for the deployment.
	NewRSAvailableReason = "NewReplicaSetAvailable"
	// TimedOutReason is added in a deployment when its newest replica set fails to show any progress
	// within the given deadline (progressDeadlineSeconds).
	TimedOutReason = "ProgressDeadlineExceeded"
	// PausedDeployReason is added in a deployment when it is paused. Lack of progress shouldn't be
	// estimated once a deployment is paused.
	PausedDeployReason = "DeploymentPaused"
	// ResumedDeployReason is added in a deployment when it is resumed. Useful for not failing accidentally
	// deployments that paused amidst a rollout and are bounded by a deadline.
	ResumedDeployReason = "DeploymentResumed"

	// MinimumReplicasAvailable is added in a deployment when it has its minimum replicas required available.
	MinimumReplicasAvailable = "MinimumReplicasAvailable"
	// MinimumReplicasUnavailable is added in a deployment when it doesn't have the minimum required replicas
	// available.
	MinimumReplicasUnavailable = "MinimumReplicasUnavailable"
)

Variables

This section is empty.

Functions

func DeploymentComplete

func DeploymentComplete(deployment *apps.Deployment, newStatus *apps.DeploymentStatus) bool

DeploymentComplete considers a deployment to be complete once all of its desired replicas are updated and available, and no old pods are running.

func DeploymentProgressing

func DeploymentProgressing(deployment *apps.Deployment, newStatus *apps.DeploymentStatus) bool

DeploymentProgressing reports progress for a deployment. Progress is estimated by comparing the current with the new status of the deployment that the controller is observing. More specifically, when new pods are scaled up or become ready or available, or old pods are scaled down, then we consider the deployment is progressing.

func DeploymentRolloutSatisfied

func DeploymentRolloutSatisfied(deployment *apps.Deployment, partition intstrutil.IntOrString) error

DeploymentRolloutSatisfied return nil if deployment has satisfied partition and replicas, or will return an error.

func DeploymentTimedOut

func DeploymentTimedOut(deployment *apps.Deployment, newStatus *apps.DeploymentStatus) bool

DeploymentTimedOut considers a deployment to have timed out once its condition that reports progress is older than progressDeadlineSeconds or a Progressing condition with a TimedOutReason reason already exists.

func EqualIgnoreHash

func EqualIgnoreHash(template1, template2 *v1.PodTemplateSpec) bool

EqualIgnoreHash returns true if two given podTemplateSpec are equal, ignoring the diff in value of Labels[pod-template-hash] We ignore pod-template-hash because:

  1. The hash result would be different upon podTemplateSpec API changes (e.g. the addition of a new field will cause the hash code to change)
  2. The deployment template won't have hash labels

func FilterActiveReplicaSets

func FilterActiveReplicaSets(replicaSets []*apps.ReplicaSet) []*apps.ReplicaSet

FilterActiveReplicaSets returns replica sets that have (or at least ought to have) pods.

func FilterReplicaSets

func FilterReplicaSets(RSes []*apps.ReplicaSet, filterFn filterRS) []*apps.ReplicaSet

FilterReplicaSets returns replica sets that are filtered by filterFn (all returned ones should match filterFn).

func FindActiveOrLatest

func FindActiveOrLatest(newRS *apps.ReplicaSet, oldRSs []*apps.ReplicaSet) *apps.ReplicaSet

FindActiveOrLatest returns the only active or the latest replica set in case there is at most one active replica set. If there are more active replica sets, then we should proportionally scale them.

func FindNewReplicaSet

func FindNewReplicaSet(deployment *apps.Deployment, rsList []*apps.ReplicaSet) *apps.ReplicaSet

FindNewReplicaSet returns the new RS this given deployment targets (the one with the same pod template).

func FindOldReplicaSets

func FindOldReplicaSets(deployment *apps.Deployment, rsList []*apps.ReplicaSet) ([]*apps.ReplicaSet, []*apps.ReplicaSet)

FindOldReplicaSets returns the old replica sets targeted by the given Deployment, with the given slice of RSes. Note that the first set of old replica sets doesn't include the ones with no pods, and the second set of old replica sets include all old replica sets.

func GetActualReplicaCountForReplicaSets

func GetActualReplicaCountForReplicaSets(replicaSets []*apps.ReplicaSet) int32

GetActualReplicaCountForReplicaSets returns the sum of actual replicas of the given replica sets.

func GetAvailableReplicaCountForReplicaSets

func GetAvailableReplicaCountForReplicaSets(replicaSets []*apps.ReplicaSet) int32

GetAvailableReplicaCountForReplicaSets returns the number of available pods corresponding to the given replica sets.

func GetDeploymentCondition

func GetDeploymentCondition(status apps.DeploymentStatus, condType apps.DeploymentConditionType) *apps.DeploymentCondition

GetDeploymentCondition returns the condition with the provided type.

func GetProportion

func GetProportion(rs *apps.ReplicaSet, d apps.Deployment, strategy *rolloutsv1alpha1.DeploymentStrategy, deploymentReplicasToAdd, deploymentReplicasAdded int32) int32

GetProportion will estimate the proportion for the provided replica set using 1. the current size of the parent deployment, 2. the replica count that needs be added on the replica sets of the deployment, and 3. the total replicas added in the replica sets of the deployment so far.

func GetReadyReplicaCountForReplicaSets

func GetReadyReplicaCountForReplicaSets(replicaSets []*apps.ReplicaSet) int32

GetReadyReplicaCountForReplicaSets returns the number of ready pods corresponding to the given replica sets.

func GetReplicaCountForReplicaSets

func GetReplicaCountForReplicaSets(replicaSets []*apps.ReplicaSet) int32

GetReplicaCountForReplicaSets returns the sum of Replicas of the given replica sets.

func GetReplicasAnnotation added in v0.5.0

func GetReplicasAnnotation(rs *apps.ReplicaSet) (int32, bool)

GetReplicasAnnotation returns the number of desired replicas

func HasProgressDeadline

func HasProgressDeadline(d *apps.Deployment) bool

HasProgressDeadline checks if the Deployment d is expected to surface the reason "ProgressDeadlineExceeded" when the Deployment progress takes longer than expected time.

func HasRevisionHistoryLimit

func HasRevisionHistoryLimit(d *apps.Deployment) bool

HasRevisionHistoryLimit checks if the Deployment d is expected to keep a specified number of old replicaSets. These replicaSets are mainly kept with the purpose of rollback. The RevisionHistoryLimit can start from 0 (no retained replicasSet). When set to math.MaxInt32, the Deployment will keep all revisions.

func IsRollingUpdate

func IsRollingUpdate(_ *apps.Deployment) bool

IsRollingUpdate returns true if the strategy type is a rolling update.

func IsSaturated

func IsSaturated(deployment *apps.Deployment, rs *apps.ReplicaSet) bool

IsSaturated checks if the new replica set is saturated by comparing its size with its deployment size. Both the deployment and the replica set have to believe this replica set can own all of the desired replicas in the deployment and the annotation helps in achieving that. All pods of the ReplicaSet need to be available.

func IsUnderRolloutControl

func IsUnderRolloutControl(deployment *apps.Deployment) bool

IsUnderRolloutControl return true if this deployment should be controlled by our controller.

func ListPods

func ListPods(deployment *apps.Deployment, rsList []*apps.ReplicaSet, getPodList podListFunc) (*v1.PodList, error)

ListPods returns a list of pods the given deployment targets. This needs a list of ReplicaSets for the Deployment, which can be found with ListReplicaSets(). Note that this does NOT attempt to reconcile ControllerRef (adopt/orphan), because only the controller itself should do that. However, it does filter out anything whose ControllerRef doesn't match.

func ListReplicaSets

func ListReplicaSets(deployment *apps.Deployment, getRSList RsListFunc) ([]*apps.ReplicaSet, error)

ListReplicaSets returns a slice of RSes the given deployment targets. Note that this does NOT attempt to reconcile ControllerRef (adopt/orphan), because only the controller itself should do that. However, it does filter out anything whose ControllerRef doesn't match.

func MaxRevision

func MaxRevision(allRSs []*apps.ReplicaSet) int64

MaxRevision finds the highest revision in the replica sets

func MaxSurge

func MaxSurge(deployment *apps.Deployment, strategy *rolloutsv1alpha1.DeploymentStrategy) int32

MaxSurge returns the maximum surge pods a rolling deployment can take.

func MaxUnavailable

func MaxUnavailable(deployment *apps.Deployment, strategy *rolloutsv1alpha1.DeploymentStrategy) int32

MaxUnavailable returns the maximum unavailable pods a rolling deployment can take.

func MinAvailable

func MinAvailable(deployment *apps.Deployment, strategy *rolloutsv1alpha1.DeploymentStrategy) int32

MinAvailable returns the minimum available pods of a given deployment

func NewDeploymentCondition

func NewDeploymentCondition(condType apps.DeploymentConditionType, status v1.ConditionStatus, reason, message string) *apps.DeploymentCondition

NewDeploymentCondition creates a new deployment condition.

func NewRSNewReplicas

func NewRSNewReplicas(deployment *apps.Deployment, allRSs []*apps.ReplicaSet, newRS *apps.ReplicaSet, strategy *rolloutsv1alpha1.DeploymentStrategy) (int32, error)

NewRSNewReplicas calculates the number of replicas a deployment's new RS should have. When one of the followings is true, we're rolling out the deployment; otherwise, we're scaling it. 1) The new RS is saturated: newRS's replicas == deployment's replicas 2) Max number of pods allowed is reached: deployment's replicas + maxSurge == all RSs' replicas

func NewRSReplicasLimit

func NewRSReplicasLimit(partition intstrutil.IntOrString, deployment *apps.Deployment) int32

NewRSReplicasLimit return a limited replicas of new RS calculated via partition.

func NewRSReplicasLowerBound

func NewRSReplicasLowerBound(deployment *apps.Deployment, strategy *rolloutsv1alpha1.DeploymentStrategy) int32

NewRSReplicasLowerBound ensure that newReplicasLowerBound is greater than 0 when create newRS unless deployment is 0 or MaxSurge > 0, this is because if we set new replicas as 0, the native deployment controller will flight with ours.

func PodKey

func PodKey(pod *v1.Pod) string

PodKey returns a key unique to the given pod within a cluster. It's used so we consistently use the same key scheme in this module. It does exactly what cache.MetaNamespaceKeyFunc would have done except there's not possibility for error since we know the exact type.

func RemoveDeploymentCondition

func RemoveDeploymentCondition(status *apps.DeploymentStatus, condType apps.DeploymentConditionType)

RemoveDeploymentCondition removes the deployment condition with the provided type.

func ReplicaSetToDeploymentCondition

func ReplicaSetToDeploymentCondition(cond apps.ReplicaSetCondition) apps.DeploymentCondition

ReplicaSetToDeploymentCondition converts a replica set condition into a deployment condition. Useful for promoting replica set failure conditions into deployments.

func ReplicasAnnotationsNeedUpdate

func ReplicasAnnotationsNeedUpdate(rs *apps.ReplicaSet, desiredReplicas, maxReplicas int32) bool

ReplicasAnnotationsNeedUpdate return true if ReplicasAnnotations need to be updated

func ResolveFenceposts

func ResolveFenceposts(maxSurge, maxUnavailable *intstrutil.IntOrString, desired int32) (int32, int32, error)

ResolveFenceposts resolves both maxSurge and maxUnavailable. This needs to happen in one step. For example:

2 desired, max unavailable 1%, surge 0% - should scale old(-1), then new(+1), then old(-1), then new(+1) 1 desired, max unavailable 1%, surge 0% - should scale old(-1), then new(+1) 2 desired, max unavailable 25%, surge 1% - should scale new(+1), then old(-1), then new(+1), then old(-1) 1 desired, max unavailable 25%, surge 1% - should scale new(+1), then old(-1) 2 desired, max unavailable 0%, surge 1% - should scale new(+1), then old(-1), then new(+1), then old(-1) 1 desired, max unavailable 0%, surge 1% - should scale new(+1), then old(-1)

func Revision

func Revision(obj runtime.Object) (int64, error)

Revision returns the revision number of the input object.

func SetDeploymentCondition

func SetDeploymentCondition(status *apps.DeploymentStatus, condition apps.DeploymentCondition)

SetDeploymentCondition updates the deployment to include the provided condition. If the condition that we are about to add already exists and has the same status and reason then we are not going to update.

func SetDeploymentRevision

func SetDeploymentRevision(deployment *apps.Deployment, revision string) bool

SetDeploymentRevision updates the revision for a deployment.

func SetNewReplicaSetAnnotations

func SetNewReplicaSetAnnotations(deployment *apps.Deployment, newRS *apps.ReplicaSet, strategy *rolloutsv1alpha1.DeploymentStrategy, newRevision string, exists bool, revHistoryLimitInChars int) bool

SetNewReplicaSetAnnotations sets new replica set's annotations appropriately by updating its revision and copying required deployment annotations to it; it returns true if replica set's annotation is changed.

func SetReplicasAnnotations

func SetReplicasAnnotations(rs *apps.ReplicaSet, desiredReplicas, maxReplicas int32) bool

SetReplicasAnnotations sets the desiredReplicas and maxReplicas into the annotations

Types

type ControllersByCreationTimestamp

type ControllersByCreationTimestamp []*v1.ReplicationController

ControllersByCreationTimestamp sorts a list of ReplicationControllers by creation timestamp, using their names as a tie breaker.

func (ControllersByCreationTimestamp) Len

func (ControllersByCreationTimestamp) Less

func (ControllersByCreationTimestamp) Swap

func (o ControllersByCreationTimestamp) Swap(i, j int)

type ReplicaSetsByCreationTimestamp

type ReplicaSetsByCreationTimestamp []*apps.ReplicaSet

ReplicaSetsByCreationTimestamp sorts a list of ReplicaSet by creation timestamp, using their names as a tie breaker.

func (ReplicaSetsByCreationTimestamp) Len

func (ReplicaSetsByCreationTimestamp) Less

func (ReplicaSetsByCreationTimestamp) Swap

func (o ReplicaSetsByCreationTimestamp) Swap(i, j int)

type ReplicaSetsByRevision

type ReplicaSetsByRevision []*apps.ReplicaSet

ReplicaSetsByRevision sorts a list of ReplicaSet by revision, using their creation timestamp or name as a tie breaker. By using the creation timestamp, this sorts from old to new replica sets.

func (ReplicaSetsByRevision) Len

func (o ReplicaSetsByRevision) Len() int

func (ReplicaSetsByRevision) Less

func (o ReplicaSetsByRevision) Less(i, j int) bool

func (ReplicaSetsByRevision) Swap

func (o ReplicaSetsByRevision) Swap(i, j int)

type ReplicaSetsBySizeNewer

type ReplicaSetsBySizeNewer []*apps.ReplicaSet

ReplicaSetsBySizeNewer sorts a list of ReplicaSet by size in descending order, using their creation timestamp or name as a tie breaker. By using the creation timestamp, this sorts from new to old replica sets.

func (ReplicaSetsBySizeNewer) Len

func (o ReplicaSetsBySizeNewer) Len() int

func (ReplicaSetsBySizeNewer) Less

func (o ReplicaSetsBySizeNewer) Less(i, j int) bool

func (ReplicaSetsBySizeNewer) Swap

func (o ReplicaSetsBySizeNewer) Swap(i, j int)

type ReplicaSetsBySizeOlder

type ReplicaSetsBySizeOlder []*apps.ReplicaSet

ReplicaSetsBySizeOlder sorts a list of ReplicaSet by size in descending order, using their creation timestamp or name as a tie breaker. By using the creation timestamp, this sorts from old to new replica sets.

func (ReplicaSetsBySizeOlder) Len

func (o ReplicaSetsBySizeOlder) Len() int

func (ReplicaSetsBySizeOlder) Less

func (o ReplicaSetsBySizeOlder) Less(i, j int) bool

func (ReplicaSetsBySizeOlder) Swap

func (o ReplicaSetsBySizeOlder) Swap(i, j int)

type ReplicaSetsBySmallerRevision

type ReplicaSetsBySmallerRevision []*apps.ReplicaSet

ReplicaSetsBySmallerRevision sorts a list of ReplicaSet by revision in desc, using their creation timestamp or name as a tie breaker. By using the creation timestamp, this sorts from old to new replica sets.

func (ReplicaSetsBySmallerRevision) Len

func (ReplicaSetsBySmallerRevision) Less

func (o ReplicaSetsBySmallerRevision) Less(i, j int) bool

func (ReplicaSetsBySmallerRevision) Swap

func (o ReplicaSetsBySmallerRevision) Swap(i, j int)

type RsListFunc

type RsListFunc func(string, metav1.ListOptions) ([]*apps.ReplicaSet, error)

RsListFunc returns the ReplicaSet from the ReplicaSet namespace and the List metav1.ListOptions.

Jump to

Keyboard shortcuts

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