/
create.go
81 lines (64 loc) · 1.95 KB
/
create.go
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
package updatemachinedeployments
import (
"context"
"github.com/giantswarm/microerror"
apiv1alpha3 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/cluster-operator/v3/pkg/label"
"github.com/giantswarm/cluster-operator/v3/service/controller/key"
)
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
cr, err := key.ToCluster(obj)
if err != nil {
return microerror.Mask(err)
}
mdList := &apiv1alpha3.MachineDeploymentList{}
{
r.logger.Debugf(ctx, "finding MachineDeployments for tenant cluster")
err = r.k8sClient.CtrlClient().List(
ctx,
mdList,
client.InNamespace(cr.Namespace),
client.MatchingLabels{label.Cluster: key.ClusterID(&cr)},
)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "found %d MachineDeployments for tenant cluster", len(mdList.Items))
}
for _, md := range mdList.Items {
md := md // dereferencing pointer value into new scope
var updated bool
// Syncing the cluster-operator version.
{
l := label.OperatorVersion
d, ok := cr.Labels[l]
c := md.Labels[l]
if ok && d != "" && d != c {
md.Labels[l] = d
updated = true
r.logger.Debugf(ctx, "label value of %#q changed from %#q to %#q", l, c, d)
}
}
// Syncing the Giant Swarm Release version.
{
l := label.ReleaseVersion
d, ok := cr.Labels[l]
c := md.Labels[l]
if ok && d != "" && d != c {
md.Labels[l] = d
updated = true
r.logger.Debugf(ctx, "label value of %#q changed from %#q to %#q", l, c, d)
}
}
if updated {
r.logger.Debugf(ctx, "updating machine deployment %#q for tenant cluster %#q", md.Namespace+"/"+md.Name, key.ClusterID(&cr))
err = r.k8sClient.CtrlClient().Update(ctx, &md)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "updated machine deployment %#q for tenant cluster %#q", md.Namespace+"/"+md.Name, key.ClusterID(&cr))
}
}
return nil
}