diff --git a/controlplane/kubeadm/controllers/alias.go b/controlplane/kubeadm/controllers/alias.go index 71e55dd87357..5da17c244afb 100644 --- a/controlplane/kubeadm/controllers/alias.go +++ b/controlplane/kubeadm/controllers/alias.go @@ -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" @@ -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 @@ -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) diff --git a/controlplane/kubeadm/internal/controllers/controller.go b/controlplane/kubeadm/internal/controllers/controller.go index b8f70b188d22..ad6fe8dcf6be 100644 --- a/controlplane/kubeadm/internal/controllers/controller.go +++ b/controlplane/kubeadm/internal/controllers/controller.go @@ -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" @@ -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" @@ -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 @@ -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))). diff --git a/controlplane/kubeadm/internal/etcd/etcd.go b/controlplane/kubeadm/internal/etcd/etcd.go index 5b62d54a55fe..9f60a77526ff 100644 --- a/controlplane/kubeadm/internal/etcd/etcd.go +++ b/controlplane/kubeadm/internal/etcd/etcd.go @@ -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" @@ -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) diff --git a/controlplane/kubeadm/main.go b/controlplane/kubeadm/main.go index ef70152fb732..80508ec693dc 100644 --- a/controlplane/kubeadm/main.go +++ b/controlplane/kubeadm/main.go @@ -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" @@ -100,6 +101,7 @@ var ( skipCRDMigrationPhases []string etcdDialTimeout time.Duration etcdCallTimeout time.Duration + etcdLogLevel int8 ) func init() { @@ -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) @@ -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")