/
highavailability_options_provider.go
208 lines (185 loc) · 5.76 KB
/
highavailability_options_provider.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
package operator
import (
appsv1alpha1 "github.com/3scale/3scale-operator/apis/apps/v1alpha1"
"github.com/3scale/3scale-operator/pkg/3scale/amp/component"
"github.com/3scale/3scale-operator/pkg/helper"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type HighAvailabilityOptionsProvider struct {
apimanager *appsv1alpha1.APIManager
namespace string
client client.Client
options *component.HighAvailabilityOptions
secretSource *helper.SecretSource
}
func NewHighAvailabilityOptionsProvider(apimanager *appsv1alpha1.APIManager, namespace string, client client.Client) *HighAvailabilityOptionsProvider {
return &HighAvailabilityOptionsProvider{
apimanager: apimanager,
namespace: namespace,
client: client,
options: component.NewHighAvailabilityOptions(),
secretSource: helper.NewSecretSource(client, namespace),
}
}
func (h *HighAvailabilityOptionsProvider) GetHighAvailabilityOptions() (*component.HighAvailabilityOptions, error) {
setOptionsFns := []func() error{}
if h.apimanager.IsExternal(appsv1alpha1.BackendRedis) {
setOptionsFns = append(setOptionsFns, h.setBackendRedisOptions)
}
if h.apimanager.IsExternal(appsv1alpha1.SystemRedis) {
setOptionsFns = append(setOptionsFns, h.setSystemRedisOptions)
}
if h.apimanager.IsExternal(appsv1alpha1.SystemDatabase) {
setOptionsFns = append(setOptionsFns, h.setSystemDatabaseOptions)
}
for _, setOptions := range setOptionsFns {
if err := setOptions(); err != nil {
return nil, err
}
}
h.options.BackendRedisLabels = h.backendRedisLabels()
h.options.SystemRedisLabels = h.SystemDatabaseLabels()
h.options.SystemDatabaseLabels = h.SystemDatabaseLabels()
err := h.options.Validate()
return h.options, err
}
func (h *HighAvailabilityOptionsProvider) setBackendRedisOptions() error {
cases := []struct {
field *string
secretName string
secretField string
}{
{
&h.options.BackendRedisStorageEndpoint,
component.BackendSecretBackendRedisSecretName,
component.BackendSecretBackendRedisStorageURLFieldName,
},
{
&h.options.BackendRedisQueuesEndpoint,
component.BackendSecretBackendRedisSecretName,
component.BackendSecretBackendRedisQueuesURLFieldName,
},
}
for _, option := range cases {
val, err := h.secretSource.RequiredFieldValueFromRequiredSecret(option.secretName, option.secretField)
if err != nil {
return err
}
*option.field = val
}
// Optional fields
casesWithDefault := []struct {
field *string
secretName string
secretField string
defValue string
}{
{
&h.options.BackendRedisStorageSentinelHosts,
component.BackendSecretBackendRedisSecretName,
component.BackendSecretBackendRedisStorageSentinelHostsFieldName,
component.DefaultBackendStorageSentinelHosts(),
},
{
&h.options.BackendRedisStorageSentinelRole,
component.BackendSecretBackendRedisSecretName,
component.BackendSecretBackendRedisStorageSentinelRoleFieldName,
component.DefaultBackendStorageSentinelRole(),
},
{
&h.options.BackendRedisQueuesSentinelHosts,
component.BackendSecretBackendRedisSecretName,
component.BackendSecretBackendRedisQueuesSentinelHostsFieldName,
component.DefaultBackendQueuesSentinelHosts(),
},
{
&h.options.BackendRedisQueuesSentinelRole,
component.BackendSecretBackendRedisSecretName,
component.BackendSecretBackendRedisQueuesSentinelRoleFieldName,
component.DefaultBackendQueuesSentinelRole(),
},
}
for _, option := range casesWithDefault {
val, err := h.secretSource.FieldValueFromRequiredSecret(option.secretName, option.secretField, option.defValue)
if err != nil {
return err
}
*option.field = val
}
return nil
}
func (h *HighAvailabilityOptionsProvider) setSystemRedisOptions() error {
cases := []struct {
field *string
secretName string
secretField string
}{
{
&h.options.SystemRedisURL,
component.SystemSecretSystemRedisSecretName,
component.SystemSecretSystemRedisURLFieldName,
},
}
for _, option := range cases {
val, err := h.secretSource.RequiredFieldValueFromRequiredSecret(option.secretName, option.secretField)
if err != nil {
return err
}
*option.field = val
}
// Optional fields
casesWithDefault := []struct {
field *string
secretName string
secretField string
defValue string
}{
{
&h.options.SystemRedisSentinelsHosts,
component.SystemSecretSystemRedisSecretName,
component.SystemSecretSystemRedisSentinelHosts,
component.DefaultSystemRedisSentinelHosts(),
},
{
&h.options.SystemRedisSentinelsRole,
component.SystemSecretSystemRedisSecretName,
component.SystemSecretSystemRedisSentinelRole,
component.DefaultSystemRedisSentinelRole(),
},
{
&h.options.SystemRedisNamespace,
component.SystemSecretSystemRedisSecretName,
component.SystemSecretSystemRedisNamespace,
component.DefaultSystemRedisNamespace(),
},
}
for _, option := range casesWithDefault {
val, err := h.secretSource.FieldValueFromRequiredSecret(option.secretName, option.secretField, option.defValue)
if err != nil {
return err
}
*option.field = val
}
return nil
}
func (h *HighAvailabilityOptionsProvider) setSystemDatabaseOptions() error {
val, err := h.secretSource.RequiredFieldValueFromRequiredSecret(
component.SystemSecretSystemDatabaseSecretName, component.SystemSecretSystemDatabaseURLFieldName)
if err != nil {
return err
}
h.options.SystemDatabaseURL = val
return nil
}
func (h *HighAvailabilityOptionsProvider) backendRedisLabels() map[string]string {
return map[string]string{
"app": *h.apimanager.Spec.AppLabel,
"threescale_component": "backend",
}
}
func (h *HighAvailabilityOptionsProvider) SystemDatabaseLabels() map[string]string {
return map[string]string{
"app": *h.apimanager.Spec.AppLabel,
"threescale_component": "system",
}
}