Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request :: Talos context support #1808

Open
lcrockett opened this issue Mar 22, 2022 · 3 comments
Open

Feature request :: Talos context support #1808

lcrockett opened this issue Mar 22, 2022 · 3 comments

Comments

@lcrockett
Copy link

Thank you for the great Z shell theme, i'm using it daily with great pleasure.

I was wondering if you could consider including suport for Talos context support. As the CLI tool talosctl shares similarities with kubectl, i've skimmed the p10k source for Kubernetes prompt segments and took a stab at it. It kinda worked except for the caching that seemed to give inconsistent results and even show the prompt when there was no Talos configuration set. So instead I made a simple custom section that is less then ideal (see snippet below) to get it working for now.

The Talos CLI tool talosctl shares similarities with kubectl in the sense that they can both set contexts (talosctl config context <context-name>) and can use an environment variable to determine it's current configuration (file) $TALOSCONFIG. Info regarding the currently set context can be retrieved from the set Talos configuration file or using the CLI talosctl config info.

Currently used snippet (yes, i'm aware of the suboptimal delay due to the recurring talosctl config info command that is ran at every new prompt line):

  function prompt_my_taloscontext() {
    if [[ -n "$TALOSCONFIG" ]]; then
        local self_talosctl_config_info_output && self_talosctl_config_info_output=(${(f)"$(talosctl config info 2>/dev/null)"}) || return
        local self_talosctl_config_info_query_string='"*"'
        local self_talosctl_config_info_context_string='([^"'\''|>]*|'$self_talosctl_config_info_query_string')'
        local self_talosctl_config_info_context_count=(${(@M)self_talosctl_config_info_output:#Current context:     $~self_talosctl_config_info_context_string})

        (( $#self_talosctl_config_info_context_count == 1 )) || return

        POWERLEVEL9K_MY_TALOSCONTEXT_SEGMENT="${self_talosctl_config_info_context_count[1]#Current context:     }"

        p10k segment -t "$POWERLEVEL9K_MY_TALOSCONTEXT_SEGMENT"
    fi
  }

  function instant_prompt_my_taloscontext() {
      prompt_my_taloscontext
  }

  typeset -g POWERLEVEL9K_MY_TALOSCONTEXT_FOREGROUND=cyan
  typeset -g POWERLEVEL9K_MY_TALOSCONTEXT_VISUAL_IDENTIFIER_EXPANSION=''
  typeset -g POWERLEVEL9K_MY_TALOSCONTEXT_SHOW_ON_COMMAND='talosctl'

Example of a Talos configuration file:

$ cat ~/.talos/config
context: orange-spoon
contexts:
    orange-spoon:
        endpoints:
            - talos-control-plane.subnetwork.domain.tld
        ca: ...
        crt: ...
        key: ...

If you'd like more information or some testing to be done, let me know. Cheers

@romkatv
Copy link
Owner

romkatv commented Mar 23, 2022

Thanks for posting the code.

Given that it invokes talosctl on every command, I cannot add it to powerlevel10k proper (it would be too slow). The usual way powerelvel10k solves this problem is either by caching or invoking the tool asynchronously. Caching is easier and gives better UX but it requires knowing the set of env vars and files on which the output of the command depends.

I clicked on https://www.talos.dev/ and then on Try it now. The latter gave me 404. I gave up there.

@lcrockett
Copy link
Author

Don't mind my example above, that was just a personal workaround for illustrative purposes. I agree asynchronous invocation and / or caching is more UX friendly as derived from the current Kubernetes implementation in powerlevel10k. It's a shame the documentation link doesn't work, it's a recent change it seems and i'm guessing it's due to them preparing for their upcoming v1.0.0 release. The correct link to the quick start guide is (https://www.talos.dev/v1.0/introduction/quickstart/).

If you'd like to give it a go, loosely following the Kubernetes example in powerlevel10k, I can assist in providing the required implementation details. As I wrote before, I got it somewhat working by re-using the existing Kubernetes context prompt segments and stripping / modifying it to work with talosctl, so i'd assume that should make things a tad easier.

Regarding some implied questions you had:

  1. talosctl depends on the environment variable $TALOSCONFIG that points to a Talos configuration file, usually located at $HOME/.talos/config. See below for an example of the Talos configuration file
  2. Changing the Talos context can be done using talosctl config context <context-name>. This changes the context value in the $TALOSCONFIG file
  3. A talosctl config info can be used to find out the currently set context, but parsing the Talos configuration file will work just as well. See below for an exmaple of the talosctl config info output

Example Talos configuration file:

context: orange-spoon
contexts:
    orange-spoon:
        endpoints:
            - talos-control-plane.subnetwork.domain.tld
        nodes:
            - talos-control-plane-a2c.subnetwork.domain.tld
            - talos-control-plane-b8d.subnetwork.domain.tld
            - talos-control-plane-c6b.subnetwork.domain.tld
            - talos-control-plane-d9f.subnetwork.domain.tld
            - talos-control-plane-e1a.subnetwork.domain.tld
            - talos-worker-a3d.subnetwork.domain.tld
            - talos-worker-b4c.subnetwork.domain.tld
            - talos-worker-c8f.subnetwork.domain.tld
            - talos-worker-d1a.subnetwork.domain.tld
            - talos-worker-e7b.subnetwork.domain.tld
        ca: <certificate-contents>
        crt: <certificate-contents>
        key: <key-contents>

Output talosctl config info:

$ talosctl config info
Current context:     orange-spoon
Nodes:               talos-control-plane-a2c.subnetwork.domain.tld, talos-control-plane-b8d.subnetwork.domain.tld, talos-control-plane-c6b.subnetwork.domain.tld, talos-control-plane-d9f.subnetwork.domain.tld, talos-control-plane-e1a.subnetwork.domain.tld, talos-worker-a3d.subnetwork.domain.tld, talos-worker-b4c.subnetwork.domain.tld, talos-worker-c8f.subnetwork.domain.tld, talos-worker-d1a.subnetwork.domain.tld, talos-worker-e7b.subnetwork.domain.tld
Endpoints:           talos-control-plane.subnetwork.domain.tld
Roles:               os:admin
Certificate expires: 10 years from now (2032-03-04)

@romkatv
Copy link
Owner

romkatv commented Mar 23, 2022

Thanks for the info. This should in theory allow me to add talosctl segment but in practice I'm unlikely to do that given my time constraints. If you send a clean PR, I'll merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants