cluster/services/ways: support static targets

This commit is contained in:
Max Headroom 2024-11-12 10:02:53 +01:00
parent 7d94ffda85
commit 9179fa9cce
2 changed files with 27 additions and 10 deletions

View file

@ -1,11 +1,15 @@
{ cluster, config, lib, pkgs, ... }: { cluster, config, depot, lib, pkgs, ... }:
let let
externalWays = lib.filterAttrs (_: cfg: !cfg.internal) cluster.config.ways; externalWays = lib.filterAttrs (_: cfg: !cfg.internal) cluster.config.ways;
internalWays = lib.filterAttrs (_: cfg: cfg.internal) cluster.config.ways; internalWays = lib.filterAttrs (_: cfg: cfg.internal) cluster.config.ways;
consulServiceWays = lib.filterAttrs (_: cfg: cfg.useConsul) cluster.config.ways; byMode = lib.pipe cluster.config.ways [
(lib.attrsToList)
(lib.groupBy (way: way.value.mode))
(lib.mapAttrs (n: v: lib.listToAttrs v))
];
in in
{ {
@ -25,7 +29,13 @@ in
]; ];
locations = lib.mkMerge [ locations = lib.mkMerge [
{ {
"/" = if cfg.grpc then { "/" = if cfg.mode == "static" then {
root = cfg.static {
inherit depot;
inherit pkgs;
inherit (pkgs) system;
};
} else if cfg.grpc then {
extraConfig = '' extraConfig = ''
set $nix_proxy_grpc_target ${cfg.target}; set $nix_proxy_grpc_target ${cfg.target};
grpc_pass $nix_proxy_grpc_target; grpc_pass $nix_proxy_grpc_target;
@ -47,7 +57,7 @@ in
}; };
}) cluster.config.ways; }) cluster.config.ways;
appendHttpConfig = lib.mkIf (consulServiceWays != {}) '' appendHttpConfig = lib.mkIf (byMode.consul != {}) ''
include /run/consul-template/nginx-ways-*.conf; include /run/consul-template/nginx-ways-*.conf;
''; '';
}; };
@ -67,7 +77,7 @@ in
value.distributed.enable = true; value.distributed.enable = true;
}) externalWays; }) externalWays;
services.consul-template.instances.ways = lib.mkIf (consulServiceWays != {}) { services.consul-template.instances.ways = lib.mkIf (byMode.consul != {}) {
user = "nginx"; user = "nginx";
group = "nginx"; group = "nginx";
settings = { settings = {
@ -86,7 +96,7 @@ in
{{ else }} {{ else }}
# upstream ${cfg.nginxUpstreamName} (${cfg.consulService}): no servers available # upstream ${cfg.nginxUpstreamName} (${cfg.consulService}): no servers available
{{ end }} {{ end }}
'') consulServiceWays; '') byMode.consul;
in pkgs.writeText "ways-upstreams.ctmpl" (lib.concatStringsSep "\n" (lib.unique upstreams)); in pkgs.writeText "ways-upstreams.ctmpl" (lib.concatStringsSep "\n" (lib.unique upstreams));
destination = "/run/consul-template/nginx-ways-upstreams.conf"; destination = "/run/consul-template/nginx-ways-upstreams.conf";
exec.command = lib.singleton (pkgs.writeShellScript "ways-reload" '' exec.command = lib.singleton (pkgs.writeShellScript "ways-reload" ''

View file

@ -58,6 +58,10 @@ with lib;
type = types.str; type = types.str;
}; };
static = mkOption {
type = with types; functionTo (coercedTo package (package: "${package.webroot or package}") str);
};
healthCheckPath = mkOption { healthCheckPath = mkOption {
type = types.path; type = types.path;
default = "/.well-known/ways/internal-health-check"; default = "/.well-known/ways/internal-health-check";
@ -69,10 +73,10 @@ with lib;
default = "https://${name}.${config.domainSuffix}"; default = "https://${name}.${config.domainSuffix}";
}; };
useConsul = mkOption { mode = mkOption {
type = types.bool; type = types.enum [ "simple" "consul" "static" ];
internal = true; internal = true;
default = false; default = "simple";
}; };
nginxUpstreamName = mkOption { nginxUpstreamName = mkOption {
@ -105,12 +109,15 @@ with lib;
config = lib.mkMerge [ config = lib.mkMerge [
(lib.mkIf options.consulService.isDefined { (lib.mkIf options.consulService.isDefined {
useConsul = true; mode = "consul";
nginxUpstreamName = "ways_upstream_${builtins.hashString "md5" options.consulService.value}"; nginxUpstreamName = "ways_upstream_${builtins.hashString "md5" options.consulService.value}";
target = "${if config.grpc then "grpc" else "http"}://${options.nginxUpstreamName.value}"; target = "${if config.grpc then "grpc" else "http"}://${options.nginxUpstreamName.value}";
}) })
(lib.mkIf options.bucket.isDefined { (lib.mkIf options.bucket.isDefined {
consulService = "garage-web"; consulService = "garage-web";
}) })
(lib.mkIf options.static.isDefined {
mode = "static";
})
]; ];
} }