-
Notifications
You must be signed in to change notification settings - Fork 22
/
ha_master.go
245 lines (202 loc) · 6.42 KB
/
ha_master.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package hamaster
import (
"context"
infrastructurev1alpha3 "github.com/giantswarm/apiextensions/v6/pkg/apis/infrastructure/v1alpha3"
"github.com/giantswarm/k8sclient/v7/pkg/k8sclient"
"github.com/giantswarm/microerror"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/aws-operator/v12/pkg/label"
"github.com/giantswarm/aws-operator/v12/service/controller/key"
"github.com/giantswarm/aws-operator/v12/service/internal/hamaster/internal/cache"
)
type Config struct {
K8sClient k8sclient.Interface
}
type HAMaster struct {
k8sClient k8sclient.Interface
awsCache *cache.AWS
g8sCache *cache.G8s
}
func New(config Config) (*HAMaster, error) {
if config.K8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.K8sClient must not be empty", config)
}
h := &HAMaster{
k8sClient: config.K8sClient,
awsCache: cache.NewAWS(),
g8sCache: cache.NewG8s(),
}
return h, nil
}
func (h *HAMaster) Enabled(ctx context.Context, obj interface{}) (bool, error) {
rep, err := h.Replicas(ctx, obj)
if err != nil {
return false, microerror.Mask(err)
}
if rep == 3 {
return true, nil
}
return false, nil
}
func (h *HAMaster) Mapping(ctx context.Context, obj interface{}) ([]Mapping, error) {
cr, err := meta.Accessor(obj)
if err != nil {
return nil, microerror.Mask(err)
}
// We need the AWSControlPlane CR because it holds the availability zones. The
// system's implementation requires there only to be 1, 2 or 3 availability
// zones.
aws, err := h.cachedAWS(ctx, cr)
if err != nil {
return nil, microerror.Mask(err)
}
// We need the G8sControlPlane CR because it holds the replica count. This
// tells us how many masters the current setup defines and ultimately dictates
// the Master IDs. The system's implementation requires there only to be 1 or
// 3 masters.
g8s, err := h.cachedG8s(ctx, cr)
if err != nil {
return nil, microerror.Mask(err)
}
if aws.Spec.AvailabilityZones == nil {
return nil, microerror.Mask(availabilityZonesNilError)
}
// We need a deterministic list of availability zones which we can loop over
// through for the required amount of masters. Eventually it happens that
// there is only 1 availability zone in a HA Masters setup. Therefore the
// computed mapping holds the given availability zones repeatedly 3 times so
// that there is always a guaranteed availability zone for each master in any
// of the allowed permutations. Note that given 3 availability zones in a HA
// Masters setup we will never make use of the repeated availability zones,
// which is just a side effect of the current implementation.
var azs []string
{
azs = append(azs, aws.Spec.AvailabilityZones...)
azs = append(azs, aws.Spec.AvailabilityZones...)
azs = append(azs, aws.Spec.AvailabilityZones...)
}
// The master IDs are only allowed to be either 0, 1, 2 or 3, so we hard code
// the list of master IDs depending on the given replicas, which must be
// either 1 or 3.
var ids []int
{
if key.G8sControlPlaneReplicas(g8s) == 1 {
ids = append(ids, 0)
}
if key.G8sControlPlaneReplicas(g8s) == 3 {
ids = append(ids, 1)
ids = append(ids, 2)
ids = append(ids, 3)
}
}
var mappings []Mapping
for i := range ids {
m := Mapping{
AZ: azs[i],
ID: ids[i],
}
mappings = append(mappings, m)
}
return mappings, nil
}
func (h *HAMaster) Replicas(ctx context.Context, obj interface{}) (int, error) {
cr, err := meta.Accessor(obj)
if err != nil {
return 0, microerror.Mask(err)
}
// We need the G8sControlPlane CR because it holds the replica count. This
// tells us how many masters the current setup defines.
g8s, err := h.cachedG8s(ctx, cr)
if err != nil {
return 0, microerror.Mask(err)
}
return key.G8sControlPlaneReplicas(g8s), nil
}
func (h *HAMaster) cachedAWS(ctx context.Context, cr metav1.Object) (infrastructurev1alpha3.AWSControlPlane, error) {
var err error
var ok bool
var cluster infrastructurev1alpha3.AWSControlPlane
{
ck := h.awsCache.Key(ctx, cr)
if ck == "" {
cluster, err = h.lookupAWS(ctx, cr)
if err != nil {
return infrastructurev1alpha3.AWSControlPlane{}, microerror.Mask(err)
}
} else {
cluster, ok = h.awsCache.Get(ctx, ck)
if !ok {
cluster, err = h.lookupAWS(ctx, cr)
if err != nil {
return infrastructurev1alpha3.AWSControlPlane{}, microerror.Mask(err)
}
h.awsCache.Set(ctx, ck, cluster)
}
}
}
return cluster, nil
}
func (h *HAMaster) cachedG8s(ctx context.Context, cr metav1.Object) (infrastructurev1alpha3.G8sControlPlane, error) {
var err error
var ok bool
var cluster infrastructurev1alpha3.G8sControlPlane
{
ck := h.g8sCache.Key(ctx, cr)
if ck == "" {
cluster, err = h.lookupG8s(ctx, cr)
if err != nil {
return infrastructurev1alpha3.G8sControlPlane{}, microerror.Mask(err)
}
} else {
cluster, ok = h.g8sCache.Get(ctx, ck)
if !ok {
cluster, err = h.lookupG8s(ctx, cr)
if err != nil {
return infrastructurev1alpha3.G8sControlPlane{}, microerror.Mask(err)
}
h.g8sCache.Set(ctx, ck, cluster)
}
}
}
return cluster, nil
}
func (h *HAMaster) lookupAWS(ctx context.Context, cr metav1.Object) (infrastructurev1alpha3.AWSControlPlane, error) {
var list infrastructurev1alpha3.AWSControlPlaneList
err := h.k8sClient.CtrlClient().List(
ctx,
&list,
client.InNamespace(cr.GetNamespace()),
client.MatchingLabels{label.Cluster: key.ClusterID(cr)},
)
if err != nil {
return infrastructurev1alpha3.AWSControlPlane{}, microerror.Mask(err)
}
if len(list.Items) == 0 {
return infrastructurev1alpha3.AWSControlPlane{}, microerror.Mask(notFoundError)
}
if len(list.Items) > 1 {
return infrastructurev1alpha3.AWSControlPlane{}, microerror.Mask(tooManyCRsError)
}
return list.Items[0], nil
}
func (h *HAMaster) lookupG8s(ctx context.Context, cr metav1.Object) (infrastructurev1alpha3.G8sControlPlane, error) {
var list infrastructurev1alpha3.G8sControlPlaneList
err := h.k8sClient.CtrlClient().List(
ctx,
&list,
client.InNamespace(cr.GetNamespace()),
client.MatchingLabels{label.Cluster: key.ClusterID(cr)},
)
if err != nil {
return infrastructurev1alpha3.G8sControlPlane{}, microerror.Mask(err)
}
if len(list.Items) == 0 {
return infrastructurev1alpha3.G8sControlPlane{}, microerror.Mask(notFoundError)
}
if len(list.Items) > 1 {
return infrastructurev1alpha3.G8sControlPlane{}, microerror.Mask(tooManyCRsError)
}
return list.Items[0], nil
}