kapicorp / kapitan

Generic templated configuration management for Kubernetes, Terraform and other things
https://kapitan.dev
Apache License 2.0
1.8k stars 197 forks source link

Kapitan crash on empty file #493

Open ademariag opened 4 years ago

ademariag commented 4 years ago

Describe the bug/feature Kapitan crashes while handling a jsonnet file that would produce an empty file Possibly related to https://github.com/deepmind/kapitan/issues/443 although ti fails on something else.

To Reproduce Steps to reproduce the behavior:

To replicate, just create a jsonnet file with:

{}

A more complete example is

local object = {};

{
  [key]: object[key]
  for key in std.objectFields(object)
}

The condition happens happens often if the object one is looping over is missing. i.e.

local kap = import 'lib/kap.libsonnet';
local utils = kap.utils;
local p = kap.parameters;

local ingresses = utils.objectGet(p, 'ingresses', {});

{
  [ingress_name + '-ingress']:
  local ingress = ingresses[ingress_name];
  kap.K8sIngress(ingress_name)
    .WithAnnotations(utils.objectGet(ingress, 'annotations', {}))
    .WithNamespace(p.namespace)
    .WithDefaultBackend(utils.objectGet(ingress, 'backend', {}))
    .WithPaths(utils.objectGet(ingress, 'paths', {}))
  for ingress_name in std.objectFields(ingresses)
}

Expected behavior Compilation is successful

Screenshots

Compiled examples/gke-pvm-killer (0.83s)
Compiled examples/postgres-proxy (0.84s)
Compiled examples/echo-server (0.86s)
Compiled examples/mysql (0.87s)
Unknown (Non-Kapitan) Error occurred
multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 121, in worker
    result = (True, func(*args, **kwds))
  File "/usr/local/lib/python3.7/site-packages/kapitan/targets.py", line 408, in compile_target
    input_compiler.compile_obj(comp_obj, ext_vars, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/kapitan/inputs/base.py", line 52, in compile_obj
    self.compile_input_path(input_path, comp_obj, ext_vars, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/kapitan/inputs/base.py", line 74, in compile_input_path
    **kwargs,
  File "/usr/local/lib/python3.7/site-packages/kapitan/inputs/jsonnet.py", line 98, in compile_file
    fp.write(item_value)
  File "/usr/local/lib/python3.7/site-packages/kapitan/inputs/base.py", line 109, in write
    self.fp.write(self.revealer.compile_raw(data, target_name=target_name))
  File "/usr/local/lib/python3.7/site-packages/kapitan/refs/base.py", line 308, in compile_raw
    compiled = self.regex.sub(self._compile_replace_match_with_args(**kwargs), data)
TypeError: expected string or bytes-like object
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/kapitan/targets.py", line 92, in compile_targets
    [p.get() for p in pool.imap_unordered(worker, target_objs) if p]
  File "/usr/local/lib/python3.7/site-packages/kapitan/targets.py", line 92, in <listcomp>
    [p.get() for p in pool.imap_unordered(worker, target_objs) if p]
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 748, in next
    raise value
TypeError: expected string or bytes-like object

expected string or bytes-like object

If it's a bug (please complete the following information):

Additional context Add any other context about the problem here.

github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open for 1 year with no activity. Remove the stale label or comment if this issue is still relevant for you. If not, please close it yourself.