-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1028 from nkvoll/use-pvcs-by-default
Use persistent storage by default for ES data
- Loading branch information
Showing
9 changed files
with
326 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package defaults | ||
|
||
import v1 "k8s.io/api/core/v1" | ||
|
||
// AppendDefaultPVCs appends defaults PVCs to a set of existing ones. | ||
// | ||
// The default PVC is not appended if: | ||
// - a Volume with the same .Name is found in podSpec.Volumes, and that volume is not a PVC volume | ||
// - a PVC with the same .Metadata.Name is found in existing. | ||
func AppendDefaultPVCs( | ||
existing []v1.PersistentVolumeClaim, | ||
podSpec v1.PodSpec, | ||
defaults ...v1.PersistentVolumeClaim, | ||
) []v1.PersistentVolumeClaim { | ||
// create a set of volume names that are not PVC-volumes for efficient testing | ||
nonPVCvolumes := map[string]struct{}{} | ||
|
||
for _, volume := range podSpec.Volumes { | ||
if volume.PersistentVolumeClaim == nil { | ||
// this volume is not a PVC | ||
nonPVCvolumes[volume.Name] = struct{}{} | ||
} | ||
} | ||
|
||
defaults: | ||
for _, defaultPVC := range defaults { | ||
for _, existingPVC := range existing { | ||
if existingPVC.Name == defaultPVC.Name { | ||
// a PVC with that name already exists, skip. | ||
continue defaults | ||
} | ||
if _, isNonPVCVolume := nonPVCvolumes[defaultPVC.Name]; isNonPVCVolume { | ||
// the corresponding volume is not a PVC | ||
continue defaults | ||
} | ||
} | ||
|
||
existing = append(existing, defaultPVC) | ||
} | ||
|
||
return existing | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package defaults | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestAppendDefaultPVCs(t *testing.T) { | ||
foo := v1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
}, | ||
} | ||
bar := v1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "bar", | ||
}, | ||
} | ||
|
||
strRef := func(s string) *string { | ||
return &s | ||
} | ||
|
||
type args struct { | ||
existing []v1.PersistentVolumeClaim | ||
podSpec v1.PodSpec | ||
defaults []v1.PersistentVolumeClaim | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []v1.PersistentVolumeClaim | ||
}{ | ||
{ | ||
name: "append new pvcs", | ||
args: args{ | ||
existing: []v1.PersistentVolumeClaim{foo}, | ||
defaults: []v1.PersistentVolumeClaim{bar}, | ||
}, | ||
want: []v1.PersistentVolumeClaim{foo, bar}, | ||
}, | ||
{ | ||
name: "do not overwrite or duplicate existing", | ||
args: args{ | ||
existing: []v1.PersistentVolumeClaim{foo}, | ||
defaults: []v1.PersistentVolumeClaim{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
}, | ||
Spec: v1.PersistentVolumeClaimSpec{ | ||
StorageClassName: strRef("custom"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: []v1.PersistentVolumeClaim{foo}, | ||
}, | ||
{ | ||
name: "not add a default pvc if a non-pvc volume with the same name exists", | ||
args: args{ | ||
existing: []v1.PersistentVolumeClaim{foo}, | ||
podSpec: v1.PodSpec{ | ||
Volumes: []v1.Volume{ | ||
{ | ||
Name: bar.Name, | ||
VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}, | ||
}, | ||
}, | ||
}, | ||
defaults: []v1.PersistentVolumeClaim{bar}, | ||
}, | ||
want: []v1.PersistentVolumeClaim{foo}, | ||
}, | ||
{ | ||
name: "add a default pvc if a pvcvolume with the same name exists", | ||
args: args{ | ||
existing: []v1.PersistentVolumeClaim{foo}, | ||
podSpec: v1.PodSpec{ | ||
Volumes: []v1.Volume{ | ||
{ | ||
Name: bar.Name, | ||
VolumeSource: v1.VolumeSource{ | ||
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
defaults: []v1.PersistentVolumeClaim{bar}, | ||
}, | ||
want: []v1.PersistentVolumeClaim{foo, bar}, | ||
}, | ||
{ | ||
name: "add a default pvc if a non-pvc volume with a different name exists", | ||
args: args{ | ||
existing: []v1.PersistentVolumeClaim{foo}, | ||
podSpec: v1.PodSpec{ | ||
Volumes: []v1.Volume{ | ||
{ | ||
Name: "not" + bar.Name, | ||
VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}, | ||
}, | ||
}, | ||
}, | ||
defaults: []v1.PersistentVolumeClaim{bar}, | ||
}, | ||
want: []v1.PersistentVolumeClaim{foo, bar}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := AppendDefaultPVCs(tt.args.existing, tt.args.podSpec, tt.args.defaults...); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("AppendDefaultPVCs() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.