Skip to content

Commit

Permalink
Merge pull request #106 from projectsyn/proxy_vars
Browse files Browse the repository at this point in the history
Add helper to get proxy environment variables
  • Loading branch information
corvus-ch authored May 28, 2020
2 parents 24d96ca + f4abb39 commit 2e24517
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
* Allow overwriting of component git repo URLs ([#100])
* Introduce trace log level with `-vvv` flag ([#100])
* Helpers for managing HTTP proxy environment variables ([#106])

## [v0.1.5]

Expand Down Expand Up @@ -75,3 +76,4 @@ Initial implementation
[#95]: https://github.com/projectsyn/commodore/pull/95
[#99]: https://github.com/projectsyn/commodore/pull/99
[#100]: https://github.com/projectsyn/commodore/pull/100
[#106]: https://github.com/projectsyn/commodore/pull/106
66 changes: 61 additions & 5 deletions lib/commodore.libjsonnet
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local kap = import 'lib/kapitan.libjsonnet';
local params = kap.inventory().parameters;

local namespaced(ns, obj) =
obj {
metadata+: { namespace: ns }
metadata+: { namespace: ns },
};

local filterNull(list) = std.filter(function(obj) obj != null, list);
Expand Down Expand Up @@ -41,9 +44,10 @@ local patch_namespace(obj_file, namespace, kinds=null, exclude_objects=[]) =
else
function(o) true;
// helper to patch the objects
local addns(obj) = obj + { metadata+: { namespace: namespace } };
local addns(obj) = obj { metadata+: { namespace: namespace } };
// add namespace to objects for which objfilter returns true
[ if kindfilter(obj) && include(obj) then addns(obj) else obj
[
if kindfilter(obj) && include(obj) then addns(obj) else obj
for obj in objs
];

Expand All @@ -55,13 +59,63 @@ local addNamespaceToHelmOutput(template_dir, namespace, exclude_objects=[]) =
local input_file(elem) = template_dir + '/' + elem;
local stem(elem) =
local elems = std.split(elem, '.');
std.join('.', elems[:std.length(elems)-1]);
std.join('.', elems[:std.length(elems) - 1]);
{
[stem(elem)]: patch_namespace(input_file(elem), namespace,
[stem(elem)]: patch_namespace(input_file(elem),
namespace,
exclude_objects=exclude_objects)
for elem in chart_files
};

/**
* \brief Helper to inject proxy variables into a containers environment.
*
* HTTP proxy configuration is supposed to be done at `parameters.global` and
* meant to be used by all components. This helper makes it easy to add those
* values to a containers environment. No need to do any checks whether or not
* they have to be added.
*
* This helper is suitable to be used with `env_:` from the Kubernetes Jsonnet
* library. If the list form is used, combine it with `com.envList`.
*
* \return Dictionary. When configured, contains the http proxy environment
* variables in both upper and lower case form. Will be empty otherwise.
*/
local proxyVars = if std.objectHas(params, 'global') then {
[if std.objectHas(params.global, 'http_proxy') then 'HTTP_PROXY']: params.global.http_proxy,
[if std.objectHas(params.global, 'http_proxy') then 'http_proxy']: params.global.http_proxy,
[if std.objectHas(params.global, 'https_proxy') then 'HTTPS_PROXY']: params.global.https_proxy,
[if std.objectHas(params.global, 'https_proxy') then 'https_proxy']: params.global.https_proxy,
[if std.objectHas(params.global, 'no_proxy') then 'NO_PROXY']: params.global.no_proxy,
[if std.objectHas(params.global, 'no_proxy') then 'no_proxy']: params.global.no_proxy,
} else {};

/**
* \brief Helper function to convert a dictionary into an environment list.
*
* Kubernetes containers require environment variables to be a list of objects.
* In its simplest form, the object contains the keys `name` and `value`.
*
* This helper converts a dictionary into such a list where keys become `name`,
* and values become `value`.
*
* \arg map Dictionary to be converted.
*
* \return List of dictionaries with `name`, `value` keys.
*/
local envList(map) = [
if std.type(map[x]) == 'object'
then {
name: x,
valueFrom: map[x],
} else {
// Let `null` value stay as such (vs string-ified)
name: x,
value: if map[x] == null then null else std.toString(map[x]),
}
for x in std.objectFields(map)
];

{
inventory: std.native('inventory'),
yaml_load: std.native('yaml_load'),
Expand All @@ -70,4 +124,6 @@ local addNamespaceToHelmOutput(template_dir, namespace, exclude_objects=[]) =
filterNull: filterNull,
patchNamespace: patch_namespace,
addNamespaceToHelmOutput: addNamespaceToHelmOutput,
proxyVars: proxyVars,
envList: envList,
}

0 comments on commit 2e24517

Please sign in to comment.