Skip to content

Container Environment Helper

Gregory Nickonov edited this page Mar 20, 2019 · 2 revisions

Quick Start

environment do
  add DB_NAME: 'backend', DB_USER: 'postgres'
  add_config_map_key :DB_NAME, :db, key: :DATABASE, optional: true
end

add

Kubernetes Documentation

Key-value pairs passed to add are converted into name and value properties of EnvVar objects and added to env array:

environment do
  add DB_NAME: 'backend', DB_USER: 'postgres'
end

If the environment variable with the same name is already defined it will be overwritten.

add_config_map_key

Kubernetes Documentation

Adds a specified key from the specified ConfigMap as an environment variable with the specified name to the env array:

environment do
  # Adds a value of the key 'DATABASE' from the ConfigMap 'db' as an environment
  # variable 'DB_NAME' without failing if it is doesn't exists
  add_config_map_key :DB_NAME, :db, key: :DATABASE, optional: true

  # Adds a value of the key 'DB_NAME' from the ConfigMap 'backend' as an environment
  # variable 'DB_NAME' failing if it is doesn't exists
  add_config_map_key :DB_NAME, :backend, key: :DB_NAME

  # The same as above, but the ConfigMap's key name is defaulted to the environment
  # variable name (which is 'DB_NAME')
  add_config_map_key :DB_NAME, :backend

  # The same as above, but the ConfigMap's key name is defaulted to the environment
  # variable name (which is 'DB_NAME') and the name of the ConfigMap itself is defaulted
  # to the name of the current scope (which is 'backend')
  add_config_map_key :DB_NAME
end

If the environment variable with the same name is already defined it will be overwritten.

add_secret_key

Kubernetes Documentation

Adds a specified key from the specified Secret as an environment variable with the specified name to the env array:

environment do
  # Adds a value of the key 'PASSWORD' from the Secret 'db' as an environment
  # variable 'DB_PASSWORD' without failing if it is doesn't exists
  add_secret_key :DB_PASSWORD, :db, key: :PASSWORD, optional: true

  # Adds a value of the key 'DB_PASSWORD' from the Secret 'backend' as an environment
  # variable 'DB_PASSWORD' failing if it is doesn't exists
  add_secret_key :DB_PASSWORD, :backend, key: :DB_PASSWORD

  # The same as above, but the ConfigMap's key name is defaulted to the environment
  # variable name (which is 'DB_PASSWORD')
  add_secret_key :DB_PASSWORD, :backend

  # The same as above, but the ConfigMap's key name is defaulted to the environment
  # variable name (which is 'DB_PASSWORD') and the name of the ConfigMap itself is defaulted
  # to the name of the current scope (which is 'backend')
  add_secret_key :DB_PASSWORD
end

If the environment variable with the same name is already defined it will be overwritten.

add_field

Kubernetes Documentation

Adds an environment variable with the reference to the property of the current object to the env array:

environment do
  add_field :KUBERNETES_POD_NAME, 'metadata.name'
end

There is an optional last argument, api_version which can be used to alter the version of the schema the field path is written in terms of.

If the environment variable with the same name is already defined it will be overwritten.

add_resource_field

Kubernetes Documentation

Adds an environment variable with the reference to the current (or specified) container's resource field to the env array:

environment do
  # Reference resource field from the same container
  add_resource_field :KUBERNETES_MEMORY_LIMITS, 'limits.memory'

  # Reference resource field from the main container with the divisor 1m
  add_resource_field :KUBERNETES_CPU_REQUESTS, 'requests.cpu',
                     container_name: :main, divisor: '1m'
end

If the environment variable with the same name is already defined it will be overwritten.

use_config_map

Kubernetes Documentation

Adds a reference to the specified ConfigMap to the env_from array:

environment do
  # Reference 'backend' ConfigMap
  use_config_map :backend

  # Reference ConfigMap with the same name as the current scope
  use_config_map

  # Reference 'diagnostics' ConfigMap, prefix all keys with 'DIAGNOSTICS_' and
  # do not fail, if that ConfigMap is doesn't exists
  use_config_map :diagnostics, optional: true, prefix: 'DIAGNOSTICS_'
end

If the ConfigMap with the same name has already been referenced then use_config_map does nothing.

use_secret

Kubernetes Documentation

Adds a reference to the specified Secret to the env_from array:

environment do
  # Reference 'backend' Secret
  use_secret :backend

  # Reference Secret with the same name as the current scope
  use_secret

  # Reference 's3' Secret, prefix all keys with 'S3_' and
  # do not fail, if that Secret is doesn't exists
  use_secret :s3, optional: true, prefix: 'S3_'
end

If the Secret with the same name has already been referenced then use_secret does nothing.

Clone this wiki locally