From 66ba6fa876eca54539ae55ab0ea0a6d1077f30df Mon Sep 17 00:00:00 2001 From: Shammah Chancellor Date: Mon, 1 Apr 2019 17:37:28 -0700 Subject: [PATCH] Allow Pod Template to provide initContainers, Volumes, and Containers Previously, `NewNatsPodSpec` would initialize it the spec from a deep copy of the PodTemplate, if it was provided by the user. However, it then overwrites initContainers, Volumes, and Containers with item lists that are build at runtime. This commit changes the behavior to append to the existing lists, enabling InitContainers, Volumes, and Containers to be specified in the pod template. Additionally, this adds a `VolumeMounts` item to the PodPolicy so that volumes provided in the template, can be mounted into the nats container. --- pkg/apis/nats/v1alpha2/cluster.go | 3 +++ pkg/util/kubernetes/kubernetes.go | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/apis/nats/v1alpha2/cluster.go b/pkg/apis/nats/v1alpha2/cluster.go index a0273f4d..b00819a7 100644 --- a/pkg/apis/nats/v1alpha2/cluster.go +++ b/pkg/apis/nats/v1alpha2/cluster.go @@ -245,6 +245,9 @@ type PodPolicy struct { // BootConfigContainerImageTag is the tag of the bootconfig container image. BootConfigContainerImageTag string `json:"bootconfigImageTag,omitempty"` + + // VolumeMounts is a list of k8s volume mount definitions for the nats container + VolumeMounts []v1.VolumeMount `json:"volumeMounts,omitempty"` } // AuthConfig is the authorization configuration for diff --git a/pkg/util/kubernetes/kubernetes.go b/pkg/util/kubernetes/kubernetes.go index f7fe1e09..404271cc 100644 --- a/pkg/util/kubernetes/kubernetes.go +++ b/pkg/util/kubernetes/kubernetes.go @@ -684,6 +684,9 @@ func NewNatsPodSpec(namespace, name, clusterName string, cs v1alpha2.ClusterSpec volumeMount = newNatsPidFileVolumeMount() volumeMounts = append(volumeMounts, volumeMount) + // User supplied volumes and mounts + volumeMounts = append(volumeMounts, cs.Pod.VolumeMounts...) + var enableClientsHostPort bool if cs.Pod != nil { enableClientsHostPort = cs.Pod.EnableClientsHostPort @@ -808,7 +811,6 @@ func NewNatsPodSpec(namespace, name, clusterName string, cs v1alpha2.ClusterSpec // Required overrides. pod.Spec.Hostname = name pod.Spec.Subdomain = ManagementServiceName(clusterName) - pod.Spec.Volumes = volumes // Set default restart policy if pod.Spec.RestartPolicy == "" { @@ -816,9 +818,8 @@ func NewNatsPodSpec(namespace, name, clusterName string, cs v1alpha2.ClusterSpec } if advertiseExternalIP { - pod.Spec.InitContainers = []v1.Container{bootconfig} + pod.Spec.InitContainers = append(pod.Spec.InitContainers, bootconfig) } - pod.Spec.Volumes = volumes // Enable PID namespace sharing and attach sidecar that // reloads the server whenever the config file is updated. @@ -867,7 +868,8 @@ func NewNatsPodSpec(namespace, name, clusterName string, cs v1alpha2.ClusterSpec containers = append(containers, metricsContainer) } - pod.Spec.Containers = containers + pod.Spec.Containers = append(pod.Spec.Containers, containers...) + pod.Spec.Volumes = append(pod.Spec.Volumes, volumes...) applyPodPolicy(clusterName, pod, cs.Pod)