canonical / jhack

Chock-full of Juju hackery.
Apache License 2.0
51 stars 24 forks source link

Machine charm detection does not work #51

Closed awnns closed 1 year ago

awnns commented 1 year ago

In ./helpers.py, get_substrate() is defined as:

def get_substrate(model: str = None) -> Literal["k8s", "machine"]:
    """Attempts to guess whether we're talking k8s or machine."""
    cmd = f'juju show-model{f" {model}" if model else ""} --format=json'
    proc = JPopen(cmd.split())
    raw = proc.stdout.read().decode("utf-8")
    model_info = jsn.loads(raw)

    if not model:
        model = list(model_info)[0]

    model_type = model_info[model]["model-type"]
    if model_type == "caas":
        return "machine"
    elif model_type == "iaas":
        return "k8s"
    else:
        raise ValueError(f"unrecognized model type: {model_type}")

This does not work in practice, because our machine charms also report model-type as "iaas". A field that should set them apart is type which says "lxd" for us but I'm not sure I have all bases covered in terms of which values are possible here so I will wait a little with contributing a fix.

PietroPasotti commented 1 year ago

Huh. Not sure I follow you. caas -> kubernetes iaas -> anything that's not kubernetes

This is all I think we need for now. Whether the non-k8s substrate is lxd, maas, or other things, it wouldn't make a difference for our use case. Although I do agree that making such a partial casting caas->k8s, iaas->'machine' doesn't sound like good practice or future-proof. I'd be fine with making get_substrate return the 'raw' type (instead of model-type) or, better, an Enum, and refactor the code using get_substrate to filter on that: if get_substrate() == Substrates.kubernetes...

PietroPasotti commented 1 year ago

Also see juju-thread on the topic: https://chat.charmhub.io/charmhub/pl/pfq6ky61fjnqzmxjgsta7pejxc

awnns commented 1 year ago

In that case I got myself a little confused. There's a little typo where they got switched around.

PietroPasotti commented 1 year ago

Oh! You're very much right. Thanks a lot :)