Skip to content

Commit

Permalink
feat(pvc): Support Snapshotting (#98)
Browse files Browse the repository at this point in the history
* feat(pvc): Support Snapshotting

* separate it out

* sample Dragonfly PVC

* get it working

* name mount path better

* have a snapshot top level field

* use fsgroup to set the dfly permissions

* add a df pvc test

* fix nits and commits

* add sample and test for schedule

* update crd bases

* remove volume and name it to df

* err if cron is set without pvc
  • Loading branch information
Pothulapati authored Oct 2, 2023
1 parent 0edd7fc commit 0fbc4a9
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 0 deletions.
17 changes: 17 additions & 0 deletions api/v1alpha1/dragonfly_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ type DragonflySpec struct {
// +optional
// +kubebuilder:validation:Optional
ServiceAccountName string `json:"serviceAccountName,omitempty"`

// (Optional) Dragonfly Snapshot configuration
// +optional
// +kubebuilder:validation:Optional
Snapshot *Snapshot `json:"snapshot,omitempty"`
}

type Snapshot struct {
// (Optional) Dragonfly snapshot schedule
// +optional
// +kubebuilder:validation:Optional
Cron string `json:"cron,omitempty"`

// (Optional) Dragonfly PVC spec
// +optional
// +kubebuilder:validation:Optional
PersistentVolumeClaimSpec *corev1.PersistentVolumeClaimSpec `json:"persistentVolumeClaimSpec,omitempty"`
}

// DragonflyStatus defines the observed state of Dragonfly
Expand Down
25 changes: 25 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

215 changes: 215 additions & 0 deletions config/crd/bases/dragonflydb.io_dragonflies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,221 @@ spec:
serviceAccountName:
description: (Optional) Dragonfly pod service account name
type: string
snapshot:
description: (Optional) Dragonfly Snapshot configuration
properties:
cron:
description: (Optional) Dragonfly snapshot schedule
type: string
persistentVolumeClaimSpec:
description: (Optional) Dragonfly PVC spec
properties:
accessModes:
description: 'accessModes contains the desired access modes
the volume should have. More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#access-modes-1'
items:
type: string
type: array
dataSource:
description: 'dataSource field can be used to specify either:
* An existing VolumeSnapshot object (snapshot.storage.k8s.io/VolumeSnapshot)
* An existing PVC (PersistentVolumeClaim) If the provisioner
or an external controller can support the specified data
source, it will create a new volume based on the contents
of the specified data source. When the AnyVolumeDataSource
feature gate is enabled, dataSource contents will be copied
to dataSourceRef, and dataSourceRef contents will be copied
to dataSource when dataSourceRef.namespace is not specified.
If the namespace is specified, then dataSourceRef will not
be copied to dataSource.'
properties:
apiGroup:
description: APIGroup is the group for the resource being
referenced. If APIGroup is not specified, the specified
Kind must be in the core API group. For any other third-party
types, APIGroup is required.
type: string
kind:
description: Kind is the type of resource being referenced
type: string
name:
description: Name is the name of resource being referenced
type: string
required:
- kind
- name
type: object
x-kubernetes-map-type: atomic
dataSourceRef:
description: 'dataSourceRef specifies the object from which
to populate the volume with data, if a non-empty volume
is desired. This may be any object from a non-empty API
group (non core object) or a PersistentVolumeClaim object.
When this field is specified, volume binding will only succeed
if the type of the specified object matches some installed
volume populator or dynamic provisioner. This field will
replace the functionality of the dataSource field and as
such if both fields are non-empty, they must have the same
value. For backwards compatibility, when namespace isn''t
specified in dataSourceRef, both fields (dataSource and
dataSourceRef) will be set to the same value automatically
if one of them is empty and the other is non-empty. When
namespace is specified in dataSourceRef, dataSource isn''t
set to the same value and must be empty. There are three
important differences between dataSource and dataSourceRef:
* While dataSource only allows two specific types of objects,
dataSourceRef allows any non-core object, as well as PersistentVolumeClaim
objects. * While dataSource ignores disallowed values (dropping
them), dataSourceRef preserves all values, and generates
an error if a disallowed value is specified. * While dataSource
only allows local objects, dataSourceRef allows objects
in any namespaces. (Beta) Using this field requires the
AnyVolumeDataSource feature gate to be enabled. (Alpha)
Using the namespace field of dataSourceRef requires the
CrossNamespaceVolumeDataSource feature gate to be enabled.'
properties:
apiGroup:
description: APIGroup is the group for the resource being
referenced. If APIGroup is not specified, the specified
Kind must be in the core API group. For any other third-party
types, APIGroup is required.
type: string
kind:
description: Kind is the type of resource being referenced
type: string
name:
description: Name is the name of resource being referenced
type: string
namespace:
description: Namespace is the namespace of resource being
referenced Note that when a namespace is specified,
a gateway.networking.k8s.io/ReferenceGrant object is
required in the referent namespace to allow that namespace's
owner to accept the reference. See the ReferenceGrant
documentation for details. (Alpha) This field requires
the CrossNamespaceVolumeDataSource feature gate to be
enabled.
type: string
required:
- kind
- name
type: object
resources:
description: 'resources represents the minimum resources the
volume should have. If RecoverVolumeExpansionFailure feature
is enabled users are allowed to specify resource requirements
that are lower than previous value but must still be higher
than capacity recorded in the status field of the claim.
More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#resources'
properties:
claims:
description: "Claims lists the names of resources, defined
in spec.resourceClaims, that are used by this container.
\n This is an alpha field and requires enabling the
DynamicResourceAllocation feature gate. \n This field
is immutable. It can only be set for containers."
items:
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
properties:
name:
description: Name must match the name of one entry
in pod.spec.resourceClaims of the Pod where this
field is used. It makes that resource available
inside a container.
type: string
required:
- name
type: object
type: array
x-kubernetes-list-map-keys:
- name
x-kubernetes-list-type: map
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Limits describes the maximum amount of compute
resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Requests describes the minimum amount of
compute resources required. If Requests is omitted for
a container, it defaults to Limits if that is explicitly
specified, otherwise to an implementation-defined value.
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
selector:
description: selector is a label query over volumes to consider
for binding.
properties:
matchExpressions:
description: matchExpressions is a list of label selector
requirements. The requirements are ANDed.
items:
description: A label selector requirement is a selector
that contains values, a key, and an operator that
relates the key and values.
properties:
key:
description: key is the label key that the selector
applies to.
type: string
operator:
description: operator represents a key's relationship
to a set of values. Valid operators are In, NotIn,
Exists and DoesNotExist.
type: string
values:
description: values is an array of string values.
If the operator is In or NotIn, the values array
must be non-empty. If the operator is Exists or
DoesNotExist, the values array must be empty.
This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
matchLabels:
additionalProperties:
type: string
description: matchLabels is a map of {key,value} pairs.
A single {key,value} in the matchLabels map is equivalent
to an element of matchExpressions, whose key field is
"key", the operator is "In", and the values array contains
only "value". The requirements are ANDed.
type: object
type: object
x-kubernetes-map-type: atomic
storageClassName:
description: 'storageClassName is the name of the StorageClass
required by the claim. More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#class-1'
type: string
volumeMode:
description: volumeMode defines what type of volume is required
by the claim. Value of Filesystem is implied when not included
in claim spec.
type: string
volumeName:
description: volumeName is the binding reference to the PersistentVolume
backing this claim.
type: string
type: object
type: object
tolerations:
description: (Optional) Dragonfly pod tolerations
items:
Expand Down
27 changes: 27 additions & 0 deletions config/samples/pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: dragonflydb.io/v1alpha1
kind: Dragonfly
metadata:
labels:
app.kubernetes.io/name: dragonfly
app.kubernetes.io/instance: dragonfly-sample
app.kubernetes.io/part-of: dragonfly-operator
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/created-by: dragonfly-operator
name: dragonfly-sample
spec:
replicas: 1
resources:
requests:
cpu: 500m
memory: 500Mi
limits:
cpu: 600m
memory: 750Mi
snapshot:
cron: "*/5 * * * *"
persistentVolumeClaimSpec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
73 changes: 73 additions & 0 deletions e2e/dragonfly_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,79 @@ var _ = Describe("Dragonfly Reconciler", Ordered, func() {
})
})

