|
| 1 | +# Copyright © 2026 Bozeman Pass, Inc. |
| 2 | + |
| 3 | +# This program is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU Affero General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | + |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU Affero General Public License for more details. |
| 12 | + |
| 13 | +# You should have received a copy of the GNU Affero General Public License |
| 14 | +# along with this program. If not, see <http:#www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +import yaml |
| 17 | + |
| 18 | +from kubernetes import client |
| 19 | + |
| 20 | +from stack.deploy.k8s.deploy_k8s import K8sDeployer |
| 21 | +from stack.log import output_main |
| 22 | +from stack.util import error_exit |
| 23 | + |
| 24 | + |
| 25 | +def _k8s_object_to_yaml(obj): |
| 26 | + api_client = client.ApiClient() |
| 27 | + sanitized = api_client.sanitize_for_serialization(obj) |
| 28 | + return yaml.dump(sanitized, default_flow_style=False, sort_keys=False) |
| 29 | + |
| 30 | + |
| 31 | +def _print_section(title, objects): |
| 32 | + if not objects: |
| 33 | + return |
| 34 | + output_main(f"--- {title} ---") |
| 35 | + for obj in objects: |
| 36 | + output_main(_k8s_object_to_yaml(obj)) |
| 37 | + |
| 38 | + |
| 39 | +def explain_op(ctx): |
| 40 | + deployer = ctx.obj.deployer |
| 41 | + |
| 42 | + if not isinstance(deployer, K8sDeployer): |
| 43 | + error_exit("The explain command is currently only supported for k8s deployments") |
| 44 | + |
| 45 | + cluster_info = deployer.cluster_info |
| 46 | + is_kind = deployer.is_kind() |
| 47 | + |
| 48 | + pvs = cluster_info.get_pvs() |
| 49 | + _print_section("PersistentVolumes", pvs) |
| 50 | + |
| 51 | + pvcs = cluster_info.get_pvcs() |
| 52 | + _print_section("PersistentVolumeClaims", pvcs) |
| 53 | + |
| 54 | + config_maps = cluster_info.get_configmaps() |
| 55 | + _print_section("ConfigMaps", config_maps) |
| 56 | + |
| 57 | + deployments = cluster_info.get_deployments(image_pull_policy=None if is_kind else "Always") |
| 58 | + _print_section("Deployments", deployments) |
| 59 | + |
| 60 | + services = cluster_info.get_services() |
| 61 | + _print_section("Services", services) |
| 62 | + |
| 63 | + http_proxy_info = cluster_info.spec.get_http_proxy() |
| 64 | + use_tls = http_proxy_info and not is_kind |
| 65 | + ingress = cluster_info.get_ingress(use_tls=use_tls) |
| 66 | + if ingress: |
| 67 | + _print_section("Ingress", [ingress]) |
0 commit comments