Adding support for namespace when collecting logs from Kubernetes deployment

If tunnel is deployed in K8s other than default namespace, then this will allow to specify namespace option to collect logs from.
This commit is contained in:
Nirav Chotai 2025-05-07 09:58:52 +02:00
parent 40dc601e9d
commit 9a4d3bdb8e
3 changed files with 29 additions and 7 deletions

View File

@ -50,6 +50,7 @@ const (
noDiagNetworkFlagName = "no-diag-network" noDiagNetworkFlagName = "no-diag-network"
diagContainerIDFlagName = "diag-container-id" diagContainerIDFlagName = "diag-container-id"
diagPodFlagName = "diag-pod-id" diagPodFlagName = "diag-pod-id"
diagNamespaceFlagName = "diag-namespace-id"
LogFieldTunnelID = "tunnelID" LogFieldTunnelID = "tunnelID"
) )
@ -211,6 +212,11 @@ var (
Usage: "Kubernetes POD to collect logs from", Usage: "Kubernetes POD to collect logs from",
Value: "", Value: "",
} }
diagNamespaceFlagName = &cli.StringFlag{
Name: diagNamespaceFlagName,
Usage: "Kubernetes Namespace to collect logs from",
Value: "",
}
noDiagLogsFlag = &cli.BoolFlag{ noDiagLogsFlag = &cli.BoolFlag{
Name: noDiagLogsFlagName, Name: noDiagLogsFlagName,
Usage: "Log collection will not be performed", Usage: "Log collection will not be performed",
@ -1099,6 +1105,7 @@ func diagCommand(ctx *cli.Context) error {
Address: sctx.c.String(flags.Metrics), Address: sctx.c.String(flags.Metrics),
ContainerID: sctx.c.String(diagContainerIDFlagName), ContainerID: sctx.c.String(diagContainerIDFlagName),
PodID: sctx.c.String(diagPodFlagName), PodID: sctx.c.String(diagPodFlagName),
NamespaceID: sctx.c.String(diagNamespaceFlagName),
Toggles: diagnostic.Toggles{ Toggles: diagnostic.Toggles{
NoDiagLogs: sctx.c.Bool(noDiagLogsFlagName), NoDiagLogs: sctx.c.Bool(noDiagLogsFlagName),
NoDiagMetrics: sctx.c.Bool(noDiagMetricsFlagName), NoDiagMetrics: sctx.c.Bool(noDiagMetricsFlagName),
@ -1130,7 +1137,7 @@ func diagCommand(ctx *cli.Context) error {
} }
if errors.Is(err, diagnostic.ErrLogConfigurationIsInvalid) { if errors.Is(err, diagnostic.ErrLogConfigurationIsInvalid) {
log.Info().Msg("Couldn't extract logs from the instance. If the instance is running in a containerized environment use the option --diag-container-id or --diag-pod-id. If there is no logging configuration use --no-diag-logs.") log.Info().Msg("Couldn't extract logs from the instance. If the instance is running in a containerized environment use the option --diag-container-id, --diag-pod-id or --diag-namespace-id. If there is no logging configuration use --no-diag-logs.")
} }
if err != nil { if err != nil {

View File

@ -92,17 +92,18 @@ type Options struct {
Address string Address string
ContainerID string ContainerID string
PodID string PodID string
NamespaceID string `default: "default"`
Toggles Toggles Toggles Toggles
} }
func collectLogs( func collectLogs(
ctx context.Context, ctx context.Context,
client HTTPClient, client HTTPClient,
diagContainer, diagPod string, diagContainer, diagPod string, diagNamespace string
) (string, error) { ) (string, error) {
var collector LogCollector var collector LogCollector
if diagPod != "" { if diagPod != "" {
collector = NewKubernetesLogCollector(diagContainer, diagPod) collector = NewKubernetesLogCollector(diagContainer, diagPod, diagNamespace)
} else if diagContainer != "" { } else if diagContainer != "" {
collector = NewDockerLogCollector(diagContainer) collector = NewDockerLogCollector(diagContainer)
} else { } else {
@ -370,6 +371,7 @@ func createJobs(
tunnel *TunnelState, tunnel *TunnelState,
diagContainer string, diagContainer string,
diagPod string, diagPod string,
diagNamespace string,
noDiagSystem bool, noDiagSystem bool,
noDiagRuntime bool, noDiagRuntime bool,
noDiagMetrics bool, noDiagMetrics bool,
@ -406,7 +408,7 @@ func createJobs(
{ {
jobName: logInformationJobName, jobName: logInformationJobName,
fn: func(ctx context.Context) (string, error) { fn: func(ctx context.Context) (string, error) {
return collectLogs(ctx, client, diagContainer, diagPod) return collectLogs(ctx, client, diagContainer, diagPod, diagNamespace)
}, },
bypass: noDiagLogs, bypass: noDiagLogs,
}, },
@ -524,6 +526,7 @@ func RunDiagnostic(
tunnel, tunnel,
options.ContainerID, options.ContainerID,
options.PodID, options.PodID,
options.NamespaceID,
options.Toggles.NoDiagSystem, options.Toggles.NoDiagSystem,
options.Toggles.NoDiagRuntime, options.Toggles.NoDiagRuntime,
options.Toggles.NoDiagMetrics, options.Toggles.NoDiagMetrics,

View File

@ -12,12 +12,20 @@ import (
type KubernetesLogCollector struct { type KubernetesLogCollector struct {
containerID string // This member identifies the container by identifier or name containerID string // This member identifies the container by identifier or name
pod string // This member identifies the pod where the container is deployed pod string // This member identifies the pod where the container is deployed
namespace string // This member identifies the namespace where the pod is deployed
}
func NewKubernetesLogCollector(containerID, pod string, namespace ...string) *KubernetesLogCollector {
ns := "default"
if len(namespace) > 0 && namespace[0] != "" {
ns = namespace[0]
} }
func NewKubernetesLogCollector(containerID, pod string) *KubernetesLogCollector {
return &KubernetesLogCollector{ return &KubernetesLogCollector{
containerID, containerID: containerID,
pod, pod: pod,
namespace: ns
} }
} }
@ -38,6 +46,8 @@ func (collector *KubernetesLogCollector) Collect(ctx context.Context) (*LogInfor
ctx, ctx,
"kubectl", "kubectl",
"logs", "logs",
"-n",
collector.namespace,
collector.pod, collector.pod,
"--since-time", "--since-time",
since, since,
@ -51,6 +61,8 @@ func (collector *KubernetesLogCollector) Collect(ctx context.Context) (*LogInfor
ctx, ctx,
"kubectl", "kubectl",
"logs", "logs",
"-n",
collector.namespace,
collector.pod, collector.pod,
"--since-time", "--since-time",
since, since,