Skip to content

Commit

Permalink
[nats helm 1.x] fix JS mount (#717)
Browse files Browse the repository at this point in the history
* [nats helm 1.x] fix JS mount

Signed-off-by: Caleb Lloyd <[email protected]>

* default max_file_store to pvc size

Signed-off-by: Caleb Lloyd <[email protected]>

---------

Signed-off-by: Caleb Lloyd <[email protected]>
  • Loading branch information
caleblloyd authored May 9, 2023
1 parent d597864 commit 56c3f8c
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 16 deletions.
2 changes: 1 addition & 1 deletion helm/charts/nats/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ keywords:
- nats
- messaging
- cncf
version: 1.0.0-beta.2
version: 1.0.0-beta.3
home: http://github.com/nats-io/k8s
maintainers:
- email: [email protected]
Expand Down
4 changes: 4 additions & 0 deletions helm/charts/nats/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ helm repo add nats https://nats-io.github.io/k8s/helm/charts/
helm upgrade --install nats nats/nats --devel
```

## 1.x Beta Upgrade Notes
- If using `1.0.0-beta.2` or lower with JetStream enabled, upgrade to `1.0.0-beta.3` first

## Values

There are a handful of explicitly defined options which are documented with comments in the [values.yaml](values.yaml) file.
Expand Down Expand Up @@ -113,6 +116,7 @@ podTemplate:
topologySpreadConstraints:
kubernetes.io/hostname:
maxSkew: 1
whenUnsatisfiable: DoNotSchedule
```
### NATS Container Resources
Expand Down
6 changes: 4 additions & 2 deletions helm/charts/nats/files/config/jetstream.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ max_memory_store: 0
{{- with .fileStore }}
{{- if .enabled }}
store_dir: {{ .dir }}
{{- with .maxSize }}
max_file_store: << {{ . }} >>
{{- if .maxSize }}
max_file_store: << {{ .maxSize }} >>
{{- else if .pvc.enabled }}
max_file_store: << {{ .pvc.size }} >>
{{- end }}
{{- else }}
max_file_store: 0
Expand Down
16 changes: 16 additions & 0 deletions helm/charts/nats/files/stateful-set/beta2-mount-fix-container.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: beta2-mount-fix
{{ include "nats.image" (merge (pick $.Values "global") .Values.natsBox.container.image) }}

command:
- sh
- -ec
- |
cd {{ .Values.config.jetstream.fileStore.dir | quote }}
mkdir -p jetstream
find . -maxdepth 1 -mindepth 1 -not -name 'lost+found' -not -name 'jetstream' -exec mv {} jetstream \;
volumeMounts:
{{- with .Values.config.jetstream.fileStore }}
- name: {{ .pvc.name }}
mountPath: {{ .dir | quote }}
{{- end }}
2 changes: 1 addition & 1 deletion helm/charts/nats/files/stateful-set/nats-container.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ volumeMounts:
{{- if and .enabled .fileStore.enabled .fileStore.pvc.enabled }}
{{- with .fileStore }}
- name: {{ .pvc.name }}
mountPath: {{ printf "%s/jetstream" .dir | quote }}
mountPath: {{ .dir | quote }}
{{- end }}
{{- end }}
{{- end }}
Expand Down
9 changes: 9 additions & 0 deletions helm/charts/nats/files/stateful-set/pod-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ metadata:
checksum/config: {{ sha256sum $configMap }}
{{- end }}
spec:
{{- with .Values.config.jetstream }}
{{- if and .enabled .fileStore.enabled .fileStore.pvc.enabled }}
# just for 1.0.0-beta.3
# add an init container in order to fix mount from <= 1.0.0-beta.2
initContainers:
- {{ include "nats.loadMergePatch" (merge (dict "file" "stateful-set/beta2-mount-fix-container.yaml" "ctx" $) dict) | nindent 4 }}
{{- end }}
{{- end }}

containers:
# nats
{{- $nats := dict }}
Expand Down
4 changes: 2 additions & 2 deletions helm/charts/nats/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ output: JSON encoded map with 1 key:
*/}}
{{- define "nats.loadMergePatch" -}}
{{- $doc := tpl (.ctx.Files.Get (printf "files/%s" .file)) .ctx | fromYaml | default dict -}}
{{- $doc = mergeOverwrite $doc (deepCopy .merge) -}}
{{- get (include "jsonpatch" (dict "doc" $doc "patch" .patch) | fromJson ) "doc" | toYaml -}}
{{- $doc = mergeOverwrite $doc (deepCopy (.merge | default dict)) -}}
{{- get (include "jsonpatch" (dict "doc" $doc "patch" (.patch | default list)) | fromJson ) "doc" | toYaml -}}
{{- end }}


