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
90 lines (73 loc) · 2.31 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
package migration
import (
"context"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-operator/v6/service/controller/key"
)
const (
// Name is the identifier of the resource.
Name = "migration"
)
type Config struct {
CtrlClient client.Client
Logger micrologger.Logger
}
type Resource struct {
client 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{
client: config.CtrlClient,
logger: config.Logger,
}
return r, nil
}
// EnsureCreated creates non-experimental MachinePool CR if it doesn't already exist.
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
oldMachinePool, err := key.ToOldExpMachinePool(obj)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "Ensuring new MachinePool and AzureMachinePool %s/%s are created", oldMachinePool.Namespace, oldMachinePool.Name)
namespacedName := types.NamespacedName{
Namespace: oldMachinePool.Namespace,
Name: oldMachinePool.Name,
}
mp, err := r.ensureNewMachinePoolCreated(ctx, oldMachinePool)
if err != nil {
return microerror.Mask(err)
}
err = r.ensureNewAzureMachinePoolCreated(ctx, namespacedName, mp)
if err != nil {
return microerror.Mask(err)
}
err = r.ensureNewMachinePoolReferencesUpdated(ctx, namespacedName)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "Ensured new MachinePool and AzureMachinePool %s/%s are created", oldMachinePool.Namespace, oldMachinePool.Name)
return nil
}
// EnsureDeleted just logs that the old exp MachinePool has been deleted.
func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error {
oldMachinePool, err := key.ToOldExpMachinePool(obj)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "Deleting old exp MachinePool CR %s/%s", oldMachinePool.Namespace, oldMachinePool.Name)
return nil
}
// Name returns the resource name.
func (r *Resource) Name() string {
return Name
}