Skip to content

Commit

Permalink
Merge pull request #141 from francescotimperi/extra-www
Browse files Browse the repository at this point in the history
  • Loading branch information
francescotimperi committed Feb 15, 2024
2 parents 4f4511e + f9fa337 commit 25999a9
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 21 deletions.
45 changes: 45 additions & 0 deletions nuvolaris/apihost_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ def extract_hostname(url):
parsed_url = urllib.parse.urlparse(url)
return parsed_url.hostname

def extract_port(url):
"""
Parse a url and extract the port part
>>> extract_port('http://localhost:8080')
8080
>>> extract_port('https://nuvolaris.org')
"""
parsed_url = urllib.parse.urlparse(url)
return parsed_url.port


def get_user_static_hostname(runtime, username, apihost):
"""
Expand Down Expand Up @@ -191,4 +201,39 @@ def get_user_api_url(runtime, hostname, api_context):
final_url = assign_protocol(runtime, url)
return final_url.geturl()

def append_prefix_to_url(url,prefix):
"""
Appends the given prefix to the specified if not present
>>> append_prefix_to_url("http://nuvolaris.dev:8080","www")
'http://www.nuvolaris.dev:8080'
>>> append_prefix_to_url("http://www.nuvolaris.dev:8080","www")
'http://www.nuvolaris.dev:8080'
>>> append_prefix_to_url("http://nuvolaris.dev:8080",None)
'http://nuvolaris.dev:8080'
>>> append_prefix_to_url("https://nuvolaris.dev","www")
'https://www.nuvolaris.dev'
"""
try:
if not prefix:
return url

tmp_url = urllib.parse.urlparse(url)
hostname = extract_hostname(url)
port = extract_port(url)

if prefix in hostname:
return url

if port:
tmp_url = tmp_url._replace(netloc = f"{prefix}.{hostname}:{port}")
else:
tmp_url = tmp_url._replace(netloc = f"{prefix}.{hostname}")

return tmp_url.geturl()

except Exception as e:
return url




82 changes: 66 additions & 16 deletions nuvolaris/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def deploy_info_route(apihost,namespace):
os.remove(path_to_template_yaml)
return res

def deploy_api_routes(apihost,namespace):
def deploy_api_routes(apihost,namespace,should_create_www=False):
logging.info(f"**** configuring openshift route based endpoint for apihost {apihost}")

api = RouteData(apihost)
Expand All @@ -81,11 +81,27 @@ def deploy_api_routes(apihost,namespace):

logging.info(f"*** configuring route for apihost-my")
path_to_template_yaml = my.render_template(namespace)
res = kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)

if should_create_www:
www_my = RouteData(apihost)
www_my.with_route_name(api_route_name(namespace,"apihost-www-my"))
www_my.with_service_name("controller-ip")
www_my.with_service_kind("Service")
www_my.with_service_port("8080")
www_my.with_context_path("/api/my")
www_my.with_rewrite_target(f"/api/v1/web/namespace/{namespace}")

logging.info(f"*** configuring route for apihost-www-my")
path_to_template_yaml = www_my.render_template(namespace)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)

return res

def deploy_info_ingress(apihost, namespace):
res = ""
info = IngressData(apihost)
info.with_ingress_name(api_ingress_name(namespace,"apihost-info"))
info.with_secret_name(api_secret_name(namespace))
Expand All @@ -99,19 +115,20 @@ def deploy_info_ingress(apihost, namespace):
if info.requires_traefik_middleware():
logging.info("*** configuring traefik middleware for apihost-info ingress")
path_to_template_yaml = info.render_traefik_middleware_template(namespace)
res = kube.kubectl("apply", "-f",path_to_template_yaml)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)

logging.info(f"*** configuring static ingress for apihost-info")
path_to_template_yaml = info.render_template(namespace)
res = kube.kubectl("apply", "-f",path_to_template_yaml)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)

return res


def deploy_api_ingresses(apihost, namespace):
logging.info(f"**** configuring ingresses based endpoint for apihost {apihost}")
def deploy_api_ingresses(apihost, namespace,should_create_www=False):
logging.info(f"**** configuring ingresses based endpoint for apihost {apihost}")
res = ""
api = IngressData(apihost)
api.with_ingress_name(api_ingress_name(namespace,"apihost"))
api.with_secret_name(api_secret_name(namespace))
Expand All @@ -135,42 +152,65 @@ def deploy_api_ingresses(apihost, namespace):
if api.requires_traefik_middleware():
logging.info("*** configuring traefik middleware for apihost ingress")
path_to_template_yaml = api.render_traefik_middleware_template(namespace)
res = kube.kubectl("apply", "-f",path_to_template_yaml)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)

if my.requires_traefik_middleware():
logging.info("*** configuring traefik middleware for apihost-my ingress")
path_to_template_yaml = my.render_traefik_middleware_template(namespace)
res = kube.kubectl("apply", "-f",path_to_template_yaml)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)

logging.info(f"*** configuring static ingress for apihost")
path_to_template_yaml = api.render_template(namespace)
res = kube.kubectl("apply", "-f",path_to_template_yaml)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)

logging.info(f"*** configuring static ingress for apihost-my")
path_to_template_yaml = my.render_template(namespace)
res = kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)

return res
if should_create_www:
www_my = IngressData(apihost)
www_my.with_ingress_name(api_ingress_name(namespace,"apihost-www-my"))
www_my.with_secret_name(api_secret_name(namespace)+"-www")
www_my.with_context_path("/api/my")
www_my.with_context_regexp("(/|$)(.*)")
www_my.with_rewrite_target(f"/api/v1/web/{namespace}/$2")
www_my.with_service_name("controller")
www_my.with_service_port("3233")
www_my.with_middleware_ingress_name(api_middleware_ingress_name(namespace,"apihost-www-my"))

if www_my.requires_traefik_middleware():
logging.info("*** configuring traefik middleware for apihost-www-my ingress")
path_to_template_yaml = www_my.render_traefik_middleware_template(namespace)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)

logging.info(f"*** configuring static ingress for apihost-www-my")
path_to_template_yaml = www_my.render_template(namespace)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)

return res

def create(owner=None):
runtime = cfg.get('nuvolaris.kube')
apihost = apihost_util.get_apihost(runtime)
hostname = apihost_util.extract_hostname(apihost)
should_create_www = "www" not in hostname and runtime not in ["kind"]

logging.info(f"*** Saving configuration for OpenWishk apihost={apihost}")
openwhisk.annotate(f"apihost={apihost}")
cfg.put("config.apihost", apihost)

if runtime == 'openshift':
res = deploy_info_route(apihost,"nuvolaris")
return deploy_api_routes(apihost,"nuvolaris")
return deploy_api_routes(apihost,"nuvolaris",should_create_www)
else:
res = deploy_info_ingress(apihost,"nuvolaris")
return deploy_api_ingresses(apihost,"nuvolaris")
return deploy_api_ingresses(apihost,"nuvolaris",should_create_www)

def delete(owner=None):
"""
Expand All @@ -180,23 +220,33 @@ def delete(owner=None):
namespace = "nuvolaris"
runtime = cfg.get('nuvolaris.kube')
ingress_class = util.get_ingress_class(runtime)
apihost = apihost_util.get_apihost(runtime)
hostname = apihost_util.extract_hostname(apihost)
should_delete_www = "www" not in hostname and runtime not in ["kind"]

