Skip to content

Commit 32a60a0

Browse files
committed
🌱 Allows to redefine ETCD client logger
1 parent c28b413 commit 32a60a0

File tree

7 files changed

+37
-15
lines changed

7 files changed

+37
-15
lines changed

controlplane/kubeadm/controllers/alias.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"time"
2222

23+
"go.uber.org/zap"
2324
ctrl "sigs.k8s.io/controller-runtime"
2425
"sigs.k8s.io/controller-runtime/pkg/client"
2526
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -36,6 +37,7 @@ type KubeadmControlPlaneReconciler struct {
3637

3738
EtcdDialTimeout time.Duration
3839
EtcdCallTimeout time.Duration
40+
EtcdLogger *zap.Logger
3941

4042
// WatchFilterValue is the label value used to filter events prior to reconciliation.
4143
WatchFilterValue string
@@ -51,6 +53,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
5153
ClusterCache: r.ClusterCache,
5254
EtcdDialTimeout: r.EtcdDialTimeout,
5355
EtcdCallTimeout: r.EtcdCallTimeout,
56+
EtcdLogger: r.EtcdLogger,
5457
WatchFilterValue: r.WatchFilterValue,
5558
RemoteConditionsGracePeriod: r.RemoteConditionsGracePeriod,
5659
}).SetupWithManager(ctx, mgr, options)

controlplane/kubeadm/internal/cluster.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/pkg/errors"
27+
"go.uber.org/zap"
2728
corev1 "k8s.io/api/core/v1"
2829
apierrors "k8s.io/apimachinery/pkg/api/errors"
2930
"k8s.io/client-go/rest"
@@ -51,6 +52,7 @@ type Management struct {
5152
ClusterCache clustercache.ClusterCache
5253
EtcdDialTimeout time.Duration
5354
EtcdCallTimeout time.Duration
55+
EtcdLogger *zap.Logger
5456
}
5557

5658
// RemoteClusterConnectionError represents a failure to connect to a remote cluster.
@@ -152,7 +154,7 @@ func (m *Management) GetWorkloadCluster(ctx context.Context, clusterKey client.O
152154
restConfig: restConfig,
153155
Client: c,
154156
CoreDNSMigrator: &CoreDNSMigrator{},
155-
etcdClientGenerator: NewEtcdClientGenerator(restConfig, tlsConfig, m.EtcdDialTimeout, m.EtcdCallTimeout),
157+
etcdClientGenerator: NewEtcdClientGenerator(restConfig, tlsConfig, m.EtcdDialTimeout, m.EtcdCallTimeout, m.EtcdLogger),
156158
}, nil
157159
}
158160

controlplane/kubeadm/internal/controllers/controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/blang/semver/v4"
2727
"github.com/pkg/errors"
28+
"go.uber.org/zap"
2829
corev1 "k8s.io/api/core/v1"
2930
apierrors "k8s.io/apimachinery/pkg/api/errors"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -84,6 +85,7 @@ type KubeadmControlPlaneReconciler struct {
8485

8586
EtcdDialTimeout time.Duration
8687
EtcdCallTimeout time.Duration
88+
EtcdLogger *zap.Logger
8789

8890
// WatchFilterValue is the label value used to filter events prior to reconciliation.
8991
WatchFilterValue string
@@ -146,6 +148,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
146148
ClusterCache: r.ClusterCache,
147149
EtcdDialTimeout: r.EtcdDialTimeout,
148150
EtcdCallTimeout: r.EtcdCallTimeout,
151+
EtcdLogger: r.EtcdLogger,
149152
}
150153
}
151154

