Skip to content

🌱 Allows to redefine ETCD client logger #12271

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions controlplane/kubeadm/controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"time"

"go.uber.org/zap/zapcore"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand All @@ -36,6 +37,7 @@ type KubeadmControlPlaneReconciler struct {

EtcdDialTimeout time.Duration
EtcdCallTimeout time.Duration
EtcdLogLevel zapcore.Level

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
Expand All @@ -51,6 +53,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
ClusterCache: r.ClusterCache,
EtcdDialTimeout: r.EtcdDialTimeout,
EtcdCallTimeout: r.EtcdCallTimeout,
EtcdLogLevel: r.EtcdLogLevel,
WatchFilterValue: r.WatchFilterValue,
RemoteConditionsGracePeriod: r.RemoteConditionsGracePeriod,
}).SetupWithManager(ctx, mgr, options)
Expand Down
10 changes: 10 additions & 0 deletions controlplane/kubeadm/internal/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (

"github.com/blang/semver/v4"
"github.com/pkg/errors"
"go.etcd.io/etcd/client/pkg/v3/logutil"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -45,6 +47,7 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
"sigs.k8s.io/cluster-api/controllers/clustercache"
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal"
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/etcd"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/contract"
"sigs.k8s.io/cluster-api/internal/util/ssa"
Expand Down Expand Up @@ -84,6 +87,7 @@ type KubeadmControlPlaneReconciler struct {

EtcdDialTimeout time.Duration
EtcdCallTimeout time.Duration
EtcdLogLevel zapcore.Level

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
Expand All @@ -109,6 +113,12 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
}

predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "kubeadmcontrolplane")
etcdLogger, err := logutil.CreateDefaultZapLogger(r.EtcdLogLevel)
if err != nil {
return errors.Wrap(err, "failed to create ETCD client zap logger")
}
etcd.SetLogger(etcdLogger)

c, err := ctrl.NewControllerManagedBy(mgr).
For(&controlplanev1.KubeadmControlPlane{}).
Owns(&clusterv1.Machine{}, builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog))).
Expand Down
6 changes: 6 additions & 0 deletions controlplane/kubeadm/internal/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/client/pkg/v3/logutil"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
kerrors "k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -141,6 +142,11 @@ var (
etcdClientLogger, _ = logutil.CreateDefaultZapLogger(zapcore.InfoLevel)
)

// SetLogger allows to redefine ETCD client logger.
func SetLogger(logger *zap.Logger) {
etcdClientLogger = logger
}

// NewClient creates a new etcd client with the given configuration.
func NewClient(ctx context.Context, config ClientConfiguration) (*Client, error) {
dialer, err := proxy.NewDialer(config.Proxy)
Expand Down
6 changes: 6 additions & 0 deletions controlplane/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/pflag"
"go.uber.org/zap/zapcore"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -100,6 +101,7 @@ var (
skipCRDMigrationPhases []string
etcdDialTimeout time.Duration
etcdCallTimeout time.Duration
etcdLogLevel int8
)

func init() {
Expand Down Expand Up @@ -190,6 +192,9 @@ func InitFlags(fs *pflag.FlagSet) {
fs.DurationVar(&etcdCallTimeout, "etcd-call-timeout-duration", etcd.DefaultCallTimeout,
"Duration that the etcd client waits at most for read and write operations to etcd.")

fs.Int8Var(&etcdLogLevel, "etcd-client-log-level", int8(zapcore.InfoLevel),
"Logging level for etcd client.")

flags.AddManagerOptions(fs, &managerOptions)

feature.MutableGates.AddFlag(fs)
Expand Down Expand Up @@ -425,6 +430,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
WatchFilterValue: watchFilterValue,
EtcdDialTimeout: etcdDialTimeout,
EtcdCallTimeout: etcdCallTimeout,
EtcdLogLevel: zapcore.Level(etcdLogLevel),
RemoteConditionsGracePeriod: remoteConditionsGracePeriod,
}).SetupWithManager(ctx, mgr, concurrency(kubeadmControlPlaneConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "KubeadmControlPlane")
Expand Down