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/add pvc for xline data dir #48

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions api/v1alpha1/xlinecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ type XlineClusterSpec struct {
// The replicas of xline nodes
// +kubebuilder:validation:Minimum=3
Replicas int32 `json:"replicas"`

// 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 {
Name *string `json:"name"`
MountPath *string `json:"mountPath"`
PubKey *string `json:"pubKey"`
PriKey *string `json:"priKey"`
}

func (s *XlineClusterSpec) BootArgs() []string {
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.

63 changes: 63 additions & 0 deletions config/crd/bases/xline.io.datenlord.com_xlineclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ spec:
spec:
description: XlineClusterSpec defines the desired state of XlineCluster
properties:
authSecret:
description: The auth secret keys
properties:
mountPath:
type: string
name:
type: string
priKey:
type: string
pubKey:
type: string
required:
- mountPath
- name
- priKey
- pubKey
type: object
bootstrapArgs:
description: / Xline container bootstrap arguments / Set additional
arguments except [`--name`, `--members`, `--storage-engine`, `--data-dir`]
Expand Down Expand Up @@ -138,17 +155,63 @@ spec:
pattern: \d+(us|ms|s|m|h|d)
type: string
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
required:
- replicas
type: object
Expand Down
74 changes: 60 additions & 14 deletions internal/transformer/xlinecluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"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 @@ -46,6 +47,38 @@
return strings.Join(members, ",")
}

func GetAuthSecretVolume(auth_sec *xapi.XlineAuthSecret) []corev1.Volume {
if auth_sec == nil {
return []corev1.Volume{}
}
return []corev1.Volume{
{Name: "auth-cred", VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: *auth_sec.Name,
},
}},
}

Check warning on line 60 in internal/transformer/xlinecluster_resource.go

View check run for this annotation

Codecov / codecov/patch

internal/transformer/xlinecluster_resource.go#L50-L60

Added lines #L50 - L60 were not covered by tests
}

func GetAuthSecretVolumeMount(auth_sec *xapi.XlineAuthSecret) []corev1.VolumeMount {
if auth_sec == nil {
return []corev1.VolumeMount{}
}
return []corev1.VolumeMount{
{Name: "auth-cred", ReadOnly: true, MountPath: *auth_sec.MountPath},
}

Check warning on line 69 in internal/transformer/xlinecluster_resource.go

View check run for this annotation

Codecov / codecov/patch

internal/transformer/xlinecluster_resource.go#L63-L69

Added lines #L63 - L69 were not covered by tests
}

func GetAuthSecretEnvVars(auth_sec *xapi.XlineAuthSecret) []corev1.EnvVar {
if auth_sec == nil {
return []corev1.EnvVar{}
}
return []corev1.EnvVar{
{Name: "AUTH_PUBLIC_KEY", Value: fmt.Sprintf("%s/%s", *auth_sec.MountPath, *auth_sec.PubKey)},
{Name: "AUTH_PRIVATE_KEY", Value: fmt.Sprintf("%s/%s", *auth_sec.MountPath, *auth_sec.PriKey)},
}

Check warning on line 79 in internal/transformer/xlinecluster_resource.go

View check run for this annotation

Codecov / codecov/patch

internal/transformer/xlinecluster_resource.go#L72-L79

Added lines #L72 - L79 were not covered by tests
}

func MakeService(cr *xapi.XlineCluster, scheme *runtime.Scheme) *corev1.Service {
svcRef := GetServiceKey(cr.ObjKey())
svcLabel := GetXlineInstanceLabels(cr.ObjKey())
Expand Down Expand Up @@ -82,9 +115,26 @@
"--storage-engine", "rocksdb",
"--data-dir", DataDir,
}

initCmd = append(initCmd, cr.Spec.BootArgs()...)

envs := []corev1.EnvVar{
{Name: "MEMBERS", Value: GetMemberTopology(stsRef, svcName, int(cr.Spec.Replicas))},
{Name: "POD_NAME", ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
}},
}
envs = append(envs, GetAuthSecretEnvVars(cr.Spec.AuthSecrets)...)

volumes := GetAuthSecretVolume(cr.Spec.AuthSecrets)
volumeMounts := GetAuthSecretVolumeMount(cr.Spec.AuthSecrets)
volumeMounts = append(volumeMounts, corev1.VolumeMount{Name: "xline-storage", MountPath: "/usr/local/xline/data-dir"})

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

Check warning on line 137 in internal/transformer/xlinecluster_resource.go

View check run for this annotation

Codecov / codecov/patch

internal/transformer/xlinecluster_resource.go#L120-L137

Added lines #L120 - L137 were not covered by tests
// pod template: main container
mainContainer := corev1.Container{
Name: "xline",
Expand All @@ -93,15 +143,9 @@
Ports: []corev1.ContainerPort{
{Name: "xline-port", ContainerPort: XlinePort},
},
Command: initCmd,
Env: []corev1.EnvVar{
{Name: "MEMBERS", Value: GetMemberTopology(stsRef, svcName, int(cr.Spec.Replicas))},
{Name: "POD_NAME", ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
}},
},
Command: initCmd,
Env: envs,
VolumeMounts: volumeMounts,

Check warning on line 148 in internal/transformer/xlinecluster_resource.go

View check run for this annotation

Codecov / codecov/patch

internal/transformer/xlinecluster_resource.go#L146-L148

Added lines #L146 - L148 were not covered by tests
}

// pod template
Expand All @@ -110,6 +154,7 @@
Labels: stsLabels,
},
Spec: corev1.PodSpec{
Volumes: volumes,

Check warning on line 157 in internal/transformer/xlinecluster_resource.go

View check run for this annotation

Codecov / codecov/patch

internal/transformer/xlinecluster_resource.go#L157

Added line #L157 was not covered by tests
Containers: []corev1.Container{mainContainer},
},
}
Expand All @@ -124,10 +169,11 @@
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,

Check warning on line 176 in internal/transformer/xlinecluster_resource.go

View check run for this annotation

Codecov / codecov/patch

internal/transformer/xlinecluster_resource.go#L172-L176

Added lines #L172 - L176 were not covered by tests
},
}

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 @@
"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 @@
}
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

Check warning on line 54 in internal/util/kubeutil.go

View check run for this annotation

Codecov / codecov/patch

internal/util/kubeutil.go#L41-L54

Added lines #L41 - L54 were not covered by tests
}
Loading
Loading