controlplane/kubeadm/internal/etcd/etcd.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import (
2424

2525
"github.com/pkg/errors"
2626
"go.etcd.io/etcd/api/v3/etcdserverpb"
27-
"go.etcd.io/etcd/client/pkg/v3/logutil"
2827
clientv3 "go.etcd.io/etcd/client/v3"
29-
"go.uber.org/zap/zapcore"
28+
"go.uber.org/zap"
3029
"google.golang.org/grpc"
3130
kerrors "k8s.io/apimachinery/pkg/util/errors"
3231

@@ -133,14 +132,9 @@ type ClientConfiguration struct {
133132
TLSConfig *tls.Config
134133
DialTimeout time.Duration
135134
CallTimeout time.Duration
135+
Logger *zap.Logger
136136
}
137137

138-
var (
139-
// Create the etcdClientLogger only once. Otherwise every call of clientv3.New
140-
// would create its own logger which leads to a lot of memory allocations.
141-
etcdClientLogger, _ = logutil.CreateDefaultZapLogger(zapcore.InfoLevel)
142-
)
143-
144138
// NewClient creates a new etcd client with the given configuration.
145139
func NewClient(ctx context.Context, config ClientConfiguration) (*Client, error) {
146140
dialer, err := proxy.NewDialer(config.Proxy)
@@ -155,7 +149,7 @@ func NewClient(ctx context.Context, config ClientConfiguration) (*Client, error)
155149
grpc.WithContextDialer(dialer.DialContextWithAddr),
156150
},
157151
TLS: config.TLSConfig,
158-
Logger: etcdClientLogger,
152+
Logger: config.Logger,
159153
})
160154
if err != nil {
161155
return nil, errors.Wrap(err, "unable to create etcd client")

controlplane/kubeadm/internal/etcd_client_generator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
"github.com/pkg/errors"
26+
"go.uber.org/zap"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
kerrors "k8s.io/apimachinery/pkg/util/errors"
2829
"k8s.io/apimachinery/pkg/util/sets"
@@ -44,7 +45,7 @@ type clientCreator func(ctx context.Context, endpoint string) (*etcd.Client, err
4445
var errEtcdNodeConnection = errors.New("failed to connect to etcd node")
4546

4647
// NewEtcdClientGenerator returns a new etcdClientGenerator instance.
47-
func NewEtcdClientGenerator(restConfig *rest.Config, tlsConfig *tls.Config, etcdDialTimeout, etcdCallTimeout time.Duration) *EtcdClientGenerator {
48+
func NewEtcdClientGenerator(restConfig *rest.Config, tlsConfig *tls.Config, etcdDialTimeout, etcdCallTimeout time.Duration, etcdLogger *zap.Logger) *EtcdClientGenerator {
4849
ecg := &EtcdClientGenerator{restConfig: restConfig, tlsConfig: tlsConfig}
4950

5051
ecg.createClient = func(ctx context.Context, endpoint string) (*etcd.Client, error) {
@@ -60,6 +61,7 @@ func NewEtcdClientGenerator(restConfig *rest.Config, tlsConfig *tls.Config, etcd
6061
TLSConfig: tlsConfig,
6162
DialTimeout: etcdDialTimeout,
6263
CallTimeout: etcdCallTimeout,
64+
Logger: etcdLogger,
6365
})
6466
}
6567

controlplane/kubeadm/internal/etcd_client_generator_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,23 @@ import (
2525
. "github.com/onsi/gomega"
2626
"github.com/pkg/errors"
2727
"go.etcd.io/etcd/api/v3/etcdserverpb"
28+
"go.etcd.io/etcd/client/pkg/v3/logutil"
2829
clientv3 "go.etcd.io/etcd/client/v3"
30+
"go.uber.org/zap/zapcore"
2931
"k8s.io/client-go/rest"
3032

3133
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/etcd"
3234
etcdfake "sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/etcd/fake"
3335
)
3436

3537
var (
36-
subject *EtcdClientGenerator
38+
subject *EtcdClientGenerator
39+
etcdClientLogger, _ = logutil.CreateDefaultZapLogger(zapcore.InfoLevel)
3740
)
3841

3942
func TestNewEtcdClientGenerator(t *testing.T) {
4043
g := NewWithT(t)
41-
subject = NewEtcdClientGenerator(&rest.Config{}, &tls.Config{MinVersion: tls.VersionTLS12}, 0, 0)
44+
subject = NewEtcdClientGenerator(&rest.Config{}, &tls.Config{MinVersion: tls.VersionTLS12}, 0, 0, etcdClientLogger)
4245
g.Expect(subject.createClient).To(Not(BeNil()))
4346
}
4447

@@ -90,7 +93,7 @@ func TestFirstAvailableNode(t *testing.T) {
9093
for _, tt := range tests {
9194
t.Run(tt.name, func(t *testing.T) {
9295
g := NewWithT(t)
93-
subject = NewEtcdClientGenerator(&rest.Config{}, &tls.Config{MinVersion: tls.VersionTLS12}, 0, 0)
96+
subject = NewEtcdClientGenerator(&rest.Config{}, &tls.Config{MinVersion: tls.VersionTLS12}, 0, 0, etcdClientLogger)
9497
subject.createClient = tt.cc
9598

9699
client, err := subject.forFirstAvailableNode(ctx, tt.nodes)
@@ -212,7 +215,7 @@ func TestForLeader(t *testing.T) {
212215
t.Run(tt.name, func(t *testing.T) {
213216
g := NewWithT(t)
214217

215-
subject = NewEtcdClientGenerator(&rest.Config{}, &tls.Config{MinVersion: tls.VersionTLS12}, 0, 0)
218+
subject = NewEtcdClientGenerator(&rest.Config{}, &tls.Config{MinVersion: tls.VersionTLS12}, 0, 0, etcdClientLogger)
216219
subject.createClient = tt.cc
217220

218221
client, err := subject.forLeader(ctx, tt.nodes)

controlplane/kubeadm/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727

2828
"github.com/pkg/errors"
2929
"github.com/spf13/pflag"
30+
"go.etcd.io/etcd/client/pkg/v3/logutil"
31+
"go.uber.org/zap/zapcore"
3032
appsv1 "k8s.io/api/apps/v1"
3133
corev1 "k8s.io/api/core/v1"
3234
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -102,6 +104,7 @@ var (
102104
skipCRDMigrationPhases []string
103105
etcdDialTimeout time.Duration
104106
etcdCallTimeout time.Duration
107+
etcdLogLevel string
105108
)
106109

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

198+
fs.StringVar(&etcdLogLevel, "etcd-client-log-level", zapcore.InfoLevel.String(),
199+
"Logging level for etcd client. Possible values are: debug, info, warn, error, dpanic, panic, fatal.")
200+
195201
flags.AddManagerOptions(fs, &managerOptions)
196202

197203
feature.MutableGates.AddFlag(fs)
@@ -420,13 +426,22 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
420426
os.Exit(1)
421427
}
422428

429+
zapLogLevel, err := zapcore.ParseLevel(etcdLogLevel)
430+
if err != nil {
431+
setupLog.Error(err, "Unable to parse etcd log level")
432+
}
433+
etcdLogger, err := logutil.CreateDefaultZapLogger(zapLogLevel)
434+
if err != nil {
435+
setupLog.Error(err, "unable to create etcd logger")
436+
}
423437
if err := (&kubeadmcontrolplanecontrollers.KubeadmControlPlaneReconciler{
424438
Client: mgr.GetClient(),
425439
SecretCachingClient: secretCachingClient,
426440
ClusterCache: clusterCache,
427441
WatchFilterValue: watchFilterValue,
428442
EtcdDialTimeout: etcdDialTimeout,
429443
EtcdCallTimeout: etcdCallTimeout,
444+
EtcdLogger: etcdLogger,
430445
RemoteConditionsGracePeriod: remoteConditionsGracePeriod,
431446
}).SetupWithManager(ctx, mgr, concurrency(kubeadmControlPlaneConcurrency)); err != nil {
432447
setupLog.Error(err, "unable to create controller", "controller", "KubeadmControlPlane")

0 commit comments

Comments
 (0)