2017-04-14 15:43:30 -07:00
/*
Copyright 2017 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.
*/
2017-07-24 07:37:56 -07:00
2017-07-24 07:43:11 -07:00
// Package provisioner to provision Rook volumes on Kubernetes.
package provisioner
2017-04-14 15:43:30 -07:00
import (
"fmt"
"strings"
2017-07-24 07:43:11 -07:00
"github.com/coreos/pkg/capnslog"
2017-07-28 16:19:34 -07:00
"github.com/rook/rook/pkg/clusterd"
2017-12-04 08:33:19 -08:00
"github.com/rook/rook/pkg/daemon/agent/flexvolume"
ceph "github.com/rook/rook/pkg/daemon/ceph/client"
2017-09-14 09:40:34 -07:00
"github.com/rook/rook/pkg/operator/cluster"
2017-10-27 14:51:50 -07:00
"github.com/rook/rook/pkg/operator/provisioner/controller"
2017-07-06 19:52:48 -07:00
"k8s.io/api/core/v1"
2017-04-14 15:43:30 -07:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
2017-04-20 16:28:29 -07:00
const (
2017-09-14 09:40:34 -07:00
attacherImageKey = "attacherImage"
storageClassBetaAnnotationKey = "volume.beta.kubernetes.io/storage-class"
2017-04-20 16:28:29 -07:00
)
2017-07-24 07:43:11 -07:00
var logger = capnslog . NewPackageLogger ( "github.com/rook/rook" , "op-provisioner" )
2017-09-14 09:40:34 -07:00
var flexdriver = fmt . Sprintf ( "%s/%s" , flexvolume . FlexvolumeVendor , flexvolume . FlexvolumeDriver )
2017-07-24 07:43:11 -07:00
// RookVolumeProvisioner is used to provision Rook volumes on Kubernetes
type RookVolumeProvisioner struct {
2017-07-28 16:19:34 -07:00
context * clusterd . Context
2017-04-14 15:43:30 -07:00
// Configuration of rook volume provisioner
provConfig provisionerConfig
}
type provisionerConfig struct {
2017-07-27 17:24:01 -07:00
// Required: The pool name to provision volumes from.
2017-04-14 15:43:30 -07:00
pool string
// Optional: Name of the cluster. Default is `rook`
clusterName string
2017-07-27 17:24:01 -07:00
// Optional: File system type used for mounting the image. Default is `ext4`
fstype string
2017-04-14 15:43:30 -07:00
}
2017-07-24 07:43:11 -07:00
// New creates RookVolumeProvisioner
2017-07-28 16:19:34 -07:00
func New ( context * clusterd . Context ) controller . Provisioner {
2017-07-24 07:43:11 -07:00
return & RookVolumeProvisioner {
2017-07-28 16:19:34 -07:00
context : context ,
2017-04-14 15:43:30 -07:00
}
}
// Provision creates a storage asset and returns a PV object representing it.
2017-07-24 07:43:11 -07:00
func ( p * RookVolumeProvisioner ) Provision ( options controller . VolumeOptions ) ( * v1 . PersistentVolume , error ) {
2017-04-14 15:43:30 -07:00
var err error
if options . PVC . Spec . Selector != nil {
return nil , fmt . Errorf ( "claim Selector is not supported" )
}
cfg , err := parseClassParameters ( options . Parameters )
if err != nil {
return nil , err
}
p . provConfig = * cfg
logger . Infof ( "creating volume with configuration %+v" , p . provConfig )
capacity := options . PVC . Spec . Resources . Requests [ v1 . ResourceName ( v1 . ResourceStorage )]
requestBytes := capacity . Value ()
2017-09-14 09:40:34 -07:00
imageName := options . PVName
2017-04-14 15:43:30 -07:00
2017-09-14 09:40:34 -07:00
storageClass , err := parseStorageClass ( options )
2017-04-14 15:43:30 -07:00
if err != nil {
return nil , err
}
2017-09-14 09:40:34 -07:00
if err := p . createVolume ( imageName , p . provConfig . pool , requestBytes ); err != nil {
2017-07-28 16:19:34 -07:00
return nil , err
2017-04-14 15:43:30 -07:00
}
2017-07-28 16:19:34 -07:00
2017-04-14 15:43:30 -07:00
pv := & v1 . PersistentVolume {
ObjectMeta : metav1 . ObjectMeta {
2017-09-14 09:40:34 -07:00
Name : imageName ,
2017-04-14 15:43:30 -07:00
},
Spec : v1 . PersistentVolumeSpec {
PersistentVolumeReclaimPolicy : options . PersistentVolumeReclaimPolicy ,
AccessModes : options . PVC . Spec . AccessModes ,
Capacity : v1 . ResourceList {
v1 . ResourceName ( v1 . ResourceStorage ): capacity ,
},
PersistentVolumeSource : v1 . PersistentVolumeSource {
2017-09-14 09:40:34 -07:00
FlexVolume : & v1 . FlexVolumeSource {
Driver : flexdriver ,
FSType : p . provConfig . fstype ,
Options : map [ string ] string {
flexvolume . StorageClassKey : storageClass ,
flexvolume . PoolKey : p . provConfig . pool ,
flexvolume . ImageKey : imageName ,
},
2017-04-14 15:43:30 -07:00
},
},
},
}
2017-09-14 09:40:34 -07:00
logger . Infof ( "successfully created Rook Block volume %+v" , pv . Spec . PersistentVolumeSource . FlexVolume )
2017-04-14 15:43:30 -07:00
return pv , nil
}
// createVolume creates a rook block volume.
2017-07-28 16:19:34 -07:00
func ( p * RookVolumeProvisioner ) createVolume ( image , pool string , size int64 ) error {
if image == "" || pool == "" || size == 0 {
return fmt . Errorf ( "image missing required fields (image=%s, pool=%s, size=%d)" , image , pool , size )
2017-04-14 15:43:30 -07:00
}
2017-07-28 16:19:34 -07:00
createdImage , err := ceph . CreateImage ( p . context , p . provConfig . clusterName , image , pool , uint64 ( size ))
2017-04-14 15:43:30 -07:00
if err != nil {
2017-07-28 16:19:34 -07:00
return fmt . Errorf ( "Failed to create rook block image %s/%s: %v" , pool , image , err )
2017-04-14 15:43:30 -07:00
}
2017-07-28 16:19:34 -07:00
logger . Infof ( "Rook block image created: %s" , createdImage . Name )
2017-04-14 15:43:30 -07:00
2017-07-28 16:19:34 -07:00
return nil
2017-04-14 15:43:30 -07:00
}
// Delete removes the storage asset that was created by Provision represented
// by the given PV.
2017-07-24 07:43:11 -07:00
func ( p * RookVolumeProvisioner ) Delete ( volume * v1 . PersistentVolume ) error {
2017-07-28 16:19:34 -07:00
logger . Infof ( "Deleting volume %s" , volume . Name )
2017-11-20 21:47:27 -05:00
if volume . Spec . PersistentVolumeSource . FlexVolume == nil {
return fmt . Errorf ( "Failed to delete rook block image %s/%s: %v" , p . provConfig . pool , volume . Name , "PersistentVolume is not a FlexVolume" )
}
if volume . Spec . PersistentVolumeSource . FlexVolume . Options == nil {
return fmt . Errorf ( "Failed to delete rook block image %s/%s: %v" , p . provConfig . pool , volume . Name , "PersistentVolume has no image defined for the FlexVolume" )
}
2017-09-14 09:40:34 -07:00
name := volume . Spec . PersistentVolumeSource . FlexVolume . Options [ flexvolume . ImageKey ]
2017-07-28 16:19:34 -07:00
err := ceph . DeleteImage ( p . context , p . provConfig . clusterName , name , p . provConfig . pool )
2017-04-14 15:43:30 -07:00
if err != nil {
return fmt . Errorf ( "Failed to delete rook block image %s/%s: %v" , p . provConfig . pool , volume . Name , err )
}
2017-07-28 16:19:34 -07:00
logger . Infof ( "succeeded deleting volume %+v" , volume )
2017-04-14 15:43:30 -07:00
return nil
}
2017-09-14 09:40:34 -07:00
func parseStorageClass ( options controller . VolumeOptions ) ( string , error ) {
if options . PVC . Spec . StorageClassName != nil {
return * options . PVC . Spec . StorageClassName , nil
}
// PVC manifest is from 1.5. Check annotation.
if val , ok := options . PVC . Annotations [ storageClassBetaAnnotationKey ]; ok {
return val , nil
}
return "" , fmt . Errorf ( "failed to get storageclass from PVC %s/%s" , options . PVC . Namespace , options . PVC . Name )
}
2017-04-14 15:43:30 -07:00
func parseClassParameters ( params map [ string ] string ) ( * provisionerConfig , error ) {
var cfg provisionerConfig
for k , v := range params {
switch strings . ToLower ( k ) {
case "pool" :
cfg . pool = v
case "clustername" :
cfg . clusterName = v
2017-07-27 17:24:01 -07:00
case "fstype" :
cfg . fstype = v
2017-04-14 15:43:30 -07:00
default :
return nil , fmt . Errorf ( "invalid option %q for volume plugin %s" , k , "rookVolumeProvisioner" )
}
}
if len ( cfg . pool ) == 0 {
return nil , fmt . Errorf ( "StorageClass for provisioner %s must contain 'pool' parameter" , "rookVolumeProvisioner" )
}
if len ( cfg . clusterName ) == 0 {
2017-09-14 09:40:34 -07:00
cfg . clusterName = cluster . DefaultClusterName
2017-04-14 15:43:30 -07:00
}
return & cfg , nil
}