var _ = Describe("Dragonfly PVC Test", Ordered, func() {

ctx := context.Background()
name := "df-pvc"
namespace := "default"
schedule := "*/1 * * * *"

args := []string{
"--vmodule=replica=1,server_family=1",
}

Context("Dragonfly resource creation and data insertion", func() {
It("Should create successfully", func() {
err := k8sClient.Create(ctx, &dragonflydbiov1alpha1.Dragonfly{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: dragonflydbiov1alpha1.DragonflySpec{
Replicas: 1,
Args: args,
Snapshot: &dragonflydbiov1alpha1.Snapshot{
Cron: schedule,
PersistentVolumeClaimSpec: &corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{
corev1.ReadWriteOnce,
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: resource.MustParse("1Gi"),
},
},
},
},
},
})
Expect(err).To(BeNil())

// Wait until Dragonfly object is marked initialized
waitForDragonflyPhase(ctx, k8sClient, name, namespace, controller.PhaseResourcesCreated, 2*time.Minute)
waitForStatefulSetReady(ctx, k8sClient, name, namespace, 2*time.Minute)

// Check for service and statefulset
var ss appsv1.StatefulSet
err = k8sClient.Get(ctx, types.NamespacedName{
Name: name,
Namespace: namespace,
}, &ss)
Expect(err).To(BeNil())

var svc corev1.Service
err = k8sClient.Get(ctx, types.NamespacedName{
Name: name,
Namespace: namespace,
}, &svc)
Expect(err).To(BeNil())

// check if the pvc is created
var pvcs corev1.PersistentVolumeClaimList
err = k8sClient.List(ctx, &pvcs, client.InNamespace(namespace), client.MatchingLabels{
"app": name,
resources.KubernetesPartOfLabelKey: "dragonfly",
})
Expect(err).To(BeNil())
Expect(pvcs.Items).To(HaveLen(1))
Expect(ss.Spec.Template.Spec.Containers[0].Args).To(ContainElement(fmt.Sprintf("--snapshot_cron=%s", schedule)))

// TODO: Do data insert testing
})

})
})

func waitForDragonflyPhase(ctx context.Context, c client.Client, name, namespace, phase string, maxDuration time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, maxDuration)
defer cancel()
Expand Down
Empty file modified hack/print-roles.go
100644 → 100755
Empty file.
Loading

0 comments on commit 0fbc4a9

Please sign in to comment.