diff --git a/skylib/k8s.bzl b/skylib/k8s.bzl index 7a60a1c0..847b52cf 100644 --- a/skylib/k8s.bzl +++ b/skylib/k8s.bzl @@ -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, @@ -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, ) @@ -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, diff --git a/skylib/k8s_test_namespace.sh.tpl b/skylib/k8s_test_namespace.sh.tpl index 05ad41e1..378d1ab7 100644 --- a/skylib/k8s_test_namespace.sh.tpl +++ b/skylib/k8s_test_namespace.sh.tpl @@ -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} "$@" diff --git a/testing/it_sidecar/it_sidecar.go b/testing/it_sidecar/it_sidecar.go index 60a69bb4..814540c2 100644 --- a/testing/it_sidecar/it_sidecar.go +++ b/testing/it_sidecar/it_sidecar.go @@ -73,6 +73,7 @@ var ( kubeconfig string waitForApps arrayFlags allowErrors bool + disablePodLogs bool ) func init() { @@ -80,6 +81,7 @@ func init() { flag.StringVar(&kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"), "path to kubernetes config file") flag.Var(&waitForApps, "waitforapp", "wait for pods with label app=") 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 @@ -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) } diff --git a/testing/it_sidecar/stern/main.go b/testing/it_sidecar/stern/main.go index 0856c774..33e15c17 100644 --- a/testing/it_sidecar/stern/main.go +++ b/testing/it_sidecar/stern/main.go @@ -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 @@ -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