-
Notifications
You must be signed in to change notification settings - Fork 22
/
machine_deployment_checker.go
65 lines (51 loc) · 1.78 KB
/
machine_deployment_checker.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
package ipam
import (
"context"
infrastructurev1alpha3 "github.com/giantswarm/apiextensions/v6/pkg/apis/infrastructure/v1alpha3"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/giantswarm/aws-operator/v16/service/controller/key"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlClient "sigs.k8s.io/controller-runtime/pkg/client"
)
type MachineDeploymentCheckerConfig struct {
CtrlClient ctrlClient.Client
Logger micrologger.Logger
}
type MachineDeploymentChecker struct {
ctrlClient ctrlClient.Client
logger micrologger.Logger
}
func NewMachineDeploymentChecker(config MachineDeploymentCheckerConfig) (*MachineDeploymentChecker, 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)
}
a := &MachineDeploymentChecker{
ctrlClient: config.CtrlClient,
logger: config.Logger,
}
return a, nil
}
func (c *MachineDeploymentChecker) Check(ctx context.Context, namespace string, name string) (bool, error) {
objectKey := client.ObjectKey{
Namespace: namespace,
Name: name,
}
cr := &infrastructurev1alpha3.AWSMachineDeployment{}
err := c.ctrlClient.Get(ctx, objectKey, cr)
if err != nil {
return false, microerror.Mask(err)
}
// We check the subnet we want to ensure in the CR annotations. In case there
// is no subnet tracked so far, we want to proceed with the allocation
// process. Thus we return true.
if key.MachineDeploymentSubnet(*cr) == "" {
return true, nil
}
// At this point the subnet is already allocated for the CR we check here. So
// we do not want to proceed further and return false.
return false, nil
}