-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseableclusterchaos_controller.go
More file actions
105 lines (91 loc) · 3.38 KB
/
Copy pathparseableclusterchaos_controller.go
File metadata and controls
105 lines (91 loc) · 3.38 KB
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
/*
Copyright (c) 2024-2026 Parseable, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package parseableclusterchoascontroller
import (
"context"
"fmt"
"os"
"time"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
metricsclient "k8s.io/metrics/pkg/client/clientset/versioned"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
v1 "parseablehq/parseable-operator/api/v1"
)
// ParseableClusterChoasReconciler reconciles a ParseableClusterChoas object
type ParseableClusterChaosReconciler struct {
client.Client
Scheme *runtime.Scheme
ReconcileWait time.Duration
metricsClientset *metricsclient.Clientset
Recorder record.EventRecorder
}
func NewParseableClusterChaosReconciler(mgr ctrl.Manager) *ParseableClusterChaosReconciler {
metricsClient, err := metricsclient.NewForConfig(mgr.GetConfig())
if err != nil {
fmt.Printf("Failed to initalise metrics client for parseable cluster autoscaler")
os.Exit(1)
}
return &ParseableClusterChaosReconciler{
Client: mgr.GetClient(),
metricsClientset: metricsClient,
Scheme: mgr.GetScheme(),
ReconcileWait: lookupReconcileTime(),
Recorder: mgr.GetEventRecorderFor("parseableclusterchaos-controller"),
}
}
// +kubebuilder:rbac:groups=parseable.com,resources=parseableclusterschaos,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=parseable.com,resources=parseableclusterschaos/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=parseable.com,resources=parseableclusterschaos/finalizers,verbs=update
// +kubebuilder:resource:shortNames=pbcc;pbccs
func (r *ParseableClusterChaosReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logr := log.FromContext(ctx)
parseableClusterChaosCR := &v1.ParseableClusterChaos{}
err := r.Get(context.TODO(), req.NamespacedName, parseableClusterChaosCR)
if err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
if err := r.do(ctx, parseableClusterChaosCR); err != nil {
logr.Error(err, err.Error())
return ctrl.Result{}, err
} else {
return ctrl.Result{RequeueAfter: r.ReconcileWait}, nil
}
}
// SetupWithManager sets up the controller with the Manager.
func (r *ParseableClusterChaosReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1.ParseableClusterChaos{}).
//WithEventFilter(GenericPredicates{}).
Complete(r)
}
func lookupReconcileTime() time.Duration {
val, exists := os.LookupEnv("PB_CHAOS_RECONCILE_WAIT")
if !exists {
return time.Second * 20
} else {
v, err := time.ParseDuration(val)
if err != nil {
// Exit Program if not valid
os.Exit(1)
}
return v
}
}