Skip to content

Commit

Permalink
Fixed support for adding additional volumeClaimTemplates (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegridman authored Sep 30, 2021
1 parent b28bda0 commit 8bbc3cd
Show file tree
Hide file tree
Showing 18 changed files with 269 additions and 4,707 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,6 @@ copyright: ## Check copyright headers
-X .tpl \
-X .txt \
-X .yaml \
-X pkg/apis/coherence/legacy/zz_generated.deepcopy.go \
-X pkg/data/assets/ \
-X zz_generated.

Expand Down
57 changes: 57 additions & 0 deletions api/v1/coherence_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2753,6 +2753,63 @@ func (in Resources) Create(kind ResourceType, name string, mgr manager.Manager,
return nil
}

// ----- PersistentVolumeClaim struct ---------------------------------------

// PersistentVolumeClaim is a request for and claim to a persistent volume
// +k8s:openapi-gen=true
type PersistentVolumeClaim struct {
// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
Metadata PersistentVolumeClaimObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
// Spec defines the desired characteristics of a volume requested by a pod author.
// More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
Spec corev1.PersistentVolumeClaimSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
}

// ToPVC converts this PersistentVolumeClaim to a k8s PersistentVolumeClaim.
func (in *PersistentVolumeClaim) ToPVC() corev1.PersistentVolumeClaim {
return corev1.PersistentVolumeClaim{
ObjectMeta: in.Metadata.toObjectMeta(),
Spec: in.Spec,
}
}

// ----- PersistentVolumeClaimObjectMeta struct -----------------------------

// PersistentVolumeClaimObjectMeta is metadata for the PersistentVolumeClaim.
// +k8s:openapi-gen=true
type PersistentVolumeClaimObjectMeta struct {
// Name must be unique within a namespace. Is required when creating resources, although
// some resources may allow a client to request the generation of an appropriate name
// automatically. Name is primarily intended for creation idempotence and configuration
// definition.
// Cannot be updated.
// More info: http://kubernetes.io/docs/user-guide/identifiers#names
Name string `json:"name,omitempty" protobuf:"bytes,1,opt,name=name"`

// Map of string keys and values that can be used to organize and categorize
// (scope and select) objects. May match selectors of replication controllers
// and services.
// More info: http://kubernetes.io/docs/user-guide/labels
// +optional
Labels map[string]string `json:"labels,omitempty" protobuf:"bytes,11,rep,name=labels"`

// Annotations is an unstructured key value map stored with a resource that may be
// set by external tools to store and retrieve arbitrary metadata. They are not
// queryable and should be preserved when modifying objects.
// More info: http://kubernetes.io/docs/user-guide/annotations
// +optional
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"`
}

func (in *PersistentVolumeClaimObjectMeta) toObjectMeta() metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: in.Name,
Annotations: in.Annotations,
Labels: in.Labels,
}
}

// ----- helper methods -----------------------------------------------------

