-
Notifications
You must be signed in to change notification settings - Fork 20
/
kafkaschema_controller.go
201 lines (167 loc) · 5.82 KB
/
kafkaschema_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
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
// Copyright (c) 2024 Aiven, Helsinki, Finland. https://aiven.io/
package controllers
import (
"context"
"fmt"
"strconv"
"time"
"github.com/aiven/aiven-go-client/v2"
avngen "github.com/aiven/go-client-codegen"
"github.com/aiven/go-client-codegen/handler/kafkaschemaregistry"
"github.com/avast/retry-go"
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"
)
// KafkaSchemaReconciler reconciles a KafkaSchema object
type KafkaSchemaReconciler struct {
Controller
}
func newKafkaSchemaReconciler(c Controller) reconcilerType {
return &KafkaSchemaReconciler{Controller: c}
}
type KafkaSchemaHandler struct{}
//+kubebuilder:rbac:groups=aiven.io,resources=kafkaschemas,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=aiven.io,resources=kafkaschemas/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=aiven.io,resources=kafkaschemas/finalizers,verbs=get;create;update
func (r *KafkaSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return r.reconcileInstance(ctx, req, KafkaSchemaHandler{}, &v1alpha1.KafkaSchema{})
}
func (r *KafkaSchemaReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.KafkaSchema{}).
Complete(r)
}
func (h KafkaSchemaHandler) createOrUpdate(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object, _ []client.Object) error {
schema, err := h.convert(obj)
if err != nil {
return err
}
// Must poll kafka until the registry ready.
// The client retries errors under the hood.
_, err = avnGen.ServiceSchemaRegistrySubjectVersionsGet(
ctx,
schema.Spec.Project,
schema.Spec.ServiceName,
schema.Spec.SubjectName,
)
if err != nil && !isNotFound(err) {
return err
}
schemaID, err := avnGen.ServiceSchemaRegistrySubjectVersionPost(
ctx,
schema.Spec.Project,
schema.Spec.ServiceName,
schema.Spec.SubjectName,
&kafkaschemaregistry.ServiceSchemaRegistrySubjectVersionPostIn{
Schema: schema.Spec.Schema,
SchemaType: schema.Spec.SchemaType,
},
)
if err != nil {
return fmt.Errorf("cannot add Kafka Schema Subject: %w", err)
}
// set compatibility level if defined for a newly created Kafka Schema Subject
if schema.Spec.CompatibilityLevel != "" {
_, err := avnGen.ServiceSchemaRegistrySubjectConfigPut(
ctx,
schema.Spec.Project,
schema.Spec.ServiceName,
schema.Spec.SubjectName,
&kafkaschemaregistry.ServiceSchemaRegistrySubjectConfigPutIn{
Compatibility: schema.Spec.CompatibilityLevel,
},
)
if err != nil {
return fmt.Errorf("cannot update Kafka Schema Configuration: %w", err)
}
}
// Gets the last version
// Because of eventual consistency, we must poll the subject
const (
pollDelay = 5 * time.Second
pollAttempts = 10
)
err = retry.Do(
func() error {
version, err := getKafkaSchemaVersion(
ctx,
avnGen,
schemaID,
schema.Spec.Project,
schema.Spec.ServiceName,
schema.Spec.SubjectName,
)
schema.Status.Version = version
return err
},
retry.Context(ctx),
retry.RetryIf(isNotFound),
retry.Delay(pollDelay),
retry.Attempts(pollAttempts),
)
if err != nil {
return fmt.Errorf("cannot get Kafka Schema Subject version: %w", err)
}
meta.SetStatusCondition(&schema.Status.Conditions,
getInitializedCondition("Added",
"Successfully created or updated the instance in Aiven"))
meta.SetStatusCondition(&schema.Status.Conditions,
getRunningCondition(metav1.ConditionUnknown, "Added",
"Successfully created or updated the instance in Aiven, status remains unknown"))
metav1.SetMetaDataAnnotation(&schema.ObjectMeta,
processedGenerationAnnotation, strconv.FormatInt(schema.GetGeneration(), formatIntBaseDecimal))
return nil
}
func (h KafkaSchemaHandler) delete(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (bool, error) {
schema, err := h.convert(obj)
if err != nil {
return false, err
}
err = avnGen.ServiceSchemaRegistrySubjectDelete(ctx, schema.Spec.Project, schema.Spec.ServiceName, schema.Spec.SubjectName)
return isDeleted(err)
}
func (h KafkaSchemaHandler) get(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (*corev1.Secret, error) {
schema, err := h.convert(obj)
if err != nil {
return nil, err
}
meta.SetStatusCondition(&schema.Status.Conditions,
getRunningCondition(metav1.ConditionTrue, "CheckRunning",
"Instance is running on Aiven side"))
metav1.SetMetaDataAnnotation(&schema.ObjectMeta, instanceIsRunningAnnotation, "true")
return nil, nil
}
func (h KafkaSchemaHandler) checkPreconditions(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (bool, error) {
schema, err := h.convert(obj)
if err != nil {
return false, err
}
return checkServiceIsOperational(ctx, avnGen, schema.Spec.Project, schema.Spec.ServiceName)
}
func (h KafkaSchemaHandler) convert(i client.Object) (*v1alpha1.KafkaSchema, error) {
schema, ok := i.(*v1alpha1.KafkaSchema)
if !ok {
return nil, fmt.Errorf("cannot convert object to KafkaSchema")
}
return schema, nil
}
func getKafkaSchemaVersion(ctx context.Context, client avngen.Client, schemaID int, projectName, serviceName, subjectName string) (int, error) {
versions, err := client.ServiceSchemaRegistrySubjectVersionsGet(ctx, projectName, serviceName, subjectName)
if err != nil {
return 0, err
}
for _, v := range versions {
version, err := client.ServiceSchemaRegistrySubjectVersionGet(ctx, projectName, serviceName, subjectName, v)
if err != nil {
return 0, err
}
if version.Id == schemaID {
return version.Version, nil
}
}
return 0, NewNotFound(fmt.Sprintf("the schema with id %d not found", schemaID))
}