Skip to content

Commit

Permalink
feat: support to specify volume mode when creating clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf committed Oct 7, 2023
1 parent bc7b193 commit e0ef873
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
34 changes: 17 additions & 17 deletions apis/apps/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,12 @@ type ClusterComponentVolumeClaimTemplate struct {
}

func (r *ClusterComponentVolumeClaimTemplate) toVolumeClaimTemplate() corev1.PersistentVolumeClaimTemplate {
t := corev1.PersistentVolumeClaimTemplate{}
t.ObjectMeta.Name = r.Name
t.Spec = r.Spec.ToV1PersistentVolumeClaimSpec()
return t
return corev1.PersistentVolumeClaimTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: r.Name,
},
Spec: r.Spec.ToV1PersistentVolumeClaimSpec(),
}
}

type PersistentVolumeClaimSpec struct {
Expand All @@ -463,34 +465,32 @@ type PersistentVolumeClaimSpec struct {
// More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#class-1.
// +optional
StorageClassName *string `json:"storageClassName,omitempty" protobuf:"bytes,5,opt,name=storageClassName"`
// TODO:
// // preferStorageClassNames added support specifying storageclasses.storage.k8s.io names, in order
// // to adapt multi-cloud deployment, where storageclasses are all distinctly different among clouds.
// // +listType=set
// // +optional
// PreferSCNames []string `json:"preferStorageClassNames,omitempty"`
// volumeMode defines what type of volume is required by the claim.
// Value of Filesystem is implied when not included in claim spec.
// +optional
VolumeMode *corev1.PersistentVolumeMode `json:"volumeMode,omitempty" protobuf:"bytes,6,opt,name=volumeMode,casttype=PersistentVolumeMode"`
}

// ToV1PersistentVolumeClaimSpec converts to corev1.PersistentVolumeClaimSpec.
func (r *PersistentVolumeClaimSpec) ToV1PersistentVolumeClaimSpec() corev1.PersistentVolumeClaimSpec {
return corev1.PersistentVolumeClaimSpec{
AccessModes: r.AccessModes,
Resources: r.Resources,
StorageClassName: r.GetStorageClassName(viper.GetString(constant.CfgKeyDefaultStorageClass)),
StorageClassName: r.getStorageClassName(viper.GetString(constant.CfgKeyDefaultStorageClass)),
VolumeMode: r.VolumeMode,
}
}

// GetStorageClassName returns PersistentVolumeClaimSpec.StorageClassName if a value is assigned; otherwise,
// getStorageClassName returns PersistentVolumeClaimSpec.StorageClassName if a value is assigned; otherwise,
// it returns preferSC argument.
func (r *PersistentVolumeClaimSpec) GetStorageClassName(preferSC string) *string {
func (r *PersistentVolumeClaimSpec) getStorageClassName(preferSC string) *string {
if r.StorageClassName != nil && *r.StorageClassName != "" {
return r.StorageClassName
}

if preferSC == "" {
return nil
if preferSC != "" {
return &preferSC
}
return &preferSC
return nil
}

type Affinity struct {
Expand Down
21 changes: 16 additions & 5 deletions apis/apps/v1alpha1/cluster_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/yaml"
Expand All @@ -33,7 +34,6 @@ import (
)

var _ = Describe("", func() {

It("test GetMinAvailable", func() {
prefer := intstr.IntOrString{}
clusterCompSpec := &ClusterComponentSpec{}
Expand All @@ -58,7 +58,8 @@ var _ = Describe("", func() {
pvcSpec := r.ToV1PersistentVolumeClaimSpec()
Expect(pvcSpec.AccessModes).Should(BeEquivalentTo(r.AccessModes))
Expect(pvcSpec.Resources).Should(BeEquivalentTo(r.Resources))
Expect(pvcSpec.StorageClassName).Should(BeEquivalentTo(r.GetStorageClassName(viper.GetString(constant.CfgKeyDefaultStorageClass))))
Expect(pvcSpec.StorageClassName).Should(BeEquivalentTo(r.getStorageClassName(viper.GetString(constant.CfgKeyDefaultStorageClass))))
Expect(pvcSpec.VolumeMode).Should(BeEquivalentTo(r.VolumeMode))
})

It("test ToV1PersistentVolumeClaimSpec with default storage class", func() {
Expand All @@ -70,14 +71,24 @@ var _ = Describe("", func() {
viper.Set(constant.CfgKeyDefaultStorageClass, "")
})

It("test GetStorageClassName", func() {
It("test ToV1PersistentVolumeClaimSpec with volume mode", func() {
for _, mode := range []corev1.PersistentVolumeMode{corev1.PersistentVolumeBlock, corev1.PersistentVolumeFilesystem} {
r := PersistentVolumeClaimSpec{
VolumeMode: &mode,
}
pvcSpec := r.ToV1PersistentVolumeClaimSpec()
Expect(pvcSpec.VolumeMode).Should(BeEquivalentTo(r.VolumeMode))
}
})

It("test getStorageClassName", func() {
preferSC := "prefer-sc"
r := PersistentVolumeClaimSpec{}
r.StorageClassName = nil
Expect(r.GetStorageClassName(preferSC)).Should(BeEquivalentTo(&preferSC))
Expect(r.getStorageClassName(preferSC)).Should(BeEquivalentTo(&preferSC))
scName := "test-sc"
r.StorageClassName = &scName
Expect(r.GetStorageClassName(preferSC)).Should(BeEquivalentTo(&scName))
Expect(r.getStorageClassName(preferSC)).Should(BeEquivalentTo(&scName))
})

It("test IsDeleting", func() {
Expand Down
4 changes: 2 additions & 2 deletions controllers/apps/components/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,9 @@ func (c *rsmComponent) restart(reqCtx intctrlutil.RequestCtx, cli client.Client)
func (c *rsmComponent) expandVolume(reqCtx intctrlutil.RequestCtx, cli client.Client) error {
for _, vct := range c.runningWorkload.Spec.VolumeClaimTemplates {
var proto *corev1.PersistentVolumeClaimTemplate
for _, v := range c.component.VolumeClaimTemplates {
for i, v := range c.component.VolumeClaimTemplates {
if v.Name == vct.Name {
proto = &v
proto = &c.component.VolumeClaimTemplates[i]
break
}
}
Expand Down
21 changes: 7 additions & 14 deletions internal/controller/factory/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ func BuildCommonLabels(cluster *appsv1alpha1.Cluster,
}
}

func BuildSts(reqCtx intctrlutil.RequestCtx, cluster *appsv1alpha1.Cluster,
component *component.SynthesizedComponent, envConfigName string) (*appsv1.StatefulSet, error) {
vctToPVC := func(vct corev1.PersistentVolumeClaimTemplate) corev1.PersistentVolumeClaim {
return corev1.PersistentVolumeClaim{
ObjectMeta: vct.ObjectMeta,
Spec: vct.Spec,
}
func vctToPVC(vct corev1.PersistentVolumeClaimTemplate) corev1.PersistentVolumeClaim {
return corev1.PersistentVolumeClaim{
ObjectMeta: vct.ObjectMeta,
Spec: vct.Spec,
}
}

func BuildSts(reqCtx intctrlutil.RequestCtx, cluster *appsv1alpha1.Cluster,
component *component.SynthesizedComponent, envConfigName string) (*appsv1.StatefulSet, error) {
commonLabels := BuildCommonLabels(cluster, component)
podBuilder := builder.NewPodBuilder("", "").
AddLabelsInMap(commonLabels).
Expand Down Expand Up @@ -274,13 +274,6 @@ func buildWellKnownLabels(clusterDefName, clusterName, componentName string) map

func BuildRSM(reqCtx intctrlutil.RequestCtx, cluster *appsv1alpha1.Cluster,
component *component.SynthesizedComponent, envConfigName string) (*workloads.ReplicatedStateMachine, error) {
vctToPVC := func(vct corev1.PersistentVolumeClaimTemplate) corev1.PersistentVolumeClaim {
return corev1.PersistentVolumeClaim{
ObjectMeta: vct.ObjectMeta,
Spec: vct.Spec,
}
}

commonLabels := buildWellKnownLabels(component.ClusterDefName, cluster.Name, component.Name)
addCommonLabels := func(service *corev1.Service) {
if service == nil {
Expand Down

0 comments on commit e0ef873

Please sign in to comment.