【问题标题】:OpenShift Create Configmap With a JSON valueOpenShift 使用 JSON 值创建 Configmap
【发布时间】:2018-04-19 01:00:27
【问题描述】:

我想创建一个使用 JSON 对象作为其值的配置映射

JSON 对象看起来像这样,(variable name = manifestJSON)

{
    "appRepoVersionsMap": {
        "repoA": "1.0.0.131",
        "repoB": "1.0.0.7393"
    },
    "deployerVersion": "49",
    "openshiftConfigCommitId": "o76to87y"
}

然后我想创建一个配置映射,它接受这个 JSON 对象并将其添加为配置映射的值。

我试图让它工作的命令是

def osCmd = "create configmap manifest-config" +
        " --from-literal=manifest.os_config_branch=${envVars.OS_CONFIG_BRANCH}" +
        " --from-literal=manifest.os_server=${envVars.OPENSHIFT_SERVER_URL}" 
        " --from-literal=manifest.os_manifest=${manifestJSON}"
os.call(osCmd)

OpenShift 客户端给出以下错误:

10:23:37 error: cannot add key manifest.os_manifest, another key by that name already exists: map[manifest.os_config_branch:deployment-orchestrator manifest.os_server:<snipped>, manifest.os_manifest:appRepoVersionsMap:repoA:1.0.0.131 ].

所以 groovy 或 OpenShift 看到 JSON 对象中的 JSON 对象并且无法处理它。

我试图避免使用--from-file,因为我必须先写入磁盘然后运行命令,而且我担心这会在 Jenkins 环境中导致对多个项目进行多次部署的问题。

【问题讨论】:

    标签: groovy openshift


    【解决方案1】:

    解决方案最终相当简单,我在尝试各种解决方案时过度考虑了角色逃逸。

    为了在创建 configmap(或秘密)时使用 JSON 作为值,请在 JSON 对象本身周围添加 ''

    def osCmd = "create configmap manifest-config" +
            " --from-literal=manifest.os_config_branch=${envVars.OS_CONFIG_BRANCH}" +
            " --from-literal=manifest.os_server=${envVars.OPENSHIFT_SERVER_URL}" 
            " --from-literal=manifest.os_manifest='${manifestJSON}'" // <----- single quotes
    os.call(osCmd)
    

    这允许创建 configmap,并且我从 OpenShift 端确认 configmap 存在于我期望的清单值中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2018-11-04
      • 2019-10-26
      • 2019-08-11
      • 2019-11-23
      相关资源
      最近更新 更多