/
controller.go
252 lines (213 loc) · 8.78 KB
/
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright 2019 The Rook Authors. All rights reserved.
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 nfs
import (
"fmt"
"reflect"
"sync"
"github.com/coreos/pkg/capnslog"
opkit "github.com/rook/operator-kit"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
cephconfig "github.com/rook/rook/pkg/daemon/ceph/config"
"github.com/rook/rook/pkg/operator/k8sutil"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
)
var logger = capnslog.NewPackageLogger("github.com/rook/rook", "op-nfs")
// CephNFSResource represents the file system custom resource
var CephNFSResource = opkit.CustomResource{
Name: "cephnfs",
Plural: "cephnfses",
Group: cephv1.CustomResourceGroup,
Version: cephv1.Version,
Scope: apiextensionsv1beta1.NamespaceScoped,
Kind: reflect.TypeOf(cephv1.CephNFS{}).Name(),
}
// CephNFSController represents a controller for NFS custom resources
type CephNFSController struct {
clusterInfo *cephconfig.ClusterInfo
context *clusterd.Context
dataDirHostPath string
namespace string
rookImage string
clusterSpec *cephv1.ClusterSpec
orchestrationMutex sync.Mutex
isUpgrade bool
}
// NewCephNFSController create controller for watching NFS custom resources created
func NewCephNFSController(clusterInfo *cephconfig.ClusterInfo, context *clusterd.Context, dataDirHostPath, namespace, rookImage string, clusterSpec *cephv1.ClusterSpec, ownerRef metav1.OwnerReference) *CephNFSController {
return &CephNFSController{
clusterInfo: clusterInfo,
context: context,
dataDirHostPath: dataDirHostPath,
namespace: namespace,
rookImage: rookImage,
clusterSpec: clusterSpec,
}
}
// StartWatch watches for instances of CephNFS custom resources and acts on them
func (c *CephNFSController) StartWatch(namespace string, stopCh chan struct{}) error {
resourceHandlerFuncs := cache.ResourceEventHandlerFuncs{
AddFunc: c.onAdd,
UpdateFunc: c.onUpdate,
DeleteFunc: c.onDelete,
}
logger.Infof("start watching ceph nfs resource in namespace %s", namespace)
watcher := opkit.NewWatcher(CephNFSResource, namespace, resourceHandlerFuncs, c.context.RookClientset.CephV1().RESTClient())
go watcher.Watch(&cephv1.CephNFS{}, stopCh)
return nil
}
func (c *CephNFSController) onAdd(obj interface{}) {
if c.clusterSpec.External.Enable && c.clusterSpec.CephVersion.Image == "" {
logger.Warningf("Creating nfs for an external ceph cluster is disabled because no Ceph image is specified")
return
}
nfs := obj.(*cephv1.CephNFS).DeepCopy()
if !c.clusterInfo.CephVersion.IsAtLeastNautilus() {
logger.Errorf("Ceph NFS is only supported with Nautilus or newer. CRD %q will be ignored.", nfs.Name)
return
}
c.acquireOrchestrationLock()
defer c.releaseOrchestrationLock()
updateCephNFSStatus(nfs.GetName(), nfs.GetNamespace(), k8sutil.ProcessingStatus, c.context)
err := c.upCephNFS(*nfs, 0)
if err != nil {
logger.Errorf("failed to create NFS Ganesha %q. %v", nfs.Name, err)
updateCephNFSStatus(nfs.GetName(), nfs.GetNamespace(), k8sutil.FailedStatus, c.context)
}
updateCephNFSStatus(nfs.GetName(), nfs.GetNamespace(), k8sutil.ReadyStatus, c.context)
}
func (c *CephNFSController) onUpdate(oldObj, newObj interface{}) {
if c.clusterSpec.External.Enable && c.clusterSpec.CephVersion.Image == "" {
logger.Warningf("Updating nfs for an external ceph cluster is disabled because no Ceph image is specified")
return
}
oldNFS := oldObj.(*cephv1.CephNFS).DeepCopy()
newNFS := newObj.(*cephv1.CephNFS).DeepCopy()
if !c.clusterInfo.CephVersion.IsAtLeastNautilus() {
logger.Errorf("Ceph NFS is only supported with Nautilus or newer. CRD %q will be ignored.", newNFS.Name)
return
}
if !nfsChanged(oldNFS.Spec, newNFS.Spec) {
logger.Debugf("nfs ganesha %q not updated", newNFS.Name)
return
}
c.acquireOrchestrationLock()
defer c.releaseOrchestrationLock()
logger.Infof("Updating the ganesha server from %d to %d active count", oldNFS.Spec.Server.Active, newNFS.Spec.Server.Active)
updateCephNFSStatus(newNFS.GetName(), newNFS.GetNamespace(), k8sutil.ProcessingStatus, c.context)
if oldNFS.Spec.Server.Active < newNFS.Spec.Server.Active {
err := c.upCephNFS(*newNFS, oldNFS.Spec.Server.Active)
if err != nil {
logger.Errorf("Failed to start daemons for CephNFS %q. %v", newNFS.Name, err)
updateCephNFSStatus(newNFS.GetName(), newNFS.GetNamespace(), k8sutil.FailedStatus, c.context)
}
} else {
err := c.downCephNFS(*oldNFS, newNFS.Spec.Server.Active)
if err != nil {
logger.Errorf("Failed to stop daemons for CephNFS %q. %v", newNFS.Name, err)
updateCephNFSStatus(newNFS.GetName(), newNFS.GetNamespace(), k8sutil.FailedStatus, c.context)
}
}
updateCephNFSStatus(newNFS.GetName(), newNFS.GetNamespace(), k8sutil.ReadyStatus, c.context)
}
func (c *CephNFSController) onDelete(obj interface{}) {
if c.clusterSpec.External.Enable && c.clusterSpec.CephVersion.Image == "" {
logger.Warningf("Deleting nfs for an external ceph cluster is disabled because no Ceph image is specified")
return
}
nfs := obj.(*cephv1.CephNFS).DeepCopy()
if !c.clusterInfo.CephVersion.IsAtLeastNautilus() {
logger.Errorf("Ceph NFS is only supported with Nautilus or newer. CRD %q cleanup will be ignored.", nfs.Name)
return
}
c.acquireOrchestrationLock()
defer c.releaseOrchestrationLock()
err := c.downCephNFS(*nfs, 0)
if err != nil {
logger.Errorf("failed to delete file system %s. %v", nfs.Name, err)
}
}
// ParentClusterChanged performs the steps needed to update the NFS cluster when the parent Ceph
// cluster has changed.
func (c *CephNFSController) ParentClusterChanged(cluster cephv1.ClusterSpec, clusterInfo *cephconfig.ClusterInfo, isUpgrade bool) {
c.clusterInfo = clusterInfo
if !isUpgrade || !c.clusterInfo.CephVersion.IsAtLeastNautilus() {
logger.Debugf("No need to update the nfs daemons after the parent cluster changed")
return
}
// This is mostly a placeholder since we don't perform any upgrade checks for nfs since it's not in Ceph's servicemap yet
// This is an upgrade so let's activate the flag
c.isUpgrade = isUpgrade
c.acquireOrchestrationLock()
defer c.releaseOrchestrationLock()
c.clusterSpec.CephVersion = cluster.CephVersion
nfses, err := c.context.RookClientset.CephV1().CephNFSes(c.namespace).List(metav1.ListOptions{})
if err != nil {
logger.Errorf("failed to retrieve NFSes to update the ceph version. %v", err)
return
}
for _, nfs := range nfses.Items {
logger.Infof("updating the ceph version for nfs %q to %q", nfs.Name, c.clusterSpec.CephVersion.Image)
err := c.upCephNFS(nfs, 0)
if err != nil {
logger.Errorf("failed to update nfs %q. %v", nfs.Name, err)
} else {
logger.Infof("updated nfs %q to ceph version %q", nfs.Name, c.clusterSpec.CephVersion.Image)
}
}
}
func nfsChanged(oldNFS, newNFS cephv1.NFSGaneshaSpec) bool {
if oldNFS.Server.Active != newNFS.Server.Active {
return true
}
return false
}
func (c *CephNFSController) acquireOrchestrationLock() {
logger.Debugf("Acquiring lock for nfs orchestration")
c.orchestrationMutex.Lock()
logger.Debugf("Acquired lock for nfs orchestration")
}
func (c *CephNFSController) releaseOrchestrationLock() {
c.orchestrationMutex.Unlock()
logger.Debugf("Released lock for nfs orchestration")
}
func ownerRefs(nfs cephv1.CephNFS) []metav1.OwnerReference {
// Set the filesystem CR as the owner
return []metav1.OwnerReference{{
APIVersion: fmt.Sprintf("%s/%s", CephNFSResource.Group, CephNFSResource.Version),
Kind: CephNFSResource.Kind,
Name: nfs.Name,
UID: nfs.UID,
}}
}
func updateCephNFSStatus(name, namespace, status string, context *clusterd.Context) {
updatedCephNFS, err := context.RookClientset.CephV1().CephNFSes(namespace).Get(name, metav1.GetOptions{})
if err != nil {
logger.Errorf("Unable to update the cephNFS %s status %v", updatedCephNFS.GetName(), err)
return
}
if updatedCephNFS.Status == nil {
updatedCephNFS.Status = &cephv1.Status{}
} else if updatedCephNFS.Status.Phase == status {
return
}
updatedCephNFS.Status.Phase = status
_, err = context.RookClientset.CephV1().CephNFSes(updatedCephNFS.Namespace).Update(updatedCephNFS)
if err != nil {
logger.Errorf("Unable to update the cephNFS %s status %v", updatedCephNFS.GetName(), err)
return
}
}