in troubleshooting/create_snapshot.py [0:0]
def main():
kubeconfig, timeout, bucket = parse_args()
timeout = "{}s".format(timeout)
if kubeconfig:
kubeconfig = \
'--kubeconfig {}'.format(pathlib.Path(kubeconfig).absolute())
namespaces_list = get_kubectl_list('namespaces', kubeconfig, timeout)
with tempfile.TemporaryDirectory() as tmp_dir:
output_dir = pathlib.PosixPath(tmp_dir)
for cmd in KUBECTL_GLOBAL_CMDS:
run_cmd(
cmd.format(kubeconfig_arg=kubeconfig, timeout=timeout),
'global', output_dir
)
for namespace in namespaces_list:
for cmd in KUBECTL_PER_NS_CMDS:
run_cmd(
cmd.format(
kubeconfig_arg=kubeconfig,
timeout=timeout,
namespace=namespace
),
'namespaces/{}'.format(namespace),
output_dir
)
for pod in get_kubectl_list('pods',
kubeconfig, timeout, namespace):
containers = get_kubectl_list('pod',
kubeconfig, timeout,
namespace=namespace,
jsonpath="{.spec.containers[*].name}", # noqa: E501
object_name=pod)
for container in containers:
for cmd in KUBECTL_PER_POD_CMDS:
run_cmd(
cmd.format(kubeconfig_arg=kubeconfig,
timeout=timeout,
namespace=namespace,
pod=pod,
container=container),
'namespaces/{}/{}'.format(namespace, pod),
output_dir
)
# Commands done
snapshot_name = 'snapshot-{}'.format(int(time.time()))
snap_file = tarfile.open('{}.tar.gz'.format(snapshot_name), 'w:gz')
snap_file.add(output_dir, snapshot_name)
snap_file.close()
print("Created snapshot: {}".format(snap_file.name))
if bucket:
upload_file(bucket, snap_file.name)