File: console.py

package info (click to toggle)
python-pykube-ng 22.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: python: 2,336; makefile: 44
file content (59 lines) | stat: -rw-r--r-- 1,720 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import argparse
import code

import pykube

# import readline to support line editing within console session.
try:
    import readline  # noqa: F401
except ImportError:
    pass


def main(argv=None):
    """
    Run the interactive Pykube console (usually invoked via python3 -m pykube)
    """
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--kubeconfig", help="Path to the kubeconfig file to use", metavar="PATH"
    )
    parser.add_argument(
        "--context", help="The name of the kubeconfig context to used", metavar="NAME"
    )
    parser.add_argument(
        "-c", help="Python program passed in as string", metavar="SCRIPT"
    )
    args = parser.parse_args(argv)

    config = pykube.KubeConfig.from_file(args.kubeconfig)

    if args.context:
        config.set_current_context(args.context)

    api = pykube.HTTPClient(config)

    context = {
        "__name__": "__console__",
        "pykube": pykube,
        "config": config,
        "api": api,
    }
    for k, v in vars(pykube).items():
        if k[0] != "_" and k[0] == k[0].upper():
            context[k] = v

    banner = f"""Pykube v{pykube.__version__}, loaded "{config.filepath}" with context "{config.current_context}".

    Example commands:
      [d.name for d in Deployment.objects(api)]              # get names of deployments in default namespace
      list(DaemonSet.objects(api, namespace='kube-system'))  # list daemonsets in "kube-system"
      Pod.objects(api).get_by_name('mypod').labels           # labels of pod "mypod"

    Use Ctrl-D to exit"""

    console = code.InteractiveConsole(locals=context)
    if args.c:
        console.runsource(args.c)
    else:
        console.interact(banner)