Pyjoyment.Command.Author.generate.kubernetes - Kubernetes generator command

Usage: APPLICATION generate kubernetes [OPTIONS] [-- SERVER OPTIONS]

  ./myapp.py generate kubernetes
  python -m MyApp generate kubernetes
  python -m MyApp generate kubernetes --image ghcr.io/me/myapp:1.0
  python -m MyApp generate kubernetes --host-path
  python -m MyApp generate kubernetes --graceful-timeout 25
  python -m MyApp generate kubernetes --grace-period 30
  python -m MyApp generate kubernetes --liveness-probe 3000
  python -m MyApp generate kubernetes --shutdown-delay 10
  python -m MyApp generate kubernetes --no-dev
  python -m MyApp generate kubernetes --server prefork -- --workers 2

Options:
      --dev                          Install the dependency group "dev" of
                                     the project in the pod of "dev", which
                                     --no-dev leaves out, defaults to
                                     installing it
      --grace-period <seconds>       terminationGracePeriodSeconds of "prod",
                                     whose graceful timeout is what the
                                     shutdown delay and 5 seconds leave
  -G, --graceful-timeout <seconds>   Graceful timeout of the workers of the
                                     server of "prod", defaults to the one of
                                     the server
  -h, --help                         Show this summary of available options
      --host-path                    Mount the directory of the project in
                                     the pod of "dev", in the place of a
                                     ConfigMap
  -i, --image <name>                 Image of "prod", the one of "generate
                                     dockerfile", defaults to the name of the
                                     application
      --liveness-probe <port|path>   Liveness probe on the port, or of the
                                     path, defaults to the liveness of the
                                     plugin Health, or the port without it
  -p, --port <port>                  Port that the server listens on,
                                     defaults to 3000
  -S, --shutdown-delay <seconds>     Time that the server of "prod" keeps
                                     serving after TERM, defaults to 5
  -s, --server <name>                Server of "dev", "daemon", "prefork" or
                                     "reload", defaults to "reload" with
                                     --host-path or "daemon"

Pyjoyment.Command.Author.generate.kubernetes generates the manifests of an application for Kubernetes, as Kustomize has them, in kubernetes: a base, with a Deployment and a Service of the name of the application, and two overlays, dev and prod, which kubectl apply -k applies.

kubernetes/
  base/
    kustomization.yaml
    deployment.yaml
    probes.yaml
    service.yaml
  dev/
    kustomization.yaml
    deployment.yaml
    files/
  prod/
    kustomization.yaml
    deployment.yaml

The server listens on the port of the PORT environment variable, 3000 by default, as in the image of Pyjoyment.Command.Author.generate.dockerfile, which the Service exposes on the port 80. The probes of Kubernetes of an application with Pyjoyment.Plugin.Health request it: the startup and the liveness its liveness, /livez by default, and the readiness its readiness, /readyz, which fails while the server stops gracefully, so that the Service sends no more requests to the pod before its end. The probes of an application without it connect to the port, which the server listens on once it has started. The probes are a patch of their own, base/probes.yaml, as the application gets the plugin or loses it.

--liveness-probe gives the liveness probe a path of its own, or a port, which the kubelet connects to with tcpSocket. A server whose workers serve one connection at a time, as prefork -c 1 of an application that blocks, keeps a request of a probe in the backlog while every worker is busy, so that a liveness probe of HTTP restarts a pod that is only busy, where the kernel accepts the connection of tcpSocket into the backlog. The startup probe stays the one of the liveness of the application, as a pod gets no requests before it has started, and the readiness probe the one of its readiness.

The overlay prod runs the image of --image, which Pyjoyment.Command.Author.generate.dockerfile builds, with the pre-forking server of the image, in the operating mode production. Kubernetes takes a pod that it stops out of the endpoints of its Service at the same time as it sends it TERM, so the server keeps serving for the 5 seconds of --shutdown-delay, which sets PYJO_SHUTDOWN_DELAY, until the proxies of the cluster know it, and terminationGracePeriodSeconds is 5 seconds above the delay and the 25 seconds that Pyjo.Server.Prefork.Pyjo_Server_Prefork.graceful_timeout gives a worker, or --graceful-timeout, which sets PYJO_GRACEFUL_TIMEOUT, so that the server stops by itself before the kill of Kubernetes. --grace-period gives terminationGracePeriodSeconds instead, as the 30 seconds that Kubernetes gives a pod by default, and the graceful timeout is what the delay and the 5 seconds leave of it, 20 seconds with the delay of 5 seconds. Without --image, the image is the one of the name of the application, as docker build -t myapp . tags it, which a cluster of Docker Desktop or OrbStack takes from Docker.

