kapicorp / kapitan

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

References of type `env` not working as expected #763

Open bdnf opened 3 years ago

bdnf commented 3 years ago

Describe the bug/feature

  1. Secrets of type env are not visible after jsonnet files are compiled.
  2. When default value for env ref type is not set but the $KAPITAN_VAR_some_variable is set, then kapitan compile fails with:
    Could not find ref backend for tag: ?{env:some_variable}
    Compile error: failed to compile target: microservice

To Reproduce Steps to reproduce the behavior:

  1. Assuming simple folder structure:

    ├── components
    |   └── test-component.jsonnet
    └── inventory
    └── classes
    |       └── base.yml
    └── targets
            └── microserviceyml
  2. Contents of components/test-component.jsonnet:

    
    local kap = import 'lib/kapitan.libjsonnet';
    local inventory = kap.inventory();

local TOPIC = inventory.parameters.arguments.topic; local database = std.asciiLower(inventory.parameters.arguments.mysql);

{ 'job': { apiVersion: 'batch/v1', kind: 'Job', metadata: { labels: { database: database, topic: std.asciiLower(TOPIC), deployment_name: inventory.parameters.deployment_name, }, name: database + '-' + std.asciiLower(TOPIC), }, spec: {} } }


3. Contents of *inventory/classes/base.yaml*:
```yaml
parameters:
  arguments:
    mysql: ?{env:database}
    topic: ?{env:topic}

  kapitan:
    compile:
      - output_path: ./${arguments:topic}/
        input_type: jsonnet
        output_type: json
        prune: True
        input_paths:
          - components/test-component.jsonnet
  1. Contents of inventory/targets/microservice.yaml:
    
    classes:
    - base

parameters: deployment_name: dev target_namespace: default

commit_tag: 1223456789

kapitan: vars: target: microservice


5. Set the reference of type **env*:
```bash
export KAPITAN_VAR_database="sql.server:port/test_db"
export KAPITAN_VAR_topic="starting_offsets.TIER1"
# set defaults
echo -n "some-db-name" | kapitan refs  --write env:database -t  microservice -f -
echo -n "some-topic-name" | kapitan refs  --write env:topic -t  microservice -f -
  1. Output is:
    kapitan compile -t microservice 
    Compiled microservice (0.04s)

And generated file is:

{
  "apiVersion": "batch/v1",
  "kind": "Job",
  "metadata": {
    "labels": {
      "database": "?{env:database:e1220f11}",
      "deployment_name": "dev",
      "topic": "?{env:topic:19fc3d6e}"
    },
    "name": "?{env:database:e1220f11}-?{env:topic:19fc3d6e}"
  }
}

Expected behavior Compiled successfully and compiled file to be compiled/microservice/job.json with contents:

{
  "apiVersion": "batch/v1",
  "kind": "Job",
  "metadata": {
    "labels": {
      "database": "sql.server:port/test_db",
      "deployment_name": "dev",
      "topic": "starting_offsets.TIER1"
    },
    "name": "sql.server:port/test_db-starting_offsets.TIER1"
  }
}

Possible Bug

Zeratyl06 commented 2 years ago

Try kapitan compile --reveal

github-actions[bot] commented 2 months 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.