/
elasticsearchindextemplate_controller.go
252 lines (205 loc) · 8.92 KB
/
elasticsearchindextemplate_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
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
246
247
248
249
250
251
252
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
core "k8s.io/api/core/v1"
condition "k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
elkv1alpha1 "github.com/disaster37/operator-elk-extra/api/v1alpha1"
"github.com/disaster37/operator-elk-extra/pkg/elasticsearchhandler"
"github.com/disaster37/operator-sdk-extra/pkg/controller"
"github.com/disaster37/operator-sdk-extra/pkg/helper"
"github.com/disaster37/operator-sdk-extra/pkg/resource"
olivere "github.com/olivere/elastic/v7"
"github.com/pkg/errors"
)
const (
indexTemplateFinalizer = "index-template.elk.k8s.webcenter.fr/finalizer"
indexTemplateCondition = "UpdateIndexTemplate"
)
// ElasticsearchIndexTemplateReconciler reconciles a ElasticsearchIndexTemplate object
type ElasticsearchIndexTemplateReconciler struct {
Reconciler
client.Client
Scheme *runtime.Scheme
}
//+kubebuilder:rbac:groups=elk.k8s.webcenter.fr,resources=elasticsearchindextemplates,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=elk.k8s.webcenter.fr,resources=elasticsearchindextemplates/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=elk.k8s.webcenter.fr,resources=elasticsearchindextemplates/finalizers,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the ElasticsearchIndexTemplate object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.11.0/pkg/reconcile
func (r *ElasticsearchIndexTemplateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
reconciler, err := controller.NewStdReconciler(r.Client, indexTemplateFinalizer, r.reconciler, r.log, r.recorder, waitDurationWhenError)
if err != nil {
return ctrl.Result{}, err
}
template := &elkv1alpha1.ElasticsearchIndexTemplate{}
data := map[string]any{}
return reconciler.Reconcile(ctx, req, template, data)
}
// SetupWithManager sets up the controller with the Manager.
func (r *ElasticsearchIndexTemplateReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&elkv1alpha1.ElasticsearchIndexTemplate{}).
Complete(r)
}
// Configure permit to init Elasticsearch handler
// It also permit to init condition
func (r *ElasticsearchIndexTemplateReconciler) Configure(ctx context.Context, req ctrl.Request, resource resource.Resource) (meta any, err error) {
template := resource.(*elkv1alpha1.ElasticsearchIndexTemplate)
// Init condition status if not exist
if condition.FindStatusCondition(template.Status.Conditions, indexTemplateCondition) == nil {
condition.SetStatusCondition(&template.Status.Conditions, v1.Condition{
Type: indexTemplateCondition,
Status: v1.ConditionFalse,
Reason: "Initialize",
})
}
// Get elasticsearch handler / client
meta, err = GetElasticsearchHandler(ctx, &template.Spec, r.Client, r.dinamicClient, req, r.log)
if err != nil {
r.recorder.Eventf(resource, core.EventTypeWarning, "Failed", "Unable to init elasticsearch handler: %s", err.Error())
return nil, err
}
return meta, err
}
// Read permit to get current index template
func (r *ElasticsearchIndexTemplateReconciler) Read(ctx context.Context, resource resource.Resource, data map[string]any, meta any) (res ctrl.Result, err error) {
template := resource.(*elkv1alpha1.ElasticsearchIndexTemplate)
esHandler := meta.(elasticsearchhandler.ElasticsearchHandler)
// Read index template from Elasticsearch
currentTemplate, err := esHandler.IndexTemplateGet(template.Name)
if err != nil {
return res, errors.Wrap(err, "Unable to get index template from Elasticsearch")
}
data["template"] = currentTemplate
return res, nil
}
// Create add new index template
func (r *ElasticsearchIndexTemplateReconciler) Create(ctx context.Context, resource resource.Resource, data map[string]interface{}, meta interface{}) (res ctrl.Result, err error) {
esHandler := meta.(elasticsearchhandler.ElasticsearchHandler)
template := resource.(*elkv1alpha1.ElasticsearchIndexTemplate)
// Create index template on Elasticsearch
expectedTemplate, err := template.ToIndexTemplate()
if err != nil {
return res, errors.Wrap(err, "Error when convert to index template")
}
if err = esHandler.IndexTemplateUpdate(template.Name, expectedTemplate); err != nil {
return res, errors.Wrap(err, "Error when update index template")
}
return res, nil
}
// Update permit to update index template from Elasticsearch
func (r *ElasticsearchIndexTemplateReconciler) Update(ctx context.Context, resource resource.Resource, data map[string]interface{}, meta interface{}) (res ctrl.Result, err error) {
return r.Create(ctx, resource, data, meta)
}
// Delete permit to delete index template from Elasticsearch
func (r *ElasticsearchIndexTemplateReconciler) Delete(ctx context.Context, resource resource.Resource, data map[string]interface{}, meta interface{}) (err error) {
esHandler := meta.(elasticsearchhandler.ElasticsearchHandler)
template := resource.(*elkv1alpha1.ElasticsearchIndexTemplate)
if err = esHandler.IndexTemplateDelete(template.Name); err != nil {
return errors.Wrap(err, "Error when delete index template")
}
return nil
}
// Diff permit to check if diff between actual and expected index template exist
func (r *ElasticsearchIndexTemplateReconciler) Diff(resource resource.Resource, data map[string]interface{}, meta interface{}) (diff controller.Diff, err error) {
esHandler := meta.(elasticsearchhandler.ElasticsearchHandler)
template := resource.(*elkv1alpha1.ElasticsearchIndexTemplate)
var currentTemplate *olivere.IndicesGetIndexTemplate
var d any
d, err = helper.Get(data, "template")
if err != nil {
return diff, err
}
currentTemplate = d.(*olivere.IndicesGetIndexTemplate)
expectedTemplate, err := template.ToIndexTemplate()
if err != nil {
return diff, err
}
diff = controller.Diff{
NeedCreate: false,
NeedUpdate: false,
}
if currentTemplate == nil {
diff.NeedCreate = true
diff.Diff = "Index template not exist"
return diff, nil
}
diffStr, err := esHandler.IndexTemplateDiff(currentTemplate, expectedTemplate)
if err != nil {
return diff, err
}
if diffStr != "" {
diff.NeedUpdate = true
diff.Diff = diffStr
return diff, nil
}
return
}
// OnError permit to set status condition on the right state and record error
func (r *ElasticsearchIndexTemplateReconciler) OnError(ctx context.Context, resource resource.Resource, data map[string]any, meta any, err error) {
template := resource.(*elkv1alpha1.ElasticsearchIndexTemplate)
r.log.Error(err)
r.recorder.Event(resource, core.EventTypeWarning, "Failed", err.Error())
condition.SetStatusCondition(&template.Status.Conditions, v1.Condition{
Type: indexTemplateCondition,
Status: v1.ConditionFalse,
Reason: "Failed",
Message: err.Error(),
})
}
// OnSuccess permit to set status condition on the right state is everithink is good
func (r *ElasticsearchIndexTemplateReconciler) OnSuccess(ctx context.Context, resource resource.Resource, data map[string]any, meta any, diff controller.Diff) (err error) {
template := resource.(*elkv1alpha1.ElasticsearchIndexTemplate)
if diff.NeedCreate {
condition.SetStatusCondition(&template.Status.Conditions, v1.Condition{
Type: indexTemplateCondition,
Status: v1.ConditionTrue,
Reason: "Success",
Message: "Index template successfully created",
})
return nil
}
if diff.NeedUpdate {
condition.SetStatusCondition(&template.Status.Conditions, v1.Condition{
Type: indexTemplateCondition,
Status: v1.ConditionTrue,
Reason: "Success",
Message: "Index template successfully updated",
})
return nil
}
// Update condition status if needed
if condition.IsStatusConditionPresentAndEqual(template.Status.Conditions, indexTemplateCondition, v1.ConditionFalse) {
condition.SetStatusCondition(&template.Status.Conditions, v1.Condition{
Type: indexTemplateCondition,
Reason: "Success",
Status: v1.ConditionTrue,
Message: "Index template already set",
})
r.recorder.Event(resource, core.EventTypeNormal, "Completed", "Index template already set")
}
return nil
}