-
Notifications
You must be signed in to change notification settings - Fork 622
Attach additional volume for postgres, #4210
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -13,9 +13,11 @@ import ( | |||||||
corev1 "k8s.io/api/core/v1" | ||||||||
"k8s.io/apimachinery/pkg/api/resource" | ||||||||
"k8s.io/apimachinery/pkg/util/rand" | ||||||||
"k8s.io/apimachinery/pkg/util/sets" | ||||||||
|
||||||||
"github.com/crunchydata/postgres-operator/internal/initialize" | ||||||||
"github.com/crunchydata/postgres-operator/internal/naming" | ||||||||
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1" | ||||||||
) | ||||||||
|
||||||||
var tmpDirSizeLimit = resource.MustParse("16Mi") | ||||||||
|
@@ -285,3 +287,61 @@ func safeHash32(content func(w io.Writer) error) (string, error) { | |||||||
} | ||||||||
return rand.SafeEncodeString(fmt.Sprint(hash.Sum32())), nil | ||||||||
} | ||||||||
|
||||||||
// AdditionalVolumeMount returns the name and mount path of the additional volume. | ||||||||
func AdditionalVolumeMount(name string, readOnly bool) corev1.VolumeMount { | ||||||||
return corev1.VolumeMount{ | ||||||||
Name: fmt.Sprintf("volumes-%s", name), | ||||||||
MountPath: "/volumes/" + name, | ||||||||
ReadOnly: readOnly, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// addAdditionalVolumesToSpecifiedContainers adds additional volumes to the specified | ||||||||
// containers in the specified pod | ||||||||
// addAdditionalVolumesToSpecifiedContainers adds the volumes to the pod | ||||||||
// as `volumes-<additionalVolumeRequest.Name>` | ||||||||
// and adds the directory to the path `/volumes/<additionalVolumeRequest.Name>` | ||||||||
func addAdditionalVolumesToSpecifiedContainers(template *corev1.PodTemplateSpec, | ||||||||
additionalVolumes []v1beta1.AdditionalVolume) { | ||||||||
|
||||||||
for _, additionalVolumeRequest := range additionalVolumes { | ||||||||
|
||||||||
additionalVolumeMount := AdditionalVolumeMount( | ||||||||
additionalVolumeRequest.Name, | ||||||||
additionalVolumeRequest.ReadOnly, | ||||||||
) | ||||||||
|
||||||||
additionalVolume := corev1.Volume{ | ||||||||
Name: additionalVolumeMount.Name, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 Ah, hmm. Volume names must be unique in a pod. What happens If someone uses 🤔 🤔 What other (Pod) validation happens on this name in K8s? Maybe a limit on the length? postgres-operator/internal/postgres/reconcile.go Lines 26 to 28 in 6a30d72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the pod.spec.volumes, its says "Must be a DNS_LABEL and unique within the pod." (cite) That's what the code comment says, at least. +1 for a prefix that prevents other problems |
||||||||
VolumeSource: corev1.VolumeSource{ | ||||||||
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ | ||||||||
ClaimName: additionalVolumeRequest.ClaimName, | ||||||||
ReadOnly: additionalVolumeMount.ReadOnly, | ||||||||
}, | ||||||||
}, | ||||||||
} | ||||||||
|
||||||||
names := sets.New(additionalVolumeRequest.Containers...) | ||||||||
|
||||||||
for i := range template.Spec.Containers { | ||||||||
if names.Len() == 0 || names.Has(template.Spec.Containers[i].Name) { | ||||||||
template.Spec.Containers[i].VolumeMounts = append( | ||||||||
template.Spec.Containers[i].VolumeMounts, | ||||||||
additionalVolumeMount) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
for i := range template.Spec.InitContainers { | ||||||||
if names.Len() == 0 || names.Has(template.Spec.InitContainers[i].Name) { | ||||||||
template.Spec.InitContainers[i].VolumeMounts = append( | ||||||||
template.Spec.InitContainers[i].VolumeMounts, | ||||||||
additionalVolumeMount) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
template.Spec.Volumes = append( | ||||||||
template.Spec.Volumes, | ||||||||
additionalVolume) | ||||||||
} | ||||||||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻 I like that this takes a template.