Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maintenance] Extract Debug CLI #1631

Merged
merged 1 commit into from
Mar 25, 2024
Merged
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
48 changes: 19 additions & 29 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const (
defaultServerPort = 8528
defaultAPIHTTPPort = 8628
defaultAPIGRPCPort = 8728
defaultLogLevel = "info"
defaultAdminSecretName = "arangodb-operator-dashboard"
defaultAPIJWTSecretName = "arangodb-operator-api-jwt"
defaultAPIJWTKeySecretName = "arangodb-operator-api-jwt-key"
Expand All @@ -96,9 +95,6 @@ var (
hardLimit uint64
}

logFormat string
logLevels []string
logSampling bool
serverOptions struct {
host string
port int
Expand Down Expand Up @@ -195,9 +191,6 @@ func init() {
f.StringVar(&serverOptions.tlsSecretName, "server.tls-secret-name", "", "Name of secret containing tls.crt & tls.key for HTTPS server (if empty, self-signed certificate is used)")
f.StringVar(&serverOptions.adminSecretName, "server.admin-secret-name", defaultAdminSecretName, "Name of secret containing username + password for login to the dashboard")
f.BoolVar(&serverOptions.allowAnonymous, "server.allow-anonymous-access", false, "Allow anonymous access to the dashboard")
f.StringVar(&logFormat, "log.format", "pretty", "Set log format. Allowed values: 'pretty', 'JSON'. If empty, default format is used")
f.StringArrayVar(&logLevels, "log.level", []string{defaultLogLevel}, fmt.Sprintf("Set log levels in format <level> or <logger>=<level>. Possible loggers: %s", strings.Join(logging.Global().Names(), ", ")))
f.BoolVar(&logSampling, "log.sampling", true, "If true, operator will try to minimize duplication of logging events")
f.BoolVar(&apiOptions.enabled, "api.enabled", true, "Enable operator HTTP and gRPC API")
f.IntVar(&apiOptions.httpPort, "api.http-port", defaultAPIHTTPPort, "HTTP API port to listen on")
f.IntVar(&apiOptions.grpcPort, "api.grpc-port", defaultAPIGRPCPort, "gRPC API port to listen on")
Expand Down Expand Up @@ -247,6 +240,9 @@ func init() {
f.StringArrayVar(&metricsOptions.excludedMetricPrefixes, "metrics.excluded-prefixes", nil, "List of the excluded metrics prefixes")
f.BoolVar(&operatorImageDiscovery.defaultStatusDiscovery, "image.discovery.status", true, "Discover Operator Image from Pod Status by default. When disabled Pod Spec is used.")
f.DurationVar(&operatorImageDiscovery.timeout, "image.discovery.timeout", time.Minute, "Timeout for image discovery process")
if err := logging.Init(&cmdMain); err != nil {
panic(err.Error())
}
if err := features.Init(&cmdMain); err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -308,24 +304,10 @@ func executeMain(cmd *cobra.Command, args []string) {
kclient.SetDefaultBurst(operatorKubernetesOptions.burst)

// Prepare log service
var err error

levels, err := logging.ParseLogLevelsFromArgs(logLevels)
if err != nil {
logger.Err(err).Fatal("Unable to parse log level")
if err := logging.Enable(); err != nil {
logger.Err(err).Fatal("Unable to enable logger")
}

// Set root logger to stdout (JSON formatted) if not prettified
if strings.ToUpper(logFormat) == "JSON" {
logging.Global().SetRoot(zerolog.New(os.Stdout).With().Timestamp().Logger())
} else if strings.ToLower(logFormat) != "pretty" && logFormat != "" {
logger.Fatal("Unknown log format: %s", logFormat)
}
logging.Global().Configure(logging.Config{
Levels: levels,
Sampling: logSampling,
})

podNameParts := strings.Split(name, "-")
operatorID := podNameParts[len(podNameParts)-1]

Expand All @@ -347,16 +329,16 @@ func executeMain(cmd *cobra.Command, args []string) {
!operatorOptions.enableBackup && !operatorOptions.enableApps && !operatorOptions.enableK2KClusterSync && !operatorOptions.enableML {
if !operatorOptions.versionOnly {
if version.GetVersionV1().IsEnterprise() {
logger.Err(err).Fatal("Turn on --operator.deployment, --operator.deployment-replication, --operator.storage, --operator.backup, --operator.apps, --operator.k2k-cluster-sync, --operator.ml or any combination of these")
logger.Fatal("Turn on --operator.deployment, --operator.deployment-replication, --operator.storage, --operator.backup, --operator.apps, --operator.k2k-cluster-sync, --operator.ml or any combination of these")
} else {
logger.Err(err).Fatal("Turn on --operator.deployment, --operator.deployment-replication, --operator.storage, --operator.backup, --operator.apps, --operator.k2k-cluster-sync or any combination of these")
logger.Fatal("Turn on --operator.deployment, --operator.deployment-replication, --operator.storage, --operator.backup, --operator.apps, --operator.k2k-cluster-sync or any combination of these")
}
}
} else if operatorOptions.versionOnly {
logger.Err(err).Fatal("Options --operator.deployment, --operator.deployment-replication, --operator.storage, --operator.backup, --operator.apps, --operator.k2k-cluster-sync, --operator.ml cannot be enabled together with --operator.version")
logger.Fatal("Options --operator.deployment, --operator.deployment-replication, --operator.storage, --operator.backup, --operator.apps, --operator.k2k-cluster-sync, --operator.ml cannot be enabled together with --operator.version")
} else if !version.GetVersionV1().IsEnterprise() {
if operatorOptions.enableML {
logger.Err(err).Fatal("Options --operator.ml can be enabled only on the Enterprise Operator")
logger.Fatal("Options --operator.ml can be enabled only on the Enterprise Operator")
}
}

Expand Down Expand Up @@ -444,7 +426,11 @@ func executeMain(cmd *cobra.Command, args []string) {
if err != nil {
logger.Err(err).Fatal("Failed to create API server")
}
go errors.LogError(logger, "while running API server", apiServer.Run)
go func() {
if err := apiServer.Run(); err != nil {
logger.Err(err).Error("while running API server")
}
}()
}

listenAddr := net.JoinHostPort(serverOptions.host, strconv.Itoa(serverOptions.port))
Expand Down Expand Up @@ -493,7 +479,11 @@ func executeMain(cmd *cobra.Command, args []string) {
}); err != nil {
logger.Err(err).Fatal("Failed to create HTTP server")
} else {
go errors.LogError(logger, "error while starting server", svr.Run)
go func() {
if err := svr.Run(); err != nil {
logger.Err(err).Error("error while starting server")
}
}()
}

// startChaos(context.Background(), cfg.KubeCli, cfg.Namespace, chaosLevel)
Expand Down
85 changes: 85 additions & 0 deletions pkg/logging/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// DISCLAIMER
//
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package logging

import (
"fmt"
"os"
"strings"
"sync"

"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/arangodb/kube-arangodb/pkg/util/errors"
)

const (
defaultLogLevel = "info"
)

var (
enableLock sync.Mutex
enabled bool

cli struct {
format string
levels []string
sampling bool
}
)

func Init(cmd *cobra.Command) error {
f := cmd.PersistentFlags()

f.StringVar(&cli.format, "log.format", "pretty", "Set log format. Allowed values: 'pretty', 'JSON'. If empty, default format is used")
f.StringArrayVar(&cli.levels, "log.level", []string{defaultLogLevel}, fmt.Sprintf("Set log levels in format <level> or <logger>=<level>. Possible loggers: %s", strings.Join(Global().Names(), ", ")))
f.BoolVar(&cli.sampling, "log.sampling", true, "If true, operator will try to minimize duplication of logging events")

return nil
}

func Enable() error {
enableLock.Lock()
defer enableLock.Unlock()

if enabled {
return errors.Errorf("Logger already enabled")
}

levels, err := ParseLogLevelsFromArgs(cli.levels)
if err != nil {
return errors.WithMessagef(err, "Unable to parse levels")
}

// Set root logger to stdout (JSON formatted) if not prettified
if strings.ToUpper(cli.format) == "JSON" {
Global().SetRoot(zerolog.New(os.Stdout).With().Timestamp().Logger())
} else if strings.ToLower(cli.format) != "pretty" && cli.format != "" {
return errors.Errorf("Unknown log format: %s", cli.format)
}
Global().Configure(Config{
Levels: levels,
Sampling: cli.sampling,
})

return nil
}
8 changes: 0 additions & 8 deletions pkg/util/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import (
"github.com/pkg/errors"

driver "github.com/arangodb/go-driver"

"github.com/arangodb/kube-arangodb/pkg/logging"
)

func Cause(err error) error {
Expand Down Expand Up @@ -209,12 +207,6 @@ func libCause(err error) (bool, error) {
}
}

func LogError(logger logging.Logger, msg string, f func() error) {
if err := f(); err != nil {
logger.Err(err).Error(msg)
}
}

type Causer interface {
Cause() error
}
Expand Down