/
create.go
100 lines (81 loc) · 2.54 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package updateinfrarefs
import (
"context"
"fmt"
"github.com/giantswarm/microerror"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/giantswarm/cluster-operator/v5/pkg/label"
"github.com/giantswarm/cluster-operator/v5/service/controller/key"
)
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
cr, err := meta.Accessor(obj)
if err != nil {
return microerror.Mask(err)
}
or, err := r.toObjRef(obj)
if err != nil {
return microerror.Mask(err)
}
componentVersions, err := r.releaseVersion.ComponentVersion(ctx, cr)
if err != nil {
return microerror.Mask(err)
}
// Here we fetch the provider specific CR defined as infrastructure reference
// in the CAPI type. We use an unstructured object and therefore need to set
// the api version and kind accordingly. If we would not do that the
// controller-runtime client cannot find the right object.
ir := &unstructured.Unstructured{}
{
r.logger.Debugf(ctx, "finding infrastructure reference")
ir.SetAPIVersion(or.APIVersion)
ir.SetKind(or.Kind)
err = r.k8sClient.CtrlClient().Get(ctx, key.ObjRefToNamespacedName(or), ir)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "found infrastructure reference")
}
var updated bool
// Syncing the provider operator version label, e.g. for aws-operator,
// kvm-operator or the like.
{
o := fmt.Sprintf("%s-operator", r.provider)
l := fmt.Sprintf("%s-operator.giantswarm.io/version", r.provider)
cv := componentVersions[o]
d := cv.Version
if d == "" {
return microerror.Maskf(notFoundError, "component version not found for %#q", o)
}
c, ok := ir.GetLabels()[l]
if ok && d != "" && d != c {
labels := ir.GetLabels()
labels[l] = d
ir.SetLabels(labels)
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.GetLabels()[l]
c := ir.GetLabels()[l]
if ok && d != "" && d != c {
labels := ir.GetLabels()
labels[l] = d
ir.SetLabels(labels)
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 infrastructure reference %#q", ir.GetNamespace()+"/"+ir.GetName())
err = r.k8sClient.CtrlClient().Update(ctx, ir)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "updated infrastructure reference %#q", ir.GetNamespace()+"/"+ir.GetName())
}
return nil
}