-
Notifications
You must be signed in to change notification settings - Fork 69
/
runner.go
138 lines (115 loc) · 4.29 KB
/
runner.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
/*
Copyright 2020 The OpenEBS Authors.
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 volumereplica
import (
"fmt"
"github.com/openebs/cstor-operators/pkg/controllers/common"
errors "github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/klog"
)
// Run will set up the event handlers for types we are interested in, as well
// as syncing informer caches and starting workers. It will block until stopCh
// is closed, at which point it will shutdown the workqueue and wait for
// workers to finish processing their current work items.
func (c *CStorVolumeReplicaController) Run(threadiness int, stopCh <-chan struct{}) error {
defer runtime.HandleCrash()
defer c.workqueue.ShutDown()
// Start the informer factories to begin populating the informer caches
klog.Info("Starting CStorVolumeReplica controller")
// Wait for the k8s caches to be synced before starting workers
klog.Info("Waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(stopCh, c.cStorReplicaSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
klog.Info("Starting CStorVolumeReplica workers")
// Launch two workers to process CStorReplica resources
for i := 0; i < threadiness; i++ {
go wait.Until(c.runWorker, common.ResourceWorkerInterval, stopCh)
}
klog.Info("Started CStorVolumeReplica workers")
<-stopCh
klog.Info("Shutting down CStorVolumeReplica workers")
return nil
}
// runWorker is a long-running function that will continually call the
// processNextWorkItem function in order to read and process a message on the
// workqueue.
func (c *CStorVolumeReplicaController) runWorker() {
for c.processNextWorkItem() {
}
}
// processNextWorkItem will read a single work item off the workqueue and
// attempt to process it, by calling the syncHandler.
func (c *CStorVolumeReplicaController) processNextWorkItem() bool {
obj, shutdown := c.workqueue.Get()
if shutdown {
return false
}
// We wrap this block in a func so we can defer c.workqueue.Done.
err := func(obj interface{}) error {
// We call Done here so the workqueue knows we have finished
// processing this item. We also must remember to call Forget if we
// do not want this work item being re-queued. For example, we do
// not call Forget if a transient error occurs, instead the item is
// put back on the workqueue and attempted again after a back-off
// period.
defer c.workqueue.Done(obj)
var q common.QueueLoad
var ok bool
// We expect a particular object type to come off the workqueue.
// These are of the form namespace/name. We do this as the delayed
// nature of the workqueue means the items in the informer cache
// may actually be more up to date that when the item was initially
// put onto the workqueue.
if q, ok = obj.(common.QueueLoad); !ok {
// As the item in the workqueue is actually invalid, we call
// Forget here else we'd go into a loop of attempting to
// process a work item that is invalid.
c.workqueue.Forget(obj)
runtime.HandleError(
errors.Errorf(
"failed to process workqueue: got unsupported obj {%#v}",
obj,
),
)
return nil
}
// run syncHandler, passing it the namespace/name string
// of cvr resource to be synced
if err := c.syncHandler(q.Key, q.Operation); err != nil {
return errors.Wrapf(
err,
"failed to process workqueue item {%s} during {%s} operation",
q.Key,
string(q.Operation),
)
}
// Finally, if no error occurs we forget this item so that it
// does not get queued again until another change happens
c.workqueue.Forget(obj)
klog.V(4).Infof(
"workqueue item {%s} processed successfully for {%s} operation",
q.Key,
string(q.Operation),
)
return nil
}(obj)
if err != nil {
runtime.HandleError(err)
return true
}
return true
}