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

swap out kubectl #614

Open
wants to merge 55 commits into
base: main
Choose a base branch
from

Conversation

mplsgrant
Copy link
Collaborator

@mplsgrant mplsgrant commented Sep 19, 2024

Took a stab at swapping out kubectl addressing #609

  • It appears to mostly work; however, something is up with the signet test, and I didn't have a chance to figure out what.

  • There was also an oddball issues around stream returning python dicts and lists instead of json. I was able to avoid the impacts of this issue.

  • Some things remain less tested such as a few namespace items and the chain save-state functionality.

@willcl-ark
Copy link
Contributor

Am I correct in thinking that all users may still require kubectl to be installed, so that the cluster can be configured? Or does the python lib bypass all that too?

I had always assumed it hinged on ~/.kube/config to know where to do the things it was going to do, but never actually checked it out. Checking the code it seems like there could be alternatives to load_kube_config() (in fact, I see here references to google auth, which sounds potentially useful to us on the credential side of things?)

Users may also want kubectl for any additional k8s applications, such as k9s, to work, unless they too can be tricked by dumping a python-generated config (from this kubernetes lib) into the user's ~.kube/config file ;)

If pure python is preferred, then this looks like a big move in the right direction!

Comment on lines 65 to 70
# The k8s lib seems to convert json into its python representation. The following dance seems to
# avoid the worst of it.
if resp.startswith("{") or resp.startswith("["):
literal = ast.literal_eval(resp)
json_string = json.dumps(literal)
return json_string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old exec_run() function added this option to stream() for this: _preload_content=False

overall i think its better to restore the old exec_run() function in k8s.py from pre-italy and just call it here.
That's as far as I got on this project: 756df48b97acb1496b2958e12b895a00

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed here via _preload_content=False: 16f869a

Comment on lines 120 to 125
logs = sclient.read_namespaced_pod_log(
name=pod_name,
namespace=get_default_namespace(),
container=container_name,
timestamps=True,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can also use the _log() internal function I added to get the log

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(or pod_log())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed up here: 778a3eb

subdir = "" if chain == "main" else f"{chain}/"
base_dir = f"/root/.bitcoin/{subdir}message_capture"

# Get the IP of node_b
cmd = f"kubectl get pod {tank_b} -o jsonpath='{{.status.podIP}}'"
tank_b_ip = run_command(cmd).strip()
tank_b_pod: V1Pod = sclient.read_namespaced_pod(name=tank_b, namespace=get_default_namespace())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use the get pod helpers in k8s.py?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed up here: 6d8ffa0
and here: e84416b

print(f"Please consider waiting for the pod to become available. Encountered: {e}")
else:
pass # cancelled by user
container_name = "bitcoincore" if pod_name.startswith("tank") else None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't ever rely on tank names conforming to any kind of convention

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed up here: 04be184

Comment on lines 174 to 198
with open(KUBECONFIG) as file:
kubeconfig_data = yaml.safe_load(file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this file always exist? is this the only way to get the namespace?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will need to revisit this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced open(KUBECONFIG) instances with two functions that handle errors: b1365d3

Regarding getting the namespace, I do believe that the namespace stored in the KUBECONFIG file is the source of truth for the namespace for user OR our default namespace which is "warnet" right now. We talked about changing that on the call to "default". I

@pinheadmz
Copy link
Contributor

Still a few mentions git grep kubectl, but mostly just docs (should update) and bash scripts (do we still need?). The port forward command will be replaced by ingress #607

@bdp-DrahtBot
Copy link
Collaborator

bdp-DrahtBot commented Sep 19, 2024

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #621 (Make test_framework available for users when creating scenarios by pinheadmz)
  • #616 (Kubeconfig Step Thru by mplsgrant)
  • #610 (Offer to install helm into venv by mplsgrant)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@mplsgrant
Copy link
Collaborator Author

Am I correct in thinking that all users may still require kubectl to be installed, so that the cluster can be configured? Or does the python lib bypass all that too?

@willcl-ark The python lib bypasses this. It currently relies on KUBECONFIG which is genreally at ~/.kube/config

it seems like there could be alternatives to load_kube_config()

I think the KubeConfigLoader provides access to KUBECONFIG, but it can also do config files in custom locations (maybe in memory, too?). Seems like the google credentials thing might be an easy win to modify KUBECONFIG with the correct google plugin data, but I have not had a chance to look into that yet.

Users may also want kubectl for any additional k8s applications, such as k9s

I don't think k9s or ktop requires kubectl, but I do think we should guide users towards downloading it b/c it is so useful.

Regarding setting the kubeconfig file, right now we rely on minikube to do that. I have also updated auth to create a KUBECONFIG file from a credential file if the user does not have an existing one: c666480

@mplsgrant
Copy link
Collaborator Author

It appears that signet remains an issue. Will need to revisit.

@mplsgrant mplsgrant force-pushed the 2024-09-swap-out-kubectl branch from c666480 to 576c86d Compare September 20, 2024 15:01
@mplsgrant
Copy link
Collaborator Author

It appears that signet remains an issue. Will need to revisit.

I fixed up the signet test issue: 02562b2

It uses shlex to split up lines. Allegedly, shlex is command-line-escape-character aware.

Also, provide some logic around overwriting existing entries and also provide a warning when the
namepsace is not set.
We need to specify a container name when we have prometheus logging enabled b/c we end up with a
couple containers per pod.
I went with the _preload_content=False approach
Instead rely on the position of the container.

Position Rule: we get logs from the first container as reported by V1Pod's spec.container metadata.
shlex appears to handle escape sequences nicely.
I figure we should add an error type for the k8s file, that way we can try...except everything from
that file using just one error type.
Incorporates better error handling
This use of get_default_namespace relies on the brittle idea that get_pods will always use
get_default_namespace. Currently, get_pods currently does, in fact, use get_default_namespace.
However, I don't like that we use get_default_namespace out of band relative to get_pods. I'd prefer
that we do not mention namespaces at all in our message to users unless we can prove it.
@mplsgrant mplsgrant force-pushed the 2024-09-swap-out-kubectl branch from f15fb2f to fcc9ed8 Compare October 1, 2024 20:23
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

Successfully merging this pull request may close these issues.

4 participants