Nomad作业模板如何为节点缺失的元数据键设置默认字符串?
解决方案
方法一:在模板中直接处理回退逻辑
直接在Go模板里使用coalesce函数结合节点元数据获取语法,跳过作业级meta的中间层,逻辑更直接可靠。修改后的模板代码如下:
job "foldingathome" { ... template { destination = "${NOMAD_ALLOC_DIR}/data/config.xml" data = <<EOT <config> <power v='{{ coalesce (node "meta.foldingathome_power") "medium" }}'/> </config> EOT } }
这里node "meta.foldingathome_power"会直接读取节点的元数据,coalesce函数会优先使用节点存在的元数据值,不存在时自动回退到"medium"。
方法二:修正作业级meta的节点元数据引用
作业级meta块中,直接引用节点元数据需要使用node.meta.xxx的语法,原代码里的meta.foldingathome_power实际指向的是作业自身的meta,而非节点meta。修正后作业配置如下:
job "foldingathome" { ... meta { foldingathome_power = coalesce(node.meta.foldingathome_power, "medium") } template { destination = "${NOMAD_ALLOC_DIR}/data/config.xml" data = <<EOT <config> <power v='{{ env "meta.foldingathome_power" }}'/> </config> EOT } }
修正后作业meta会正确读取节点的foldingathome_power元数据,没有该键时自动使用"medium"作为默认值,模板中通过env调用作业meta的值就会符合预期。
内容的提问来源于stack exchange,提问作者hufman
相关产品推荐
相关产品推荐

