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

Add volumeClaimTemplates to raft storage api #5

Open
wants to merge 8 commits into
base: master
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
41 changes: 24 additions & 17 deletions deploy/examples/raft-storage.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
apiVersion: storage.cloud.atomix.io/v1beta1
kind: RaftStorageClass
metadata:
name: raft
labels:
app: raft
name: raft-database
labels:
app: raft-database
spec:
image: atomix/raft-replica:latest
imagePullPolicy: IfNotPresent
image: atomix/raft-storage-node:v0.1.0
replicas: 1
partitionsPerCluster: 3
volumeClaimTemplates:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep this here as an example?

data:
metadata:
name: "test"
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "standard"
resources:
requests:
storage: "1Gi"
---
apiVersion: cloud.atomix.io/v1beta2
apiVersion: cloud.atomix.io/v1beta3
kind: Database
metadata:
name: raft
name: raft-database
spec:
clusters: 3
template:
spec:
partitions: 10
storage:
group: storage.cloud.atomix.io
version: v1beta1
kind: RaftStorageClass
name: raft
namespace: kube-system
partitions: 1
storageClass:
group: storage.cloud.atomix.io
version: v1beta1
kind: RaftStorageClass
name: raft-database
namespace: kube-system
54 changes: 41 additions & 13 deletions deploy/raft-storage-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,52 @@ spec:
type: string
replicas:
type: integer
min: 1
max: 9
minimum: 1
maximum: 9
default: 1
partitionsPerCluster:
type: integer
min: 1
max: 1024
minimum: 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be updated in helm charts as well.

maximum: 1024
default: 1
volumeClaimTemplates:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just convert this to YAML just so it's clear and so we're sure we can use it in the Helm chart as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will figure that out. It looks like it is working in helm chart as well :)

type: object
required:
- data
properties:
data:
type: object
required:
- spec
properties:
metadata:
type: object
properties:
name:
type: string
default: ''
spec:
type: object
properties:
accessModes:
type: array
items:
type: string
default: ''
storageClassName:
type: string
default: ''
resources:
type: object
properties:
requests:
type: object
properties:
storage:
type: string
default: ''

---
apiVersion: cloud.atomix.io/v1beta2
kind: StorageController
metadata:
name: raft
spec:
group: storage.cloud.atomix.io
version: v1beta1
kind: RaftStorageClass
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/storage/v1beta1/storageclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type RaftStorageClassSpec struct {

// ImagePullPolicy is the pull policy to apply
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`

// VolumeClaimTemplates are volume claim templates to define persistent volumes
VolumeClaimTemplates VolumeClaimTemplates `json:"volumeClaimTemplates,omitempty"`
}

// +genclient
Expand Down
24 changes: 24 additions & 0 deletions pkg/apis/storage/v1beta1/volumeclaim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2020-present Open Networking Foundation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1beta1

import (
corev1 "k8s.io/api/core/v1"
)

// VolumeClaimTemplates k8s persistent volume claims
type VolumeClaimTemplates struct {
Data *corev1.PersistentVolumeClaim `json:"data,omitempty"`
}
96 changes: 67 additions & 29 deletions pkg/controller/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,34 @@ func (r *Reconciler) reconcileStatefulSet(database *v1beta3.Database, storage *v
return err
}

func newDataVolume(database *v1beta3.Database, storage *v1beta1.RaftStorageClass, cluster int) corev1.Volume {
dataVolume := corev1.Volume{
Name: dataVolume,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
}

return dataVolume

}

func newConfigVolume(database *v1beta3.Database, storage *v1beta1.RaftStorageClass, cluster int) corev1.Volume {
configVolume := corev1.Volume{

Name: configVolume,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: getClusterName(database, cluster),
},
},
},
}
return configVolume

}

func (r *Reconciler) addStatefulSet(database *v1beta3.Database, storage *v1beta1.RaftStorageClass, cluster int) error {
log.Info("Creating raft replicas", "Name", database.Name, "Namespace", database.Namespace)

Expand All @@ -274,6 +302,41 @@ func (r *Reconciler) addStatefulSet(database *v1beta3.Database, storage *v1beta1
pullPolicy = corev1.PullIfNotPresent
}

volumes := []corev1.Volume{
newConfigVolume(database, storage, cluster),
}

var volumeClaimTemplates []corev1.PersistentVolumeClaim
var volumeMounts []corev1.VolumeMount

configVolumeMount := corev1.VolumeMount{
Name: configVolume,
MountPath: configPath,
}

volumeMounts = append(volumeMounts, configVolumeMount)
if storage.Spec.VolumeClaimTemplates.Data == nil {
dataVolumeMount := corev1.VolumeMount{
Name: dataVolume,
MountPath: dataPath,
}
volumes = append(volumes, newDataVolume(database, storage, cluster))
volumeMounts = append(volumeMounts, dataVolumeMount)
} else {
dataVolumeMount := corev1.VolumeMount{
Name: storage.Spec.VolumeClaimTemplates.Data.Name,
MountPath: dataPath,
}
volumeMounts = append(volumeMounts, dataVolumeMount)
persistentVolumeClaim := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: storage.Spec.VolumeClaimTemplates.Data.Name,
},
Spec: storage.Spec.VolumeClaimTemplates.Data.Spec,
}
volumeClaimTemplates = append(volumeClaimTemplates, persistentVolumeClaim)
}

set := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: getClusterName(database, cluster),
Expand All @@ -289,7 +352,8 @@ func (r *Reconciler) addStatefulSet(database *v1beta3.Database, storage *v1beta1
UpdateStrategy: appsv1.StatefulSetUpdateStrategy{
Type: appsv1.RollingUpdateStatefulSetStrategyType,
},
PodManagementPolicy: appsv1.ParallelPodManagement,
PodManagementPolicy: appsv1.ParallelPodManagement,
VolumeClaimTemplates: volumeClaimTemplates,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: newClusterLabels(database, cluster),
Expand Down Expand Up @@ -344,36 +408,10 @@ func (r *Reconciler) addStatefulSet(database *v1beta3.Database, storage *v1beta1
InitialDelaySeconds: 60,
TimeoutSeconds: 10,
},
VolumeMounts: []corev1.VolumeMount{
{
Name: dataVolume,
MountPath: dataPath,
},
{
Name: configVolume,
MountPath: configPath,
},
},
},
},
Volumes: []corev1.Volume{
{
Name: configVolume,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: getClusterName(database, cluster),
},
},
},
},
{
Name: dataVolume,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
VolumeMounts: volumeMounts,
},
},
Volumes: volumes,
},
},
},
Expand Down