The overlay dev runs the application in the image of uv with Python, of the version that runs the generator, which installs the dependencies of the application when the pod starts, as the image of Pyjoyment.Command.Author.generate.dockerfile does when it is built, the ones of pyproject.toml or of the inline metadata of PEP 723 of a script, without which the generator fails with the commands that add them, in an emptyDir, with the dependency group dev of a project, which --no-dev leaves out, and runs it in the operating mode development, without building an image, with the server of --server: the command daemon, prefork, or pyjo-reload of Pyjo.Server.Reload, with the options after --. The application is a ConfigMap that configMapGenerator of Kustomize makes of a copy of it in kubernetes/dev/files, which the pod copies, as the files of a ConfigMap are read-only. The application is the files of the current directory, without hidden ones, __pycache__, dist and kubernetes, as for Pyjoyment.Command.Author.generate.lambda_zip, and a ConfigMap holds 1 MiB at most. A new ConfigMap of a changed application has a new name, which rolls the Deployment out. kubernetes/dev/files is a build of the application, as dist of Pyjoyment.Command.Author.generate.lambda_zip, which goes into .gitignore.

With --host-path, the pod of dev mounts the directory of the project in the place of the ConfigMap, as a cluster that sees the disk of the machine, as the one of Docker Desktop or OrbStack, or of kind with extraMounts, mounts it, and the server is pyjo-reload, which restarts the application when a file of it changes, where a ConfigMap changes with a new pod and the server is daemon.

The files of base and prod, which are to be edited, are written once, and the ones of dev and base/probes.yaml anew each time, as the application changes, which a patch of its own in the place of the one of base/probes.yaml in base/kustomization.yaml keeps from changing the probes.

This is a core command, that means it is always enabled and its code a good example for learning to build new commands, you’re welcome to fork it.

See Pyjoyment.Commands for a list of commands that are available by default.

See also

Pyjoyment, https://mojolicious.org.

It has no counterpart in Mojolicious.

Classes

class Pyjoyment.Command.Author.generate.kubernetes.Pyjoyment_Command_Author_generate_kubernetes(**kwargs: Any)[source]

Pyjoyment.Command.Author.generate.kubernetes inherits all attributes and methods from Pyjoyment.Command and implements the following new ones.

description = 'Generate the manifests of Kubernetes'
description = kubernetes.description
kubernetes.description = 'Foo'

Short description of this command, used for the command list.

run(*args: str) → None[source]
kubernetes.run(*sys.argv[1:])

Run this command.

usage = 'Usage: APPLICATION generate kubernetes [OPTIONS] [-- SERVER OPTIONS]\n\n  ./myapp.py generate kubernetes\n  python -m MyApp generate kubernetes\n  python -m MyApp generate kubernetes --image ghcr.io/me/myapp:1.0\n  python -m MyApp generate kubernetes --host-path\n  python -m MyApp generate kubernetes --graceful-timeout 25\n  python -m MyApp generate kubernetes --grace-period 30\n  python -m MyApp generate kubernetes --liveness-probe 3000\n  python -m MyApp generate kubernetes --shutdown-delay 10\n  python -m MyApp generate kubernetes --no-dev\n  python -m MyApp generate kubernetes --server prefork -- --workers 2\n\nOptions:\n      --dev                          Install the dependency group "dev" of\n                                     the project in the pod of "dev", which\n                                     --no-dev leaves out, defaults to\n                                     installing it\n      --grace-period <seconds>       terminationGracePeriodSeconds of "prod",\n                                     whose graceful timeout is what the\n                                     shutdown delay and 5 seconds leave\n  -G, --graceful-timeout <seconds>   Graceful timeout of the workers of the\n                                     server of "prod", defaults to the one of\n                                     the server\n  -h, --help                         Show this summary of available options\n      --host-path                    Mount the directory of the project in\n                                     the pod of "dev", in the place of a\n                                     ConfigMap\n  -i, --image <name>                 Image of "prod", the one of "generate\n                                     dockerfile", defaults to the name of the\n                                     application\n      --liveness-probe <port|path>   Liveness probe on the port, or of the\n                                     path, defaults to the liveness of the\n                                     plugin Health, or the port without it\n  -p, --port <port>                  Port that the server listens on,\n                                     defaults to 3000\n  -S, --shutdown-delay <seconds>     Time that the server of "prod" keeps\n                                     serving after TERM, defaults to 5\n  -s, --server <name>                Server of "dev", "daemon", "prefork" or\n                                     "reload", defaults to "reload" with\n                                     --host-path or "daemon"\n'
usage = kubernetes.usage
kubernetes.usage = 'Foo'

Usage information for this command, used for the help screen.