nixos/k3s: add configPath option

This is useful when the whole k3s config is generated on boot. The
server address or token might be dynamically injected using user-data.
This commit is contained in:
zimbatm 2021-07-19 13:29:39 +02:00
parent ca0a54d8e1
commit efbd199ffb
No known key found for this signature in database
GPG key ID: 71BAF6D40C1D63D7

View file

@ -67,6 +67,12 @@ in
default = false; default = false;
description = "Only run the server. This option only makes sense for a server."; description = "Only run the server. This option only makes sense for a server.";
}; };
configPath = mkOption {
type = types.nullOr types.path;
default = null;
description = "File path containing the k3s YAML config. This is useful when the config is generated (for example on boot).";
};
}; };
# implementation # implementation
@ -74,12 +80,12 @@ in
config = mkIf cfg.enable { config = mkIf cfg.enable {
assertions = [ assertions = [
{ {
assertion = cfg.role == "agent" -> cfg.serverAddr != ""; assertion = cfg.role == "agent" -> (cfg.configPath != null || cfg.serverAddr != "");
message = "serverAddr should be set if role is 'agent'"; message = "serverAddr or configPath (with 'server' key) should be set if role is 'agent'";
} }
{ {
assertion = cfg.role == "agent" -> cfg.token != "" || cfg.tokenFile != null; assertion = cfg.role == "agent" -> cfg.configPath != null || cfg.tokenFile != null || cfg.token != "";
message = "token or tokenFile should be set if role is 'agent'"; message = "token or tokenFile or configPath (with 'token' or 'token-file' keys) should be set if role is 'agent'";
} }
]; ];
@ -115,12 +121,10 @@ in
"${cfg.package}/bin/k3s ${cfg.role}" "${cfg.package}/bin/k3s ${cfg.role}"
] ++ (optional cfg.docker "--docker") ] ++ (optional cfg.docker "--docker")
++ (optional cfg.disableAgent "--disable-agent") ++ (optional cfg.disableAgent "--disable-agent")
++ (optional (cfg.role == "agent") "--server ${cfg.serverAddr} ${ ++ (optional (cfg.serverAddr != "") "--server ${cfg.serverAddr}")
if cfg.tokenFile != null then ++ (optional (cfg.token != "") "--token ${cfg.token}")
"--token-file ${cfg.tokenFile}" ++ (optional (cfg.tokenFile != null) "--token-file ${cfg.tokenFile}")
else ++ (optional (cfg.configPath != null) "--config ${cfg.configPath}")
"--token ${cfg.token}"
}")
++ [ cfg.extraFlags ] ++ [ cfg.extraFlags ]
); );
}; };