From 4cc7b14faca6df1bf0e050e34e31391080eb3ab1 Mon Sep 17 00:00:00 2001 From: Tomas Nozicka Date: Tue, 3 Dec 2024 12:28:47 +0100 Subject: [PATCH] Propagate operator loglevel --- pkg/cmdutil/helpers.go | 29 +++++++++++++++---- pkg/controller/nodeconfig/resource.go | 3 +- pkg/controller/nodetune/resource.go | 3 +- .../scylladbdatacenter/init_test.go | 15 ++++++++++ pkg/controller/scylladbdatacenter/resource.go | 7 +++-- .../scylladbdatacenter/resource_test.go | 6 ++-- 6 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 pkg/controller/scylladbdatacenter/init_test.go diff --git a/pkg/cmdutil/helpers.go b/pkg/cmdutil/helpers.go index c3b89133140..fe6902c5f4c 100644 --- a/pkg/cmdutil/helpers.go +++ b/pkg/cmdutil/helpers.go @@ -3,14 +3,17 @@ package cmdutil import ( + "errors" "flag" "fmt" "os" + "strconv" "strings" + "github.com/scylladb/scylla-operator/pkg/helpers" "github.com/spf13/cobra" "github.com/spf13/pflag" - "k8s.io/apimachinery/pkg/util/errors" + utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/klog/v2" ) @@ -55,7 +58,7 @@ func ReadFlagsFromEnv(prefix string, cmd *cobra.Command) error { return }) - return errors.NewAggregate(errs) + return utilerrors.NewAggregate(errs) } func InstallKlog(cmd *cobra.Command) { @@ -74,11 +77,25 @@ func InstallKlog(cmd *cobra.Command) { } } -func GetLoglevel() string { +func getLoglevelOrDefault() (int, error) { f := flag.CommandLine.Lookup("v") - if f != nil { - return f.Value.String() + if f == nil { + return 0, errors.New(`can't lookup klog "v" flag`) } - return "" + s := f.Value.String() + if len(s) == 0 { + return 0, nil + } + + loglevel, err := strconv.Atoi(s) + if err != nil { + panic(fmt.Errorf("can't parse log level %q: %q", s, err)) + } + + return loglevel, nil +} + +func GetLoglevelOrDefaultOrDie() int { + return helpers.Must(getLoglevelOrDefault()) } diff --git a/pkg/controller/nodeconfig/resource.go b/pkg/controller/nodeconfig/resource.go index a0c16a1895f..4199e7185a0 100644 --- a/pkg/controller/nodeconfig/resource.go +++ b/pkg/controller/nodeconfig/resource.go @@ -6,6 +6,7 @@ import ( "fmt" scyllav1alpha1 "github.com/scylladb/scylla-operator/pkg/api/scylla/v1alpha1" + "github.com/scylladb/scylla-operator/pkg/cmdutil" "github.com/scylladb/scylla-operator/pkg/naming" "github.com/scylladb/scylla-operator/pkg/pointer" appsv1 "k8s.io/api/apps/v1" @@ -370,7 +371,7 @@ exec chroot ./ /scylla-operator/usr/bin/scylla-operator node-setup-daemon \ --scylla-image=` + fmt.Sprintf("%q", scyllaImage) + ` \ --operator-image=` + fmt.Sprintf("%q", operatorImage) + ` \ --disable-optimizations=` + fmt.Sprintf("%t", nc.Spec.DisableOptimizations) + ` \ ---loglevel=` + fmt.Sprintf("%d", 4) + ` +` + fmt.Sprintf("--loglevel=%d", cmdutil.GetLoglevelOrDefaultOrDie()) + ` `}, Env: []corev1.EnvVar{ { diff --git a/pkg/controller/nodetune/resource.go b/pkg/controller/nodetune/resource.go index 1ca35bb3009..7f809da25dd 100644 --- a/pkg/controller/nodetune/resource.go +++ b/pkg/controller/nodetune/resource.go @@ -7,6 +7,7 @@ import ( "os" "path" + "github.com/scylladb/scylla-operator/pkg/cmdutil" "github.com/scylladb/scylla-operator/pkg/controllerhelpers" "github.com/scylladb/scylla-operator/pkg/naming" "github.com/scylladb/scylla-operator/pkg/pointer" @@ -157,7 +158,7 @@ func makeRlimitsJobForContainer(controllerRef *metav1.OwnerReference, namespace, "/usr/bin/scylla-operator", "rlimits-job", fmt.Sprintf("--pid=%d", scyllaHostPID), - "--loglevel=2", + fmt.Sprintf("--loglevel=%d", cmdutil.GetLoglevelOrDefaultOrDie()), }, SecurityContext: &corev1.SecurityContext{ Privileged: pointer.Ptr(true), diff --git a/pkg/controller/scylladbdatacenter/init_test.go b/pkg/controller/scylladbdatacenter/init_test.go new file mode 100644 index 00000000000..e80ea8fba13 --- /dev/null +++ b/pkg/controller/scylladbdatacenter/init_test.go @@ -0,0 +1,15 @@ +package scylladbdatacenter + +import ( + "flag" + + "k8s.io/klog/v2" +) + +func init() { + klog.InitFlags(flag.CommandLine) + err := flag.Set("alsologtostderr", "true") + if err != nil { + panic(err) + } +} diff --git a/pkg/controller/scylladbdatacenter/resource.go b/pkg/controller/scylladbdatacenter/resource.go index 7b7f02f478c..68ebb728fd6 100644 --- a/pkg/controller/scylladbdatacenter/resource.go +++ b/pkg/controller/scylladbdatacenter/resource.go @@ -12,6 +12,7 @@ import ( scylladbassets "github.com/scylladb/scylla-operator/assets/scylladb" scyllav1 "github.com/scylladb/scylla-operator/pkg/api/scylla/v1" scyllav1alpha1 "github.com/scylladb/scylla-operator/pkg/api/scylla/v1alpha1" + "github.com/scylladb/scylla-operator/pkg/cmdutil" "github.com/scylladb/scylla-operator/pkg/controllerhelpers" "github.com/scylladb/scylla-operator/pkg/features" "github.com/scylladb/scylla-operator/pkg/helpers" @@ -664,7 +665,7 @@ exec /mnt/shared/scylla-operator sidecar \ }() + ` \ --service-name=$(SERVICE_NAME) \ --cpu-count=$(CPU_COUNT) \ ---loglevel=2 \ +` + fmt.Sprintf("--loglevel=%d", cmdutil.GetLoglevelOrDefaultOrDie()) + ` \ ` + func() string { var optionalArgs []string @@ -855,7 +856,7 @@ wait "scylladb-api-status", fmt.Sprintf("--port=%d", naming.ScyllaDBAPIStatusProbePort), "--service-name=$(SERVICE_NAME)", - "--loglevel=2", + fmt.Sprintf("--loglevel=%d", cmdutil.GetLoglevelOrDefaultOrDie()), }, Env: []corev1.EnvVar{ { @@ -908,7 +909,7 @@ wait } return scyllav1alpha1.BroadcastAddressTypeServiceClusterIP }()), - "--loglevel=2", + fmt.Sprintf("--loglevel=%d", cmdutil.GetLoglevelOrDefaultOrDie()), }, Env: []corev1.EnvVar{ { diff --git a/pkg/controller/scylladbdatacenter/resource_test.go b/pkg/controller/scylladbdatacenter/resource_test.go index afa71c0197b..bfb586bb0ad 100644 --- a/pkg/controller/scylladbdatacenter/resource_test.go +++ b/pkg/controller/scylladbdatacenter/resource_test.go @@ -743,7 +743,7 @@ exec /mnt/shared/scylla-operator sidecar \ --clients-broadcast-address-type=ServiceClusterIP \ --service-name=$(SERVICE_NAME) \ --cpu-count=$(CPU_COUNT) \ ---loglevel=2 \ +--loglevel=0 \ -- "$@" `), "--", @@ -895,7 +895,7 @@ wait`, "scylladb-api-status", "--port=8080", "--service-name=$(SERVICE_NAME)", - "--loglevel=2", + "--loglevel=0", }, Env: []corev1.EnvVar{ { @@ -938,7 +938,7 @@ wait`, "--service-name=$(SERVICE_NAME)", "--nodes-broadcast-address-type=ServiceClusterIP", "--clients-broadcast-address-type=ServiceClusterIP", - "--loglevel=2", + "--loglevel=0", }, Env: []corev1.EnvVar{ {