Skip to content

Commit 8bbc3cd

Browse files
authored
Fixed support for adding additional volumeClaimTemplates (#510)
1 parent b28bda0 commit 8bbc3cd

18 files changed

+269
-4707
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,6 @@ copyright: ## Check copyright headers
654654
-X .tpl \
655655
-X .txt \
656656
-X .yaml \
657-
-X pkg/apis/coherence/legacy/zz_generated.deepcopy.go \
658657
-X pkg/data/assets/ \
659658
-X zz_generated.
660659

api/v1/coherence_types.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,6 +2753,63 @@ func (in Resources) Create(kind ResourceType, name string, mgr manager.Manager,
27532753
return nil
27542754
}
27552755

2756+
// ----- PersistentVolumeClaim struct ---------------------------------------
2757+
2758+
// PersistentVolumeClaim is a request for and claim to a persistent volume
2759+
// +k8s:openapi-gen=true
2760+
type PersistentVolumeClaim struct {
2761+
// Standard object's metadata.
2762+
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
2763+
Metadata PersistentVolumeClaimObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
2764+
// Spec defines the desired characteristics of a volume requested by a pod author.
2765+
// More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
2766+
Spec corev1.PersistentVolumeClaimSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
2767+
}
2768+
2769+
// ToPVC converts this PersistentVolumeClaim to a k8s PersistentVolumeClaim.
2770+
func (in *PersistentVolumeClaim) ToPVC() corev1.PersistentVolumeClaim {
2771+
return corev1.PersistentVolumeClaim{
2772+
ObjectMeta: in.Metadata.toObjectMeta(),
2773+
Spec: in.Spec,
2774+
}
2775+
}
2776+
2777+
// ----- PersistentVolumeClaimObjectMeta struct -----------------------------
2778+
2779+
// PersistentVolumeClaimObjectMeta is metadata for the PersistentVolumeClaim.
2780+
// +k8s:openapi-gen=true
2781+
type PersistentVolumeClaimObjectMeta struct {
2782+
// Name must be unique within a namespace. Is required when creating resources, although
2783+
// some resources may allow a client to request the generation of an appropriate name
2784+
// automatically. Name is primarily intended for creation idempotence and configuration
2785+
// definition.
2786+
// Cannot be updated.
2787+
// More info: http://kubernetes.io/docs/user-guide/identifiers#names
2788+
Name string `json:"name,omitempty" protobuf:"bytes,1,opt,name=name"`
2789+
2790+
// Map of string keys and values that can be used to organize and categorize
2791+
// (scope and select) objects. May match selectors of replication controllers
2792+
// and services.
2793+
// More info: http://kubernetes.io/docs/user-guide/labels
2794+
// +optional
2795+
Labels map[string]string `json:"labels,omitempty" protobuf:"bytes,11,rep,name=labels"`
2796+
2797+
// Annotations is an unstructured key value map stored with a resource that may be
2798+
// set by external tools to store and retrieve arbitrary metadata. They are not
2799+
// queryable and should be preserved when modifying objects.
2800+
// More info: http://kubernetes.io/docs/user-guide/annotations
2801+
// +optional
2802+
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"`
2803+
}
2804+
2805+
func (in *PersistentVolumeClaimObjectMeta) toObjectMeta() metav1.ObjectMeta {
2806+
return metav1.ObjectMeta{
2807+
Name: in.Name,
2808+
Annotations: in.Annotations,
2809+
Labels: in.Labels,
2810+
}
2811+
}
2812+
27562813
// ----- helper methods -----------------------------------------------------
27572814

27582815
// Int32PtrToStringWithDefault converts an int32 pointer to a string using the default if the pointer is nil.

api/v1/coherence_webhook_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ func TestValidateVolumeClaimUpdateWhenVolumeClaimsNilAndEmpty(t *testing.T) {
447447
current := &coh.Coherence{
448448
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
449449
Spec: coh.CoherenceResourceSpec{
450-
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{},
450+
VolumeClaimTemplates: []coh.PersistentVolumeClaim{},
451451
},
452452
}
453453

@@ -466,10 +466,10 @@ func TestValidateVolumeClaimUpdateWhenVolumeClaimsAdded(t *testing.T) {
466466
current := &coh.Coherence{
467467
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
468468
Spec: coh.CoherenceResourceSpec{
469-
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
469+
VolumeClaimTemplates: []coh.PersistentVolumeClaim{
470470
{
471-
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
472-
Spec: corev1.PersistentVolumeClaimSpec{},
471+
Metadata: coh.PersistentVolumeClaimObjectMeta{Name: "foo"},
472+
Spec: corev1.PersistentVolumeClaimSpec{},
473473
},
474474
},
475475
},
@@ -495,10 +495,10 @@ func TestValidateVolumeClaimUpdateWhenVolumeClaimsRemoved(t *testing.T) {
495495
prev := &coh.Coherence{
496496
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
497497
Spec: coh.CoherenceResourceSpec{
498-
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
498+
VolumeClaimTemplates: []coh.PersistentVolumeClaim{
499499
{
500-
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
501-
Spec: corev1.PersistentVolumeClaimSpec{},
500+
Metadata: coh.PersistentVolumeClaimObjectMeta{Name: "foo"},
501+
Spec: corev1.PersistentVolumeClaimSpec{},
502502
},
503503
},
504504
},

api/v1/coherenceresourcespec_types.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,14 @@ type CoherenceResourceSpec struct {
186186
// +optional
187187
Volumes []corev1.Volume `json:"volumes,omitempty"`
188188
// VolumeClaimTemplates defines extra PVC mappings that will be added to the Coherence Pod.
189-
// The content of this yaml should match the normal k8s volumeClaimTemplates section of a Pod definition
190-
// as described in https://kubernetes.io/docs/concepts/storage/persistent-volumes/
189+
// The content of this yaml should match the normal k8s volumeClaimTemplates section of a StatefulSet spec
190+
// as described in https://kubernetes.io/docs/concepts/storage/persistent-volumes/
191+
// Every claim in this list must have at least one matching (by name) volumeMount in one
192+
// container in the template. A claim in this list takes precedence over any volumes in the
193+
// template, with the same name.
191194
// +listType=atomic
192195
// +optional
193-
VolumeClaimTemplates []corev1.PersistentVolumeClaim `json:"volumeClaimTemplates,omitempty"`
196+
VolumeClaimTemplates []PersistentVolumeClaim `json:"volumeClaimTemplates,omitempty"`
194197
// VolumeMounts defines extra volume mounts to map to the additional volumes or PVCs declared above
195198
// in store.volumes and store.volumeClaimTemplates
196199
// +listType=map
@@ -734,7 +737,9 @@ func (in *CoherenceResourceSpec) CreateStatefulSet(deployment *Coherence) Resour
734737
// append any additional Volumes
735738
sts.Spec.Template.Spec.Volumes = append(sts.Spec.Template.Spec.Volumes, in.Volumes...)
736739
// append any additional PVCs
737-
sts.Spec.VolumeClaimTemplates = append(sts.Spec.VolumeClaimTemplates, in.VolumeClaimTemplates...)
740+
for _, v := range in.VolumeClaimTemplates {
741+
sts.Spec.VolumeClaimTemplates = append(sts.Spec.VolumeClaimTemplates, v.ToPVC())
742+
}
738743

739744
return Resource{
740745
Kind: ResourceTypeStatefulSet,

api/v1/create_statefulset_volumeclaimtemplates_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
@@ -9,14 +9,13 @@ package v1_test
99
import (
1010
coh "github.com/oracle/coherence-operator/api/v1"
1111
corev1 "k8s.io/api/core/v1"
12-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1312
"testing"
1413
)
1514

1615
func TestCreateStatefulSetWithEmptyVolumeClaimTemplates(t *testing.T) {
1716

1817
spec := coh.CoherenceResourceSpec{
19-
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{},
18+
VolumeClaimTemplates: []coh.PersistentVolumeClaim{},
2019
}
2120

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

3130
func TestCreateStatefulSetWithOneVolumeClaimTemplate(t *testing.T) {
3231

33-
volumeOne := corev1.PersistentVolumeClaim{
34-
ObjectMeta: metav1.ObjectMeta{
32+
volumeOne := coh.PersistentVolumeClaim{
33+
Metadata: coh.PersistentVolumeClaimObjectMeta{
3534
Name: "PVCOne",
3635
},
3736
Spec: corev1.PersistentVolumeClaimSpec{
@@ -41,23 +40,23 @@ func TestCreateStatefulSetWithOneVolumeClaimTemplate(t *testing.T) {
4140
}
4241

4342
spec := coh.CoherenceResourceSpec{
44-
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{volumeOne},
43+
VolumeClaimTemplates: []coh.PersistentVolumeClaim{volumeOne},
4544
}
4645

4746
// Create the test deployment
4847
deployment := createTestDeployment(spec)
4948
// Create expected StatefulSet
5049
stsExpected := createMinimalExpectedStatefulSet(deployment)
51-
stsExpected.Spec.VolumeClaimTemplates = append(stsExpected.Spec.VolumeClaimTemplates, volumeOne)
50+
stsExpected.Spec.VolumeClaimTemplates = append(stsExpected.Spec.VolumeClaimTemplates, volumeOne.ToPVC())
5251

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

5756
func TestCreateStatefulSetWithTwoVolumeClaimTemplates(t *testing.T) {
5857

59-
volumeOne := corev1.PersistentVolumeClaim{
60-
ObjectMeta: metav1.ObjectMeta{
58+
volumeOne := coh.PersistentVolumeClaim{
59+
Metadata: coh.PersistentVolumeClaimObjectMeta{
6160
Name: "PVCOne",
6261
},
6362
Spec: corev1.PersistentVolumeClaimSpec{
@@ -66,8 +65,8 @@ func TestCreateStatefulSetWithTwoVolumeClaimTemplates(t *testing.T) {
6665
},
6766
}
6867

69-
volumeTwo := corev1.PersistentVolumeClaim{
70-
ObjectMeta: metav1.ObjectMeta{
68+
volumeTwo := coh.PersistentVolumeClaim{
69+
Metadata: coh.PersistentVolumeClaimObjectMeta{
7170
Name: "PVCTwo",
7271
},
7372
Spec: corev1.PersistentVolumeClaimSpec{
@@ -77,14 +76,14 @@ func TestCreateStatefulSetWithTwoVolumeClaimTemplates(t *testing.T) {
7776
}
7877

7978
spec := coh.CoherenceResourceSpec{
80-
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{volumeOne, volumeTwo},
79+
VolumeClaimTemplates: []coh.PersistentVolumeClaim{volumeOne, volumeTwo},
8180
}
8281

8382
// Create the test deployment
8483
deployment := createTestDeployment(spec)
8584
// Create expected StatefulSet
8685
stsExpected := createMinimalExpectedStatefulSet(deployment)
87-
stsExpected.Spec.VolumeClaimTemplates = append(stsExpected.Spec.VolumeClaimTemplates, volumeOne, volumeTwo)
86+
stsExpected.Spec.VolumeClaimTemplates = append(stsExpected.Spec.VolumeClaimTemplates, volumeOne.ToPVC(), volumeTwo.ToPVC())
8887

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

api/v1/zz_generated.deepcopy.go

Lines changed: 47 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)