Skip to content

Commit 26af984

Browse files
authored
implement disable_pod_logs (#31)
1 parent f9da828 commit 26af984

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

skylib/k8s.bzl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,18 @@ def _k8s_test_setup_impl(ctx):
507507

508508
files.append(ctx.executable._template_engine)
509509

510+
sidecar_args = []
511+
if ctx.attr.setup_timeout:
512+
sidecar_args.append("-timeout=%s" % ctx.attr.setup_timeout)
513+
for service in ctx.attr.portforward_services:
514+
sidecar_args.append("--portforward=%s" % service)
515+
for app in ctx.attr.wait_for_apps:
516+
sidecar_args.append("--waitforapp=%s" % app)
517+
if ctx.attr.allow_errors:
518+
sidecar_args.append("--allow_errors")
519+
if ctx.attr.disable_pod_logs:
520+
sidecar_args.append("--disable_pod_logs")
521+
510522
# create namespace script
511523
ctx.actions.expand_template(
512524
template = ctx.file._namespace_template,
@@ -515,13 +527,11 @@ def _k8s_test_setup_impl(ctx):
515527
"%{cluster}": ctx.file.cluster.path,
516528
"%{kubeconfig}": ctx.file.kubeconfig.path,
517529
"%{kubectl}": ctx.file.kubectl.path,
518-
"%{portforwards}": " ".join(["-portforward=" + p for p in ctx.attr.portforward_services]),
519530
"%{push_statements}": push_statements,
520531
"%{set_namespace}": ctx.executable._set_namespace.short_path,
521532
"%{it_manifest_filter}": ctx.executable._it_manifest_filter.short_path,
522533
"%{statements}": "\n".join(commands),
523-
"%{test_timeout}": ctx.attr.setup_timeout,
524-
"%{waitforapps}": " ".join(["-waitforapp=" + p for p in ctx.attr.wait_for_apps]),
534+
"%{sidecar_args}": " ".join(sidecar_args),
525535
},
526536
output = ctx.outputs.executable,
527537
)
@@ -555,6 +565,14 @@ k8s_test_setup = rule(
555565
"portforward_services": attr.string_list(),
556566
"setup_timeout": attr.string(default = "10m"),
557567
"wait_for_apps": attr.string_list(),
568+
"allow_errors": attr.bool(
569+
default = False,
570+
doc = "If true, the test will ignore any kuberntetes errors. Use only in situations when error is a part of the normal workflow, like crashlooping to wait for dependencies.",
571+
),
572+
"disable_pod_logs": attr.bool(
573+
default = False,
574+
doc = "If true, the test will not collect logs from pods.",
575+
),
558576
"cluster": attr.label(
559577
#default = Label("@k8s_test//:cluster"),
560578
allow_single_file = True,

skylib/k8s_test_namespace.sh.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,4 @@ function waitpids() {
110110
# create k8s objects
111111
%{statements}
112112

113-
%{it_sidecar} -namespace=${NAMESPACE} -timeout=%{test_timeout} %{portforwards} %{waitforapps} ${DELETE_NAMESPACE_FLAG} "$@"
113+
%{it_sidecar} -namespace=${NAMESPACE} %{sidecar_args} ${DELETE_NAMESPACE_FLAG} "$@"

testing/it_sidecar/it_sidecar.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,15 @@ var (
7373
kubeconfig string
7474
waitForApps arrayFlags
7575
allowErrors bool
76+
disablePodLogs bool
7677
)
7778

7879
func init() {
7980
flag.Var(&pfconfig, "portforward", "set a port forward item in form of servicename:port")
8081
flag.StringVar(&kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"), "path to kubernetes config file")
8182
flag.Var(&waitForApps, "waitforapp", "wait for pods with label app=<this parameter>")
8283
flag.BoolVar(&allowErrors, "allow_errors", false, "do not treat Failed in events as error. Use only if crashloop is expected")
84+
flag.BoolVar(&disablePodLogs, "disable_pod_logs", false, "do not forward pod logs")
8385
}
8486

8587
// contains returns true if slice v contains an item
@@ -372,7 +374,7 @@ func main() {
372374
defer cleanup(clientset)
373375

374376
go func() {
375-
err := stern.Run(ctx, *namespace, clientset, allowErrors)
377+
err := stern.Run(ctx, *namespace, clientset, allowErrors, disablePodLogs)
376378
if err != nil {
377379
log.Print(err)
378380
}

testing/it_sidecar/stern/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ import (
2323
)
2424

2525
// Run starts the main run loop
26-
func Run(ctx context.Context, namespace string, clientset *kubernetes.Clientset, allowErrors bool) error {
26+
func Run(ctx context.Context, namespace string, clientset *kubernetes.Clientset, allowErrors, disablePodLogs bool) error {
2727

2828
tails := make(map[string]*Tail)
2929

3030
err := Watch(ctx, clientset.CoreV1().Pods(namespace), RUNNING, labels.Everything(), allowErrors, func(p *Target) {
31+
if disablePodLogs {
32+
return
33+
}
3134
id := p.GetID()
3235
if tails[id] != nil {
3336
return
@@ -37,6 +40,9 @@ func Run(ctx context.Context, namespace string, clientset *kubernetes.Clientset,
3740
tails[id] = tail
3841
tail.Start(clientset.CoreV1().Pods(p.Namespace))
3942
}, func(p *Target) {
43+
if disablePodLogs {
44+
return
45+
}
4046
id := p.GetID()
4147
if tails[id] == nil {
4248
return

0 commit comments

Comments
 (0)