try:
res = ""
if(runtime=='openshift'):
res = kube.kubectl("delete", "route",api_route_name(namespace,"apihost"))
res += kube.kubectl("delete", "route",api_route_name(namespace,"apihost-my"))
res += kube.kubectl("delete", "route",api_route_name(namespace,"apihost-info"))

if should_delete_www:
res += kube.kubectl("delete", "route",api_route_name(namespace,"apihost-www-my"))
return res

if(ingress_class == 'traefik'):
res = kube.kubectl("delete", "middleware.traefik.containo.us",api_middleware_ingress_name(namespace,"apihost"))
res += kube.kubectl("delete", "middleware.traefik.containo.us",api_middleware_ingress_name(namespace,"apihost-my"))
res += kube.kubectl("delete", "middleware.traefik.containo.us",api_middleware_ingress_name(namespace,"apihost-info"))
res += kube.kubectl("delete", "middleware.traefik.containo.us",api_middleware_ingress_name(namespace,"apihost-info"))
if should_delete_www:
res += kube.kubectl("delete", "middleware.traefik.containo.us",api_middleware_ingress_name(namespace,"apihost-www-my"))

res += kube.kubectl("delete", "ingress",api_ingress_name(namespace,"apihost"))
res += kube.kubectl("delete", "ingress",api_ingress_name(namespace,"apihost-my"))
res += kube.kubectl("delete", "ingress",api_ingress_name(namespace,"apihost-info"))
if should_delete_www:
res += kube.kubectl("delete", "ingress",api_ingress_name(namespace,"apihost-www-my"))
return res
except Exception as e:
logging.warn(e)
Expand Down
39 changes: 34 additions & 5 deletions nuvolaris/minio_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,23 @@ def create(owner=None):
return res

def create_nuv_static_ingress(runtime, owner=None):
""""
Deploys the static ingresses for the nuvolaris user
"""
apihost_url = apihost_util.get_apihost(runtime)

hostname = apihost_util.extract_hostname(apihost_url)
should_create_www = "www" not in hostname and runtime not in ["kind"]

if runtime == 'openshift':
return deploy_content_route_template("nuvolaris","nuvolaris-web", apihost_url)
res = deploy_content_route_template("nuvolaris","nuvolaris-web", apihost_url)
if should_create_www:
res += deploy_content_route_template("www-nuvolaris","nuvolaris-web",apihost_util.append_prefix_to_url(apihost_url,"www"))
return res
else:
return deploy_content_ingress_template("nuvolaris","nuvolaris-web",apihost_url)
res = deploy_content_ingress_template("nuvolaris","nuvolaris-web",apihost_url)
if should_create_www:
res += deploy_content_ingress_template("www-nuvolaris","nuvolaris-web",apihost_util.append_prefix_to_url(apihost_url,"www"))
return res

def static_ingress_name(namespace, default="apihost"):
return namespace == "nuvolaris" and f"{default}-static-ingress" or f"{namespace}-static-ingress"
Expand Down Expand Up @@ -121,7 +132,9 @@ def deploy_content_ingress_template(namespace, bucket, url):
path_to_template_yaml = content.render_template(namespace)
res += kube.kubectl("apply", "-f",path_to_template_yaml)
os.remove(path_to_template_yaml)
return res

return res


def create_ow_static_endpoint(ucfg, user_metadata: UserMetadata, owner=None):
"""
Expand Down Expand Up @@ -193,20 +206,36 @@ def delete_nuv_ingresses():
logging.info("*** deleting nuvolaris static ingresses")
runtime = cfg.get('nuvolaris.kube')
ingress_class = util.get_ingress_class(runtime)
apihost_url = apihost_util.get_apihost(runtime)
hostname = apihost_util.extract_hostname(apihost_url)
should_delete_www = "www" not in hostname and runtime not in ["kind"]

try:
res = ""
if(runtime=='openshift'):
route_name = static_route_name("nuvolaris")
res = kube.kubectl("delete", "route",route_name)

if should_delete_www:
route_name = static_route_name("www-nuvolaris")
res += kube.kubectl("delete", "route",route_name)
return res

if(ingress_class == 'traefik'):
middleware_name = static_middleware_ingress_name("nuvolaris")
res += kube.kubectl("delete", "middleware.traefik.containo.us",middleware_name)
res += kube.kubectl("delete", "middleware.traefik.containo.us",middleware_name)

if should_delete_www:
middleware_name = static_middleware_ingress_name("www-nuvolaris")
res += kube.kubectl("delete", "middleware.traefik.containo.us",middleware_name)

ingress_name = static_ingress_name("nuvolaris")
res += kube.kubectl("delete", "ingress",ingress_name)

if should_delete_www:
ingress_name = static_ingress_name("www-nuvolaris")
res += kube.kubectl("delete", "ingress",ingress_name)

return res
except Exception as e:
logging.warn(e)
Expand Down

0 comments on commit 25999a9

Please sign in to comment.