-
Notifications
You must be signed in to change notification settings - Fork 17
/
cache_controller.go
144 lines (123 loc) · 4.8 KB
/
cache_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
package hazelcast
import (
"context"
"reflect"
"time"
"github.com/go-logr/logr"
"github.com/hazelcast/hazelcast-go-client"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
hazelcastv1alpha1 "github.com/hazelcast/hazelcast-platform-operator/api/v1alpha1"
recoptions "github.com/hazelcast/hazelcast-platform-operator/internal/controller"
hzclient "github.com/hazelcast/hazelcast-platform-operator/internal/hazelcast-client"
"github.com/hazelcast/hazelcast-platform-operator/internal/protocol/codec"
codecTypes "github.com/hazelcast/hazelcast-platform-operator/internal/protocol/types"
)
// CacheReconciler reconciles a Cache object
type CacheReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
phoneHomeTrigger chan struct{}
clientRegistry hzclient.ClientRegistry
}
func NewCacheReconciler(c client.Client, log logr.Logger, s *runtime.Scheme, pht chan struct{}, cr *hzclient.HazelcastClientRegistry) *CacheReconciler {
return &CacheReconciler{
Client: c,
Log: log,
Scheme: s,
phoneHomeTrigger: pht,
clientRegistry: cr,
}
}
//+kubebuilder:rbac:groups=hazelcast.com,resources=caches,verbs=get;list;watch;create;update;patch;delete,namespace=watched
//+kubebuilder:rbac:groups=hazelcast.com,resources=caches/status,verbs=get;update;patch,namespace=watched
//+kubebuilder:rbac:groups=hazelcast.com,resources=caches/finalizers,verbs=update,namespace=watched
func (r *CacheReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := r.Log.WithValues("hazelcast-cache", req.NamespacedName)
c := &hazelcastv1alpha1.Cache{}
cl, res, err := initialSetupDS(ctx, r.Client, req.NamespacedName, c, r.Update, r.clientRegistry, logger)
if cl == nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return res, nil
}
ms, err := r.ReconcileCacheConfig(ctx, c, cl, logger)
if err != nil {
return updateDSStatus(ctx, r.Client, c, recoptions.RetryAfter(retryAfterForDataStructures),
withDSState(hazelcastv1alpha1.DataStructurePending),
withDSMessage(err.Error()),
withDSMemberStatuses(ms))
}
requeue, err := updateDSStatus(ctx, r.Client, c, recoptions.RetryAfter(1*time.Second),
withDSState(hazelcastv1alpha1.DataStructurePersisting),
withDSMessage("Persisting the applied cache config."),
withDSMemberStatuses(ms))
if err != nil {
return requeue, err
}
persisted, err := r.validateCacheConfigPersistence(ctx, c)
if err != nil {
return updateDSStatus(ctx, r.Client, c, recoptions.Error(err),
withDSFailedState(err.Error()))
}
if !persisted {
return updateDSStatus(ctx, r.Client, c, recoptions.RetryAfter(1*time.Second),
withDSState(hazelcastv1alpha1.DataStructurePersisting),
withDSMessage("Waiting for Cache Config to be persisted."),
withDSMemberStatuses(ms))
}
return finalSetupDS(ctx, r.Client, r.phoneHomeTrigger, c, logger)
}
func (r *CacheReconciler) ReconcileCacheConfig(
ctx context.Context,
c *hazelcastv1alpha1.Cache,
cl hzclient.Client,
logger logr.Logger,
) (map[string]hazelcastv1alpha1.DataStructureConfigState, error) {
var req *hazelcast.ClientMessage
cacheInput := codecTypes.DefaultCacheConfigInput()
fillCacheConfigInput(cacheInput, c)
req = codec.EncodeDynamicConfigAddCacheConfigRequest(cacheInput)
return sendCodecRequest(ctx, cl, c, req, logger)
}
func fillCacheConfigInput(cacheInput *codecTypes.CacheConfigInput, c *hazelcastv1alpha1.Cache) {
cacheInput.Name = c.GetDSName()
cs := c.Spec
cacheInput.BackupCount = *cs.BackupCount
cacheInput.AsyncBackupCount = cs.AsyncBackupCount
cacheInput.KeyType = cs.KeyType
cacheInput.ValueType = cs.ValueType
cacheInput.HotRestartConfig.Enabled = cs.PersistenceEnabled
cacheInput.InMemoryFormat = codecTypes.InMemoryFormat(cs.InMemoryFormat)
if c.Spec.EventJournal != nil {
cacheInput.EventJournalConfig.Enabled = true
cacheInput.EventJournalConfig.Capacity = c.Spec.EventJournal.Capacity
cacheInput.EventJournalConfig.TimeToLiveSeconds = c.Spec.EventJournal.TimeToLiveSeconds
}
cacheInput.UserCodeNamespace = c.Spec.UserCodeNamespace
}
func (r *CacheReconciler) validateCacheConfigPersistence(ctx context.Context, c *hazelcastv1alpha1.Cache) (bool, error) {
hzConfig, err := getHazelcastConfig(ctx, r.Client, getHzNamespacedName(c))
if err != nil {
return false, err
}
ccfg, ok := hzConfig.Hazelcast.Cache[c.GetDSName()]
if !ok {
return false, nil
}
currentQCfg := createCacheConfig(c)
if !reflect.DeepEqual(ccfg, currentQCfg) {
return false, nil
}
return true, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *CacheReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&hazelcastv1alpha1.Cache{}).
Complete(r)
}