Skip to content

Commit

Permalink
Enabling ephemeral storage for zookeeper.
Browse files Browse the repository at this point in the history
  • Loading branch information
HoustonPutman committed May 2, 2019
1 parent 7d42ddf commit 68336c7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
20 changes: 12 additions & 8 deletions pkg/apis/zookeeper/v1beta1/zookeepercluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type ZookeeperClusterSpec struct {
// PersistentVolumeClaimSpec is the spec to describe PVC for the container
// This field is optional. If no PVC spec, stateful containers will use
// emptyDir as volume.
PersistentVolumeClaimSpec v1.PersistentVolumeClaimSpec `json:"persistence,omitempty"`
PersistentVolumeClaimSpec *v1.PersistentVolumeClaimSpec `json:"persistence,omitempty"`

// Conf is the zookeeper configuration, which will be used to generate the
// static zookeeper configuration. If no configuration is provided required
Expand Down Expand Up @@ -121,15 +121,19 @@ func (s *ZookeeperClusterSpec) withDefaults(z *ZookeeperCluster) (changed bool)
if s.Pod.withDefaults(z) {
changed = true
}
s.PersistentVolumeClaimSpec.AccessModes = []v1.PersistentVolumeAccessMode{
v1.ReadWriteOnce,
}
if len(s.PersistentVolumeClaimSpec.Resources.Requests) == 0 {
s.PersistentVolumeClaimSpec.Resources.Requests = v1.ResourceList{
v1.ResourceStorage: resource.MustParse("20Gi"),

if s.PersistentVolumeClaimSpec != nil {
s.PersistentVolumeClaimSpec.AccessModes = []v1.PersistentVolumeAccessMode{
v1.ReadWriteOnce,
}
if len(s.PersistentVolumeClaimSpec.Resources.Requests) == 0 {
s.PersistentVolumeClaimSpec.Resources.Requests = v1.ResourceList{
v1.ResourceStorage: resource.MustParse("20Gi"),
}
changed = true
}
changed = true
}

return changed
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/apis/zookeeper/v1beta1/zookeepercluster_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ var _ = Describe("ZookeeperCluster Types", func() {
ObjectMeta: metav1.ObjectMeta{
Name: "example",
},
Spec: v1beta1.ZookeeperClusterSpec{
PersistentVolumeClaimSpec: &corev1.PersistentVolumeClaimSpec{},
},
}
})

Expand Down Expand Up @@ -176,7 +179,7 @@ var _ = Describe("ZookeeperCluster Types", func() {
})

Context("PersistentVoluemClaim Spec", func() {
var p corev1.PersistentVolumeClaimSpec
var p *corev1.PersistentVolumeClaimSpec

BeforeEach(func() {
p = z.Spec.PersistentVolumeClaimSpec
Expand Down
6 changes: 5 additions & 1 deletion pkg/apis/zookeeper/v1beta1/zz_generated.deepcopy.go

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

63 changes: 39 additions & 24 deletions pkg/zk/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,30 @@ func headlessSvcName(z *v1beta1.ZookeeperCluster) string {
return fmt.Sprintf("%s-headless", z.GetName())
}

var zkDataVolume = "data"

// MakeStatefulSet return a zookeeper stateful set from the zk spec
func MakeStatefulSet(z *v1beta1.ZookeeperCluster) *appsv1.StatefulSet {
extraVolumes := []v1.Volume{}
pvcSpec := z.Spec.PersistentVolumeClaimSpec
pvcs := []v1.PersistentVolumeClaim{}
if pvcSpec != nil {
pvcs = append(pvcs, v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: zkDataVolume,
Labels: map[string]string{"app": z.GetName()},
},
Spec: *pvcSpec,
})
} else {
extraVolumes = append(extraVolumes, v1.Volume{
Name: zkDataVolume,
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
})
}

return &appsv1.StatefulSet{
TypeMeta: metav1.TypeMeta{
Kind: "StatefulSet",
Expand Down Expand Up @@ -64,22 +86,14 @@ func MakeStatefulSet(z *v1beta1.ZookeeperCluster) *appsv1.StatefulSet {
"kind": "ZookeeperMember",
},
},
Spec: makeZkPodSpec(z),
},
VolumeClaimTemplates: []v1.PersistentVolumeClaim{
{
ObjectMeta: metav1.ObjectMeta{
Name: "data",
Labels: map[string]string{"app": z.GetName()},
},
Spec: z.Spec.PersistentVolumeClaimSpec,
},
Spec: makeZkPodSpec(z, extraVolumes),
},
VolumeClaimTemplates: pvcs,
},
}
}

func makeZkPodSpec(z *v1beta1.ZookeeperCluster) v1.PodSpec {
func makeZkPodSpec(z *v1beta1.ZookeeperCluster, volumes []v1.Volume) v1.PodSpec {
zkContainer := v1.Container{
Name: "zookeeper",
Image: z.Spec.Image.ToString(),
Expand Down Expand Up @@ -115,22 +129,23 @@ func makeZkPodSpec(z *v1beta1.ZookeeperCluster) v1.PodSpec {
if z.Spec.Pod.Resources.Limits != nil || z.Spec.Pod.Resources.Requests != nil {
zkContainer.Resources = z.Spec.Pod.Resources
}
zkContainer.Env = z.Spec.Pod.Env
podSpec := v1.PodSpec{
Containers: []v1.Container{zkContainer},
Affinity: z.Spec.Pod.Affinity,
Volumes: []v1.Volume{
{
Name: "conf",
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{
Name: z.ConfigMapName(),
},
},

volumes = append(volumes, v1.Volume{
Name: "conf",
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{
Name: z.ConfigMapName(),
},
},
},
})

zkContainer.Env = z.Spec.Pod.Env
podSpec := v1.PodSpec{
Containers: []v1.Container{zkContainer},
Affinity: z.Spec.Pod.Affinity,
Volumes: volumes,
TerminationGracePeriodSeconds: &z.Spec.Pod.TerminationGracePeriodSeconds,
}
if reflect.DeepEqual(v1.PodSecurityContext{}, z.Spec.Pod.SecurityContext) {
Expand Down

0 comments on commit 68336c7

Please sign in to comment.