mirror of
https://github.com/TECHNOFAB11/zfs-localpv.git
synced 2025-12-12 06:20:11 +01:00
feat(zfspv): adding snapshot and clone support for ZFSPV (#39)
This commits support snapshot and clone commands via CSI driver. User can create snap and clone using the following steps.
Note:
- Snapshot is created via reconciliation CR
- Cloned volume will be on the same zpool where the snapshot is taken
- Cloned volume will have same properties as source volume.
-----------------------------------
Create a Snapshotclass
```
kind: VolumeSnapshotClass
apiVersion: snapshot.storage.k8s.io/v1beta1
metadata:
name: zfspv-snapclass
annotations:
snapshot.storage.kubernetes.io/is-default-class: "true"
driver: zfs.csi.openebs.io
deletionPolicy: Delete
```
Once snapshotclass is created, we can use this class to create a Snapshot
```
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshot
metadata:
name: zfspv-snap
spec:
volumeSnapshotClassName: zfspv-snapclass
source:
persistentVolumeClaimName: csi-zfspv
```
```
$ kubectl get volumesnapshot
NAME AGE
zfspv-snap 7m52s
```
```
$ kubectl get volumesnapshot -o yaml
apiVersion: v1
items:
- apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshot
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"snapshot.storage.k8s.io/v1beta1","kind":"VolumeSnapshot","metadata":{"annotations":{},"name":"zfspv-snap","namespace":"default"},"spec":{"source":{"persistentVolumeClaimName":"csi-zfspv"},"volumeSnapshotClassName":"zfspv-snapclass"}}
creationTimestamp: "2020-01-30T10:31:24Z"
finalizers:
- snapshot.storage.kubernetes.io/volumesnapshot-as-source-protection
- snapshot.storage.kubernetes.io/volumesnapshot-bound-protection
generation: 1
name: zfspv-snap
namespace: default
resourceVersion: "30040"
selfLink: /apis/snapshot.storage.k8s.io/v1beta1/namespaces/default/volumesnapshots/zfspv-snap
uid: 1a5cf166-c599-4f58-9f3c-f1148be47fca
spec:
source:
persistentVolumeClaimName: csi-zfspv
volumeSnapshotClassName: zfspv-snapclass
status:
boundVolumeSnapshotContentName: snapcontent-1a5cf166-c599-4f58-9f3c-f1148be47fca
creationTime: "2020-01-30T10:31:24Z"
readyToUse: true
restoreSize: "0"
kind: List
metadata:
resourceVersion: ""
selfLink: ""
```
Openebs resource for the created snapshot
```
$ kubectl get snap -n openebs -o yaml
apiVersion: v1
items:
- apiVersion: openebs.io/v1alpha1
kind: ZFSSnapshot
metadata:
creationTimestamp: "2020-01-30T10:31:24Z"
finalizers:
- zfs.openebs.io/finalizer
generation: 2
labels:
kubernetes.io/nodename: pawan-2
openebs.io/persistent-volume: pvc-18cab7c3-ec5e-4264-8507-e6f7df4c789a
name: snapshot-1a5cf166-c599-4f58-9f3c-f1148be47fca
namespace: openebs
resourceVersion: "30035"
selfLink: /apis/openebs.io/v1alpha1/namespaces/openebs/zfssnapshots/snapshot-1a5cf166-c599-4f58-9f3c-f1148be47fca
uid: e29d571c-42b5-4fb7-9110-e1cfc9b96641
spec:
capacity: "4294967296"
fsType: zfs
ownerNodeID: pawan-2
poolName: zfspv-pool
status: Ready
volumeType: DATASET
kind: List
metadata:
resourceVersion: ""
selfLink: ""
```
Create a clone volume
We can provide a datasource as snapshot name to create a clone volume
```yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: zfspv-clone
spec:
storageClassName: openebs-zfspv
dataSource:
name: zfspv-snap
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
```
It will create a ZFS clone volume from the mentioned snapshot and create the PV on the same node where original volume is there.
Here, As resize is not supported yet, the clone PVC size should match the size of the snapshot.
Also, all the properties from the storageclass will not be considered for the clone case, it will take the properties from the snapshot and create the clone volume. One thing to note here is that, the storageclass in clone PVC should have the same poolname as that of the original volume as across the pool, clone is not supported.
Signed-off-by: Pawan <pawan@mayadata.io>
This commit is contained in:
parent
b0434bb537
commit
287606b78a
40 changed files with 2995 additions and 123 deletions
232
pkg/builder/volbuilder/build.go
Normal file
232
pkg/builder/volbuilder/build.go
Normal file
|
|
@ -0,0 +1,232 @@
|
|||
/*
|
||||
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 volbuilder
|
||||
|
||||
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 ZFSVolume
|
||||
func (b *Builder) WithNamespace(namespace string) *Builder {
|
||||
if namespace == "" {
|
||||
b.errs = append(
|
||||
b.errs,
|
||||
errors.New(
|
||||
"failed to build zfs volume object: missing namespace",
|
||||
),
|
||||
)
|
||||
return b
|
||||
}
|
||||
b.volume.Object.Namespace = namespace
|
||||
return b
|
||||
}
|
||||
|
||||
// WithName sets the name of ZFSVolume
|
||||
func (b *Builder) WithName(name string) *Builder {
|
||||
if name == "" {
|
||||
b.errs = append(
|
||||
b.errs,
|
||||
errors.New(
|
||||
"failed to build zfs volume object: missing name",
|
||||
),
|
||||
)
|
||||
return b
|
||||
}
|
||||
b.volume.Object.Name = name
|
||||
return b
|
||||
}
|
||||
|
||||
// WithCapacity sets the Capacity of zfs 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 zfs volume object: missing capacity",
|
||||
),
|
||||
)
|
||||
return b
|
||||
}
|
||||
b.volume.Object.Spec.Capacity = capacity
|
||||
return b
|
||||
}
|
||||
|
||||
// WithEncryption sets the encryption on ZFSVolume
|
||||
func (b *Builder) WithEncryption(encr string) *Builder {
|
||||
b.volume.Object.Spec.Encryption = encr
|
||||
return b
|
||||
}
|
||||
|
||||
// WithKeyLocation sets the encryption key location on ZFSVolume
|
||||
func (b *Builder) WithKeyLocation(kl string) *Builder {
|
||||
b.volume.Object.Spec.KeyLocation = kl
|
||||
return b
|
||||
}
|
||||
|
||||
// WithKeyFormat sets the encryption key format on ZFSVolume
|
||||
func (b *Builder) WithKeyFormat(kf string) *Builder {
|
||||
b.volume.Object.Spec.KeyFormat = kf
|
||||
return b
|
||||
}
|
||||
|
||||
// WithCompression sets compression of ZFSVolume
|
||||
func (b *Builder) WithCompression(compression string) *Builder {
|
||||
b.volume.Object.Spec.Compression = compression
|
||||
return b
|
||||
}
|
||||
|
||||
// WithDedup sets dedup property of ZFSVolume
|
||||
func (b *Builder) WithDedup(dedup string) *Builder {
|
||||
b.volume.Object.Spec.Dedup = dedup
|
||||
return b
|
||||
}
|
||||
|
||||
// WithThinProv sets if ZFSVolume needs to be thin provisioned
|
||||
func (b *Builder) WithThinProv(thinprov string) *Builder {
|
||||
b.volume.Object.Spec.ThinProvision = thinprov
|
||||
return b
|
||||
}
|
||||
|
||||
// WithOwnerNode sets owner node for the ZFSVolume where the volume should be provisioned
|
||||
func (b *Builder) WithOwnerNode(host string) *Builder {
|
||||
b.volume.Object.Spec.OwnerNodeID = host
|
||||
return b
|
||||
}
|
||||
|
||||
// WithRecordSize sets the recordsize of ZFSVolume
|
||||
func (b *Builder) WithRecordSize(rs string) *Builder {
|
||||
b.volume.Object.Spec.RecordSize = rs
|
||||
return b
|
||||
}
|
||||
|
||||
// WithVolBlockSize sets the volblocksize of ZFSVolume
|
||||
func (b *Builder) WithVolBlockSize(bs string) *Builder {
|
||||
b.volume.Object.Spec.VolBlockSize = bs
|
||||
return b
|
||||
}
|
||||
|
||||
// WithVolumeType sets if ZFSVolume needs to be thin provisioned
|
||||
func (b *Builder) WithVolumeType(vtype string) *Builder {
|
||||
b.volume.Object.Spec.VolumeType = vtype
|
||||
return b
|
||||
}
|
||||
|
||||
// WithFsType sets filesystem for the ZFSVolume
|
||||
func (b *Builder) WithFsType(fstype string) *Builder {
|
||||
b.volume.Object.Spec.FsType = fstype
|
||||
return b
|
||||
}
|
||||
|
||||
// WithSnapshot sets Snapshot name for creating clone volume
|
||||
func (b *Builder) WithSnapshot(snap string) *Builder {
|
||||
b.volume.Object.Spec.SnapName = snap
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Builder) WithPoolName(pool string) *Builder {
|
||||
if pool == "" {
|
||||
b.errs = append(
|
||||
b.errs,
|
||||
errors.New(
|
||||
"failed to build zfs 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 zfs 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 {
|
||||
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 ZFSVolume 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/volbuilder/buildlist.go
Normal file
72
pkg/builder/volbuilder/buildlist.go
Normal 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 volbuilder
|
||||
|
||||
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/volbuilder/kubernetes.go
Normal file
427
pkg/builder/volbuilder/kubernetes.go
Normal 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 volbuilder
|
||||
|
||||
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 zfs 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 zfs 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 zfs 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 zfs volume instance
|
||||
type delFn func(
|
||||
cli *clientset.Clientset,
|
||||
name,
|
||||
namespace string,
|
||||
opts *metav1.DeleteOptions,
|
||||
) error
|
||||
|
||||
// updateFn is a typed function that abstracts
|
||||
// updating zfs volume instance
|
||||
type updateFn func(
|
||||
cs *clientset.Clientset,
|
||||
vol *apis.ZFSVolume,
|
||||
namespace string,
|
||||
) (*apis.ZFSVolume, error)
|
||||
|
||||
// Kubeclient enables kubernetes API operations
|
||||
// on zfs volume instance
|
||||
type Kubeclient struct {
|
||||
// clientset refers to zfs 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 zfs 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
|
||||
// zfs 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 zfs 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 zfs 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 zfs 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 zfs 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 zfs 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 zfs volume {%s} in namespace {%s}",
|
||||
vol.Name,
|
||||
k.namespace,
|
||||
)
|
||||
}
|
||||
|
||||
return k.create(cs, vol, k.namespace)
|
||||
}
|
||||
|
||||
// Get returns zfs 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 zfs volume: missing zfs volume name",
|
||||
)
|
||||
}
|
||||
|
||||
cli, err := k.getClientOrCached()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(
|
||||
err,
|
||||
"failed to get zfs volume {%s} in namespace {%s}",
|
||||
name,
|
||||
k.namespace,
|
||||
)
|
||||
}
|
||||
|
||||
return k.get(cli, name, k.namespace, opts)
|
||||
}
|
||||
|
||||
// GetRaw returns zfs 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 zfs volume: missing vol name",
|
||||
)
|
||||
}
|
||||
csiv, err := k.Get(name, opts)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(
|
||||
err,
|
||||
"failed to get zfs volume {%s} in namespace {%s}",
|
||||
name,
|
||||
k.namespace,
|
||||
)
|
||||
}
|
||||
|
||||
return json.Marshal(csiv)
|
||||
}
|
||||
|
||||
// List returns a list of zfs 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 zfs volumes in namespace {%s}",
|
||||
k.namespace,
|
||||
)
|
||||
}
|
||||
|
||||
return k.list(cli, k.namespace, opts)
|
||||
}
|
||||
|
||||
// Delete deletes the zfs 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 zfs 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)
|
||||
}
|
||||
117
pkg/builder/volbuilder/volume.go
Normal file
117
pkg/builder/volbuilder/volume.go
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
// 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 volbuilder
|
||||
|
||||
import (
|
||||
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/core/v1alpha1"
|
||||
)
|
||||
|
||||
// ZFSVolume is a wrapper over
|
||||
// ZFSVolume API instance
|
||||
type ZFSVolume struct {
|
||||
// ZFSVolume object
|
||||
Object *apis.ZFSVolume
|
||||
}
|
||||
|
||||
// From returns a new instance of
|
||||
// zfs 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 zfs volume instances
|
||||
type ZFSVolumeList struct {
|
||||
// List conatils list of volumes
|
||||
List apis.ZFSVolumeList
|
||||
}
|
||||
|
||||
// Len returns the number of items present
|
||||
// in the ZFSVolumeList
|
||||
func (volList *ZFSVolumeList) Len() int {
|
||||
return len(volList.List.Items)
|
||||
}
|
||||
|
||||
// all returns true if all the predicates
|
||||
// succeed against the provided ZFSVolume
|
||||
// instance
|
||||
func (l predicateList) all(vol *ZFSVolume) bool {
|
||||
for _, pred := range l {
|
||||
if !pred(vol) {
|
||||
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(vol *ZFSVolume) bool {
|
||||
for key, value := range keyValuePair {
|
||||
if !vol.HasLabel(key, value) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// HasLabel returns true if provided label
|
||||
// is present in the provided ZFSVolume instance
|
||||
func (vol *ZFSVolume) HasLabel(key, value string) bool {
|
||||
val, ok := vol.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(vol *ZFSVolume) bool {
|
||||
return vol.HasLabel(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
// IsNil returns true if the zfs volume instance
|
||||
// is nil
|
||||
func (vol *ZFSVolume) IsNil() bool {
|
||||
return vol.Object == nil
|
||||
}
|
||||
|
||||
// IsNil is predicate to filter out nil zfs volume
|
||||
// instances
|
||||
func IsNil() Predicate {
|
||||
return func(vol *ZFSVolume) bool {
|
||||
return vol.IsNil()
|
||||
}
|
||||
}
|
||||
|
||||
// GetAPIObject returns zfs volume's API instance
|
||||
func (vol *ZFSVolume) GetAPIObject() *apis.ZFSVolume {
|
||||
return vol.Object
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue