Skip to content

Commit

Permalink
Propagate kubernetes custom metadata annotations to sub-services (#3767)
Browse files Browse the repository at this point in the history
* propagate kubernetes custom annotations to the loadbalancer

* Adding annotation propagation for ingress kubernetes services

* propagating labels for kubernetes
  • Loading branch information
fozziethebeat authored Jul 19, 2024
1 parent c0246ab commit aea7322
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ sky/clouds/service_catalog/data_fetchers/*.csv
.vscode/
.idea/
.env

# For editor files
*.swp
12 changes: 12 additions & 0 deletions sky/provision/kubernetes/network_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ def fill_loadbalancer_template(namespace: str, service_name: str,

with open(template_path, 'r', encoding='utf-8') as fin:
template = fin.read()
annotations = skypilot_config.get_nested(
('kubernetes', 'custom_metadata', 'annotations'), {})
labels = skypilot_config.get_nested(
('kubernetes', 'custom_metadata', 'labels'), {})
j2_template = jinja2.Template(template)
cont = j2_template.render(
namespace=namespace,
service_name=service_name,
ports=ports,
selector_key=selector_key,
selector_value=selector_value,
annotations=annotations,
labels=labels,
)
content = yaml.safe_load(cont)
return content
Expand All @@ -98,6 +104,10 @@ def fill_ingress_template(namespace: str, service_details: List[Tuple[str, int,
f'Template "{_INGRESS_TEMPLATE_NAME}" does not exist.')
with open(template_path, 'r', encoding='utf-8') as fin:
template = fin.read()
annotations = skypilot_config.get_nested(
('kubernetes', 'custom_metadata', 'annotations'), {})
labels = skypilot_config.get_nested(
('kubernetes', 'custom_metadata', 'labels'), {})
j2_template = jinja2.Template(template)
cont = j2_template.render(
namespace=namespace,
Expand All @@ -109,6 +119,8 @@ def fill_ingress_template(namespace: str, service_details: List[Tuple[str, int,
ingress_name=ingress_name,
selector_key=selector_key,
selector_value=selector_value,
annotations=annotations,
labels=labels,
)
content = yaml.safe_load(cont)

Expand Down
7 changes: 7 additions & 0 deletions sky/templates/kubernetes-ingress.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ ingress_spec:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
labels:
{%- for label_key, label_value in labels.items() %}
{{ label_key }}: {{ label_value|tojson }}
{%- endfor %}
annotations:
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
{%- for key, value in annotations.items() %}
{{ key }}: {{ value|tojson }}
{%- endfor %}
name: {{ ingress_name }}
namespace: {{ namespace }}
spec:
Expand Down
7 changes: 7 additions & 0 deletions sky/templates/kubernetes-loadbalancer.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ service_spec:
name: {{ service_name }}
labels:
parent: skypilot
{%- for label_key, label_value in labels.items() %}
{{ label_key }}: {{ label_value|tojson }}
{%- endfor %}
annotations:
{%- for key, value in annotations.items() %}
{{ key }}: {{ value|tojson }}
{%- endfor %}
spec:
type: LoadBalancer
selector:
Expand Down

0 comments on commit aea7322

Please # to comment.