/
cluster_checker.go
59 lines (47 loc) · 1.66 KB
/
cluster_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
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/v15/service/controller/key"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlClient "sigs.k8s.io/controller-runtime/pkg/client"
)
type ClusterCheckerConfig struct {
CtrlClient ctrlClient.Client
Logger micrologger.Logger
}
type ClusterChecker struct {
ctrlClient ctrlClient.Client
logger micrologger.Logger
}
func NewClusterChecker(config ClusterCheckerConfig) (*ClusterChecker, 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 := &ClusterChecker{
ctrlClient: config.CtrlClient,
logger: config.Logger,
}
return a, nil
}
func (c *ClusterChecker) Check(ctx context.Context, namespace string, name string) (bool, error) {
cr := &infrastructurev1alpha3.AWSCluster{}
err := c.ctrlClient.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, cr)
if err != nil {
return false, microerror.Mask(err)
}
// We check the subnet we want to ensure in the CR status. In case there is no
// subnet tracked so far, we want to proceed with the allocation process. Thus
// we return true.
if key.StatusClusterNetworkCIDR(*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
}