Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: volume snapshot class #183

Merged
merged 5 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions bundle/manifests/lvm-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ spec:
- delete
- get
- update
- apiGroups:
- snapshot.storage.k8s.io
resources:
- volumesnapshotclasses
verbs:
- create
- delete
- get
- list
- watch
- apiGroups:
- storage.k8s.io
resources:
Expand Down Expand Up @@ -236,6 +246,18 @@ spec:
exporter: lvm-operator
spec:
containers:
- args:
- --secure-listen-address=0.0.0.0:8443
- --upstream=http://127.0.0.1:8080/
- --logtostderr=true
- --v=10
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.8.0
name: kube-rbac-proxy
ports:
- containerPort: 8443
name: https
protocol: TCP
resources: {}
- args:
- --health-probe-bind-address=:8081
- --metrics-bind-address=127.0.0.1:8080
Expand Down Expand Up @@ -286,18 +308,6 @@ spec:
memory: 20Mi
securityContext:
allowPrivilegeEscalation: false
- args:
- --secure-listen-address=0.0.0.0:8443
- --upstream=http://127.0.0.1:8080/
- --logtostderr=true
- --v=10
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.8.0
name: kube-rbac-proxy
ports:
- containerPort: 8443
name: https
protocol: TCP
resources: {}
- command:
- /metricsexporter
image: quay.io/ocs-dev/lvm-operator:latest
Expand Down
10 changes: 10 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ rules:
- delete
- get
- update
- apiGroups:
- snapshot.storage.k8s.io
resources:
- volumesnapshotclasses
verbs:
- create
- delete
- get
- list
- watch
- apiGroups:
- storage.k8s.io
resources:
Expand Down
2 changes: 2 additions & 0 deletions controllers/lvmcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (r *LVMClusterReconciler) reconcile(ctx context.Context, instance *lvmv1alp
&vgManager{},
&lvmVG{},
&topolvmStorageClass{},
&topolvmVolumeSnapshotClass{},
}

