Add template for endpoint render
Adjust some logic in svc and rc templates.
Change-Id: I292d9bc68c62afa4d082ae3a7231c500f5b93b1f
Related-prod: PROD:27350
diff --git a/kubernetes/control/endpoint.sls b/kubernetes/control/endpoint.sls
new file mode 100644
index 0000000..4bb4df3
--- /dev/null
+++ b/kubernetes/control/endpoint.sls
@@ -0,0 +1,60 @@
+{% from "kubernetes/map.jinja" import control with context %}
+include:
+ - kubernetes.control
+
+{%- for endpoint_name, endpoint in control.endpoints.items() %}
+ {%- if endpoint.get('service_enabled', false) %}
+
+/srv/kubernetes/services/{{ endpoint.cluster }}/{{ endpoint.service }}-svc.yml:
+ file.managed:
+ - source: salt://kubernetes/files/svc.yml
+ - user: root
+ - group: root
+ - template: jinja
+ - makedirs: true
+ - require:
+ - file: /srv/kubernetes
+ - defaults:
+ service: {{ endpoint|yaml }}
+
+ {%- if endpoint.get('create', false) %}
+ {%- set service_name = endpoint.service + '-' + endpoint.role if endpoint.role is defined else endpoint.service %}
+kubernetes_service_create_{{ endpoint.service }}:
+ cmd.run:
+ - name: kubectl apply -f /srv/kubernetes/services/{{ endpoint.cluster }}/{{ endpoint.service }}-svc.yml
+ - unless: kubectl get service -o=custom-columns=NAME:.metadata.name --namespace {{ endpoint.namespace }} | grep -xq {{ endpoint.service }}
+ {%- if grains.get('noservices') %}
+ - onlyif: /bin/false
+ {%- endif %}
+ - watch:
+ - file: /srv/kubernetes/services/{{ endpoint.cluster }}/{{ endpoint.service }}-svc.yml
+ {%- endif %}
+
+ {%- endif %}
+
+/srv/kubernetes/endpoints/{{ endpoint.cluster }}/{{ endpoint_name }}.yml:
+ file.managed:
+ - source: salt://kubernetes/files/endpoint.yml
+ - user: root
+ - group: root
+ - template: jinja
+ - makedirs: true
+ - require:
+ - file: /srv/kubernetes
+ - defaults:
+ endpoint: {{ endpoint|yaml }}
+ endpoint_name: {{ endpoint_name }}
+
+ {%- if endpoint.get('create', false) %}
+kubernetes_endpoint_create_{{ endpoint_name }}:
+ cmd.run:
+ - name: kubectl apply -f /srv/kubernetes/endpoints/{{ endpoint.cluster }}/{{ endpoint_name }}.yml
+ - unless: kubectl get endpoint -o=custom-columns=NAME:.metadata.name --namespace {{ endpoint.namespace }} | grep -xq {{ endpoint_name }}
+ {%- if grains.get('noservices') %}
+ - onlyif: /bin/false
+ {%- endif %}
+ - watch:
+ - file: /srv/kubernetes/endpoints/{{ endpoint.cluster }}/{{ endpoint_name }}.yml
+ {%- endif %}
+
+{%- endfor %}
diff --git a/kubernetes/control/init.sls b/kubernetes/control/init.sls
index b8ea755..508f167 100644
--- a/kubernetes/control/init.sls
+++ b/kubernetes/control/init.sls
@@ -15,6 +15,9 @@
{%- if control.priorityclass is defined %}
- kubernetes.control.priorityclass
{%- endif %}
+ {%- if control.endpoints is defined %}
+ - kubernetes.control.endpoint
+ {%- endif %}
/srv/kubernetes:
file.directory:
diff --git a/kubernetes/control/service.sls b/kubernetes/control/service.sls
index e7e9330..3e28bf7 100644
--- a/kubernetes/control/service.sls
+++ b/kubernetes/control/service.sls
@@ -3,7 +3,7 @@
- kubernetes.control
{%- for service_name, service in control.service.items() %}
- {%- if service.enabled %}
+ {%- if service.get('enabled', false) %}
/srv/kubernetes/services/{{ service.cluster }}/{{ service_name }}-svc.yml:
file.managed:
@@ -17,6 +17,19 @@
- defaults:
service: {{ service|yaml }}
+ {%- if service.get('create', false) %}
+ {%- set service_real_name = service.service + '-' + service.role if service.role is defined else service.service %}
+kubernetes_service_create_{{ service_name }}:
+ cmd.run:
+ - name: kubectl apply -f /srv/kubernetes/services/{{ service.cluster }}/{{ service_name }}-svc.yml
+ - unless: kubectl get service -o=custom-columns=NAME:.metadata.name --namespace {{ service.namespace }} | grep -xq {{ service_real_name }}
+ {%- if grains.get('noservices') %}
+ - onlyif: /bin/false
+ {%- endif %}
+ - watch:
+ - file: /srv/kubernetes/services/{{ service.cluster }}/{{ service_name }}-svc.yml
+ {%- endif %}
+
{%- endif %}
/srv/kubernetes/{{ service.kind|lower }}/{{ service_name }}-{{ service.kind }}.yml:
@@ -31,6 +44,19 @@
- defaults:
service: {{ service|yaml }}
+ {%- if service.get('create', false) %}
+ {%- set service_real_name = service.service + '-' + service.role if service.role is defined else service.service %}
+kubernetes_{{ service.kind|lower }}_create_{{ service_name }}:
+ cmd.run:
+ - name: kubectl apply -f /srv/kubernetes/{{ service.kind|lower }}/{{ service_name }}-{{ service.kind }}.yml
+ - unless: kubectl get {{ service.kind|lower }} -o=custom-columns=NAME:.metadata.name --namespace {{ service.namespace }} | grep -xq {{ service_real_name }}
+ {%- if grains.get('noservices') %}
+ - onlyif: /bin/false
+ {%- endif %}
+ - watch:
+ - file: /srv/kubernetes/{{ service.kind|lower }}/{{ service_name }}-{{ service.kind }}.yml
+ {%- endif %}
+
{%- endfor %}
{%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').items() %}
@@ -51,6 +77,19 @@
- defaults:
service: {{ service|yaml }}
+ {%- if service.get('create', false) %}
+ {%- set service_real_name = service.service + '-' + service.role if service.role is defined else service.service %}
+kubernetes_service_create_{{ service.service }}:
+ cmd.run:
+ - name: kubectl apply -f /srv/kubernetes/services/{{ node_name }}-svc.yml
+ - unless: kubectl get service -o=custom-columns=NAME:.metadata.name --namespace {{ service.namespace }} | grep -xq {{ service_real_name }}
+ {%- if grains.get('noservices') %}
+ - onlyif: /bin/false
+ {%- endif %}
+ - watch:
+ - file: /srv/kubernetes/services/{{ node_name }}-svc.yml
+ {%- endif %}
+
{%- endif %}
/srv/kubernetes/{{ service.kind|lower }}/{{ node_name }}-{{ service.kind }}.yml:
file.managed:
@@ -64,6 +103,19 @@
- defaults:
service: {{ service|yaml }}
+ {%- if service.get('create', false) %}
+ {%- set service_real_name = service.service + '-' + service.role if service.role is defined else service.service %}
+kubernetes_{{ service.kind|lower }}_create_{{ service_name }}:
+ cmd.run:
+ - name: kubectl apply -f /srv/kubernetes/{{ service.kind|lower }}/{{ node_name }}-{{ service.kind }}.yml
+ - unless: kubectl get {{ service.kind|lower }} -o=custom-columns=NAME:.metadata.name --namespace {{ service.namespace }} | grep -xq {{ service_real_name }}
+ {%- if grains.get('noservices') %}
+ - onlyif: /bin/false
+ {%- endif %}
+ - watch:
+ - file: /srv/kubernetes/{{ service.kind|lower }}/{{ node_name }}-{{ service.kind }}.yml
+ {%- endif %}
+
{%- endif %}
{%- endfor %}
diff --git a/kubernetes/files/endpoint.yml b/kubernetes/files/endpoint.yml
new file mode 100644
index 0000000..6109bf8
--- /dev/null
+++ b/kubernetes/files/endpoint.yml
@@ -0,0 +1,18 @@
+{% from "kubernetes/map.jinja" import control with context %}
+apiVersion: v1
+kind: Endpoints
+metadata:
+ name: {{ endpoint_name }}
+ {%- if endpoint.namespace is defined %}
+ namespace: {{ endpoint.namespace }}
+ {%- endif %}
+subsets:
+{%- for subset in endpoint.subsets %}
+ - addresses:
+ - ip: {{ subset.ip }}
+ ports:
+ - port: {{ subset.port.number }}
+ {%- if subset.port.name is defined %}
+ name: {{ subset.port.name }}
+ {%- endif %}
+{%- endfor %}
diff --git a/kubernetes/files/rc.yml b/kubernetes/files/rc.yml
index 0a31b6c..d793510 100644
--- a/kubernetes/files/rc.yml
+++ b/kubernetes/files/rc.yml
@@ -2,10 +2,18 @@
apiVersion: {{ service.apiVersion }}
kind: {{ service.kind }}
metadata:
- name: {{ service.service }}-{{ service.role }}
- namespace: {{ service.namespace }}
+ {%- if service.role is defined %}
labels:
+ name: {{ service.service }}-{{ service.role }}
app: {{ service.service }}-{{ service.role }}
+ name: {{ service.service }}-{{ service.role }}
+ {%- else %}
+ labels:
+ name: {{ service.service }}
+ app: {{ service.service }}
+ name: {{ service.service }}
+ {%- endif %}
+ namespace: {{ service.namespace }}
spec:
replicas: {{ service.replicas }}
{%- if service.kind == 'PetSet' %}
@@ -14,7 +22,11 @@
template:
metadata:
labels:
+ {%- if service.role is defined %}
app: {{ service.service }}-{{ service.role }}
+ {%- else %}
+ app: {{ service.service }}
+ {%- endif %}
annotations:
{%- if service.hostname is defined %}
pod.beta.kubernetes.io/hostname: {{ service.hostname }}
diff --git a/kubernetes/files/svc.yml b/kubernetes/files/svc.yml
index b236417..d730639 100644
--- a/kubernetes/files/svc.yml
+++ b/kubernetes/files/svc.yml
@@ -2,10 +2,17 @@
apiVersion: v1
kind: Service
metadata:
+ {%- if service.role is defined %}
labels:
name: {{ service.service }}-{{ service.role }}
app: {{ service.service }}-{{ service.role }}
name: {{ service.service }}-{{ service.role }}
+ {%- else %}
+ labels:
+ name: {{ service.service }}
+ app: {{ service.service }}
+ name: {{ service.service }}
+ {%- endif %}
namespace: {{ service.namespace }}
spec:
ports:
@@ -15,7 +22,11 @@
{%- endfor %}
type: {{ service.type }}
selector:
+ {%- if service.role is defined %}
app: {{ service.service }}-{{ service.role }}
+ {%- else %}
+ app: {{ service.service }}
+ {%- endif %}
{%- if service.cluster_ip is defined %}
clusterIP: {{ service.cluster_ip }}
{%- endif %}