-
Notifications
You must be signed in to change notification settings - Fork 18
/
serviceintegration_controller.go
175 lines (141 loc) · 5.19 KB
/
serviceintegration_controller.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
// Copyright (c) 2022 Aiven, Helsinki, Finland. https://aiven.io/
package controllers
import (
"context"
"fmt"
"strconv"
"strings"
"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"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/aiven/aiven-operator/api/v1alpha1"
)
// ServiceIntegrationReconciler reconciles a ServiceIntegration object
type ServiceIntegrationReconciler struct {
Controller
}
type ServiceIntegrationHandler struct{}
// +kubebuilder:rbac:groups=aiven.io,resources=serviceintegrations,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=aiven.io,resources=serviceintegrations/status,verbs=get;update;patch
func (r *ServiceIntegrationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return r.reconcileInstance(ctx, req, ServiceIntegrationHandler{}, &v1alpha1.ServiceIntegration{})
}
func (r *ServiceIntegrationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.ServiceIntegration{}).
Complete(r)
}
func (h ServiceIntegrationHandler) createOrUpdate(avn *aiven.Client, i client.Object, refs []client.Object) error {
si, err := h.convert(i)
if err != nil {
return err
}
var integration *aiven.ServiceIntegration
var reason string
if si.Status.ID == "" {
integration, err = avn.ServiceIntegrations.Create(
si.Spec.Project,
aiven.CreateServiceIntegrationRequest{
DestinationEndpointID: toOptionalStringPointer(si.Spec.DestinationEndpointID),
DestinationService: toOptionalStringPointer(si.Spec.DestinationServiceName),
IntegrationType: si.Spec.IntegrationType,
SourceEndpointID: toOptionalStringPointer(si.Spec.SourceEndpointID),
SourceService: toOptionalStringPointer(si.Spec.SourceServiceName),
UserConfig: h.getUserConfig(si),
},
)
if err != nil {
return fmt.Errorf("cannot createOrUpdate service integration: %w", err)
}
reason = "Created"
} else {
integration, err = avn.ServiceIntegrations.Update(
si.Spec.Project,
si.Status.ID,
aiven.UpdateServiceIntegrationRequest{
UserConfig: h.getUserConfig(si),
},
)
reason = "Updated"
if err != nil {
if strings.Contains(err.Error(), "user config not changed") {
return nil
}
return err
}
}
si.Status.ID = integration.ServiceIntegrationID
meta.SetStatusCondition(&si.Status.Conditions,
getInitializedCondition(reason,
"Instance was created or update on Aiven side"))
meta.SetStatusCondition(&si.Status.Conditions,
getRunningCondition(metav1.ConditionUnknown, reason,
"Instance was created or update on Aiven side, status remains unknown"))
metav1.SetMetaDataAnnotation(&si.ObjectMeta,
processedGenerationAnnotation, strconv.FormatInt(si.GetGeneration(), formatIntBaseDecimal))
return nil
}
func (h ServiceIntegrationHandler) delete(avn *aiven.Client, i client.Object) (bool, error) {
si, err := h.convert(i)
if err != nil {
return false, err
}
err = avn.ServiceIntegrations.Delete(si.Spec.Project, si.Status.ID)
if err != nil && !aiven.IsNotFound(err) {
return false, fmt.Errorf("aiven client delete service ingtegration error: %w", err)
}
return true, nil
}
func (h ServiceIntegrationHandler) get(_ *aiven.Client, i client.Object) (*corev1.Secret, error) {
si, err := h.convert(i)
if err != nil {
return nil, err
}
meta.SetStatusCondition(&si.Status.Conditions,
getRunningCondition(metav1.ConditionTrue, "CheckRunning",
"Instance is running on Aiven side"))
metav1.SetMetaDataAnnotation(&si.ObjectMeta, instanceIsRunningAnnotation, "true")
return nil, nil
}
func (h ServiceIntegrationHandler) checkPreconditions(avn *aiven.Client, i client.Object) (bool, error) {
si, err := h.convert(i)
if err != nil {
return false, err
}
meta.SetStatusCondition(&si.Status.Conditions,
getInitializedCondition("Preconditions", "Checking preconditions"))
sourceCheck, err := checkServiceIsRunning(avn, si.Spec.Project, si.Spec.SourceServiceName)
if err != nil {
return false, err
}
destinationCheck, err := checkServiceIsRunning(avn, si.Spec.Project, si.Spec.DestinationServiceName)
if err != nil {
return false, err
}
return sourceCheck && destinationCheck, nil
}
func (h ServiceIntegrationHandler) convert(i client.Object) (*v1alpha1.ServiceIntegration, error) {
si, ok := i.(*v1alpha1.ServiceIntegration)
if !ok {
return nil, fmt.Errorf("cannot convert object to ServiceIntegration")
}
return si, nil
}
func (h ServiceIntegrationHandler) getUserConfig(int *v1alpha1.ServiceIntegration) map[string]interface{} {
if int.Spec.IntegrationType == "datadog" {
return UserConfigurationToAPI(int.Spec.DatadogUserConfig).(map[string]interface{})
}
if int.Spec.IntegrationType == "kafka_connect" {
return UserConfigurationToAPI(int.Spec.KafkaConnectUserConfig).(map[string]interface{})
}
if int.Spec.IntegrationType == "kafka_logs" {
return UserConfigurationToAPI(int.Spec.KafkaLogsUserConfig).(map[string]interface{})
}
if int.Spec.IntegrationType == "metrics" {
return UserConfigurationToAPI(int.Spec.MetricsUserConfig).(map[string]interface{})
}
return nil
}