Skip to content

Commit 1ba34b0

Browse files
authored
Merge pull request #12468 from dmvolod/redefine-etcd-client-logger-release-1.9
[release-1.9] 🌱 Add --etcd-client-log-level flag to KCP
2 parents ec03189 + 7e0ad1a commit 1ba34b0

File tree

7 files changed

+39
-15
lines changed

7 files changed

+39
-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
@@ -54,6 +56,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
5456
ClusterCache: r.ClusterCache,
5557
EtcdDialTimeout: r.EtcdDialTimeout,
5658
EtcdCallTimeout: r.EtcdCallTimeout,
59+
EtcdLogger: r.EtcdLogger,
5760
WatchFilterValue: r.WatchFilterValue,
5861
RemoteConditionsGracePeriod: r.RemoteConditionsGracePeriod,
5962
DeprecatedInfraMachineNaming: r.DeprecatedInfraMachineNaming,

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"
@@ -57,6 +58,7 @@ type Management struct {
5758
ClusterCache clustercache.ClusterCache
5859
EtcdDialTimeout time.Duration
5960
EtcdCallTimeout time.Duration
61+
EtcdLogger *zap.Logger
6062
}
6163

6264
// RemoteClusterConnectionError represents a failure to connect to a remote cluster.
@@ -158,7 +160,7 @@ func (m *Management) GetWorkloadCluster(ctx context.Context, clusterKey client.O
158160
restConfig: restConfig,
159161
Client: c,
160162
CoreDNSMigrator: &CoreDNSMigrator{},
161-
etcdClientGenerator: NewEtcdClientGenerator(restConfig, tlsConfig, m.EtcdDialTimeout, m.EtcdCallTimeout),
163+
etcdClientGenerator: NewEtcdClientGenerator(restConfig, tlsConfig, m.EtcdDialTimeout, m.EtcdCallTimeout, m.EtcdLogger),
162164
}, nil
163165
}
164166

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"
@@ -85,6 +86,7 @@ type KubeadmControlPlaneReconciler struct {
8586

8687
EtcdDialTimeout time.Duration
8788
EtcdCallTimeout time.Duration
89+
EtcdLogger *zap.Logger
8890

8991
// WatchFilterValue is the label value used to filter events prior to reconciliation.
9092
WatchFilterValue string
@@ -147,6 +149,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
147149
ClusterCache: r.ClusterCache,
148150
EtcdDialTimeout: r.EtcdDialTimeout,
149151
EtcdCallTimeout: r.EtcdCallTimeout,
152+
EtcdLogger: r.EtcdLogger,
150153
}
151154
}
152155

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

@@ -138,14 +137,9 @@ type ClientConfiguration struct {
138137
TLSConfig *tls.Config
139138
DialTimeout time.Duration
140139
CallTimeout time.Duration
140+
Logger *zap.Logger
141141
}
142142

143-
var (
144-
// Create the etcdClientLogger only once. Otherwise every call of clientv3.New
145-
// would create its own logger which leads to a lot of memory allocations.
146-
etcdClientLogger, _ = logutil.CreateDefaultZapLogger(zapcore.InfoLevel)
147-
)
148-
149143
// NewClient creates a new etcd client with the given configuration.
150144
func NewClient(ctx context.Context, config ClientConfiguration) (*Client, error) {
151145
dialer, err := proxy.NewDialer(config.Proxy)
@@ -160,7 +154,7 @@ func NewClient(ctx context.Context, config ClientConfiguration) (*Client, error)
160154
grpc.WithContextDialer(dialer.DialContextWithAddr),
161155
},
162156
TLS: config.TLSConfig,
163-
Logger: etcdClientLogger,
157+
Logger: config.Logger,
164158
})
165159
if err != nil {
166160
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
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

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

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

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

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: 17 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"
@@ -96,6 +98,7 @@ var (
9698
clusterCacheConcurrency int
9799
etcdDialTimeout time.Duration
98100
etcdCallTimeout time.Duration
101+
etcdLogLevel string
99102
useDeprecatedInfraMachineNaming bool
100103
)
101104

@@ -184,6 +187,9 @@ func InitFlags(fs *pflag.FlagSet) {
184187
fs.DurationVar(&etcdCallTimeout, "etcd-call-timeout-duration", etcd.DefaultCallTimeout,
185188
"Duration that the etcd client waits at most for read and write operations to etcd.")
186189

190+
fs.StringVar(&etcdLogLevel, "etcd-client-log-level", zapcore.InfoLevel.String(),
191+
"Logging level for etcd client. Possible values are: debug, info, warn, error, dpanic, panic, fatal.")
192+
187193
fs.BoolVar(&useDeprecatedInfraMachineNaming, "use-deprecated-infra-machine-naming", false,
188194
"Use the deprecated naming convention for infra machines where they are named after the InfraMachineTemplate.")
189195
_ = fs.MarkDeprecated("use-deprecated-infra-machine-naming", "This flag will be removed in v1.10.")
@@ -389,13 +395,24 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
389395
os.Exit(1)
390396
}
391397

398+
zapLogLevel, err := zapcore.ParseLevel(etcdLogLevel)
399+
if err != nil {
400+
setupLog.Error(err, "Unable to parse etcd log level")
401+
os.Exit(1)
402+
}
403+
etcdLogger, err := logutil.CreateDefaultZapLogger(zapLogLevel)
404+
if err != nil {
405+
setupLog.Error(err, "unable to create etcd logger")
406+
os.Exit(1)
407+
}
392408
if err := (&kubeadmcontrolplanecontrollers.KubeadmControlPlaneReconciler{
393409
Client: mgr.GetClient(),
394410
SecretCachingClient: secretCachingClient,
395411
ClusterCache: clusterCache,
396412
WatchFilterValue: watchFilterValue,
397413
EtcdDialTimeout: etcdDialTimeout,
398414
EtcdCallTimeout: etcdCallTimeout,
415+
EtcdLogger: etcdLogger,
399416
RemoteConditionsGracePeriod: remoteConditionsGracePeriod,
400417
DeprecatedInfraMachineNaming: useDeprecatedInfraMachineNaming,
401418
}).SetupWithManager(ctx, mgr, concurrency(kubeadmControlPlaneConcurrency)); err != nil {

0 commit comments

Comments
 (0)