// handle create/update
Expand Down Expand Up @@ -360,6 +361,7 @@ func (r *LVMClusterReconciler) processDelete(ctx context.Context, instance *lvmv
if controllerutil.ContainsFinalizer(instance, lvmClusterFinalizer) {

resourceDeletionList := []resourceManager{
&topolvmVolumeSnapshotClass{},
&topolvmStorageClass{},
&lvmVG{},
&topolvmController{},
Expand Down
11 changes: 10 additions & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

snapapi "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
secv1client "github.com/openshift/client-go/security/clientset/versioned/typed/security/v1"
lvmv1alpha1 "github.com/red-hat-storage/lvm-operator/api/v1alpha1"
topolvmv1 "github.com/topolvm/topolvm/api/v1"
Expand Down Expand Up @@ -73,8 +74,12 @@ var _ = BeforeSuite(func() {

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases"),
filepath.Join("..", "e2e", "testdata")},
ErrorIfCRDPathMissing: true,
sp98 marked this conversation as resolved.
Show resolved Hide resolved
CRDInstallOptions: envtest.CRDInstallOptions{
CleanUpAfterUse: true,
},
}

cfg, err := testEnv.Start()
Expand All @@ -86,6 +91,10 @@ var _ = BeforeSuite(func() {

err = topolvmv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

err = snapapi.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expand Down
103 changes: 103 additions & 0 deletions controllers/topolvm_snapshotclass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package controllers

import (
"context"
"fmt"

snapapi "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
lvmv1alpha1 "github.com/red-hat-storage/lvm-operator/api/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
cutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

const (
vscName = "topolvm-volumeSnapshotClass"
)

type topolvmVolumeSnapshotClass struct{}

// topolvmVolumeSnapshotClass unit satisfies resourceManager interface
var _ resourceManager = topolvmVolumeSnapshotClass{}

func (s topolvmVolumeSnapshotClass) getName() string {
return vscName
}

//+kubebuilder:rbac:groups=snapshot.storage.k8s.io,resources=volumesnapshotclasses,verbs=get;create;delete;watch;list

func (s topolvmVolumeSnapshotClass) ensureCreated(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error {

// one volume snapshot class for every deviceClass based on CR is created
topolvmSnapshotClasses := getTopolvmSnapshotClasses(lvmCluster)
for _, vsc := range topolvmSnapshotClasses {

// we anticipate no edits to volume snapshot class
result, err := cutil.CreateOrUpdate(ctx, r.Client, vsc, func() error { return nil })
if err != nil {
r.Log.Error(err, "topolvm volume snapshot class reconcile failure", "name", vsc.Name)
return err
} else {
r.Log.Info("topolvm volume snapshot class", "operation", result, "name", vsc.Name)
}
}
return nil
}

func (s topolvmVolumeSnapshotClass) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error {

// construct name of volume snapshot class based on CR spec deviceClass field and
// delete the corresponding volume snapshot class
for _, deviceClass := range lvmCluster.Spec.Storage.DeviceClasses {
vsc := &snapapi.VolumeSnapshotClass{}
vscName := fmt.Sprintf("odf-lvm-%s", deviceClass.Name)
err := r.Client.Get(ctx, types.NamespacedName{Name: vscName}, vsc)

if err != nil {
// already deleted in previous reconcile
if errors.IsNotFound(err) {
r.Log.Info("topolvm volume snapshot class is deleted", "VolumeSnapshotClass", vscName)
return nil
}
r.Log.Error(err, "failed to retrieve topolvm volume snapshot class", "VolumeSnapshotClass", vscName)
return err
}

// VolumeSnapshotClass exists, initiate deletion
if vsc.GetDeletionTimestamp().IsZero() {
if err = r.Client.Delete(ctx, vsc); err != nil {
r.Log.Error(err, "failed to delete topolvm volume snapshot class", "VolumeSnapshotClass", vscName)
return err
} else {
r.Log.Info("initiated topolvm volume snapshot class deletion", "VolumeSnapshotClass", vscName)
}
} else {
// return error for next reconcile to confirm deletion
return fmt.Errorf("topolvm volume snapshot class %s is already marked for deletion", vscName)
}
}
return nil
}

func (s topolvmVolumeSnapshotClass) updateStatus(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error {
// intentionally empty as there'll be no status field on VolumeSnapshotClass resource
return nil
}

func getTopolvmSnapshotClasses(lvmCluster *lvmv1alpha1.LVMCluster) []*snapapi.VolumeSnapshotClass {
vsc := []*snapapi.VolumeSnapshotClass{}

for _, deviceClass := range lvmCluster.Spec.Storage.DeviceClasses {
snapshotClass := &snapapi.VolumeSnapshotClass{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("odf-lvm-%s", deviceClass.Name),
},

Driver: TopolvmCSIDriverName,
DeletionPolicy: snapapi.VolumeSnapshotContentDelete,
}
vsc = append(vsc, snapshotClass)
}
return vsc
}
4 changes: 4 additions & 0 deletions controllers/vgmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

snapapi "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
lvmv1alpha1 "github.com/red-hat-storage/lvm-operator/api/v1alpha1"
"gotest.tools/v3/assert"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -34,6 +35,9 @@ func newFakeLVMClusterReconciler(t *testing.T, objs ...client.Object) *LVMCluste
err = storagev1.AddToScheme(scheme)
assert.NilError(t, err, "adding storagev1 to scheme")

err = snapapi.AddToScheme(scheme)
assert.NilError(t, err, "adding snapshot api to scheme")

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()

return &LVMClusterReconciler{
Expand Down
133 changes: 133 additions & 0 deletions e2e/testdata/snapshot.storage.k8s.io_volumesnapshotclasses.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.8.0
api-approved.kubernetes.io: "https://github.com/kubernetes-csi/external-snapshotter/pull/665"
creationTimestamp: null
name: volumesnapshotclasses.snapshot.storage.k8s.io
spec:
group: snapshot.storage.k8s.io
names:
kind: VolumeSnapshotClass
listKind: VolumeSnapshotClassList
plural: volumesnapshotclasses
shortNames:
- vsclass
- vsclasses
singular: volumesnapshotclass
scope: Cluster
versions:
- additionalPrinterColumns:
- jsonPath: .driver
name: Driver
type: string
- description: Determines whether a VolumeSnapshotContent created through the
VolumeSnapshotClass should be deleted when its bound VolumeSnapshot is deleted.
jsonPath: .deletionPolicy
name: DeletionPolicy
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1
schema:
openAPIV3Schema:
description: VolumeSnapshotClass specifies parameters that a underlying storage
system uses when creating a volume snapshot. A specific VolumeSnapshotClass
is used by specifying its name in a VolumeSnapshot object. VolumeSnapshotClasses
are non-namespaced
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
deletionPolicy:
description: deletionPolicy determines whether a VolumeSnapshotContent
created through the VolumeSnapshotClass should be deleted when its bound
VolumeSnapshot is deleted. Supported values are "Retain" and "Delete".
"Retain" means that the VolumeSnapshotContent and its physical snapshot
on underlying storage system are kept. "Delete" means that the VolumeSnapshotContent
and its physical snapshot on underlying storage system are deleted.
Required.
enum:
- Delete
- Retain
type: string
driver:
description: driver is the name of the storage driver that handles this
VolumeSnapshotClass. Required.
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
parameters:
additionalProperties:
type: string
description: parameters is a key-value map with storage driver specific parameters for creating snapshots. These values are opaque to Kubernetes.
type: object
required:
- deletionPolicy
- driver
type: object
served: true
storage: true
subresources: {}
- additionalPrinterColumns:
- jsonPath: .driver
name: Driver
type: string
- description: Determines whether a VolumeSnapshotContent created through the VolumeSnapshotClass should be deleted when its bound VolumeSnapshot is deleted.
jsonPath: .deletionPolicy
name: DeletionPolicy
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1beta1
# This indicates the v1beta1 version of the custom resource is deprecated.
# API requests to this version receive a warning in the server response.
deprecated: true
# This overrides the default warning returned to clients making v1beta1 API requests.
deprecationWarning: "snapshot.storage.k8s.io/v1beta1 VolumeSnapshotClass is deprecated; use snapshot.storage.k8s.io/v1 VolumeSnapshotClass"
schema:
openAPIV3Schema:
description: VolumeSnapshotClass specifies parameters that a underlying storage system uses when creating a volume snapshot. A specific VolumeSnapshotClass is used by specifying its name in a VolumeSnapshot object. VolumeSnapshotClasses are non-namespaced
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
deletionPolicy:
description: deletionPolicy determines whether a VolumeSnapshotContent created through the VolumeSnapshotClass should be deleted when its bound VolumeSnapshot is deleted. Supported values are "Retain" and "Delete". "Retain" means that the VolumeSnapshotContent and its physical snapshot on underlying storage system are kept. "Delete" means that the VolumeSnapshotContent and its physical snapshot on underlying storage system are deleted. Required.
enum:
- Delete
- Retain
type: string
driver:
description: driver is the name of the storage driver that handles this VolumeSnapshotClass. Required.
type: string
kind:
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
parameters:
additionalProperties:
type: string
description: parameters is a key-value map with storage driver specific parameters for creating snapshots. These values are opaque to Kubernetes.
type: object
required:
- deletionPolicy
- driver
type: object
served: true
storage: false
subresources: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
Loading