使用JSONNET覆盖嵌套列表元素



我有以下JSON

{
    "namespace": "monitoring",
    "name": "alok",
    "spec": {
        "replicas": 1,
        "template": {
            "metadata": "aaa",
            "spec": {
                "containers": [
                    {
                        "image": "practodev/test:test",
                        "env": [
                            {
                                "name":"GF_SERVER_HTTP_PORT",
                                "value":"3000"
                            },
                            {
                                "name":"GF_SERVER_HTTPS_PORT",
                                "value":"443"
                            },
                        ]
                    }
                ]
            }
        }
    }
}

如何使用JSONNET添加deployment_env.json

{
    "env": [
        {
            "name":"GF_AUTH_DISABLE_LOGIN_FORM",
            "value":"false"
        },
        {
            "name":"GF_AUTH_BASIC_ENABLED",
            "value":"false"
        },
    ]
}

我需要在spec.template.containers [0] .env = deployment_env.json

下添加它

我写了以下JSONNET来做到这一点。它附加了一个新元素。但是我需要更改JSON中现有的0个容器元素。请建议如何做。

local grafana_envs = (import 'custom_grafana/deployment_env.json');
local grafanaDeployment = (import 'nested.json') + {
    spec+: {
        template+: {
            spec+: {
                containers+: [{
                    envs: grafana_envs.env,
                }]
            }
        }
    },
};
grafanaDeployment 

请参见下文,以便实现该实现,该实现允许通过其containers[]数组中的索引将env添加到现有容器中。

请注意,jsonnet更适合使用对象(即字典/地图)而不是数组,因此它需要通过std.mapWithIndex()进行人为处理,以便能够从其匹配索引中修改条目。

local grafana_envs = (import 'deployment_env.json');
// Add extra_env to a container by its idx passed containers array
local override_env(containers, idx, extra_env) = (
  local f(i, x) = (
    if i == idx then x {env+: extra_env} else x
  );
  std.mapWithIndex(f, containers)
);
local grafanaDeployment = (import 'nested.json') + {
    spec+: {
        template+: {
            spec+: {
                containers: override_env(super.containers, 0, grafana_envs.env)
            }
        }
    },
};
grafanaDeployment 

替代实现,而不是依赖数组索引位置,而是image值(在这里更有意义,因为必须通过图像实现来理解env

local grafana_envs = (import 'deployment_env.json');
local TARGET_CONTAINER_IMAGE = 'practodev/test:test';
local grafanaDeployment = (import 'nested.json') + {
  spec+: {
    template+: {
      spec+: {
        containers: [
          // TARGET_CONTAINER_IMAGE identifies which container to modify
          if x.image == TARGET_CONTAINER_IMAGE
          then x { env+: grafana_envs.env }
          else x
          for x in super.containers
        ],
      },
    },
  },
};
grafanaDeployment

std.mapWithIndex的替代方法是根据列表的大小明确迭代索引。

local grafana_envs = (import 'deployment_env.json');
local grafanaDeployment = (import 'nested.json') + {
    spec+: {
        template+: {
            spec+: {
                containers: 
                    [super.containers[0] { env+: grafana_envs.env }] 
                    + 
                    [
                      super.containers[i] 
                      for i in std.range(1, std.length(super.containers) - 1)
                    ]
            }
        }
    },
};
grafanaDeployment 

如果需要修改0以外的特定索引,例如5,则可以通过将if i == 5放在循环中。

最新更新