-
Notifications
You must be signed in to change notification settings - Fork 20
/
generic_service_handler.go
215 lines (185 loc) · 6.53 KB
/
generic_service_handler.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
package controllers
import (
"fmt"
"strconv"
"github.com/aiven/aiven-go-client"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/aiven/aiven-operator/api/v1alpha1"
)
func newGenericServiceHandler(fabric serviceAdapterFabric) Handlers {
return &genericServiceHandler{fabric: fabric}
}
// genericServiceHandler provides common CRUD management for all service types using serviceAdapter,
// which turns specific service (mysql, redis) into a generic.
type genericServiceHandler struct {
fabric serviceAdapterFabric
}
func (h *genericServiceHandler) createOrUpdate(a *aiven.Client, object client.Object, refs []client.Object) error {
o, err := h.fabric(a, object)
if err != nil {
return err
}
spec := o.getServiceCommonSpec()
ometa := o.getObjectMeta()
// Project id reference
// Could be right in spec or referenced (has ref)
projectVPCID := spec.ProjectVPCID
if projectVPCID == "" {
if p := v1alpha1.FindProjectVPC(refs); p != nil {
projectVPCID = p.Status.ID
}
}
_, err = a.Services.Get(spec.Project, ometa.Name)
exists := err == nil
if !exists && !aiven.IsNotFound(err) {
return fmt.Errorf("failed to fetch service: %w", err)
}
// Creates if not exists or updates existing service
var reason string
if !exists {
reason = "Created"
userConfig, err := UserConfigurationToAPIV2(o.getUserConfig(), []string{"create", "update"})
if err != nil {
return err
}
req := aiven.CreateServiceRequest{
Cloud: spec.CloudName,
DiskSpaceMB: v1alpha1.ConvertDiscSpace(o.getDiskSpace()),
MaintenanceWindow: getMaintenanceWindow(spec.MaintenanceWindowDow, spec.MaintenanceWindowTime),
Plan: spec.Plan,
ProjectVPCID: toOptionalStringPointer(projectVPCID),
ServiceIntegrations: nil,
ServiceName: ometa.Name,
ServiceType: o.getServiceType(),
TerminationProtection: fromAnyPointer(spec.TerminationProtection),
UserConfig: userConfig,
}
for _, s := range spec.ServiceIntegrations {
i := aiven.NewServiceIntegration{
IntegrationType: s.IntegrationType,
SourceService: &s.SourceServiceName,
// todo: fix in go client, sends None
UserConfig: make(map[string]interface{}),
}
req.ServiceIntegrations = append(req.ServiceIntegrations, i)
}
_, err = a.Services.Create(spec.Project, req)
if err != nil {
return fmt.Errorf("failed to create service: %w", err)
}
} else {
reason = "Updated"
userConfig, err := UserConfigurationToAPIV2(o.getUserConfig(), []string{"update"})
if err != nil {
return err
}
req := aiven.UpdateServiceRequest{
Cloud: spec.CloudName,
DiskSpaceMB: v1alpha1.ConvertDiscSpace(o.getDiskSpace()),
MaintenanceWindow: getMaintenanceWindow(spec.MaintenanceWindowDow, spec.MaintenanceWindowTime),
Plan: spec.Plan,
Powered: true,
ProjectVPCID: toOptionalStringPointer(projectVPCID),
TerminationProtection: fromAnyPointer(spec.TerminationProtection),
UserConfig: userConfig,
}
_, err = a.Services.Update(spec.Project, ometa.Name, req)
if err != nil {
return fmt.Errorf("failed to update service: %w", err)
}
}
// Updates tags.
// Four scenarios: service created/updated * with/without tags
// By sending empty tags it clears existing list
req := aiven.ServiceTagsRequest{
Tags: make(map[string]string),
}
if spec.Tags != nil {
req.Tags = spec.Tags
}
_, err = a.ServiceTags.Set(spec.Project, ometa.Name, req)
if err != nil {
return fmt.Errorf("failed to update tags: %w", err)
}
status := o.getServiceStatus()
meta.SetStatusCondition(&status.Conditions,
getInitializedCondition(reason, "Instance was created or update on Aiven side"))
meta.SetStatusCondition(&status.Conditions,
getRunningCondition(metav1.ConditionUnknown, reason, "Instance was created or update on Aiven side, status remains unknown"))
metav1.SetMetaDataAnnotation(
o.getObjectMeta(),
processedGenerationAnnotation,
strconv.FormatInt(object.GetGeneration(), formatIntBaseDecimal),
)
return nil
}
func (h *genericServiceHandler) delete(a *aiven.Client, object client.Object) (bool, error) {
o, err := h.fabric(a, object)
if err != nil {
return false, err
}
spec := o.getServiceCommonSpec()
if fromAnyPointer(spec.TerminationProtection) {
return false, errTerminationProtectionOn
}
err = a.Services.Delete(spec.Project, o.getObjectMeta().Name)
if err == nil || aiven.IsNotFound(err) {
return true, nil
}
return false, fmt.Errorf("failed to delete service in Aiven: %w", err)
}
func (h *genericServiceHandler) get(a *aiven.Client, object client.Object) (*corev1.Secret, error) {
o, err := h.fabric(a, object)
if err != nil {
return nil, err
}
s, err := a.Services.Get(o.getServiceCommonSpec().Project, o.getObjectMeta().Name)
if err != nil {
return nil, fmt.Errorf("failed to get service from Aiven: %w", err)
}
status := o.getServiceStatus()
status.State = s.State
if s.State == "RUNNING" {
meta.SetStatusCondition(&status.Conditions,
getRunningCondition(metav1.ConditionTrue, "CheckRunning", "Instance is running on Aiven side"))
metav1.SetMetaDataAnnotation(o.getObjectMeta(), instanceIsRunningAnnotation, "true")
// Some services get secrets after they are running only,
// like ip addresses (hosts)
return o.newSecret(s)
}
return nil, nil
}
// checkPreconditions not required for now by services to be implemented
func (h *genericServiceHandler) checkPreconditions(a *aiven.Client, object client.Object) (bool, error) {
o, err := h.fabric(a, object)
if err != nil {
return false, err
}
spec := o.getServiceCommonSpec()
for _, s := range spec.ServiceIntegrations {
// Validates that read_replica is running
// If not, the wrapper controller will try later
if s.IntegrationType == "read_replica" {
r, err := checkServiceIsRunning(a, spec.Project, s.SourceServiceName)
if !(r && err == nil) {
return false, nil
}
}
}
return true, nil
}
// serviceAdapterFabric returns serviceAdapter for specific service, like MySQL
type serviceAdapterFabric func(*aiven.Client, client.Object) (serviceAdapter, error)
// serviceAdapter turns client.Object into a generic thing
type serviceAdapter interface {
getObjectMeta() *metav1.ObjectMeta
getServiceStatus() *v1alpha1.ServiceStatus
getServiceCommonSpec() *v1alpha1.ServiceCommonSpec
getServiceType() string
getDiskSpace() string
getUserConfig() any
newSecret(*aiven.Service) (*corev1.Secret, error)
}