Skip to content
Merged

#21 #56

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion getdeck/__main__.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3.10
#!/usr/bin/env python3
import argparse
import logging
import os
Expand Down Expand Up @@ -99,9 +99,20 @@ def check_positive(value):
version_parser = action.add_parser("version")


hosts_parser = action.add_parser("hosts")
hosts_parser.add_argument("host_action", help="list/write/remove")
hosts_parser.add_argument(
"Deckfile", help="the deck.yaml location (as file, git or https)"
)
hosts_parser.add_argument(
"--name", help="the Deck whose hosts will be considered", required=False
)


def main():
from getdeck import configuration
from getdeck.api import get_available_decks, run_deck, remove_cluster, remove_deck
from getdeck.api.hosts import run_hosts

try:
args = parser.parse_args()
Expand Down Expand Up @@ -134,6 +145,12 @@ def main():
stop_cluster(args.Deckfile, ignore_cluster=args.no_cluster)
elif args.action == "version":
logger.info(f"Deck version: {configuration.__VERSION__}")
elif args.action == "hosts":
run_hosts(
args.Deckfile,
args.host_action,
deck_name=args.name,
)
else:
parser.print_help()
exit(0)
Expand Down
29 changes: 23 additions & 6 deletions getdeck/api/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Callable

from getdeck.api import stopwatch, remove
from getdeck.api.hosts import verify_all_hosts
from getdeck.configuration import default_configuration

logger = logging.getLogger("deck")
Expand All @@ -21,8 +22,7 @@ def run_deck(
from getdeck.utils import read_deckfile_from_location, ensure_cluster
from kubernetes.client import V1Namespace, V1ObjectMeta
from kubernetes.client.rest import ApiException
from getdeck.k8s import k8s_create_or_patch
from getdeck.k8s import get_ingress_display
from getdeck.k8s import k8s_create_or_patch, get_ingress_rules

cluster_created = False
if progress_callback:
Expand Down Expand Up @@ -99,10 +99,11 @@ def run_deck(
progress_callback(100)
logger.info(f"All workloads from Deck {generated_deck.name} applied")

ingress = get_ingress_display(config, generated_deck.namespace)
if ingress:
for path in ingress:
logger.info(f"Ingress: {path[0]} -> {path[1]}")
ingress_rules = get_ingress_rules(config, generated_deck.namespace)
if ingress_rules:
for host, path in ingress_rules:
logger.info(f"Ingress: {host} -> {path}")
handle_hosts_resolution(deckfile_location, deckfile, deck_name)
logger.info(f"Published ports are: {k8s_provider.get_ports()}")
if notes := deckfile.get_deck(deck_name).notes:
logger.info(notes)
Expand All @@ -112,6 +113,22 @@ def run_deck(
return True


def handle_hosts_resolution(deckfile_location, deckfile, deck_name):
deck = deckfile.get_deck(deck_name)
deck_hosts = deck.hosts
deck_flag = " "
if deck_name:
deck_flag = f" --name {deck_name} "
if not verify_all_hosts(*deck_hosts):
logger.warning("Some of your deck hosts do not resolve to localhost.")
logger.info(
f"If these ingress hosts do not resolve to localhost, you can configure them manually "
f"by executing\n"
f"'deck hosts write{deck_flag}{deckfile_location}'.\n"
f"with admin rights."
)


def _wait_ready(config, generated_deck, timeout):
from getdeck.utils import wait_for_pods_ready

Expand Down
61 changes: 61 additions & 0 deletions getdeck/api/hosts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import logging
import socket

from python_hosts import Hosts, HostsEntry

from getdeck.api import stopwatch
from getdeck.configuration import default_configuration
from getdeck.utils import read_deckfile_from_location

logger = logging.getLogger("deck")


@stopwatch
def run_hosts(
deckfile_location: str,
host_action: str,
deck_name: str = None,
config=default_configuration,
) -> bool:
deckfile = read_deckfile_from_location(deckfile_location, config)
deck = deckfile.get_deck(deck_name)
deck_hosts = deck.hosts

hosts = Hosts()
if host_action == "list":

logger.info("Ingress hosts:")
for host in deck_hosts:
logger.info(f"{host}")

elif host_action == "remove":
logger.debug("Removing hosts from hosts file...")
for host in deck_hosts:
hosts.remove_all_matching(name=host)
hosts.write()
logger.info("Hosts have been removed from hosts file...")

elif host_action == "write":

logger.info("Writing hosts to hosts file...")
new_entry = HostsEntry(entry_type="ipv4", address="127.0.0.1", names=deck_hosts)
hosts.add([new_entry])
hosts.write()
logger.info("Hosts should resolve to '127.0.0.1' now.")

else:
logger.error(f"Unknown host action '{host_action}'")

return True


def verify_host(host) -> bool:
try:
ip = socket.gethostbyname(host)
except socket.gaierror:
return False
return ip == "127.0.0.1"


def verify_all_hosts(*hosts):
return all(verify_host(host) for host in hosts)
3 changes: 2 additions & 1 deletion getdeck/deckfile/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class DeckfileDeck(BaseModel):
name: str
namespace: str = "default"
notes: str = ""
hosts: List[str] = []
sources: List[
Union[
DeckfileHelmSource,
Expand Down Expand Up @@ -115,5 +116,5 @@ def get_decks(self) -> List[DeckfileDeck]:
raise NotImplementedError

@abstractmethod
def get_deck(self) -> DeckfileDeck:
def get_deck(self, name: str = None) -> DeckfileDeck:
raise NotImplementedError
13 changes: 13 additions & 0 deletions getdeck/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ def convert_camel_2_snake(_string) -> str:
return _string


def get_ingress_rules(
config: ClientConfiguration, namespace: str
) -> List[Tuple[str, str]]:
result = []
ingresss = config.K8S_NETWORKING_API.list_namespaced_ingress(namespace)
for ingress in ingresss.items:
for rule in ingress.spec.rules:
_host = rule.host
for path in rule.http.paths:
result.append((_host, path.path))
return result


def get_ingress_display(
config: ClientConfiguration, namespace: str
) -> List[Tuple[str, str]]:
Expand Down
Loading