Skip to content

Commit

Permalink
implement disable_pod_logs (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
apesternikov authored May 3, 2024
1 parent f9da828 commit 26af984
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
24 changes: 21 additions & 3 deletions skylib/k8s.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,18 @@ def _k8s_test_setup_impl(ctx):

files.append(ctx.executable._template_engine)

sidecar_args = []
if ctx.attr.setup_timeout:
sidecar_args.append("-timeout=%s" % ctx.attr.setup_timeout)
for service in ctx.attr.portforward_services:
sidecar_args.append("--portforward=%s" % service)
for app in ctx.attr.wait_for_apps:
sidecar_args.append("--waitforapp=%s" % app)
if ctx.attr.allow_errors:
sidecar_args.append("--allow_errors")
if ctx.attr.disable_pod_logs:
sidecar_args.append("--disable_pod_logs")

# create namespace script
ctx.actions.expand_template(
template = ctx.file._namespace_template,
Expand All @@ -515,13 +527,11 @@ def _k8s_test_setup_impl(ctx):
"%{cluster}": ctx.file.cluster.path,
"%{kubeconfig}": ctx.file.kubeconfig.path,
"%{kubectl}": ctx.file.kubectl.path,
"%{portforwards}": " ".join(["-portforward=" + p for p in ctx.attr.portforward_services]),
"%{push_statements}": push_statements,
"%{set_namespace}": ctx.executable._set_namespace.short_path,
"%{it_manifest_filter}": ctx.executable._it_manifest_filter.short_path,
"%{statements}": "\n".join(commands),
"%{test_timeout}": ctx.attr.setup_timeout,
"%{waitforapps}": " ".join(["-waitforapp=" + p for p in ctx.attr.wait_for_apps]),
"%{sidecar_args}": " ".join(sidecar_args),
},
output = ctx.outputs.executable,
)
Expand Down Expand Up @@ -555,6 +565,14 @@ k8s_test_setup = rule(
"portforward_services": attr.string_list(),
"setup_timeout": attr.string(default = "10m"),
"wait_for_apps": attr.string_list(),
"allow_errors": attr.bool(
default = False,
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.",
),
"disable_pod_logs": attr.bool(
default = False,
doc = "If true, the test will not collect logs from pods.",
),
"cluster": attr.label(
#default = Label("@k8s_test//:cluster"),
allow_single_file = True,
Expand Down
2 changes: 1 addition & 1 deletion skylib/k8s_test_namespace.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ function waitpids() {
# create k8s objects
%{statements}

%{it_sidecar} -namespace=${NAMESPACE} -timeout=%{test_timeout} %{portforwards} %{waitforapps} ${DELETE_NAMESPACE_FLAG} "$@"
%{it_sidecar} -namespace=${NAMESPACE} %{sidecar_args} ${DELETE_NAMESPACE_FLAG} "$@"
4 changes: 3 additions & 1 deletion testing/it_sidecar/it_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ var (
kubeconfig string
waitForApps arrayFlags
allowErrors bool
disablePodLogs bool
)

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

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

go func() {
err := stern.Run(ctx, *namespace, clientset, allowErrors)
err := stern.Run(ctx, *namespace, clientset, allowErrors, disablePodLogs)
if err != nil {
log.Print(err)
}
Expand Down
8 changes: 7 additions & 1 deletion testing/it_sidecar/stern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ import (
)

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

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

err := Watch(ctx, clientset.CoreV1().Pods(namespace), RUNNING, labels.Everything(), allowErrors, func(p *Target) {
if disablePodLogs {
return
}
id := p.GetID()
if tails[id] != nil {
return
Expand All @@ -37,6 +40,9 @@ func Run(ctx context.Context, namespace string, clientset *kubernetes.Clientset,
tails[id] = tail
tail.Start(clientset.CoreV1().Pods(p.Namespace))
}, func(p *Target) {
if disablePodLogs {
return
}
id := p.GetID()
if tails[id] == nil {
return
Expand Down

0 comments on commit 26af984

Please sign in to comment.