You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jsonnet中传入动态配置文件名作为参数?

在Jsonnet中动态传入外部配置文件的实现方法

实现步骤

  • 给config函数新增配置文件路径参数
    在函数参数列表里添加configFilePath参数,设置默认值为原来硬编码的config.json,既兼容原有调用逻辑,又支持传入自定义配置文件路径。

  • 读取并解析指定配置文件
    利用Jsonnet标准库的std.extract()读取目标文件内容,再通过std.parseJson()将内容解析为Jsonnet对象,最后用std.manifestJsonEx()生成格式化的JSON字符串,填入ConfigMap的data字段。

修改后的完整代码示例

local config(
   args,
   resources,
   nodeAffinity,
   replicas=1,
   ttl='',
   fullName=naming.NewFullName('allm', namespace),
   image=naming.WithRegistry('allm:' + env.global.User + '-latest'),
   routes=null,
   command=null,
   configFilePath='config.json'  // 新增配置文件路径参数,默认使用原硬编码文件
) = (
   local confData = std.parseJson(std.extract(configFilePath));  // 读取并解析指定配置文件
   local configMap = {
     apiVersion: 'v1',
     kind: 'ConfigMap',
     metadata: {
       name: fullName.name,
       namespace: fullName.namespace,
       labels: { app: fullName.name },
     },
     data: {
       'conf-multiturn.json': std.manifestJsonEx(confData, '  '),
     },
   };
   configMap  // 返回生成的ConfigMap资源,可根据原函数逻辑补充其他返回内容
);

调用方式

  • 使用默认配置文件:config(args, resources, nodeAffinity)
  • 指定config_v1.json:config(args, resources, nodeAffinity, configFilePath='config_v1.json')
  • 指定其他版本文件(如config_v2.json):config(args, resources, nodeAffinity, configFilePath='config_v2.json')

注意:如果传入的文件路径不存在,Jsonnet编译时会直接抛出错误,确保路径指向正确的配置文件即可。

内容的提问来源于stack exchange,提问作者vkaul11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 11:42:06