Skip to content

Commit 506814b

Browse files
committed
Move etcdLogger to reconcile struct
1 parent a64e1a1 commit 506814b

File tree

7 files changed

+34
-37
lines changed

7 files changed

+34
-37
lines changed

controlplane/kubeadm/controllers/alias.go

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

23-
"go.uber.org/zap/zapcore"
23+
"go.uber.org/zap"
2424
ctrl "sigs.k8s.io/controller-runtime"
2525
"sigs.k8s.io/controller-runtime/pkg/client"
2626
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -37,7 +37,7 @@ type KubeadmControlPlaneReconciler struct {
3737

3838
EtcdDialTimeout time.Duration
3939
EtcdCallTimeout time.Duration
40-
EtcdLogLevel zapcore.Level
40+
EtcdLogger *zap.Logger
4141

4242
// WatchFilterValue is the label value used to filter events prior to reconciliation.
4343
WatchFilterValue string
@@ -53,7 +53,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
5353
ClusterCache: r.ClusterCache,
5454
EtcdDialTimeout: r.EtcdDialTimeout,
5555
EtcdCallTimeout: r.EtcdCallTimeout,
56-
EtcdLogLevel: r.EtcdLogLevel,
56+
EtcdLogger: r.EtcdLogger,
5757
WatchFilterValue: r.WatchFilterValue,
5858
RemoteConditionsGracePeriod: r.RemoteConditionsGracePeriod,
5959
}).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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import (
2525

2626
"github.com/blang/semver/v4"
2727
"github.com/pkg/errors"
28-
"go.etcd.io/etcd/client/pkg/v3/logutil"
29-
"go.uber.org/zap/zapcore"
28+
"go.uber.org/zap"
3029
corev1 "k8s.io/api/core/v1"
3130
apierrors "k8s.io/apimachinery/pkg/api/errors"
3231
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -47,7 +46,6 @@ import (
4746
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
4847
"sigs.k8s.io/cluster-api/controllers/clustercache"
4948
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal"
50-
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/etcd"
5149
"sigs.k8s.io/cluster-api/feature"
5250
"sigs.k8s.io/cluster-api/internal/contract"
5351
"sigs.k8s.io/cluster-api/internal/util/ssa"
@@ -87,7 +85,7 @@ type KubeadmControlPlaneReconciler struct {
8785

8886
EtcdDialTimeout time.Duration
8987
EtcdCallTimeout time.Duration
90-
EtcdLogLevel zapcore.Level
88+
EtcdLogger *zap.Logger
9189

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

115113
predicateLog := ctrl.LoggerFrom(ctx).WithValues("controller", "kubeadmcontrolplane")
116-
etcdLogger, err := logutil.CreateDefaultZapLogger(r.EtcdLogLevel)
117-
if err != nil {
118-
return errors.Wrap(err, "failed to create ETCD client zap logger")
119-
}
120-
etcd.SetLogger(etcdLogger)
121-
122114
c, err := ctrl.NewControllerManagedBy(mgr).
123115
For(&controlplanev1.KubeadmControlPlane{}).
124116
Owns(&clusterv1.Machine{}, builder.WithPredicates(predicates.ResourceIsChanged(mgr.GetScheme(), predicateLog))).
@@ -156,6 +148,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
156148
ClusterCache: r.ClusterCache,
157149
EtcdDialTimeout: r.EtcdDialTimeout,
158150
EtcdCallTimeout: r.EtcdCallTimeout,
151+
EtcdLogger: r.EtcdLogger,
159152
}
160153
}
161154

controlplane/kubeadm/internal/etcd/etcd.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +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"
2928
"go.uber.org/zap"
30-
"go.uber.org/zap/zapcore"
3129
"google.golang.org/grpc"
3230
kerrors "k8s.io/apimachinery/pkg/util/errors"
3331

@@ -134,17 +132,7 @@ type ClientConfiguration struct {
134132
TLSConfig *tls.Config
135133
DialTimeout time.Duration
136134
CallTimeout time.Duration
137-
}
138-
139-
var (
140-
// Create the etcdClientLogger only once. Otherwise every call of clientv3.New
141-
// would create its own logger which leads to a lot of memory allocations.
142-
etcdClientLogger, _ = logutil.CreateDefaultZapLogger(zapcore.InfoLevel)
143-
)
144-
145-
// SetLogger allows to redefine ETCD client logger.
146-
func SetLogger(logger *zap.Logger) {
147-
etcdClientLogger = logger
135+
Logger *zap.Logger
148136
}
149137

150138
// NewClient creates a new etcd client with the given configuration.
@@ -161,7 +149,7 @@ func NewClient(ctx context.Context, config ClientConfiguration) (*Client, error)
161149
grpc.WithContextDialer(dialer.DialContextWithAddr),
162150
},
163151
TLS: config.TLSConfig,
164-
Logger: etcdClientLogger,
152+
Logger: config.Logger,
165153
})
166154
if err != nil {
167155
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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/pkg/errors"
2929
"github.com/spf13/pflag"
30+
"go.etcd.io/etcd/client/pkg/v3/logutil"
3031
"go.uber.org/zap/zapcore"
3132
appsv1 "k8s.io/api/apps/v1"
3233
corev1 "k8s.io/api/core/v1"
@@ -101,7 +102,7 @@ var (
101102
skipCRDMigrationPhases []string
102103
etcdDialTimeout time.Duration
103104
etcdCallTimeout time.Duration
104-
etcdLogLevel int8
105+
etcdLogLevel string
105106
)
106107

107108
func init() {
@@ -192,8 +193,8 @@ func InitFlags(fs *pflag.FlagSet) {
192193
fs.DurationVar(&etcdCallTimeout, "etcd-call-timeout-duration", etcd.DefaultCallTimeout,
193194
"Duration that the etcd client waits at most for read and write operations to etcd.")
194195

195-
fs.Int8Var(&etcdLogLevel, "etcd-client-log-level", int8(zapcore.InfoLevel),
196-
"Logging level for etcd client.")
196+
fs.StringVar(&etcdLogLevel, "etcd-client-log-level", zapcore.InfoLevel.String(),
197+
"Logging level for etcd client. Possible values are: debug, info, warn, error, dpanic, panic, fatal.")
197198

198199
flags.AddManagerOptions(fs, &managerOptions)
199200

@@ -423,14 +424,22 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
423424
os.Exit(1)
424425
}
425426

427+
zapLogLevel, err := zapcore.ParseLevel(etcdLogLevel)
428+
if err != nil {
429+
setupLog.Error(err, "Unable to parse etcd log level")
430+
}
431+
etcdLogger, err := logutil.CreateDefaultZapLogger(zapLogLevel)
432+
if err != nil {
433+
setupLog.Error(err, "unable to create etcd logger")
434+
}
426435
if err := (&kubeadmcontrolplanecontrollers.KubeadmControlPlaneReconciler{
427436
Client: mgr.GetClient(),
428437
SecretCachingClient: secretCachingClient,
429438
ClusterCache: clusterCache,
430439
WatchFilterValue: watchFilterValue,
431440
EtcdDialTimeout: etcdDialTimeout,
432441
EtcdCallTimeout: etcdCallTimeout,
433-
EtcdLogLevel: zapcore.Level(etcdLogLevel),
442+
EtcdLogger: etcdLogger,
434443
RemoteConditionsGracePeriod: remoteConditionsGracePeriod,
435444
}).SetupWithManager(ctx, mgr, concurrency(kubeadmControlPlaneConcurrency)); err != nil {
436445
setupLog.Error(err, "unable to create controller", "controller", "KubeadmControlPlane")

0 commit comments

Comments
 (0)