Expand Down
103 changes: 98 additions & 5 deletions helm/charts/nats/test/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ config:
},
}
expected.Conf.Value["jetstream"] = map[string]any{
"max_file_store": int64(10737418240),
"max_memory_store": int64(0),
"store_dir": "/data",
}
Expand All @@ -65,7 +66,7 @@ config:

vm := expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].VolumeMounts
expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].VolumeMounts = append(vm, corev1.VolumeMount{
MountPath: "/data/jetstream",
MountPath: "/data",
Name: test.FullName + "-js",
})

Expand All @@ -88,6 +89,29 @@ config:
},
}

nbc := expected.NatsBoxDeployment.Value.Spec.Template.Spec.Containers[0]
expected.StatefulSet.Value.Spec.Template.Spec.InitContainers = []corev1.Container{
{
Command: []string{
"sh",
"-ec",
`cd "/data"
mkdir -p jetstream
find . -maxdepth 1 -mindepth 1 -not -name 'lost+found' -not -name 'jetstream' -exec mv {} jetstream \;
`,
},
Image: nbc.Image,
ImagePullPolicy: nbc.ImagePullPolicy,
Name: "beta2-mount-fix",
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/data",
Name: test.FullName + "-js",
},
},
},
}

expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].Ports = []corev1.ContainerPort{
{
Name: "nats",
Expand Down Expand Up @@ -210,9 +234,32 @@ config:
},
}

nbc := expected.NatsBoxDeployment.Value.Spec.Template.Spec.Containers[0]
expected.StatefulSet.Value.Spec.Template.Spec.InitContainers = []corev1.Container{
{
Command: []string{
"sh",
"-ec",
`cd "/mnt"
mkdir -p jetstream
find . -maxdepth 1 -mindepth 1 -not -name 'lost+found' -not -name 'jetstream' -exec mv {} jetstream \;
`,
},
Image: nbc.Image,
ImagePullPolicy: nbc.ImagePullPolicy,
Name: "beta2-mount-fix",
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/mnt",
Name: test.FullName + "-js",
},
},
},
}

vm := expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].VolumeMounts
expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].VolumeMounts = append(vm, corev1.VolumeMount{
MountPath: "/mnt/jetstream",
MountPath: "/mnt",
Name: test.FullName + "-js",
}, corev1.VolumeMount{
MountPath: "/mnt/resolver",
Expand Down Expand Up @@ -370,7 +417,7 @@ config:

vm := expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].VolumeMounts
expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].VolumeMounts = append(vm, corev1.VolumeMount{
MountPath: "/data/jetstream",
MountPath: "/data",
Name: test.FullName + "-js",
}, corev1.VolumeMount{
MountPath: "/data/resolver",
Expand Down Expand Up @@ -417,6 +464,29 @@ config:
},
}

nbc := expected.NatsBoxDeployment.Value.Spec.Template.Spec.Containers[0]
expected.StatefulSet.Value.Spec.Template.Spec.InitContainers = []corev1.Container{
{
Command: []string{
"sh",
"-ec",
`cd "/data"
mkdir -p jetstream
find . -maxdepth 1 -mindepth 1 -not -name 'lost+found' -not -name 'jetstream' -exec mv {} jetstream \;
`,
},
Image: nbc.Image,
ImagePullPolicy: nbc.ImagePullPolicy,
Name: "beta2-mount-fix",
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/data",
Name: test.FullName + "-js",
},
},
},
}

expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].Ports = []corev1.ContainerPort{
{
Name: "nats",
Expand Down Expand Up @@ -737,7 +807,7 @@ config:
jetstream:
enabled: true
merge:
000$include: "js.conf"
zzz$include: "js.conf"
merge:
$include: "my-config.conf"
zzz$include: "my-config-last.conf"
Expand Down Expand Up @@ -780,7 +850,7 @@ max_outstanding_catchup: 64MB

vm := expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].VolumeMounts
expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].VolumeMounts = append(vm, corev1.VolumeMount{
MountPath: "/data/jetstream",
MountPath: "/data",
Name: test.FullName + "-js",
})

Expand All @@ -803,6 +873,29 @@ max_outstanding_catchup: 64MB
},
}

nbc := expected.NatsBoxDeployment.Value.Spec.Template.Spec.Containers[0]
expected.StatefulSet.Value.Spec.Template.Spec.InitContainers = []corev1.Container{
{
Command: []string{
"sh",
"-ec",
`cd "/data"
mkdir -p jetstream
find . -maxdepth 1 -mindepth 1 -not -name 'lost+found' -not -name 'jetstream' -exec mv {} jetstream \;
`,
},
Image: nbc.Image,
ImagePullPolicy: nbc.ImagePullPolicy,
Name: "beta2-mount-fix",
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/data",
Name: test.FullName + "-js",
},
},
},
}

RenderAndCheck(t, test, expected)
}

Expand Down
30 changes: 27 additions & 3 deletions helm/charts/nats/test/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ reloader:
key: token
natsVolumeMountPrefixes:
- /etc/
- /data/
- /data
promExporter:
enabled: true
port: 7778
Expand Down Expand Up @@ -101,6 +101,7 @@ natsBox:
expected := DefaultResources(t, test)

expected.Conf.Value["jetstream"] = map[string]any{
"max_file_store": int64(10737418240),
"max_memory_store": int64(0),
"store_dir": "/data",
}
Expand Down Expand Up @@ -141,7 +142,7 @@ natsBox:
ctr[0].ImagePullPolicy = "IfNotPresent"
ctr[0].VolumeMounts = append(ctr[0].VolumeMounts, corev1.VolumeMount{
Name: test.FullName + "-js",
MountPath: "/data/jetstream",
MountPath: "/data",
})

// reloader
Expand All @@ -150,7 +151,7 @@ natsBox:
ctr[1].ImagePullPolicy = "Always"
ctr[1].VolumeMounts = append(ctr[1].VolumeMounts, corev1.VolumeMount{
Name: test.FullName + "-js",
MountPath: "/data/jetstream",
MountPath: "/data",
})

// promExporter
Expand Down Expand Up @@ -308,6 +309,29 @@ natsBox:
},
}

nbc := expected.NatsBoxDeployment.Value.Spec.Template.Spec.Containers[0]
expected.StatefulSet.Value.Spec.Template.Spec.InitContainers = []corev1.Container{
{
Command: []string{
"sh",
"-ec",
`cd "/data"
mkdir -p jetstream
find . -maxdepth 1 -mindepth 1 -not -name 'lost+found' -not -name 'jetstream' -exec mv {} jetstream \;
`,
},
Image: nbc.Image,
ImagePullPolicy: nbc.ImagePullPolicy,
Name: "beta2-mount-fix",
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/data",
Name: test.FullName + "-js",
},
},
},
}

expected.StatefulSet.Value.Spec.Template.Spec.Containers[0].Ports = []corev1.ContainerPort{
{
Name: "nats",
Expand Down
3 changes: 1 addition & 2 deletions helm/charts/nats/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ config:

fileStore:
enabled: true
# disk will be mounted to <dir>/jetstream
dir: /data

############################################################
Expand All @@ -68,7 +67,7 @@ config:
# defaults to "{{ include "nats.fullname" $ }}-js"
name:

# defaults to use all of the available pvc size
# defaults to the PVC size
maxSize:

memoryStore:
Expand Down

0 comments on commit 56c3f8c

Please sign in to comment.