-
Notifications
You must be signed in to change notification settings - Fork 22
/
resource.go
101 lines (86 loc) · 3.14 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
100
101
package ipam
import (
"net"
"reflect"
"github.com/giantswarm/k8sclient/v7/pkg/k8sclient"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/giantswarm/aws-operator/v16/service/internal/locker"
)
const (
Name = "ipam"
)
const (
// minAllocatedSubnetMaskBits is the maximum size of guest subnet i.e.
// smaller number here -> larger subnet per guest cluster. For now anything
// under 16 doesn't make sense in here.
minAllocatedSubnetMaskBits = 16
)
type Config struct {
Checker Checker
Collector Collector
K8sClient k8sclient.Interface
Locker locker.Interface
Logger micrologger.Logger
Persister Persister
AllocatedSubnetMaskBits int
NetworkRange net.IPNet
PrivateSubnetMaskBits int
PublicSubnetMaskBits int
}
type Resource struct {
checker Checker
collector Collector
k8sClient k8sclient.Interface
locker locker.Interface
logger micrologger.Logger
persister Persister
allocatedSubnetMask net.IPMask
networkRange net.IPNet
}
func New(config Config) (*Resource, error) {
if config.Checker == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Checker must not be empty", config)
}
if config.Collector == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Collector must not be empty", config)
}
if config.K8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.K8sClient must not be empty", config)
}
if config.Locker == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Locker must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
if config.Persister == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Persister must not be empty", config)
}
if config.AllocatedSubnetMaskBits < minAllocatedSubnetMaskBits {
return nil, microerror.Maskf(invalidConfigError, "%T.AllocatedSubnetMaskBits (%d) must not be smaller than %d", config, config.AllocatedSubnetMaskBits, minAllocatedSubnetMaskBits)
}
if reflect.DeepEqual(config.NetworkRange, net.IPNet{}) {
return nil, microerror.Maskf(invalidConfigError, "%T.NetworkRange must not be empty", config)
}
if config.PrivateSubnetMaskBits <= config.AllocatedSubnetMaskBits {
return nil, microerror.Maskf(invalidConfigError, "%T.PrivateSubnetMaskBits (%d) must not be smaller or equal than %T.AllocatedSubnetMaskBits (%d)", config, config.PrivateSubnetMaskBits, config, config.AllocatedSubnetMaskBits)
}
if config.PublicSubnetMaskBits <= config.AllocatedSubnetMaskBits {
return nil, microerror.Maskf(invalidConfigError, "%T.PublicSubnetMaskBits (%d) must not be smaller or equal than %T.AllocatedSubnetMaskBits (%d)", config, config.PublicSubnetMaskBits, config, config.AllocatedSubnetMaskBits)
}
r := &Resource{
checker: config.Checker,
collector: config.Collector,
k8sClient: config.K8sClient,
locker: config.Locker,
logger: config.Logger,
persister: config.Persister,
allocatedSubnetMask: net.CIDRMask(config.AllocatedSubnetMaskBits, 32),
networkRange: config.NetworkRange,
}
return r, nil
}
func (r *Resource) Name() string {
return Name
}