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

drenv: Improve gather logs #1748

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/user-quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,17 @@ enough resources:
1. Install the `kubectl-gather` plugin

```
curl -L -o kubectl-gather https://github.com/nirs/kubectl-gather/releases/download/v0.4.1/kubectl-gather-v0.4.1-linux-amd64
tag="$(curl -fsSL https://api.github.com/repos/nirs/kubectl-gather/releases/latest | jq -r .tag_name)"
os="$(uname | tr '[:upper:]' '[:lower:]')"
machine="$(uname -m)"
if [ "$machine" = "aarch64" ]; then machine="arm64"; fi
if [ "$machine" = "x86_64" ]; then machine="amd64"; fi
curl -L -o kubectl-gather https://github.com/nirs/kubectl-gather/releases/download/$tag/kubectl-gather-$tag-$os-$machine
sudo install kubectl-gather /usr/local/bin
rm kubectl-gather
```

kubectl-gather version 0.6.0 or later is required.
For more info see [kubectl-gather](https://github.com/nirs/kubectl-gather)

1. Install `helm` tool - on Fedora you can use:
Expand Down
8 changes: 6 additions & 2 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ environment.
1. Install the `kubectl-gather` plugin

```
curl -L -o kubectl-gather https://github.com/nirs/kubectl-gather/releases/download/v0.5.1/kubectl-gather-v0.5.1-linux-amd64
tag="$(curl -fsSL https://api.github.com/repos/nirs/kubectl-gather/releases/latest | jq -r .tag_name)"
curl -L -o kubectl-gather https://github.com/nirs/kubectl-gather/releases/download/$tag/kubectl-gather-$tag-linux-amd64
sudo install kubectl-gather /usr/local/bin
rm kubectl-gather
```

kubectl-gather version 0.6.0 or later is required.
For more info see [kubectl-gather](https://github.com/nirs/kubectl-gather)

## Setup on macOS
Expand Down Expand Up @@ -158,11 +160,13 @@ environment.
1. Install the `kubectl-gather` plugin

```
curl -L -o kubectl-gather https://github.com/nirs/kubectl-gather/releases/download/v0.5.1/kubectl-gather-v0.5.1-darwin-arm64
tag="$(curl -fsSL https://api.github.com/repos/nirs/kubectl-gather/releases/latest | jq -r .tag_name)"
curl -L -o kubectl-gather https://github.com/nirs/kubectl-gather/releases/download/$tag/kubectl-gather-$tag-darwin-arm64
sudo install kubectl-gather /usr/local/bin
rm kubectl-gather
```

kubectl-gather version 0.6.0 or later is required.
For more info see [kubectl-gather](https://github.com/nirs/kubectl-gather)

1. Install `socket_vmnet`
Expand Down
2 changes: 2 additions & 0 deletions test/drenv/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ def do_gather(args):
[p["name"] for p in env["profiles"]],
directory=args.directory,
namespaces=args.namespaces,
name=env["name"],
verbose=args.verbose,
)
logging.info(
"[%s] Environment gathered in %.2f seconds",
Expand Down
30 changes: 26 additions & 4 deletions test/drenv/kubectl.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# SPDX-FileCopyrightText: The RamenDR authors
# SPDX-License-Identifier: Apache-2.0

import logging
import subprocess

from . import commands
from . import zap

JSONPATH_NEWLINE = '{"\\n"}'

Expand Down Expand Up @@ -188,16 +192,34 @@ def watch(
return commands.watch(*cmd, timeout=timeout)


def gather(contexts, namespaces=None, directory=None):
def gather(contexts, namespaces=None, directory=None, name="gather", verbose=False):
"""
Run kubectl gather plugin.
Run kubectl gather plugin, logging gather logs.
"""
cmd = ["kubectl", "gather", "--contexts", ",".join(contexts)]
cmd = [
"kubectl",
"gather",
"--log-format",
"json",
"--contexts",
",".join(contexts),
]
if namespaces:
cmd.extend(("--namespaces", ",".join(namespaces)))
if directory:
cmd.extend(("--directory", directory))
commands.run(*cmd)
if verbose:
cmd.append("--verbose")

# Redirecting stderr to stdout to get the logs. kubectl-gather does not
# output anything to stdout.
for line in commands.watch(*cmd, stderr=subprocess.STDOUT):
try:
zap.log_json(line, name=name)
except ValueError:
# We don't want to crash if kubectl-gather has a logging bug, and
# the line may contain useful info.
logging.debug("[%s] %s", name, line)


def _run(cmd, *args, env=None, context=None):
Expand Down
30 changes: 30 additions & 0 deletions test/drenv/zap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-FileCopyrightText: The RamenDR authors
# SPDX-License-Identifier: Apache-2.0

import json
import logging

# Map zap log levels to python log levels.
# https://github.com/uber-go/zap/blob/6d482535bdd97f4d97b2f9573ac308f1cf9b574e/zapcore/level.go#L113
_LOGGERS = {
"debug": logging.debug,
"info": logging.info,
"warn": logging.warn,
"error": logging.error,
"dpanic": logging.error,
"panic": logging.error,
"fatal": logging.error,
}


def log_json(line, name="zap"):
info = json.loads(line)
info.pop("ts", None)
msg = info.pop("msg", None)
level = info.pop("level", None)
logger = info.pop("logger", name)
log = _LOGGERS.get(level.lower(), logging.info)
if info:
log("[%s] %s %s", logger, msg, info)
else:
log("[%s] %s", logger, msg)