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

218 lines
6.1 KiB
Go
Raw Normal View History

2017-03-27 09:12:56 -07:00
/*
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.
Some of the code below came from https://github.com/coreos/etcd-operator
which also has the apache 2.0 license.
*/
2017-07-24 07:37:56 -07:00
2017-07-24 07:43:11 -07:00
// Package pool to manage a rook pool.
package pool
2017-03-27 09:12:56 -07:00
import (
"fmt"
2017-07-24 07:43:11 -07:00
"github.com/coreos/pkg/capnslog"
"github.com/rook/rook/pkg/model"
"github.com/rook/rook/pkg/operator/api"
"github.com/rook/rook/pkg/operator/k8sutil"
"github.com/rook/rook/pkg/operator/kit"
2017-03-27 09:12:56 -07:00
rookclient "github.com/rook/rook/pkg/rook/client"
2017-07-24 07:43:11 -07:00
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
2017-03-27 09:12:56 -07:00
)
const (
2017-07-24 07:43:11 -07:00
customResourceName = "pool"
customResourceNamePlural = "pools"
replicatedType = "replicated"
erasureCodeType = "erasure-coded"
2017-03-27 09:12:56 -07:00
)
2017-07-24 07:43:11 -07:00
var logger = capnslog.NewPackageLogger("github.com/rook/rook", "op-pool")
// PoolController represents a controller object for pool custom resources
type PoolController struct {
clientset kubernetes.Interface
rookClient rookclient.RookRestClient
scheme *runtime.Scheme
}
2017-07-24 07:43:11 -07:00
// NewPoolController create controller for watching pool custom resources created
func NewPoolController(clientset kubernetes.Interface) (*PoolController, error) {
return &PoolController{
clientset: clientset,
}, nil
2017-03-27 09:12:56 -07:00
}
2017-07-24 07:43:11 -07:00
// Watch watches for instances of Pool custom resources and acts on them
func (c *PoolController) StartWatch(namespace string, stopCh chan struct{}) error {
client, scheme, err := kit.NewHTTPClient(k8sutil.CustomResourceGroup, k8sutil.V1Alpha1, schemeBuilder)
if err != nil {
return fmt.Errorf("failed to get a k8s client for watching pool resources: %v", err)
}
c.scheme = scheme
rclient, err := api.GetRookClient(namespace, c.clientset)
if err != nil {
return fmt.Errorf("Failed to get rook client: %v", err)
}
c.rookClient = rclient
resourceHandlerFuncs := cache.ResourceEventHandlerFuncs{
AddFunc: c.onAdd,
UpdateFunc: c.onUpdate,
DeleteFunc: c.onDelete,
}
watcher := kit.NewWatcher(PoolResource, namespace, resourceHandlerFuncs, client)
go watcher.Watch(&Pool{}, stopCh)
return nil
2017-03-27 09:12:56 -07:00
}
2017-07-24 07:43:11 -07:00
func (c *PoolController) onAdd(obj interface{}) {
pool := obj.(*Pool)
// NEVER modify objects from the store. It's a read-only, local cache.
// Use scheme.Copy() to make a deep copy of original object.
copyObj, err := c.scheme.Copy(pool)
if err != nil {
fmt.Printf("ERROR creating a deep copy of pool object: %v\n", err)
return
}
poolCopy := copyObj.(*Pool)
err = poolCopy.create(c.rookClient)
if err != nil {
logger.Errorf("failed to create pool %s. %+v", pool.ObjectMeta.Name, err)
}
}
func (c *PoolController) onUpdate(oldObj, newObj interface{}) {
//oldPool := oldObj.(*Pool)
newPool := newObj.(*Pool)
// if the pool is modified, allow the pool to be created if it wasn't already
err := newPool.create(c.rookClient)
if err != nil {
logger.Errorf("failed to create (modify) pool %s. %+v", newPool.ObjectMeta.Name, err)
}
}
func (c *PoolController) onDelete(obj interface{}) {
pool := obj.(*Pool)
err := pool.delete(c.rookClient)
if err != nil {
logger.Errorf("failed to delete pool %s. %+v", pool.ObjectMeta.Name, err)
}
}
// Create the pool
func (p *Pool) create(rclient rookclient.RookRestClient) error {
2017-03-27 09:12:56 -07:00
// validate the pool settings
if err := p.validate(); err != nil {
return fmt.Errorf("invalid pool %s arguments. %+v", p.Name, err)
2017-03-27 09:12:56 -07:00
}
// check if the pool already exists
exists, err := p.exists(rclient)
if err == nil && exists {
2017-04-10 08:47:16 -07:00
logger.Infof("pool %s already exists in namespace %s ", p.Name, p.Namespace)
2017-03-27 09:12:56 -07:00
return nil
}
// create the pool
pool := model.Pool{Name: p.Name}
2017-04-10 08:47:16 -07:00
2017-03-27 09:12:56 -07:00
r := p.replication()
if r != nil {
2017-04-10 08:47:16 -07:00
logger.Infof("creating pool %s in namespace %s with replicas %d", p.Name, p.Namespace, r.Size)
pool.ReplicationConfig.Size = r.Size
2017-03-27 09:12:56 -07:00
pool.Type = model.Replicated
} else {
ec := p.erasureCode()
logger.Infof("creating pool %s in namespace %s. coding chunks = %d, data chunks = %d", p.Name, p.Namespace, ec.CodingChunks, ec.DataChunks)
2017-03-27 09:12:56 -07:00
pool.ErasureCodedConfig.CodingChunkCount = ec.CodingChunks
pool.ErasureCodedConfig.DataChunkCount = ec.DataChunks
pool.Type = model.ErasureCoded
}
info, err := rclient.CreatePool(pool)
if err != nil {
return fmt.Errorf("failed to create pool %s. %+v", p.Name, err)
2017-03-27 09:12:56 -07:00
}
logger.Infof("created pool %s. %s", p.Name, info)
2017-03-27 09:12:56 -07:00
return nil
}
// Delete the pool
2017-07-24 07:43:11 -07:00
func (p *Pool) delete(rclient rookclient.RookRestClient) error {
2017-03-27 09:12:56 -07:00
// check if the pool exists
exists, err := p.exists(rclient)
if err == nil && !exists {
return nil
}
logger.Infof("TODO: delete pool %s from namespace %s", p.Name, p.Namespace)
2017-03-27 09:12:56 -07:00
//return p.client.DeletePool(p.PoolSpec.Name)
return nil
}
// Check if the pool exists
func (p *Pool) exists(rclient rookclient.RookRestClient) (bool, error) {
pools, err := rclient.GetPools()
if err != nil {
return false, err
}
for _, pool := range pools {
if pool.Name == p.Name {
2017-03-27 09:12:56 -07:00
return true, nil
}
}
return false, nil
}
// Validate the pool arguments
func (p *Pool) validate() error {
if p.Name == "" {
2017-03-27 09:12:56 -07:00
return fmt.Errorf("missing name")
}
if p.Namespace == "" {
2017-03-27 09:12:56 -07:00
return fmt.Errorf("missing namespace")
}
if p.replication() != nil && p.erasureCode() != nil {
return fmt.Errorf("both replication and erasure code settings cannot be specified")
}
if p.replication() == nil && p.erasureCode() == nil {
return fmt.Errorf("neither replication nor erasure code settings were specified")
}
return nil
}
func (p *Pool) replication() *ReplicationSpec {
2017-07-24 07:43:11 -07:00
if p.Spec.Replication.Size > 0 {
return &p.Spec.Replication
2017-03-27 09:12:56 -07:00
}
return nil
}
func (p *Pool) erasureCode() *ErasureCodeSpec {
2017-07-24 07:43:11 -07:00
ec := &p.Spec.ErasureCoding
2017-03-27 09:12:56 -07:00
if ec.CodingChunks > 0 || ec.DataChunks > 0 {
return ec
}
return nil
}