cluster/services/dns: support alternative rewrite types in declarative dns

This commit is contained in:
Max Headroom 2023-11-02 23:40:43 +01:00
parent 7ff75a72f5
commit 88754861db
2 changed files with 14 additions and 6 deletions

View file

@ -17,9 +17,11 @@ let
configList = lib.mapAttrsToList (n: v: "${n}=${v}") cfg;
in lib.concatStringsSep "\n" configList;
rewriteRecords = lib.filterAttrs (_: record: record.rewriteTarget != null) cluster.config.dns.records;
rewriteRecords = lib.filterAttrs (_: record: record.rewrite.target != null) cluster.config.dns.records;
rewrites = lib.mapAttrsToList (_: record: "rewrite stop name exact ${record.name}.${record.root}. ${record.rewriteTarget}.") rewriteRecords;
rewrites = lib.mapAttrsToList (_: record: let
maybeEscapeRegex = str: if record.write.type == "regex" then "^${lib.escapeRegex str}$" else str;
in "rewrite stop name ${record.rewrite.type} ${record.name}${maybeEscapeRegex ".${record.root}."} ${record.rewrite.target}. answer auto") rewriteRecords;
rewriteConf = pkgs.writeText "coredns-rewrites.conf" (lib.concatStringsSep "\n" rewrites);
in {

View file

@ -34,13 +34,19 @@ let
type = with types; nullOr str;
default = null;
};
rewriteTarget = mkOption {
type = with types; nullOr str;
default = null;
rewrite = {
target = mkOption {
type = with types; nullOr str;
default = null;
};
type = mkOption {
type = types.enum [ "exact" "substring" "prefix" "suffix" "regex" ];
default = "exact";
};
};
};
config = {
rewriteTarget = mkIf (config.consulService != null) "${config.consulService}.${config.consulServicesRoot}";
rewrite.target = mkIf (config.consulService != null) "${config.consulService}.${config.consulServicesRoot}";
};
});
in