{% set daemon_args = "$DAEMON_ARGS" %}
{% if grains['os_family'] == 'RedHat' %}
	{% set daemon_args = "" %}
{% endif %}

{% set machines = ""%}
{% set cloud_provider = "" %}
{% set minion_regexp = "-minion_regexp=.*" %}
{% if grains.cloud_provider is defined %}
  {% set cloud_provider = "-cloud_provider=" + grains.cloud_provider %}
{% endif %}

{% set address = "-address=127.0.0.1" %}

{% if pillar['node_instance_prefix'] is defined %}
  {% set minion_regexp = "-minion_regexp='" + pillar['node_instance_prefix'] + ".*'" %}
{% endif %}

{% if grains.etcd_servers is defined %}
  {% set etcd_servers = "-etcd_servers=http://" + grains.etcd_servers + ":4001" %}
{% else %}
  {% set ips = salt['mine.get']('roles:kubernetes-master', 'network.ip_addrs', 'grain').values() %}
  {% set etcd_servers = "-etcd_servers=http://" + ips[0][0] + ":4001" %}
{% endif %}

{% if grains.cloud is defined %}
{% if grains.cloud == 'gce' %}
  {% set cloud_provider = "-cloud_provider=gce" %}
  {% set machines = "-machines=" + ','.join(salt['mine.get']('roles:kubernetes-pool', 'network.ip_addrs', expr_form='grain').keys()) %}
{% endif %}
{% if grains.cloud == 'azure' %}
  MACHINES="{{ salt['mine.get']('roles:kubernetes-pool', 'grains.items', expr_form='grain').values()|join(',', attribute='hostnamef') }}"
  {% set machines = "-machines=$MACHINES" %}
{% endif %}
{% if grains.cloud == 'vsphere' %}
  # Collect IPs of minions as machines list.
  #
  # Use a bash array to build the value we need. Jinja 2.7 does support a 'map'
  # filter that would simplify this.  However, some installations (specifically
  # Debian Wheezy) only install Jinja 2.6.
  MACHINE_IPS=()
  {% for addrs in salt['mine.get']('roles:kubernetes-pool', 'network.ip_addrs', expr_form='grain').values() %}
  MACHINE_IPS+=( {{ addrs[0] }} )
  {% endfor %}
  {% set machines = "-machines=$(echo ${MACHINE_IPS[@]} | xargs -n1 echo | paste -sd,)" %}
  {% set minion_regexp = "" %}
{% endif %}
{% endif %}
{% if pillar['portal_net'] is defined %}
  {% set portal_net = "-portal_net=" + pillar['portal_net'] %}
{% endif %}

DAEMON_ARGS="{{daemon_args}} {{address}} {{machines}} {{etcd_servers}} {{ minion_regexp }} {{ cloud_provider }} --allow_privileged={{pillar['allow_privileged']}} {{portal_net}}"
