0

I had a jsonnet file where there is a file config.json which is hard coded in the variable. What I want to do is to have the ability to pass config_v1.json or config.v*.json as an argument to the config. How do I do that in the jsonnet ?

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,
       ) = (
   local configMap = {
     apiVersion: 'v1',
     kind: 'ConfigMap',
     metadata: {
       name: fullName.name,
       namespace: fullName.namespace,
       labels: { app: fullName.name },
     },
     data: {
       'conf-multiturn.json': std.manifestJsonEx(confData, '  '),
     },
   };
vkaul11
  • 4,098
  • 12
  • 47
  • 79

1 Answers1

0

Given that you want the configData JSON to be verbatim manifested, you really don't need to manipulate it further.

In the below example, I use importstr to load configdata.json as-is, then build two data fields: one verbatim from loaded JSON, then another one with one of its fields changed via jsonnet just to show you how the possible transformations should be done (not needed in your original use case):

foo.jsonnet

local confData = importstr 'configdata.json';

local config(confData) = (
  local configMap = {
    apiVersion: 'v1',
    kind: 'ConfigMap',
    metadata: {
      name: 'myname',
      namespace: 'mynamespace',
      labels: { app: 'myname' },
    },
    // data fields need to be: { key: value }, both as strings
    data: {
      // verbatim JSON manifested as string
      'confData.json': confData,
      // original JSON parsed into jsonnet object, modified, then manifested as string
      'confData2.json': std.manifestJson(
        std.parseJson(confData) { foo: 'otherBar' }
      ),
    },
  };
  configMap
);

config(confData)

configdata.json

{
  "foo": "bar",
  "qqq": "qux"
}

output

$ jsonnet foo.jsonnet | jq -r '.data["confData.json"]' 
{
  "foo": "bar",
  "qqq": "qux"
}

$ jsonnet foo.jsonnet | jq -r '.data["confData2.json"]'
{
    "foo": "otherBar",
    "qqq": "qux"
}

$ jsonnet foo.jsonnet | kubeconform -verbose
stdin - ConfigMap myname is valid
jjo
  • 2,595
  • 1
  • 8
  • 16