forked from rancher/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartition.go
More file actions
119 lines (102 loc) · 4.21 KB
/
partition.go
File metadata and controls
119 lines (102 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package target
import (
"reflect"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type partition struct {
Status fleet.PartitionStatus
Targets []*Target
}
// UpdatePartitions recomputes status, including partitions, from data in allTargets.
// It creates Deployments in allTargets if they are missing.
// It updates Deployments in allTargets if they are out of sync (DeploymentID != StagedDeploymentID).
func UpdatePartitions(status *fleet.BundleStatus, allTargets []*Target) (err error) {
rollout := getRollout(allTargets)
maxNew := DefaultMaxNew
if rollout.MaxNew != nil {
maxNew = *rollout.MaxNew
}
partitions, err := partitions(allTargets)
if err != nil {
return err
}
status.UnavailablePartitions = 0
status.MaxUnavailablePartitions, err = maxUnavailablePartitions(partitions, allTargets)
if err != nil {
return err
}
for _, partition := range partitions {
for _, target := range partition.Targets {
// for a new bundledeployment, only stage the first maxNew targets
if target.Deployment == nil && status.NewlyCreated < maxNew {
status.NewlyCreated++
target.Deployment = &fleet.BundleDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: target.Bundle.Name,
Namespace: target.Cluster.Status.Namespace,
Labels: target.BundleDeploymentLabels(target.Cluster.Namespace, target.Cluster.Name),
},
}
}
// stage targets that have a Deployment struct
if target.Deployment != nil {
// NOTE merged options from targets.Targets() are set to be staged
target.Deployment.Spec.StagedOptions = target.Options
target.Deployment.Spec.StagedDeploymentID = target.DeploymentID
}
}
for _, currentTarget := range partition.Targets {
// NOTE this will propagate the staged, merged options to the current deployment
updateDeploymentFromStaged(currentTarget, status, &partition.Status)
}
if updatePartitionStatus(&partition.Status, partition.Targets) {
status.UnavailablePartitions++
}
if status.UnavailablePartitions > status.MaxUnavailablePartitions {
break
}
}
for _, partition := range partitions {
status.PartitionStatus = append(status.PartitionStatus, partition.Status)
}
return nil
}
// maxUnavailablePartitions returns the maximum number of unavailable partitions given the targets and partitions (pure function)
func maxUnavailablePartitions(partitions []partition, targets []*Target) (int, error) {
rollout := getRollout(targets)
return limit(len(partitions), rollout.MaxUnavailablePartitions, &defMaxUnavailablePartitions)
}
// updateDeploymentFromStaged will update DeploymentID and Options for the target to the
// staging values, if it's in a deployable state
func updateDeploymentFromStaged(t *Target, bundleStatus *fleet.BundleStatus, partitionStatus *fleet.PartitionStatus) {
if t.Deployment != nil &&
!t.IsPaused() &&
t.Deployment.Spec.StagedDeploymentID != "" &&
t.Deployment.Spec.DeploymentID == t.Deployment.Spec.StagedDeploymentID &&
!reflect.DeepEqual(t.Deployment.Spec.Options.Diff, t.Deployment.Spec.StagedOptions.Diff) {
// Keep diff options in sync even when the DeploymentID is unchanged.
// This enables diff updates to be propagated downstream to resolve modified statuses
// (for instance after updating bundle diffs in a fleet.yaml).
t.Deployment.Spec.Options.Diff = t.Deployment.Spec.StagedOptions.Diff
}
if t.Deployment != nil &&
// Not Paused
!t.IsPaused() &&
// Has been staged
t.Deployment.Spec.StagedDeploymentID != "" &&
// Is out of sync
t.Deployment.Spec.DeploymentID != t.Deployment.Spec.StagedDeploymentID &&
// Global max unavailable not reached
(bundleStatus.Unavailable < bundleStatus.MaxUnavailable || isUnavailable(t.Deployment)) &&
// Partition max unavailable not reached
(partitionStatus.Unavailable < partitionStatus.MaxUnavailable || isUnavailable(t.Deployment)) {
if !isUnavailable(t.Deployment) {
// If this was previously available, now increment unavailable count. "Upgrading" is treated as unavailable.
bundleStatus.Unavailable++
partitionStatus.Unavailable++
}
t.Deployment.Spec.DeploymentID = t.Deployment.Spec.StagedDeploymentID
t.Deployment.Spec.Options = t.Deployment.Spec.StagedOptions
}
}