Skip to content

Commit

Permalink
add special route tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ikethecoder committed May 6, 2024
1 parent 0e2ff4d commit a64a972
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
7 changes: 5 additions & 2 deletions microservices/gatewayApi/v2/routes/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from werkzeug.exceptions import HTTPException, NotFound
from flask import Blueprint, config, jsonify, request, Response, make_response, abort, g, current_app as app
from io import TextIOWrapper
from clients.ocp_routes import get_host_list
from clients.ocp_routes import get_host_list, get_route_overrides

from v2.auth.auth import admin_jwt, uma_enforce

Expand Down Expand Up @@ -339,7 +339,10 @@ def write_config(namespace: str) -> object:
route_payload = {
"hosts": host_list,
"select_tag": selectTag,
"ns_attributes": ns_attributes.getAttrs()
"ns_attributes": ns_attributes.getAttrs(),
"overrides": {
"aps.route.session.cookie.enabled": get_route_overrides(tempFolder, "aps.route.session.cookie.enabled")
}
}
rqst_url = app.config['data_planes'][dp]["kube-api"]
log.debug("[%s] - Initiating request to kube API" % (dp))
Expand Down
4 changes: 4 additions & 0 deletions microservices/gatewayJobScheduler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ def transform_data_by_ns(data):

# check if namespace has data plane attribute
if ns_attr_dict[namespace].get('perm-data-plane', [''])[0] == os.getenv('DATA_PLANE'):
session_cookie_enabled = False
if 'aps.route.session.cookie.enabled' in route_obj['tags']:
session_cookie_enabled = True
for host in route_obj['hosts']:
name = 'wild-%s-%s' % (select_tag.replace(".", "-"), host)
ns_dict[namespace].append({"name": name, "selectTag": select_tag, "host": host,
"session_cookie_enabled": session_cookie_enabled,
"dataPlane": os.getenv('DATA_PLANE')})
return ns_dict
except Exception as err:
Expand Down
11 changes: 8 additions & 3 deletions microservices/kubeApi/clients/ocp_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,23 @@ def prepare_route_last_version(ns, select_tag):
return resource_versions


def prepare_apply_routes(ns, select_tag, hosts, rootPath, data_plane, template_version):
def prepare_apply_routes(ns, select_tag, hosts, rootPath, data_plane, ns_template_version, overrides):
out_filename = "%s/routes-current.yaml" % rootPath
ts = int(time.time())
fmt_time = datetime.now().strftime("%Y.%m-%b.%d")

resource_versions = prepare_route_last_version(ns, select_tag)

route_template = ROUTES[template_version]["ROUTE"]

with open(out_filename, 'w') as out_file:
index = 1
for host in hosts:
templ_version = ns_template_version
if 'aps.route.session.cookie.enabled' in overrides and host in overrides['aps.route.session.cookie.enabled']:
templ_version = 'v1'

route_template = ROUTES[templ_version]["ROUTE"]

# If host transformation is disabled, then select the appropriate
# SSL cert based on the suffix mapping
ssl_ref = "tls"
Expand All @@ -168,7 +173,7 @@ def prepare_apply_routes(ns, select_tag, hosts, rootPath, data_plane, template_v
(select_tag, index, select_tag.replace('.', '-'), host, resource_version))
out_file.write(route_template.substitute(name=name, ns=ns, select_tag=select_tag, resource_version=resource_version, host=host, path='/',
ssl_ref=ssl_ref, ssl_key=ssl_key, ssl_crt=ssl_crt, service_name=data_plane, timestamp=ts, fmt_time=fmt_time, data_plane=data_plane,
template_version=template_version))
template_version=templ_version))
out_file.write('\n---\n')
index = index + 1
out_file.close()
Expand Down
12 changes: 8 additions & 4 deletions microservices/kubeApi/routers/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class OCPRoute(BaseModel):
hosts: list
select_tag: str
ns_attributes: dict
overrides: dict


@router.put("/namespaces/{namespace}/routes", status_code=201, dependencies=[Depends(verify_credentials)])
Expand Down Expand Up @@ -59,13 +60,13 @@ def add_routes(namespace: str, route: OCPRoute):
try:
hosts = [a for a in route.hosts if not a.endswith(".cluster.local")]

template_version = get_template_version(route.ns_attributes)
ns_template_version = get_template_version(route.ns_attributes)

# do routeable hosts
source_folder = "%s/%s/%s" % ('/tmp', uuid.uuid4(), namespace)
os.makedirs(source_folder, exist_ok=False)
route_count = prepare_apply_routes(namespace, route.select_tag, hosts,
source_folder, get_data_plane(route.ns_attributes), template_version)
source_folder, get_data_plane(route.ns_attributes), ns_template_version, route.overrides)
logger.debug("[%s] - Prepared %s routes" % (namespace, route_count))
if route_count > 0:
apply_routes(source_folder)
Expand Down Expand Up @@ -158,7 +159,7 @@ async def verify_and_create_routes(namespace: str, request: Request):

# TODO: We shouldn't assume it is always v2 - caller needs to get
# this info from ns_attributes
template_version = "v2"
ns_template_version = "v2"

try:
if len(insert_batch) > 0:
Expand All @@ -167,8 +168,11 @@ async def verify_and_create_routes(namespace: str, request: Request):
os.makedirs(source_folder, exist_ok=False)

for route in insert_batch:
overrides = {}
if route['session_cookie_enabled']:
overrides['aps.route.session.cookie.enabled'] = [ route['host'] ]
route_count = prepare_apply_routes(namespace, route['selectTag'], [
route['host']], source_folder, route["dataPlane"], template_version)
route['host']], source_folder, route["dataPlane"], ns_template_version, overrides)
logger.debug("[%s] - Prepared %d routes" % (namespace, route_count))
apply_routes(source_folder)
logger.debug("[%s] - Applied %d routes" % (namespace, route_count))
Expand Down

0 comments on commit a64a972

Please sign in to comment.