Skip to content

Commit

Permalink
update logging (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
aobort committed Jun 6, 2024
2 parents 010228b + 0782ef6 commit 3d90173
Show file tree
Hide file tree
Showing 20 changed files with 538 additions and 166 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ COPY go.mod go.sum ./
RUN go mod download

# Copy the go source
COPY cmd/main.go ./cmd/
COPY cmd/ ./cmd/
COPY api/ ./api/
COPY internal/ ./internal/

Expand Down
114 changes: 114 additions & 0 deletions cmd/app/commandline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Copyright 2024 The etcd-operator Authors.
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.
*/

package app

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/aenix-io/etcd-operator/internal/log"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

type Flags struct {
Kubeconfig string
MetricsAddress string
ProbeAddress string
LeaderElection bool
SecureMetrics bool
EnableHTTP2 bool
DisableWebhooks bool
LogLevel string
StacktraceLevel string
Dev bool
}

func ParseCmdLine() Flags {
pflag.Usage = usage
pflag.ErrHelp = nil
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError)

pflag.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
pflag.String("metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
pflag.String("health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
pflag.Bool("leader-elect", false, "Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
pflag.Bool("metrics-secure", false, "If set the metrics endpoint is served securely.")
pflag.Bool("enable-http2", false, "If set, HTTP/2 will be enabled for the metrics and webhook servers.")
pflag.Bool("disable-webhooks", false, "If set, the webhooks will be disabled.")
pflag.String("log-level", "info", "Logger verbosity level.Applicable values are debug, info, warn, error.")
pflag.String("stacktrace-level", "error", "Logger level to add stacktrace. "+
"Applicable values are debug, info, warn, error.")
pflag.Bool("dev", false, "development mode.")

var help bool
pflag.BoolVarP(&help, "help", "h", false, "Show this help message.")
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
exitUsage(err)
}
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
err = pflag.CommandLine.Parse(os.Args[1:])
if err != nil {
exitUsage(err)
}
if help {
exitUsage(nil)
}

return Flags{
Kubeconfig: viper.GetString("kubeconfig"),
MetricsAddress: viper.GetString("metrics-bind-address"),
ProbeAddress: viper.GetString("health-probe-bind-address"),
LeaderElection: viper.GetBool("leader-elect"),
SecureMetrics: viper.GetBool("metrics-secure"),
EnableHTTP2: viper.GetBool("enable-http2"),
DisableWebhooks: viper.GetBool("disable-webhooks"),
LogLevel: viper.GetString("log-level"),
StacktraceLevel: viper.GetString("stacktrace-level"),
Dev: viper.GetBool("dev"),
}
}

func usage() {
name := filepath.Base(os.Args[0])
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s [--option]...\n", name)
_, _ = fmt.Fprintf(os.Stderr, "Options:\n")
pflag.PrintDefaults()
}

func exitUsage(err error) {
code := 0
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s: %s\n", filepath.Base(os.Args[0]), err)
code = 2
}
pflag.Usage()
os.Exit(code)
}

func LogParameters(flags Flags) log.Parameters {
return log.Parameters{
LogLevel: flags.LogLevel,
StacktraceLevel: flags.StacktraceLevel,
Development: flags.Dev,
}
}
86 changes: 36 additions & 50 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,48 @@ limitations under the License.
package main

import (
"context"
"crypto/tls"
"flag"
"os"
"syscall"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

"github.com/aenix-io/etcd-operator/cmd/app"
"github.com/aenix-io/etcd-operator/internal/log"
"github.com/aenix-io/etcd-operator/internal/signal"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1"
"github.com/aenix-io/etcd-operator/internal/controller"
//+kubebuilder:scaffold:imports
// +kubebuilder:scaffold:imports
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
scheme = runtime.NewScheme()
signals = []os.Signal{os.Interrupt, syscall.SIGTERM}
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(etcdaenixiov1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
// +kubebuilder:scaffold:scheme
}

func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var secureMetrics bool
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&secureMetrics, "metrics-secure", false,
"If set the metrics endpoint is served securely")
flag.BoolVar(&enableHTTP2, "enable-http2", false,
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
flags := app.ParseCmdLine()
ctx := signal.NotifyContext(log.Setup(context.TODO(), app.LogParameters(flags)), signals...)
ctrl.SetLogger(logr.FromContextOrDiscard(ctx))

// if the enable-http2 flag is false (the default), http/2 should be disabled
// due to its vulnerabilities. More specifically, disabling http/2 will
Expand All @@ -81,12 +67,12 @@ func main() {
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
// - https://github.com/advisories/GHSA-4374-p667-p6c8
disableHTTP2 := func(c *tls.Config) {
setupLog.Info("disabling http/2")
log.Info(ctx, "disabling http/2")
c.NextProtos = []string{"http/1.1"}
}

tlsOpts := []func(*tls.Config){}
if !enableHTTP2 {
tlsOpts := make([]func(*tls.Config), 0)
if !flags.EnableHTTP2 {
tlsOpts = append(tlsOpts, disableHTTP2)
}

Expand All @@ -97,13 +83,13 @@ func main() {
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
SecureServing: secureMetrics,
BindAddress: flags.MetricsAddress,
SecureServing: flags.SecureMetrics,
TLSOpts: tlsOpts,
},
WebhookServer: webhookServer,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
HealthProbeBindAddress: flags.ProbeAddress,
LeaderElection: flags.LeaderElection,
LeaderElectionID: "1b04a718.etcd.aenix.io",
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
Expand All @@ -118,37 +104,37 @@ func main() {
// LeaderElectionReleaseOnCancel: true,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
log.Error(ctx, err, "unable to setup manager")
os.Exit(1)
}

if err = (&controller.EtcdClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EtcdCluster")
log.Error(ctx, err, "unable to create controller", "controller", "EtcdCluster")
os.Exit(1)
}
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if !flags.DisableWebhooks {
if err = (&etcdaenixiov1alpha1.EtcdCluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "EtcdCluster")
log.Error(ctx, err, "unable to create webhook", "webhook", "EtcdCluster")
os.Exit(1)
}
}
//+kubebuilder:scaffold:builder
// +kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
if err = mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
log.Error(ctx, err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
if err = mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
log.Error(ctx, err, "unable to set up ready check")
os.Exit(1)
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
log.Info(ctx, "starting manager")
if err = mgr.Start(ctx); err != nil {
log.Error(ctx, err, "problem running manager")
os.Exit(1)
}
}
36 changes: 24 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ module github.com/aenix-io/etcd-operator
go 1.22.3

require (
github.com/go-logr/logr v1.4.1
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
go.etcd.io/etcd/client/v3 v3.5.14
go.uber.org/zap v1.26.0
go.uber.org/zap/exp v0.2.0
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
Expand All @@ -19,11 +24,10 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -36,40 +40,48 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240311132316-a219d84964c2 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.30.1 // indirect
Expand Down
Loading

0 comments on commit 3d90173

Please sign in to comment.