2

I have the following 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"
                            },
                        ]
                    }
                ]
            }
        }
    }
}

How do I add deployment_env.json using jsonnet?

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

    ]
}

I need to add it under spec.template.containers[0].env = deployment_env.json

I wrote the below jsonnet to do that. It appends a new element. But i need to change the existing 0th container element in the json. Please suggest how to do it.

local grafana_envs = (import 'custom_grafana/deployment_env.json');
local grafanaDeployment = (import 'nested.json') + {
    spec+: {
        template+: {
            spec+: {
                containers+: [{
                    envs: grafana_envs.env,
                }]
            }
        }
    },
};
grafanaDeployment 
grosser
  • 14,707
  • 7
  • 57
  • 61
Alok Kumar Singh
  • 2,331
  • 3
  • 18
  • 37

3 Answers3

4

See below for an implementation that allows adding env to an existing container by its index in the containers[] array.

Do note that jsonnet is much better suited to work with objects (i.e. dictionaries / maps) rather than arrays, thus it needs contrived handling via std.mapWithIndex(), to be able to modify an entry from its matching index.

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 
jjo
  • 2,595
  • 1
  • 8
  • 16
3

Alternative implementation, not relying on the array index position, but image value instead (which makes more sense here as the env must be understood by the image implementation)

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
jjo
  • 2,595
  • 1
  • 8
  • 16
0

An alternative to std.mapWithIndex is to explicitly iterate through the indexes based on the size of the list.

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 

If one needed to modify a specific index other than 0, say 5, then they could do so by putting an if i == 5 in the loop.

Jaden Travnik
  • 1,107
  • 13
  • 27