-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathtarget.go
More file actions
180 lines (151 loc) · 5.74 KB
/
target.go
File metadata and controls
180 lines (151 loc) · 5.74 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Package target provides functionality around building and deploying bundledeployments.
//
// Each "Target" represents a bundle, cluster pair and will be transformed into a bundledeployment.
// The manifest, persisted in the content resource, contains the resources available to
// these bundledeployments.
package target
import (
"strings"
"github.com/rancher/fleet/internal/cmd/controller/summary"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/wrangler/v3/pkg/yaml"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
var (
// Default limit is 100%, make sure the default behavior doesn't block rollout
defLimit = intstr.FromString("100%")
defAutoPartitionSize = intstr.FromString("25%")
defAutoPartitionThreshold = 200
defMaxUnavailablePartitions = intstr.FromInt(0)
)
const (
maxTemplateRecursionDepth = 50
clusterLabelPrefix = "global.fleet.clusterLabels."
byBundleIndexerName = "fleet.byBundle"
)
// BundleFromDeployment returns the namespace and name of the bundle that
// created the bundledeployment
func BundleFromDeployment(labels map[string]string) (string, string) {
return labels[fleet.BundleNamespaceLabel],
labels[fleet.BundleLabel]
}
// Target represents a bundle deployment target, encapsulating all relevant
// information about the deployment, associated cluster groups, cluster,
// bundle, deployment options, and deployment identifier.
type Target struct {
Deployment *fleet.BundleDeployment
ClusterGroups []*fleet.ClusterGroup
Cluster *fleet.Cluster
Bundle *fleet.Bundle
Options fleet.BundleDeploymentOptions
DeploymentID string
}
// BundleDeployment returns a new BundleDeployment, it discards annotations, status, etc.
// The labels are copied from the Bundle.
func (t *Target) BundleDeployment() *fleet.BundleDeployment {
bd := &fleet.BundleDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: t.Deployment.Name,
Namespace: t.Deployment.Namespace,
Labels: t.BundleDeploymentLabels(t.Cluster.Namespace, t.Cluster.Name),
UID: t.Deployment.UID, // useful for optimised owner reference population if the BD already exists.
},
Spec: t.Deployment.Spec,
}
bd.Spec.Paused = t.IsPaused()
bd.Spec.OffSchedule = t.Cluster.Status.Scheduled && !t.Cluster.Status.ActiveSchedule
initialiseOptionsMaps(bd)
for _, bundleTarget := range t.Bundle.Spec.Targets {
for key, value := range bundleTarget.NamespaceLabels {
bd.Spec.Options.NamespaceLabels[key] = value
bd.Spec.StagedOptions.NamespaceLabels[key] = value
}
for key, value := range bundleTarget.NamespaceAnnotations {
bd.Spec.Options.NamespaceAnnotations[key] = value
bd.Spec.StagedOptions.NamespaceAnnotations[key] = value
}
}
bd.Spec.DependsOn = t.Bundle.Spec.DependsOn
bd.Spec.CorrectDrift = t.Options.CorrectDrift
return bd
}
func (t *Target) IsPaused() bool {
return t.Cluster.Spec.Paused ||
t.Bundle.Spec.Paused
}
// BundleDeploymentLabels builds all labels for a bundledeployment
func (t *Target) BundleDeploymentLabels(clusterNamespace string, clusterName string) map[string]string {
// remove labels starting with kubectl.kubernetes.io or containing
// cattle.io from bundle
labels := yaml.CleanAnnotationsForExport(t.Bundle.Labels)
// copy fleet labels from bundle to bundledeployment
for k, v := range t.Bundle.Labels {
if strings.HasPrefix(k, "fleet.cattle.io/") {
labels[k] = v
}
}
// labels for the bundledeployment by bundle selector
labels[fleet.BundleLabel] = t.Bundle.Name
labels[fleet.BundleNamespaceLabel] = t.Bundle.Namespace
// ManagedLabel allows clean up of the bundledeployment
labels[fleet.ManagedLabel] = "true"
// add labels to identify the cluster this bundledeployment belongs to
labels[fleet.ClusterNamespaceLabel] = clusterNamespace
labels[fleet.ClusterLabel] = clusterName
return labels
}
func (t *Target) modified() []fleet.ModifiedStatus {
if t.Deployment == nil {
return nil
}
return t.Deployment.Status.ModifiedStatus
}
func (t *Target) nonReady() []fleet.NonReadyStatus {
if t.Deployment == nil {
return nil
}
return t.Deployment.Status.NonReadyStatus
}
// state calculates a fleet.BundleState from t (pure function)
func (t *Target) state() fleet.BundleState {
switch t.Deployment {
case nil:
return fleet.Pending
default:
return summary.GetDeploymentState(t.effectiveDeployment())
}
}
// message returns a relevant message from the target (pure function)
func (t *Target) message() string {
return summary.MessageFromDeployment(t.effectiveDeployment())
}
// effectiveDeployment returns t.Deployment with Spec.DeploymentID overridden
// to t.DeploymentID, or t.Deployment unchanged when the IDs already match.
// Status is computed (via state and message) before the reconciler writes the
// new BD spec, so the cached Spec.DeploymentID lags at that point.
func (t *Target) effectiveDeployment() *fleet.BundleDeployment {
if t.Deployment == nil || t.DeploymentID == t.Deployment.Spec.DeploymentID {
return t.Deployment
}
bd := *t.Deployment
bd.Spec.DeploymentID = t.DeploymentID
return &bd
}
// initialiseOptionsMaps initialises options and staged options maps of namespace labels and annotations on bd, if
// needed.
// Assumes that bd is not nil.
func initialiseOptionsMaps(bd *fleet.BundleDeployment) {
if bd.Spec.Options.NamespaceLabels == nil {
bd.Spec.Options.NamespaceLabels = map[string]string{}
}
if bd.Spec.Options.NamespaceAnnotations == nil {
bd.Spec.Options.NamespaceAnnotations = map[string]string{}
}
if bd.Spec.StagedOptions.NamespaceLabels == nil {
bd.Spec.StagedOptions.NamespaceLabels = map[string]string{}
}
if bd.Spec.StagedOptions.NamespaceAnnotations == nil {
bd.Spec.StagedOptions.NamespaceAnnotations = map[string]string{}
}
}