forked from OT-CONTAINER-KIT/redis-operator
/
redis_controller.go
101 lines (86 loc) · 3.3 KB
/
redis_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
/*
Copyright 2020 Opstree Solutions.
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"
"time"
"github.com/elrondwong/redis-operator/api/status"
redisv1beta2 "github.com/elrondwong/redis-operator/api/v1beta2"
"github.com/elrondwong/redis-operator/k8sutils"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// RedisReconciler reconciles a Redis object
type RedisReconciler struct {
client.Client
K8sClient kubernetes.Interface
Dk8sClinet dynamic.Interface
Log logr.Logger
Scheme *runtime.Scheme
}
// Reconcile is part of the main kubernetes reconciliation loop which aims
func (r *RedisReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
reqLogger := r.Log.WithValues("Request.Namespace", req.Namespace, "Request.Name", req.Name)
reqLogger.Info("Reconciling opstree redis controller")
instance := &redisv1beta2.Redis{}
err := r.Client.Get(context.TODO(), req.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
if _, found := instance.ObjectMeta.GetAnnotations()["redis.opstreelabs.in/skip-reconcile"]; found {
reqLogger.Info("Found annotations redis.opstreelabs.in/skip-reconcile, so skipping reconcile")
return ctrl.Result{RequeueAfter: time.Second * 10}, nil
}
if err = k8sutils.HandleRedisFinalizer(r.Client, r.K8sClient, r.Log, instance); err != nil {
return ctrl.Result{}, err
}
if err = k8sutils.AddRedisFinalizer(instance, r.Client); err != nil {
return ctrl.Result{}, err
}
if instance.Status.State != status.RedisStandaloneReady {
err = k8sutils.UpdateRedisStandaloneStatus(instance, status.RedisStandaloneInitializing, status.InitializingStandaloneReason)
if err != nil {
return ctrl.Result{RequeueAfter: time.Second * 10}, err
}
}
err = k8sutils.CreateStandaloneRedis(instance)
if err != nil {
return ctrl.Result{}, err
}
err = k8sutils.CreateStandaloneService(instance)
if err != nil {
return ctrl.Result{}, err
}
if instance.Status.State != status.RedisStandaloneReady && k8sutils.CheckRedisStandaloneReady(instance) {
err = k8sutils.UpdateRedisStandaloneStatus(instance, status.RedisStandaloneReady, status.ReadyStandaloneReason)
if err != nil {
return ctrl.Result{RequeueAfter: time.Second * 10}, err
}
}
reqLogger.Info("Will reconcile redis operator in again 10 seconds")
return ctrl.Result{RequeueAfter: time.Second * 10}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *RedisReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&redisv1beta2.Redis{}).
Complete(r)
}