// Int32PtrToStringWithDefault converts an int32 pointer to a string using the default if the pointer is nil.
Expand Down
14 changes: 7 additions & 7 deletions api/v1/coherence_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func TestValidateVolumeClaimUpdateWhenVolumeClaimsNilAndEmpty(t *testing.T) {
current := &coh.Coherence{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: coh.CoherenceResourceSpec{
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{},
VolumeClaimTemplates: []coh.PersistentVolumeClaim{},
},
}

Expand All @@ -466,10 +466,10 @@ func TestValidateVolumeClaimUpdateWhenVolumeClaimsAdded(t *testing.T) {
current := &coh.Coherence{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: coh.CoherenceResourceSpec{
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
VolumeClaimTemplates: []coh.PersistentVolumeClaim{
{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: corev1.PersistentVolumeClaimSpec{},
Metadata: coh.PersistentVolumeClaimObjectMeta{Name: "foo"},
Spec: corev1.PersistentVolumeClaimSpec{},
},
},
},
Expand All @@ -495,10 +495,10 @@ func TestValidateVolumeClaimUpdateWhenVolumeClaimsRemoved(t *testing.T) {
prev := &coh.Coherence{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: coh.CoherenceResourceSpec{
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
VolumeClaimTemplates: []coh.PersistentVolumeClaim{
{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: corev1.PersistentVolumeClaimSpec{},
Metadata: coh.PersistentVolumeClaimObjectMeta{Name: "foo"},
Spec: corev1.PersistentVolumeClaimSpec{},
},
},
},
Expand Down
13 changes: 9 additions & 4 deletions api/v1/coherenceresourcespec_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,14 @@ type CoherenceResourceSpec struct {
// +optional
Volumes []corev1.Volume `json:"volumes,omitempty"`
// VolumeClaimTemplates defines extra PVC mappings that will be added to the Coherence Pod.
// The content of this yaml should match the normal k8s volumeClaimTemplates section of a Pod definition
// as described in https://kubernetes.io/docs/concepts/storage/persistent-volumes/
// The content of this yaml should match the normal k8s volumeClaimTemplates section of a StatefulSet spec
// as described in https://kubernetes.io/docs/concepts/storage/persistent-volumes/
// Every claim in this list must have at least one matching (by name) volumeMount in one
// container in the template. A claim in this list takes precedence over any volumes in the
// template, with the same name.
// +listType=atomic
// +optional
VolumeClaimTemplates []corev1.PersistentVolumeClaim `json:"volumeClaimTemplates,omitempty"`
VolumeClaimTemplates []PersistentVolumeClaim `json:"volumeClaimTemplates,omitempty"`
// VolumeMounts defines extra volume mounts to map to the additional volumes or PVCs declared above
// in store.volumes and store.volumeClaimTemplates
// +listType=map
Expand Down Expand Up @@ -734,7 +737,9 @@ func (in *CoherenceResourceSpec) CreateStatefulSet(deployment *Coherence) Resour
// append any additional Volumes
sts.Spec.Template.Spec.Volumes = append(sts.Spec.Template.Spec.Volumes, in.Volumes...)
// append any additional PVCs
sts.Spec.VolumeClaimTemplates = append(sts.Spec.VolumeClaimTemplates, in.VolumeClaimTemplates...)
for _, v := range in.VolumeClaimTemplates {
sts.Spec.VolumeClaimTemplates = append(sts.Spec.VolumeClaimTemplates, v.ToPVC())
}

return Resource{
Kind: ResourceTypeStatefulSet,
Expand Down
25 changes: 12 additions & 13 deletions api/v1/create_statefulset_volumeclaimtemplates_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates.
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
Expand All @@ -9,14 +9,13 @@ package v1_test
import (
coh "github.com/oracle/coherence-operator/api/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

func TestCreateStatefulSetWithEmptyVolumeClaimTemplates(t *testing.T) {

spec := coh.CoherenceResourceSpec{
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{},
VolumeClaimTemplates: []coh.PersistentVolumeClaim{},
}

// Create the test deployment
Expand All @@ -30,8 +29,8 @@ func TestCreateStatefulSetWithEmptyVolumeClaimTemplates(t *testing.T) {

func TestCreateStatefulSetWithOneVolumeClaimTemplate(t *testing.T) {

volumeOne := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
volumeOne := coh.PersistentVolumeClaim{
Metadata: coh.PersistentVolumeClaimObjectMeta{
Name: "PVCOne",
},
Spec: corev1.PersistentVolumeClaimSpec{
Expand All @@ -41,23 +40,23 @@ func TestCreateStatefulSetWithOneVolumeClaimTemplate(t *testing.T) {
}

spec := coh.CoherenceResourceSpec{
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{volumeOne},
VolumeClaimTemplates: []coh.PersistentVolumeClaim{volumeOne},
}

// Create the test deployment
deployment := createTestDeployment(spec)
// Create expected StatefulSet
stsExpected := createMinimalExpectedStatefulSet(deployment)
stsExpected.Spec.VolumeClaimTemplates = append(stsExpected.Spec.VolumeClaimTemplates, volumeOne)
stsExpected.Spec.VolumeClaimTemplates = append(stsExpected.Spec.VolumeClaimTemplates, volumeOne.ToPVC())

// assert that the StatefulSet is as expected
assertStatefulSetCreation(t, deployment, stsExpected)
}

func TestCreateStatefulSetWithTwoVolumeClaimTemplates(t *testing.T) {

volumeOne := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
volumeOne := coh.PersistentVolumeClaim{
Metadata: coh.PersistentVolumeClaimObjectMeta{
Name: "PVCOne",
},
Spec: corev1.PersistentVolumeClaimSpec{
Expand All @@ -66,8 +65,8 @@ func TestCreateStatefulSetWithTwoVolumeClaimTemplates(t *testing.T) {
},
}

volumeTwo := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
volumeTwo := coh.PersistentVolumeClaim{
Metadata: coh.PersistentVolumeClaimObjectMeta{
Name: "PVCTwo",
},
Spec: corev1.PersistentVolumeClaimSpec{
Expand All @@ -77,14 +76,14 @@ func TestCreateStatefulSetWithTwoVolumeClaimTemplates(t *testing.T) {
}

spec := coh.CoherenceResourceSpec{
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{volumeOne, volumeTwo},
VolumeClaimTemplates: []coh.PersistentVolumeClaim{volumeOne, volumeTwo},
}

// Create the test deployment
deployment := createTestDeployment(spec)
// Create expected StatefulSet
stsExpected := createMinimalExpectedStatefulSet(deployment)
stsExpected.Spec.VolumeClaimTemplates = append(stsExpected.Spec.VolumeClaimTemplates, volumeOne, volumeTwo)
stsExpected.Spec.VolumeClaimTemplates = append(stsExpected.Spec.VolumeClaimTemplates, volumeOne.ToPVC(), volumeTwo.ToPVC())

// assert that the StatefulSet is as expected
assertStatefulSetCreation(t, deployment, stsExpected)
Expand Down
48 changes: 47 additions & 1 deletion api/v1/zz_generated.deepcopy.go

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

Loading

0 comments on commit 8bbc3cd

Please sign in to comment.