From 8935c1a4865ff98f5b6261a2d4d3f960b3983c9d Mon Sep 17 00:00:00 2001 From: Antoine Legrand <2t.antoine@gmail.com> Date: Thu, 8 Sep 2016 01:49:48 +0200 Subject: [PATCH] Add jsonnet patch function --- kpm/jsonnet/lib/kpm.libjsonnet | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/kpm/jsonnet/lib/kpm.libjsonnet b/kpm/jsonnet/lib/kpm.libjsonnet index b4c0e75..0715bde 100644 --- a/kpm/jsonnet/lib/kpm.libjsonnet +++ b/kpm/jsonnet/lib/kpm.libjsonnet @@ -5,8 +5,8 @@ kpmstd { utils:: kpmstd, variables(package, params):: ( - local p = package + {variables: package.variables + std.mergePatch(super.variables, params.variables)}; - p.variables + local p = package { variables: package.variables + std.mergePatch(super.variables, params.variables) }; + p.variables ), deploy(dep):: ( @@ -66,6 +66,7 @@ kpmstd { } ), + resource_defaults(resource, package):: ( local default_expander = if std.objectHas(package, 'expander') then package.expander @@ -90,6 +91,7 @@ kpmstd { else error "Unknown expander: %s.\n Supported expanders are: [fmt, jinja2, jsonnet, none]" % [resource.expander]; std.mergePatch(r, kpm.extra_resource_values(resource)) + ), sharded_resource(resource, shards, variables):: ( @@ -111,7 +113,7 @@ kpmstd { ), resources(resources, shards, variables, package):: ( - std.flattenArrays([ + std.flattenArrays([ local r = kpm.resource_defaults(resource, package) + resource; local result = if std.objectHas(resource, "sharded") then kpm.sharded_resource(r, shards, variables) @@ -120,14 +122,24 @@ kpmstd { result for resource in resources if resource != null]) ), + patch(resources):: ( + { resources: [ + if std.objectHas(resource, "patch") == true then + resource { value: std.mergePatch(resource.value, resource.patch) } + else + resource + for resource in resources.resources + ], + } + ), ### package(pack, env):: ( - local default_params = { - namespace: "default", - variables: { namespace: default_params.namespace }, - shards: null, - }; + local default_params = { + namespace: "default", + variables: { namespace: default_params.namespace }, + shards: null, + }; local params = std.mergePatch(default_params, env); @@ -147,10 +159,10 @@ kpmstd { { resources: kpm.resources(p.resources, shards.shards, variables.variables, p.package) } else { resources: [] }; - + local patched_resources = kpm.patch(resources); local package = { package: p.package, - deploy: kpmstd.compact(p.deploy) } + variables + shards + resources; + deploy: kpmstd.compact(p.deploy) } + variables + shards + patched_resources; package ),