This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
resource.go
77 lines (62 loc) · 2.19 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
package cloudconfigblob
import (
"context"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
capiexp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-operator/v6/client"
)
const (
// Name is the identifier of the resource.
Name = "cloudconfigblob"
)
type Config struct {
ClientFactory client.OrganizationFactory
CtrlClient ctrlclient.Client
Logger micrologger.Logger
}
// Resource manages the blob saved in Azure Storage Account that contains the cloudconfig files to bootstrap our virtual machines.
type Resource struct {
clientFactory client.OrganizationFactory
ctrlClient ctrlclient.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{
clientFactory: config.ClientFactory,
ctrlClient: config.CtrlClient,
logger: config.Logger,
}
return r, nil
}
// Name returns the resource name.
func (r *Resource) Name() string {
return Name
}
// getMachinePoolByName finds and return a MachinePool object using the specified params.
func (r *Resource) getMachinePoolByName(ctx context.Context, namespace, name string) (*capiexp.MachinePool, error) {
machinePool := &capiexp.MachinePool{}
objectKey := ctrlclient.ObjectKey{Name: name, Namespace: namespace}
if err := r.ctrlClient.Get(ctx, objectKey, machinePool); err != nil {
return nil, err
}
r.logger = r.logger.With("machinePool", machinePool.Name)
return machinePool, nil
}
// getOwnerMachinePool returns the MachinePool object owning the current resource.
func (r *Resource) getOwnerMachinePool(ctx context.Context, obj metav1.ObjectMeta) (*capiexp.MachinePool, error) {
for _, ref := range obj.OwnerReferences {
if ref.Kind == "MachinePool" && ref.APIVersion == capiexp.GroupVersion.String() {
return r.getMachinePoolByName(ctx, obj.Namespace, ref.Name)
}
}
return nil, nil
}