Skip to content

Commit

Permalink
Allow tuning remote configuration per individual host in Helm chart (#…
Browse files Browse the repository at this point in the history
…1041, #954)

- add `conf-renderer` container and `template.sh` script generating YAML configuration per Ingress host
- support YAML format in `Config.init()` for remote configuration parsing
- use `try_files` for `/conf` Nginx route to return `$host` based configuration

Co-authored-by: tyranron <tyranron@gmail.com>
  • Loading branch information
SleepySquash and tyranron committed Jun 24, 2024
1 parent fb56d44 commit e23283b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 17 deletions.
9 changes: 8 additions & 1 deletion helm/messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ All user visible changes to this project will be documented in this file. This p
## [0.1.4] · 2024-??-?? (unreleased)
[0.1.4]: https://github.com/team113/messenger/tree/helm%2Fmessenger%2F0.1.4/helm/messenger

### Added

- Ability to tune configuration per `Ingress` host. ([#1041], [#954])

### Changed

- Set `Cross-Origin-Embedder-Policy` header to `credentialless` for Safari. ([#1004])
- Set `Cross-Origin-Embedder-Policy` header to `credentialless` for [Safari]. ([#1004])

[#954]: https://github.com/team113/messenger/issues/954
[#1004]: https://github.com/team113/messenger/pull/1004
[#1041]: https://github.com/team113/messenger/pull/1041



Expand Down Expand Up @@ -72,4 +78,5 @@ All user visible changes to this project will be documented in this file. This p


[Nginx]: https://nginx.org
[Safari]: https://www.apple.com/safari
[Semantic Versioning 2.0.0]: https://semver.org
7 changes: 4 additions & 3 deletions helm/messenger/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ server {
try_files $uri $uri/ /index.html;
}
location = /conf.toml {
root /var/www;
expires 1m;
location = /conf {
root /var/www;
expires 1m;
try_files /conf/$host.yaml /conf/$host.toml $uri.toml $uri.yaml =404;
}
location = /privacy {
Expand Down
14 changes: 14 additions & 0 deletions helm/messenger/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,17 @@ data:
nginx.conf: |
{{- tpl (.Files.Get "conf/nginx.conf") . | nindent 4 }}
template.sh: |
#!/bin/bash
set -ex
# Comma separated hosts to pass to `omit`.
keys="$(echo $INGRESS_HOSTS | sed 's| |", "|g;s|.*|"&"|')"
for e in $INGRESS_HOSTS
do
yq -oy '. * (."'$e'" // {}) | . |= omit(['"$keys"'])' $CONF_FILE \
> $OUT_DIR/$e.yaml
done
34 changes: 28 additions & 6 deletions helm/messenger/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,30 @@ spec:
imagePullSecrets:
- name: {{ printf "%s.registry" (include "messenger.fullname" .) | quote }}
{{- end }}
{{- if .Values.sftp.enabled }}
initContainers:
- name: conf-renderer
image: mikefarah/yq:4
command: ["sh"]
args: ["template.sh"]
env:
- name: CONF_FILE
value: /var/run/conf.toml
- name: INGRESS_HOSTS
value: {{ join " " $.Values.ingress.hosts | quote }}
- name: OUT_DIR
value: /tmp/conf
volumeMounts:
- name: conf
subPath: conf.toml
mountPath: /var/run/conf.toml
readOnly: true
- name: conf
subPath: template.sh
mountPath: /workdir/template.sh
readOnly: true
- name: conf-rendered
mountPath: /tmp/conf/
{{- if .Values.sftp.enabled }}
- name: copy-src
image: {{ printf "%s:%s" .Values.image.repository .Values.image.tag | quote }}
imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
Expand All @@ -82,7 +104,7 @@ spec:
volumeMounts:
- name: src
mountPath: /target/
{{- end }}
{{- end }}
containers:
- name: app
image: {{ printf "%s:%s" .Values.image.repository .Values.image.tag | quote }}
Expand All @@ -92,14 +114,12 @@ spec:
containerPort: 80
protocol: TCP
volumeMounts:
- name: conf
subPath: conf.toml
mountPath: /var/www/conf.toml
readOnly: true
- name: conf
subPath: nginx.conf
mountPath: /etc/nginx/conf.d/default.conf
readOnly: true
- name: conf-rendered
mountPath: /var/www/conf/
{{- if .Values.deployment.src.hostPath }}
- name: src
subPath: build/web/
Expand Down Expand Up @@ -138,6 +158,8 @@ spec:
- name: conf
configMap:
name: {{ printf "%s.conf" (include "messenger.fullname" .) | quote }}
- name: conf-rendered
emptyDir: {}
{{- if .Values.deployment.src.hostPath }}
- name: src
hostPath:
Expand Down
14 changes: 10 additions & 4 deletions lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:log_me/log_me.dart' as me;
import 'package:toml/toml.dart';
import 'package:yaml/yaml.dart';

import '/util/log.dart';
import '/util/platform_utils.dart';
Expand Down Expand Up @@ -280,10 +281,15 @@ class Config {
if (confRemote) {
try {
final response = await (await PlatformUtils.dio)
.fetch(RequestOptions(path: '$url:$port/conf.toml'));
.fetch(RequestOptions(path: '$url:$port/conf'));
if (response.statusCode == 200) {
final Map<String, dynamic> remote =
TomlDocument.parse(response.data.toString()).toMap();
dynamic remote;

try {
remote = TomlDocument.parse(response.data.toString()).toMap();
} catch (e) {
remote = loadYaml(response.data.toString());
}

confRemote = remote['conf']?['remote'] ?? confRemote;
if (confRemote) {
Expand All @@ -302,7 +308,7 @@ class Config {
vapidKey = remote['fcm']?['vapidKey'] ?? vapidKey;
link = remote['link']?['prefix'] ?? link;
appcast = remote['appcast']?['url'] ?? appcast;
copyright = remote['legal']?[Uri.base.host]['copyright'] ??
copyright = remote['legal']?[Uri.base.host]?['copyright'] ??
remote['legal']?['copyright'] ??
copyright;
support = remote['legal']?['support'] ?? support;
Expand Down
4 changes: 3 additions & 1 deletion lib/ui/page/home/page/chat/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,9 @@ class ChatController extends GetxController {
}

WidgetsBinding.instance.addPostFrameCallback((_) async {
if (offset != null && offset < loaderHeight) {
if (listController.hasClients &&
offset != null &&
offset < loaderHeight) {
listController.jumpTo(
listController.position.pixels - (loaderHeight + 28),
);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2318,7 +2318,7 @@ packages:
source: hosted
version: "6.5.0"
yaml:
dependency: "direct dev"
dependency: "direct main"
description:
name: yaml
sha256: "75769501ea3489fca56601ff33454fe45507ea3bfb014161abc3b43ae25989d5"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ dependencies:
window_manager: ^0.3.5
windows_taskbar: ^1.1.1
xml: ^6.5.0
yaml: ^3.1.2

dev_dependencies:
artemis: ^7.13.1
Expand All @@ -136,7 +137,6 @@ dev_dependencies:
json_serializable: ^6.6.2
mockito: ^5.4.2
sentry_dart_plugin: ^1.2.0
yaml: ^3.1.2

dependency_overrides:
# TODO: Remove when `flutter_gherkin` updates.
Expand Down

0 comments on commit e23283b

Please sign in to comment.