This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource.go
99 lines (79 loc) · 2.32 KB
/
resource.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
package clusterid
import (
"context"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/giantswarm/operatorkit/v7/pkg/controller/context/reconciliationcanceledcontext"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-operator/v8/pkg/label"
"github.com/giantswarm/azure-operator/v8/service/controller/key"
)
const (
// Name is the identifier of the resource.
Name = "clusterid"
)
type Config struct {
CtrlClient client.Client
Logger micrologger.Logger
}
// Resource manages the cluster ID label in the AzureConfig CR.
type Resource struct {
ctrlClient client.Client
logger micrologger.Logger
}
func New(config Config) (*Resource, error) {
if config.CtrlClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.CtrlClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
r := &Resource{
ctrlClient: config.CtrlClient,
logger: config.Logger,
}
return r, nil
}
// EnsureCreated ensures that reconciled AzureConfig CR has cluster ID label.
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
cr, err := key.ToCustomResource(obj)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "ensuring cluster id label is set")
{
// Refresh the CR object.
nsName := types.NamespacedName{
Name: cr.Name,
Namespace: cr.Namespace,
}
err := r.ctrlClient.Get(ctx, nsName, &cr)
if err != nil {
return microerror.Mask(err)
}
}
v, exists := cr.Labels[label.Cluster]
if !exists || v != cr.Spec.Cluster.ID {
cr.Labels[label.Cluster] = cr.Spec.Cluster.ID
r.logger.Debugf(ctx, "updating CR labels with cluster id")
err := r.ctrlClient.Update(ctx, &cr)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "updated CR labels with cluster id")
r.logger.Debugf(ctx, "canceling reconciliation")
reconciliationcanceledcontext.SetCanceled(ctx)
return nil
}
r.logger.Debugf(ctx, "ensured cluster id label is set")
return nil
}
// EnsureDeleted is no-op.
func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error {
return nil
}
// Name returns the resource name.
func (r *Resource) Name() string {
return Name
}