-
Notifications
You must be signed in to change notification settings - Fork 1
/
metricbeat_reconciler.go
73 lines (64 loc) · 2.31 KB
/
metricbeat_reconciler.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
package kibana
import (
"context"
"emperror.dev/errors"
"github.com/disaster37/operator-sdk-extra/pkg/apis/shared"
"github.com/disaster37/operator-sdk-extra/pkg/controller"
"github.com/disaster37/operator-sdk-extra/pkg/helper"
"github.com/disaster37/operator-sdk-extra/pkg/object"
"github.com/sirupsen/logrus"
beatcrd "github.com/webcenter-fr/elasticsearch-operator/apis/beat/v1"
kibanacrd "github.com/webcenter-fr/elasticsearch-operator/apis/kibana/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
MetricbeatCondition shared.ConditionName = "MetricbeatReady"
MetricbeatPhase shared.PhaseName = "Metricbeat"
)
type metricbeatReconciler struct {
controller.BaseReconciler
controller.MultiPhaseStepReconcilerAction
}
func newMetricbeatReconciler(client client.Client, logger *logrus.Entry, recorder record.EventRecorder) (multiPhaseStepReconcilerAction controller.MultiPhaseStepReconcilerAction) {
return &metricbeatReconciler{
MultiPhaseStepReconcilerAction: controller.NewBasicMultiPhaseStepReconcilerAction(
client,
MetricbeatPhase,
MetricbeatCondition,
logger,
recorder,
),
BaseReconciler: controller.BaseReconciler{
Client: client,
Recorder: recorder,
Log: logger,
},
}
}
// Read existing Metricbeat
func (r *metricbeatReconciler) Read(ctx context.Context, resource object.MultiPhaseObject, data map[string]any) (read controller.MultiPhaseRead, res ctrl.Result, err error) {
o := resource.(*kibanacrd.Kibana)
metricbeat := &beatcrd.Metricbeat{}
read = controller.NewBasicMultiPhaseRead()
// Read current metricbeat
if err = r.Client.Get(ctx, types.NamespacedName{Namespace: o.Namespace, Name: GetMetricbeatName(o)}, metricbeat); err != nil {
if !k8serrors.IsNotFound(err) {
return read, res, errors.Wrapf(err, "Error when read metricbeat")
}
metricbeat = nil
}
if metricbeat != nil {
read.SetCurrentObjects([]client.Object{metricbeat})
}
// Generate expected metricbeat
expectedMetricbeats, err := buildMetricbeats(o)
if err != nil {
return read, res, errors.Wrap(err, "Error when generate metricbeat")
}
read.SetExpectedObjects(helper.ToSliceOfObject(expectedMetricbeats))
return read, res, nil
}