Files
my-rook-config/pkg/operator/file/controller.go
T

123 lines
4.3 KiB
Go

/*
Copyright 2016 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 mds to manage a rook file system.
package file
import (
"reflect"
"github.com/coreos/pkg/capnslog"
opkit "github.com/rook/operator-kit"
rookalpha "github.com/rook/rook/pkg/apis/rook.io/v1alpha1"
"github.com/rook/rook/pkg/clusterd"
mds "github.com/rook/rook/pkg/operator/file/ceph"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/client-go/tools/cache"
)
var logger = capnslog.NewPackageLogger("github.com/rook/rook", "op-file")
// FilesystemResource represents the file system custom resource
var FilesystemResource = opkit.CustomResource{
Name: "filesystem",
Plural: "filesystems",
Group: rookalpha.CustomResourceGroup,
Version: rookalpha.Version,
Scope: apiextensionsv1beta1.NamespaceScoped,
Kind: reflect.TypeOf(rookalpha.Filesystem{}).Name(),
}
// FilesystemController represents a controller for file system custom resources
type FilesystemController struct {
context *clusterd.Context
rookImage string
hostNetwork bool
}
// NewFilesystemController create controller for watching file system custom resources created
func NewFilesystemController(context *clusterd.Context, rookImage string, hostNetwork bool) *FilesystemController {
return &FilesystemController{
context: context,
rookImage: rookImage,
hostNetwork: hostNetwork,
}
}
// StartWatch watches for instances of Filesystem custom resources and acts on them
func (c *FilesystemController) StartWatch(namespace string, stopCh chan struct{}) error {
resourceHandlerFuncs := cache.ResourceEventHandlerFuncs{
AddFunc: c.onAdd,
UpdateFunc: c.onUpdate,
DeleteFunc: c.onDelete,
}
logger.Infof("start watching filesystem resource in namespace %s", namespace)
watcher := opkit.NewWatcher(FilesystemResource, namespace, resourceHandlerFuncs, c.context.RookClientset.Rook().RESTClient())
go watcher.Watch(&rookalpha.Filesystem{}, stopCh)
return nil
}
func (c *FilesystemController) onAdd(obj interface{}) {
filesystem := obj.(*rookalpha.Filesystem).DeepCopy()
err := mds.CreateFilesystem(c.context, *filesystem, c.rookImage, c.hostNetwork)
if err != nil {
logger.Errorf("failed to create file system %s. %+v", filesystem.Name, err)
}
}
func (c *FilesystemController) onUpdate(oldObj, newObj interface{}) {
oldFS := oldObj.(*rookalpha.Filesystem)
newFS := newObj.(*rookalpha.Filesystem)
if !filesystemChanged(oldFS.Spec, newFS.Spec) {
logger.Debugf("filesystem %s not updated", newFS.Name)
return
}
// if the file system is modified, allow the file system to be created if it wasn't already
logger.Infof("updating filesystem %s", newFS)
err := mds.CreateFilesystem(c.context, *newFS, c.rookImage, c.hostNetwork)
if err != nil {
logger.Errorf("failed to create (modify) file system %s. %+v", newFS.Name, err)
}
}
func (c *FilesystemController) onDelete(obj interface{}) {
filesystem := obj.(*rookalpha.Filesystem)
err := mds.DeleteFilesystem(c.context, *filesystem)
if err != nil {
logger.Errorf("failed to delete file system %s. %+v", filesystem.Name, err)
}
}
func filesystemChanged(oldFS, newFS rookalpha.FilesystemSpec) bool {
if len(oldFS.DataPools) != len(newFS.DataPools) {
logger.Infof("number of data pools changed from %d to %d", len(oldFS.DataPools), len(newFS.DataPools))
return true
}
if oldFS.MetadataServer.ActiveCount != newFS.MetadataServer.ActiveCount {
logger.Infof("number of mds active changed from %d to %d", oldFS.MetadataServer.ActiveCount, newFS.MetadataServer.ActiveCount)
return true
}
if oldFS.MetadataServer.ActiveStandby != newFS.MetadataServer.ActiveStandby {
logger.Infof("mds active standby changed from %t to %t", oldFS.MetadataServer.ActiveStandby, newFS.MetadataServer.ActiveStandby)
return true
}
return false
}