feat(zfs-localpv): initial commit

provisioning and deprovisioning of
the volumes on the node where zfs pool
has already been setup. Pool name and the volume
parameters has to be given in storage class
which will be used to provision the volume.

Signed-off-by: Pawan <pawan@mayadata.io>
This commit is contained in:
Pawan 2019-09-12 12:32:17 +05:30 committed by Kiran Mova
parent 485e2a21f0
commit 9f5cf445df
46 changed files with 6339 additions and 0 deletions

208
pkg/builder/build.go Normal file
View file

@ -0,0 +1,208 @@
/*
Copyright 2019 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 builder
import (
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/core/v1alpha1"
"github.com/openebs/zfs-localpv/pkg/common/errors"
)
// Builder is the builder object for ZFSVolume
type Builder struct {
volume *ZFSVolume
errs []error
}
// NewBuilder returns new instance of Builder
func NewBuilder() *Builder {
return &Builder{
volume: &ZFSVolume{
Object: &apis.ZFSVolume{},
},
}
}
// BuildFrom returns new instance of Builder
// from the provided api instance
func BuildFrom(volume *apis.ZFSVolume) *Builder {
if volume == nil {
b := NewBuilder()
b.errs = append(
b.errs,
errors.New("failed to build volume object: nil volume"),
)
return b
}
return &Builder{
volume: &ZFSVolume{
Object: volume,
},
}
}
// WithNamespace sets the namespace of csi volume
func (b *Builder) WithNamespace(namespace string) *Builder {
if namespace == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi volume object: missing namespace",
),
)
return b
}
b.volume.Object.Namespace = namespace
return b
}
// WithName sets the name of csi volume
func (b *Builder) WithName(name string) *Builder {
if name == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi volume object: missing name",
),
)
return b
}
b.volume.Object.Name = name
return b
}
// WithCapacity sets the Capacity of csi volume by converting string
// capacity into Quantity
func (b *Builder) WithCapacity(capacity string) *Builder {
if capacity == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi volume object: missing capacity",
),
)
return b
}
b.volume.Object.Spec.Capacity = capacity
return b
}
// WithCompression sets compression of CStorVolumeClaim
func (b *Builder) WithCompression(compression string) *Builder {
comp := "off"
if compression == "on" {
comp = "on"
}
b.volume.Object.Spec.Compression = comp
return b
}
// WithDedup sets compression of CStorVolumeClaim
func (b *Builder) WithDedup(dedup string) *Builder {
dp := "off"
if dedup == "on" {
dp = "on"
}
b.volume.Object.Spec.Dedup = dp
return b
}
// WithThinProv sets compression of CStorVolumeClaim
func (b *Builder) WithThinProv(thinprov string) *Builder {
tp := "no"
if thinprov == "yes" {
tp = "yes"
}
b.volume.Object.Spec.ThinProvision = tp
return b
}
// WithBlockSize sets blocksize of CStorVolumeClaim
func (b *Builder) WithBlockSize(blockSize string) *Builder {
bs := "4k"
if len(blockSize) > 0 {
bs = blockSize
}
b.volume.Object.Spec.BlockSize = bs
return b
}
func (b *Builder) WithPoolName(pool string) *Builder {
if pool == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi volume object: missing pool name",
),
)
return b
}
b.volume.Object.Spec.PoolName = pool
return b
}
func (b *Builder) WithNodename(name string) *Builder {
if name == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi volume object: missing node name",
),
)
return b
}
b.volume.Object.Spec.OwnerNodeID = name
return b
}
// WithLabels merges existing labels if any
// with the ones that are provided here
func (b *Builder) WithLabels(labels map[string]string) *Builder {
if len(labels) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build cstorvolume object: missing labels"),
)
return b
}
if b.volume.Object.Labels == nil {
b.volume.Object.Labels = map[string]string{}
}
for key, value := range labels {
b.volume.Object.Labels[key] = value
}
return b
}
func (b *Builder) WithFinalizer(finalizer []string) *Builder {
b.volume.Object.Finalizers = append(b.volume.Object.Finalizers, finalizer...)
return b
}
// Build returns csi volume API object
func (b *Builder) Build() (*apis.ZFSVolume, error) {
if len(b.errs) > 0 {
return nil, errors.Errorf("%+v", b.errs)
}
return b.volume.Object, nil
}

72
pkg/builder/buildlist.go Normal file
View file

@ -0,0 +1,72 @@
/*
Copyright 2019 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 builder
import (
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/core/v1alpha1"
)
// ListBuilder enables building an instance of
// ZFSVolumeList
type ListBuilder struct {
list *apis.ZFSVolumeList
filters predicateList
}
// NewListBuilder returns a new instance of ListBuilder
func NewListBuilder() *ListBuilder {
return &ListBuilder{
list: &apis.ZFSVolumeList{},
}
}
// ListBuilderFrom returns a new instance of
// ListBuilder from API list instance
func ListBuilderFrom(vols apis.ZFSVolumeList) *ListBuilder {
b := &ListBuilder{list: &apis.ZFSVolumeList{}}
if len(vols.Items) == 0 {
return b
}
b.list.Items = append(b.list.Items, vols.Items...)
return b
}
// List returns the list of pod
// instances that was built by this
// builder
func (b *ListBuilder) List() *apis.ZFSVolumeList {
if b.filters == nil || len(b.filters) == 0 {
return b.list
}
filtered := &apis.ZFSVolumeList{}
for _, vol := range b.list.Items {
vol := vol // pin it
if b.filters.all(From(&vol)) {
filtered.Items = append(filtered.Items, vol)
}
}
return filtered
}
// WithFilter add filters on which the pod
// has to be filtered
func (b *ListBuilder) WithFilter(pred ...Predicate) *ListBuilder {
b.filters = append(b.filters, pred...)
return b
}

427
pkg/builder/kubernetes.go Normal file
View file

@ -0,0 +1,427 @@
// Copyright © 2019 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 builder
import (
"encoding/json"
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/core/v1alpha1"
client "github.com/openebs/zfs-localpv/pkg/common/kubernetes/client"
clientset "github.com/openebs/zfs-localpv/pkg/generated/clientset/internalclientset"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// getClientsetFn is a typed function that
// abstracts fetching of internal clientset
type getClientsetFn func() (clientset *clientset.Clientset, err error)
// getClientsetFromPathFn is a typed function that
// abstracts fetching of clientset from kubeConfigPath
type getClientsetForPathFn func(kubeConfigPath string) (
clientset *clientset.Clientset,
err error,
)
// createFn is a typed function that abstracts
// creating csi volume instance
type createFn func(
cs *clientset.Clientset,
upgradeResultObj *apis.ZFSVolume,
namespace string,
) (*apis.ZFSVolume, error)
// getFn is a typed function that abstracts
// fetching a csi volume instance
type getFn func(
cli *clientset.Clientset,
name,
namespace string,
opts metav1.GetOptions,
) (*apis.ZFSVolume, error)
// listFn is a typed function that abstracts
// listing of csi volume instances
type listFn func(
cli *clientset.Clientset,
namespace string,
opts metav1.ListOptions,
) (*apis.ZFSVolumeList, error)
// delFn is a typed function that abstracts
// deleting a csi volume instance
type delFn func(
cli *clientset.Clientset,
name,
namespace string,
opts *metav1.DeleteOptions,
) error
// updateFn is a typed function that abstracts
// updating csi volume instance
type updateFn func(
cs *clientset.Clientset,
vol *apis.ZFSVolume,
namespace string,
) (*apis.ZFSVolume, error)
// Kubeclient enables kubernetes API operations
// on csi volume instance
type Kubeclient struct {
// clientset refers to csi volume's
// clientset that will be responsible to
// make kubernetes API calls
clientset *clientset.Clientset
kubeConfigPath string
// namespace holds the namespace on which
// kubeclient has to operate
namespace string
// functions useful during mocking
getClientset getClientsetFn
getClientsetForPath getClientsetForPathFn
get getFn
list listFn
del delFn
create createFn
update updateFn
}
// KubeclientBuildOption defines the abstraction
// to build a kubeclient instance
type KubeclientBuildOption func(*Kubeclient)
// defaultGetClientset is the default implementation to
// get kubernetes clientset instance
func defaultGetClientset() (clients *clientset.Clientset, err error) {
config, err := client.GetConfig(client.New())
if err != nil {
return nil, err
}
return clientset.NewForConfig(config)
}
// defaultGetClientsetForPath is the default implementation to
// get kubernetes clientset instance based on the given
// kubeconfig path
func defaultGetClientsetForPath(
kubeConfigPath string,
) (clients *clientset.Clientset, err error) {
config, err := client.GetConfig(
client.New(client.WithKubeConfigPath(kubeConfigPath)))
if err != nil {
return nil, err
}
return clientset.NewForConfig(config)
}
// defaultGet is the default implementation to get
// a csi volume instance in kubernetes cluster
func defaultGet(
cli *clientset.Clientset,
name, namespace string,
opts metav1.GetOptions,
) (*apis.ZFSVolume, error) {
return cli.OpenebsV1alpha1().
ZFSVolumes(namespace).
Get(name, opts)
}
// defaultList is the default implementation to list
// csi volume instances in kubernetes cluster
func defaultList(
cli *clientset.Clientset,
namespace string,
opts metav1.ListOptions,
) (*apis.ZFSVolumeList, error) {
return cli.OpenebsV1alpha1().
ZFSVolumes(namespace).
List(opts)
}
// defaultCreate is the default implementation to delete
// a csi volume instance in kubernetes cluster
func defaultDel(
cli *clientset.Clientset,
name, namespace string,
opts *metav1.DeleteOptions,
) error {
deletePropagation := metav1.DeletePropagationForeground
opts.PropagationPolicy = &deletePropagation
err := cli.OpenebsV1alpha1().
ZFSVolumes(namespace).
Delete(name, opts)
return err
}
// defaultCreate is the default implementation to create
// a csi volume instance in kubernetes cluster
func defaultCreate(
cli *clientset.Clientset,
vol *apis.ZFSVolume,
namespace string,
) (*apis.ZFSVolume, error) {
return cli.OpenebsV1alpha1().
ZFSVolumes(namespace).
Create(vol)
}
// defaultUpdate is the default implementation to update
// a csi volume instance in kubernetes cluster
func defaultUpdate(
cli *clientset.Clientset,
vol *apis.ZFSVolume,
namespace string,
) (*apis.ZFSVolume, error) {
return cli.OpenebsV1alpha1().
ZFSVolumes(namespace).
Update(vol)
}
// withDefaults sets the default options
// of kubeclient instance
func (k *Kubeclient) withDefaults() {
if k.getClientset == nil {
k.getClientset = defaultGetClientset
}
if k.getClientsetForPath == nil {
k.getClientsetForPath = defaultGetClientsetForPath
}
if k.get == nil {
k.get = defaultGet
}
if k.list == nil {
k.list = defaultList
}
if k.del == nil {
k.del = defaultDel
}
if k.create == nil {
k.create = defaultCreate
}
if k.update == nil {
k.update = defaultUpdate
}
}
// WithClientSet sets the kubernetes client against
// the kubeclient instance
func WithClientSet(c *clientset.Clientset) KubeclientBuildOption {
return func(k *Kubeclient) {
k.clientset = c
}
}
// WithNamespace sets the kubernetes client against
// the provided namespace
func WithNamespace(namespace string) KubeclientBuildOption {
return func(k *Kubeclient) {
k.namespace = namespace
}
}
// WithNamespace sets the provided namespace
// against this Kubeclient instance
func (k *Kubeclient) WithNamespace(namespace string) *Kubeclient {
k.namespace = namespace
return k
}
// WithKubeConfigPath sets the kubernetes client
// against the provided path
func WithKubeConfigPath(path string) KubeclientBuildOption {
return func(k *Kubeclient) {
k.kubeConfigPath = path
}
}
// NewKubeclient returns a new instance of
// kubeclient meant for csi volume operations
func NewKubeclient(opts ...KubeclientBuildOption) *Kubeclient {
k := &Kubeclient{}
for _, o := range opts {
o(k)
}
k.withDefaults()
return k
}
func (k *Kubeclient) getClientsetForPathOrDirect() (
*clientset.Clientset,
error,
) {
if k.kubeConfigPath != "" {
return k.getClientsetForPath(k.kubeConfigPath)
}
return k.getClientset()
}
// getClientOrCached returns either a new instance
// of kubernetes client or its cached copy
func (k *Kubeclient) getClientOrCached() (*clientset.Clientset, error) {
if k.clientset != nil {
return k.clientset, nil
}
c, err := k.getClientsetForPathOrDirect()
if err != nil {
return nil,
errors.Wrapf(
err,
"failed to get clientset",
)
}
k.clientset = c
return k.clientset, nil
}
// Create creates a csi volume instance
// in kubernetes cluster
func (k *Kubeclient) Create(vol *apis.ZFSVolume) (*apis.ZFSVolume, error) {
if vol == nil {
return nil,
errors.New(
"failed to create csivolume: nil vol object",
)
}
cs, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to create csi volume {%s} in namespace {%s}",
vol.Name,
k.namespace,
)
}
return k.create(cs, vol, k.namespace)
}
// Get returns csi volume object for given name
func (k *Kubeclient) Get(
name string,
opts metav1.GetOptions,
) (*apis.ZFSVolume, error) {
if name == "" {
return nil,
errors.New(
"failed to get csi volume: missing csi volume name",
)
}
cli, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to get csi volume {%s} in namespace {%s}",
name,
k.namespace,
)
}
return k.get(cli, name, k.namespace, opts)
}
// GetRaw returns csi volume instance
// in bytes
func (k *Kubeclient) GetRaw(
name string,
opts metav1.GetOptions,
) ([]byte, error) {
if name == "" {
return nil, errors.New(
"failed to get raw csi volume: missing vol name",
)
}
csiv, err := k.Get(name, opts)
if err != nil {
return nil, errors.Wrapf(
err,
"failed to get csi volume {%s} in namespace {%s}",
name,
k.namespace,
)
}
return json.Marshal(csiv)
}
// List returns a list of csi volume
// instances present in kubernetes cluster
func (k *Kubeclient) List(opts metav1.ListOptions) (*apis.ZFSVolumeList, error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to list csi volumes in namespace {%s}",
k.namespace,
)
}
return k.list(cli, k.namespace, opts)
}
// Delete deletes the csi volume from
// kubernetes
func (k *Kubeclient) Delete(name string) error {
if name == "" {
return errors.New(
"failed to delete csivolume: missing vol name",
)
}
cli, err := k.getClientOrCached()
if err != nil {
return errors.Wrapf(
err,
"failed to delete csivolume {%s} in namespace {%s}",
name,
k.namespace,
)
}
return k.del(cli, name, k.namespace, &metav1.DeleteOptions{})
}
// Update updates this csi volume instance
// against kubernetes cluster
func (k *Kubeclient) Update(vol *apis.ZFSVolume) (*apis.ZFSVolume, error) {
if vol == nil {
return nil,
errors.New(
"failed to update csivolume: nil vol object",
)
}
cs, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to update csivolume {%s} in namespace {%s}",
vol.Name,
vol.Namespace,
)
}
return k.update(cs, vol, k.namespace)
}

115
pkg/builder/volume.go Normal file
View file

@ -0,0 +1,115 @@
// Copyright © 2019 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 builder
import (
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/core/v1alpha1"
)
// ZFSVolume is a wrapper over
// ZFSVolume API instance
type ZFSVolume struct {
Object *apis.ZFSVolume
}
// From returns a new instance of
// csi volume
func From(vol *apis.ZFSVolume) *ZFSVolume {
return &ZFSVolume{
Object: vol,
}
}
// Predicate defines an abstraction
// to determine conditional checks
// against the provided pod instance
type Predicate func(*ZFSVolume) bool
// PredicateList holds a list of predicate
type predicateList []Predicate
// ZFSVolumeList holds the list
// of csi volume instances
type ZFSVolumeList struct {
List apis.ZFSVolumeList
}
// Len returns the number of items present
// in the ZFSVolumeList
func (p *ZFSVolumeList) Len() int {
return len(p.List.Items)
}
// all returns true if all the predicates
// succeed against the provided ZFSVolume
// instance
func (l predicateList) all(p *ZFSVolume) bool {
for _, pred := range l {
if !pred(p) {
return false
}
}
return true
}
// HasLabels returns true if provided labels
// are present in the provided ZFSVolume instance
func HasLabels(keyValuePair map[string]string) Predicate {
return func(p *ZFSVolume) bool {
for key, value := range keyValuePair {
if !p.HasLabel(key, value) {
return false
}
}
return true
}
}
// HasLabel returns true if provided label
// is present in the provided ZFSVolume instance
func (p *ZFSVolume) HasLabel(key, value string) bool {
val, ok := p.Object.GetLabels()[key]
if ok {
return val == value
}
return false
}
// HasLabel returns true if provided label
// is present in the provided ZFSVolume instance
func HasLabel(key, value string) Predicate {
return func(p *ZFSVolume) bool {
return p.HasLabel(key, value)
}
}
// IsNil returns true if the csi volume instance
// is nil
func (p *ZFSVolume) IsNil() bool {
return p.Object == nil
}
// IsNil is predicate to filter out nil csi volume
// instances
func IsNil() Predicate {
return func(p *ZFSVolume) bool {
return p.IsNil()
}
}
// GetAPIObject returns csi volume's API instance
func (p *ZFSVolume) GetAPIObject() *apis.ZFSVolume {
return p.Object
}