Skip to content
Open
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
9 changes: 8 additions & 1 deletion cmd/cloudflared/tunnel/subcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
noDiagNetworkFlagName = "no-diag-network"
diagContainerIDFlagName = "diag-container-id"
diagPodFlagName = "diag-pod-id"
diagNamespaceFlagName = "diag-namespace-id"

LogFieldTunnelID = "tunnelID"
)
Expand Down Expand Up @@ -211,6 +212,11 @@ var (
Usage: "Kubernetes POD to collect logs from",
Value: "",
}
diagNamespaceFlagName = &cli.StringFlag{
Name: diagNamespaceFlagName,
Usage: "Kubernetes Namespace to collect logs from",
Value: "",
}
noDiagLogsFlag = &cli.BoolFlag{
Name: noDiagLogsFlagName,
Usage: "Log collection will not be performed",
Expand Down Expand Up @@ -1099,6 +1105,7 @@ func diagCommand(ctx *cli.Context) error {
Address: sctx.c.String(flags.Metrics),
ContainerID: sctx.c.String(diagContainerIDFlagName),
PodID: sctx.c.String(diagPodFlagName),
NamespaceID: sctx.c.String(diagNamespaceFlagName),
Toggles: diagnostic.Toggles{
NoDiagLogs: sctx.c.Bool(noDiagLogsFlagName),
NoDiagMetrics: sctx.c.Bool(noDiagMetricsFlagName),
Expand Down Expand Up @@ -1130,7 +1137,7 @@ func diagCommand(ctx *cli.Context) error {
}

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 {
Expand Down
9 changes: 6 additions & 3 deletions diagnostic/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,18 @@ type Options struct {
Address string
ContainerID string
PodID string
NamespaceID string `default: "default"`
Toggles Toggles
}

func collectLogs(
ctx context.Context,
client HTTPClient,
diagContainer, diagPod string,
diagContainer, diagPod string, diagNamespace string
) (string, error) {
var collector LogCollector
if diagPod != "" {
collector = NewKubernetesLogCollector(diagContainer, diagPod)
collector = NewKubernetesLogCollector(diagContainer, diagPod, diagNamespace)
} else if diagContainer != "" {
collector = NewDockerLogCollector(diagContainer)
} else {
Expand Down Expand Up @@ -370,6 +371,7 @@ func createJobs(
tunnel *TunnelState,
diagContainer string,
diagPod string,
diagNamespace string,
noDiagSystem bool,
noDiagRuntime bool,
noDiagMetrics bool,
Expand Down Expand Up @@ -406,7 +408,7 @@ func createJobs(
{
jobName: logInformationJobName,
fn: func(ctx context.Context) (string, error) {
return collectLogs(ctx, client, diagContainer, diagPod)
return collectLogs(ctx, client, diagContainer, diagPod, diagNamespace)
},
bypass: noDiagLogs,
},
Expand Down Expand Up @@ -524,6 +526,7 @@ func RunDiagnostic(
tunnel,
options.ContainerID,
options.PodID,
options.NamespaceID,
options.Toggles.NoDiagSystem,
options.Toggles.NoDiagRuntime,
options.Toggles.NoDiagMetrics,
Expand Down
18 changes: 15 additions & 3 deletions diagnostic/log_collector_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ import (
type KubernetesLogCollector struct {
containerID string // This member identifies the container by identifier or name
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) *KubernetesLogCollector {
func NewKubernetesLogCollector(containerID, pod string, namespace ...string) *KubernetesLogCollector {
ns := "default"

if len(namespace) > 0 && namespace[0] != "" {
ns = namespace[0]
}

return &KubernetesLogCollector{
containerID,
pod,
containerID: containerID,
pod: pod,
namespace: ns
}
}

Expand All @@ -38,6 +46,8 @@ func (collector *KubernetesLogCollector) Collect(ctx context.Context) (*LogInfor
ctx,
"kubectl",
"logs",
"-n",
collector.namespace,
collector.pod,
"--since-time",
since,
Expand All @@ -51,6 +61,8 @@ func (collector *KubernetesLogCollector) Collect(ctx context.Context) (*LogInfor
ctx,
"kubectl",
"logs",
"-n",
collector.namespace,
collector.pod,
"--since-time",
since,
Expand Down