/
new_volume_controller.go
141 lines (118 loc) · 5.81 KB
/
new_volume_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
/*
Copyright 2018 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 volumecontroller
import (
"os"
corev1 "k8s.io/api/core/v1"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog"
"github.com/openebs/maya/cmd/cstor-volume-mgmt/controller/common"
apis "github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"
clientset "github.com/openebs/maya/pkg/client/generated/clientset/versioned"
openebsScheme "github.com/openebs/maya/pkg/client/generated/clientset/versioned/scheme"
informers "github.com/openebs/maya/pkg/client/generated/informers/externalversions"
)
const volumeControllerName = "CStorVolume"
// CStorVolumeController is the controller implementation for CStorVolume resources.
type CStorVolumeController struct {
// kubeclientset is a standard kubernetes clientset
kubeclientset kubernetes.Interface
// clientset is a openebs custom resource package generated for custom API group.
clientset clientset.Interface
// cStorVolumeSynced is used for caches sync to get populated
cStorVolumeSynced cache.InformerSynced
// workqueue is a rate limited work queue. This is used to queue work to be
// processed instead of performing it as soon as a change happens. This
// means we can ensure we only process a fixed amount of resources at a
// time, and makes it easy to ensure we are never processing the same item
// simultaneously in two different workers.
workqueue workqueue.RateLimitingInterface
// recorder is an event recorder for recording Event resources to the
// Kubernetes API.
recorder record.EventRecorder
}
// NewCStorVolumeController returns a new instance of CStorVolume controller
func NewCStorVolumeController(
kubeclientset kubernetes.Interface,
clientset clientset.Interface,
kubeInformerFactory kubeinformers.SharedInformerFactory,
cStorInformerFactory informers.SharedInformerFactory) *CStorVolumeController {
// obtain references to shared index informers for the cStorVolume resources
cStorVolumeInformer := cStorInformerFactory.Openebs().V1alpha1().CStorVolumes()
openebsScheme.AddToScheme(scheme.Scheme)
// Create event broadcaster to receive events and send them to any EventSink, watcher, or log.
// Add NewCstorVolumeController types to the default Kubernetes Scheme so Events can be
// logged for CstorVolume Controller types.
klog.Info("Creating event broadcaster")
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(klog.Infof)
// StartEventWatcher starts sending events received from this EventBroadcaster to the given
// event handler function. The return value can be ignored or used to stop recording, if
// desired. Events("") denotes empty namespace
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeclientset.CoreV1().Events("")})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: os.Getenv("POD_NAME"), Host: os.Getenv("NODE_NAME")})
controller := &CStorVolumeController{
kubeclientset: kubeclientset,
clientset: clientset,
cStorVolumeSynced: cStorVolumeInformer.Informer().HasSynced,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "CStorVolume"),
recorder: recorder,
}
klog.Info("Setting up event handlers")
// Instantiating QueueLoad before entering workqueue.
q := common.QueueLoad{}
// Set up an event handler for when CstorVolume resources change.
cStorVolumeInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if !IsValidCStorVolumeMgmt(obj.(*apis.CStorVolume)) {
return
}
q.Operation = common.QOpAdd
klog.Infof("Add event received for cstorvolume : %s", obj.(*apis.CStorVolume).Name)
controller.enqueueCStorVolume(obj.(*apis.CStorVolume), q)
},
UpdateFunc: func(old, new interface{}) {
newCStorVolume := new.(*apis.CStorVolume)
oldCStorVolume := old.(*apis.CStorVolume)
if !IsValidCStorVolumeMgmt(newCStorVolume) {
return
}
// Periodic resync will send update events for all known CStorVolume.
// Two different versions of the same CStorVolume will always have different RVs.
if newCStorVolume.ResourceVersion == oldCStorVolume.ResourceVersion {
q.Operation = common.QOpPeriodicSync
klog.V(4).Infof("cStorVolume periodic sync event : %v %v", newCStorVolume.ObjectMeta.Name, string(newCStorVolume.ObjectMeta.UID))
} else if IsOnlyStatusChange(oldCStorVolume, newCStorVolume) {
klog.V(4).Infof("Only cStorVolume status change: %v, %v", newCStorVolume.ObjectMeta.Name, string(newCStorVolume.ObjectMeta.UID))
return
} else if IsDestroyEvent(newCStorVolume) {
q.Operation = common.QOpDestroy
klog.Infof("cStorVolume Destroy event : %v, %v", newCStorVolume.ObjectMeta.Name, string(newCStorVolume.ObjectMeta.UID))
} else {
q.Operation = common.QOpModify
klog.Infof("cStorVolume Modify event : %v, %v", newCStorVolume.ObjectMeta.Name, string(newCStorVolume.ObjectMeta.UID))
}
controller.enqueueCStorVolume(newCStorVolume, q)
},
DeleteFunc: func(obj interface{}) {
klog.Infof("Delete event received for cstorvolume : %s", obj.(*apis.CStorVolume).Name)
},
})
return controller
}