Skip to content

Commit

Permalink
feat: support pvc for xline
Browse files Browse the repository at this point in the history
Signed-off-by: Phoeniix Zhao <[email protected]>
  • Loading branch information
Phoenix500526 committed Dec 23, 2023
1 parent a8e3e9d commit d298ebd
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 7 deletions.
8 changes: 8 additions & 0 deletions api/v1alpha1/xlinecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ type XlineClusterSpec struct {

// The auth secret keys
AuthSecrets *XlineAuthSecret `json:"authSecret,omitempty"`

// K8s storage-class-name of the Xline storage
// Defaults to Kubernetes default storage class.
// +optional
StorageClassName *string `json:"storageClassName"`

// Defines the specification of resource cpu, mem, storage.
corev1.ResourceRequirements `json:",inline"`
}

type XlineAuthSecret struct {
Expand Down
46 changes: 46 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.

46 changes: 46 additions & 0 deletions config/crd/bases/xline.kvstore.datenlord.com_xlineclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,63 @@ spec:
- priKey
- pubKey
type: object
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
image:
description: Xline cluster image
type: string
imagePullPolicy:
description: ImagePullPolicy of Xline cluster Pods
type: string
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
replicas:
description: The replicas of xline nodes
format: int32
minimum: 3
type: integer
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. Requests cannot exceed Limits. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
storageClassName:
description: K8s storage-class-name of the Xline storage Defaults
to Kubernetes default storage class.
type: string
version:
description: Xline cluster image version
type: string
Expand Down
20 changes: 13 additions & 7 deletions internal/transformer/xlinecluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

xapi "github.com/xline-kv/xline-operator/api/v1alpha1"
"github.com/xline-kv/xline-operator/internal/util"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -108,14 +109,18 @@ func MakeStatefulSet(cr *xapi.XlineCluster, scheme *runtime.Scheme) *appv1.State
svcName := GetServiceKey(cr.ObjKey()).Name

volumes := GetAuthSecretVolume(cr.Spec.AuthSecrets)
volumeMount := GetAuthSecretVolumeMount(cr.Spec.AuthSecrets)

volumeMounts := GetAuthSecretVolumeMount(cr.Spec.AuthSecrets)
volumeMounts = append(volumeMounts, corev1.VolumeMount{Name: "xline-storage", MountPath: "/usr/local/xline/data-dir"})
envs := []corev1.EnvVar{
{Name: "MEMBERS", Value: GetMemberTopology(stsRef, svcName, int(cr.Spec.Replicas))},
}

envs = append(envs, GetAuthSecretEnvVars(cr.Spec.AuthSecrets)...)

pvcTemplates := []corev1.PersistentVolumeClaim{
util.NewReadWriteOncePVC("xline-storage", cr.Spec.StorageClassName, cr.Spec.Requests.Storage()),
}

// pod template: main container
mainContainer := corev1.Container{
Name: "xline",
Expand All @@ -125,7 +130,7 @@ func MakeStatefulSet(cr *xapi.XlineCluster, scheme *runtime.Scheme) *appv1.State
{Name: "xline-port", ContainerPort: 2379},
},
Env: envs,
VolumeMounts: volumeMount,
VolumeMounts: volumeMounts,
}

// pod template
Expand All @@ -149,10 +154,11 @@ func MakeStatefulSet(cr *xapi.XlineCluster, scheme *runtime.Scheme) *appv1.State
Labels: stsLabels,
},
Spec: appv1.StatefulSetSpec{
Replicas: &cr.Spec.Replicas,
ServiceName: svcName,
Selector: &metav1.LabelSelector{MatchLabels: stsLabels},
Template: podTemplate,
Replicas: &cr.Spec.Replicas,
ServiceName: svcName,
Selector: &metav1.LabelSelector{MatchLabels: stsLabels},
VolumeClaimTemplates: pvcTemplates,
Template: podTemplate,
},
}

Expand Down
19 changes: 19 additions & 0 deletions internal/util/kubeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"encoding/json"
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

Expand Down Expand Up @@ -34,3 +37,19 @@ func Md5HashOr(obj any, fallback string) string {
}
return hash
}

func NewReadWriteOncePVC(name string, storageClassName *string, storageRequest *resource.Quantity) corev1.PersistentVolumeClaim {
pvc := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
StorageClassName: storageClassName,
},
}
if storageRequest != nil {
pvc.Spec.Resources.Requests = corev1.ResourceList{corev1.ResourceStorage: *storageRequest}
}
return pvc
}

0 comments on commit d298ebd

Please sign in to comment.