Skip to content

Commit daeb324

Browse files
committed
chore: adjust etcd space calculation and NOSPACE alarm dismissal
chore: replace span/log/recorder with emit
1 parent cbb591a commit daeb324

32 files changed

Lines changed: 866 additions & 480 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ testbin/*
4040
*.swp
4141
*.swo
4242
*~
43+
44+
.claude

.golangci.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@ linters:
264264
# msg: Use slog instead
265265
- pattern: ^(record|events)\.NewFakeRecorder(For)?$
266266
msg: Use pkg/operator/util/recorder/fake_recorder.go instead.
267+
- pattern: ^trace\.Span\.AddEvent$
268+
msg: use emit.Warnf/Infof with SinkSpanEvent instead
269+
- pattern: ^slog\.Logger\.(Warn|Info)Context$
270+
msg: use emit.Warnf/emit.Infof instead
271+
- pattern: ^recorder\.Recorder\.(Warnf|Normalf)$
272+
msg: use emit.Warnf/emit.Infof instead
273+
- pattern: ^(gomega\.)?NewWithT$
274+
pkg: ^github\.com/onsi/gomega$
275+
msg: use G() from the test package instead
267276
revive:
268277
rules:
269278
- name: exported
@@ -338,6 +347,13 @@ linters:
338347
- gocritic
339348
text: "deferInLoop: Possible resource leak, 'defer' is called in the 'for' loop"
340349
path: *testFiles
350+
- linters:
351+
- forbidigo
352+
path: pkg/operator/util/emit/emit\.go
353+
# volume_stats_test cannot import the test package: test imports volume_stats (cycle)
354+
- linters:
355+
- forbidigo
356+
path: pkg/reconcilers/etcd_cluster/volume_stats/volume_stats_test\.go
341357

342358
run:
343359
timeout: 10m

api/v1alpha1/webhook/hostedcontrolplane_webhook_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
. "github.com/onsi/gomega"
77
"github.com/teutonet/cluster-api-provider-hosted-control-plane/api/v1alpha1"
88
"github.com/teutonet/cluster-api-provider-hosted-control-plane/pkg/importcycle"
9+
. "github.com/teutonet/cluster-api-provider-hosted-control-plane/test"
910
"k8s.io/apimachinery/pkg/api/resource"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"k8s.io/utils/ptr"
@@ -186,7 +187,7 @@ func TestHostedControlPlaneWebhook_ValidateCreate(t *testing.T) {
186187

187188
for _, tt := range tests {
188189
t.Run(tt.name, func(t *testing.T) {
189-
g := NewWithT(t)
190+
g, _, _ := G(t)
190191
_, err := webhook.ValidateCreate(t.Context(), tt.hcp)
191192

192193
if tt.expectErr {
@@ -318,7 +319,7 @@ func TestHostedControlPlaneWebhook_ValidateUpdate(t *testing.T) {
318319

319320
for _, tt := range tests {
320321
t.Run(tt.name, func(t *testing.T) {
321-
g := NewWithT(t)
322+
g, _, _ := G(t)
322323
_, err := webhook.ValidateUpdate(t.Context(), tt.oldHCP, tt.newHCP)
323324

324325
if tt.expectErr {
@@ -332,7 +333,7 @@ func TestHostedControlPlaneWebhook_ValidateUpdate(t *testing.T) {
332333

333334
func TestHostedControlPlaneWebhook_ValidateDelete(t *testing.T) {
334335
webhook := &hostedControlPlaneWebhook{}
335-
g := NewWithT(t)
336+
g, _, _ := G(t)
336337

337338
hcp := &v1alpha1.HostedControlPlane{
338339
ObjectMeta: metav1.ObjectMeta{
@@ -386,7 +387,7 @@ func TestHostedControlPlaneWebhook_ParseVersion(t *testing.T) {
386387

387388
for _, tt := range tests {
388389
t.Run(tt.name, func(t *testing.T) {
389-
g := NewWithT(t)
390+
g, _, _ := G(t)
390391
hcp := &v1alpha1.HostedControlPlane{
391392
Spec: v1alpha1.HostedControlPlaneSpec{
392393
Version: tt.version,

pkg/hostedcontrolplane/controller.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/teutonet/cluster-api-provider-hosted-control-plane/api"
2020
"github.com/teutonet/cluster-api-provider-hosted-control-plane/api/v1alpha1"
2121
"github.com/teutonet/cluster-api-provider-hosted-control-plane/pkg/importcycle"
22+
"github.com/teutonet/cluster-api-provider-hosted-control-plane/pkg/operator/util/emit"
2223
"github.com/teutonet/cluster-api-provider-hosted-control-plane/pkg/operator/util/recorder"
2324
"github.com/teutonet/cluster-api-provider-hosted-control-plane/pkg/reconcilers/alias"
2425
"github.com/teutonet/cluster-api-provider-hosted-control-plane/pkg/reconcilers/apiserverresources"
@@ -386,7 +387,10 @@ func (r *hostedControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R
386387
return ctrl.Result{}, fmt.Errorf("failed to retrieve owner Cluster: %w", err)
387388
}
388389
if cluster == nil {
389-
span.AddEvent("Cluster Controller has not yet set OwnerRef")
390+
emit.Info(ctx, emit.SinkSpanEvent, hostedControlPlane,
391+
"ClusterOwnerRefMissing", "OwnerRefCheck",
392+
"Cluster Controller has not yet set OwnerRef",
393+
)
390394
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
391395
}
392396

@@ -405,12 +409,13 @@ func (r *hostedControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R
405409
clusterMatch = cluster.Name == r.reconcileFilter
406410
}
407411
if !hcpMatch && !clusterMatch {
408-
logr.FromContextAsSlogLogger(ctx).
409-
InfoContext(ctx, "skipping reconciliation due to reconcile filter",
410-
"filter", r.reconcileFilter,
411-
"hcp", hostedControlPlane.Namespace+"/"+hostedControlPlane.Name,
412-
"cluster", cluster.Namespace+"/"+cluster.Name,
413-
)
412+
emit.Info(ctx, emit.SinkLogger, hostedControlPlane,
413+
"ReconcileFilterMismatch", "SkipReconcile",
414+
"skipping reconciliation due to reconcile filter",
415+
"filter", r.reconcileFilter,
416+
"hcp", hostedControlPlane.Namespace+"/"+hostedControlPlane.Name,
417+
"cluster", cluster.Namespace+"/"+cluster.Name,
418+
)
414419
return reconcile.Result{}, nil
415420
}
416421
}
@@ -588,7 +593,6 @@ func (r *hostedControlPlaneReconciler) reconcileNormal(
588593
r.caCertificatesDuration,
589594
r.certificatesDuration,
590595
r.konnectivityServerAudience,
591-
recorder.FromContext(ctx),
592596
)
593597
kubeconfigReconciler := kubeconfig.NewKubeconfigReconciler(
594598
r.managementClusterClient,
@@ -605,7 +609,6 @@ func (r *hostedControlPlaneReconciler) reconcileNormal(
605609
r.etcdClientFactory,
606610
r.s3ClientFactory,
607611
r.volumeStatsProvider,
608-
recorder.FromContext(ctx),
609612
r.etcdComponentLabel,
610613
r.apiServerComponentLabel,
611614
r.controllerNamespace,

pkg/hostedcontrolplane/controller_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/teutonet/cluster-api-provider-hosted-control-plane/pkg/reconcilers/alias"
1414
"github.com/teutonet/cluster-api-provider-hosted-control-plane/pkg/reconcilers/etcd_cluster/etcd_client"
1515
"github.com/teutonet/cluster-api-provider-hosted-control-plane/pkg/reconcilers/etcd_cluster/s3_client"
16-
"github.com/teutonet/cluster-api-provider-hosted-control-plane/test"
16+
. "github.com/teutonet/cluster-api-provider-hosted-control-plane/test"
1717
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1818
"k8s.io/apimachinery/pkg/runtime"
1919
"k8s.io/apimachinery/pkg/types"
@@ -34,7 +34,7 @@ var (
3434
_ *v1alpha1.HostedControlPlane,
3535
_ *capiv2.Cluster,
3636
) (s3_client.S3Client, error) {
37-
return test.NewS3ClientStub(), nil
37+
return NewS3ClientStub(), nil
3838
}
3939
etcdClientStubFactory = func(
4040
_ context.Context,
@@ -43,7 +43,7 @@ var (
4343
_ *capiv2.Cluster,
4444
_ int32,
4545
) (etcd_client.EtcdClient, error) {
46-
return test.NewEtcdClientStub(), nil
46+
return NewEtcdClientStub(), nil
4747
}
4848
workloadClusterClientStubFactory = func(
4949
_ context.Context,
@@ -71,7 +71,7 @@ func createTestReconcilerWithFilter(client client.Client, reconcileFilter string
7171
workloadClusterClientStubFactory,
7272
etcdClientStubFactory,
7373
s3ClientStubFactory,
74-
test.NewEtcdVolumeStatsProviderStub(),
74+
NewEtcdVolumeStatsProviderStub(),
7575
&recorder.InfiniteDiscardingFakeRecorder{},
7676
"test-namespace",
7777
reconcileFilter,
@@ -220,7 +220,7 @@ func TestHostedControlPlaneReconciler_ReconcileWorkflow(t *testing.T) {
220220
for _, tt := range tests {
221221
t.Run(tt.name, func(t *testing.T) {
222222
ctx := log.IntoContext(t.Context(), log.Log)
223-
g := NewWithT(t)
223+
g, _, _ := G(t)
224224
scheme := runtime.NewScheme()
225225
g.Expect(capiv2.AddToScheme(scheme)).To(Succeed())
226226
g.Expect(v1alpha1.AddToScheme(scheme)).To(Succeed())
@@ -270,13 +270,13 @@ func TestHostedControlPlaneReconciler_ReconcileWorkflow(t *testing.T) {
270270

271271
func TestHostedControlPlaneReconciler_FinalizerManagement(t *testing.T) {
272272
scheme := runtime.NewScheme()
273-
g := NewWithT(t)
273+
g, _, _ := G(t)
274274
g.Expect(v1alpha1.AddToScheme(scheme)).To(Succeed())
275275
g.Expect(capiv2.AddToScheme(scheme)).To(Succeed())
276276

277277
t.Run("finalizer behavior during reconcile lifecycle", func(t *testing.T) {
278278
ctx := log.IntoContext(t.Context(), log.Log)
279-
g := NewWithT(t)
279+
g, _, _ := G(t)
280280
cluster := createTestCluster("test-cluster", "default")
281281
hostedControlPlane := withReplicas(
282282
withOwnerReference(createTestHostedControlPlane("test-hcp", "default"), cluster),
@@ -319,7 +319,7 @@ func TestHostedControlPlaneReconciler_FinalizerManagement(t *testing.T) {
319319

320320
t.Run("finalizer should be removed during deletion", func(t *testing.T) {
321321
ctx := log.IntoContext(t.Context(), log.Log)
322-
g := NewWithT(t)
322+
g, _, _ := G(t)
323323
cluster := createTestClusterWithPausedCondition("test-cluster", "default", false)
324324
hostedControlPlane := withDeletion(
325325
withOwnerReference(createTestHostedControlPlane("test-hcp", "default"), cluster),
@@ -366,13 +366,13 @@ func TestHostedControlPlaneReconciler_FinalizerManagement(t *testing.T) {
366366

367367
func TestHostedControlPlaneReconciler_OwnerReferenceValidation(t *testing.T) {
368368
scheme := runtime.NewScheme()
369-
g := NewWithT(t)
369+
g, _, _ := G(t)
370370
g.Expect(v1alpha1.AddToScheme(scheme)).To(Succeed())
371371
g.Expect(capiv2.AddToScheme(scheme)).To(Succeed())
372372

373373
t.Run("should requeue when owner cluster is not found", func(t *testing.T) {
374374
ctx := log.IntoContext(t.Context(), log.Log)
375-
g := NewWithT(t)
375+
g, _, _ := G(t)
376376
hostedControlPlane := createTestHostedControlPlane("test-hcp", "default")
377377

378378
fakeClient := fakeClient.NewClientBuilder().
@@ -399,7 +399,7 @@ func TestHostedControlPlaneReconciler_OwnerReferenceValidation(t *testing.T) {
399399

400400
t.Run("should proceed when valid owner cluster is found", func(t *testing.T) {
401401
ctx := log.IntoContext(t.Context(), log.Log)
402-
g := NewWithT(t)
402+
g, _, _ := G(t)
403403
cluster := createTestCluster("test-cluster", "default")
404404
hostedControlPlane := withReplicas(
405405
withOwnerReference(createTestHostedControlPlane("test-hcp", "default"), cluster),
@@ -436,13 +436,13 @@ func TestHostedControlPlaneReconciler_OwnerReferenceValidation(t *testing.T) {
436436

437437
func TestHostedControlPlaneReconciler_StatusConditions(t *testing.T) {
438438
scheme := runtime.NewScheme()
439-
g := NewWithT(t)
439+
g, _, _ := G(t)
440440
g.Expect(v1alpha1.AddToScheme(scheme)).To(Succeed())
441441
g.Expect(capiv2.AddToScheme(scheme)).To(Succeed())
442442

443443
t.Run("should set paused condition when cluster is paused", func(t *testing.T) {
444444
ctx := log.IntoContext(t.Context(), log.Log)
445-
g := NewWithT(t)
445+
g, _, _ := G(t)
446446
cluster := withPaused(createTestCluster("test-cluster", "default"), true)
447447
hostedControlPlane := withOwnerReference(createTestHostedControlPlane("test-hcp", "default"), cluster)
448448

@@ -488,7 +488,7 @@ func TestHostedControlPlaneReconciler_StatusConditions(t *testing.T) {
488488

489489
func TestHostedControlPlaneReconciler_ObservedGeneration(t *testing.T) {
490490
scheme := runtime.NewScheme()
491-
g := NewWithT(t)
491+
g, _, _ := G(t)
492492
g.Expect(v1alpha1.AddToScheme(scheme)).To(Succeed())
493493
g.Expect(capiv2.AddToScheme(scheme)).To(Succeed())
494494

@@ -540,7 +540,7 @@ func TestHostedControlPlaneReconciler_ObservedGeneration(t *testing.T) {
540540
for _, tt := range tests {
541541
t.Run(tt.name, func(t *testing.T) {
542542
ctx := log.IntoContext(t.Context(), log.Log)
543-
g := NewWithT(t)
543+
g, _, _ := G(t)
544544

545545
objs := []client.Object{tt.hostedControlPlane}
546546
if tt.cluster != nil {
@@ -590,7 +590,7 @@ func TestHostedControlPlaneReconciler_ObservedGeneration(t *testing.T) {
590590

591591
func TestHostedControlPlaneReconciler_NonExistentResource(t *testing.T) {
592592
scheme := runtime.NewScheme()
593-
g := NewWithT(t)
593+
g, _, _ := G(t)
594594
g.Expect(v1alpha1.AddToScheme(scheme)).To(Succeed())
595595

596596
fakeClient := fakeClient.NewClientBuilder().
@@ -613,7 +613,7 @@ func TestHostedControlPlaneReconciler_NonExistentResource(t *testing.T) {
613613

614614
func TestHostedControlPlaneReconciler_ReconcileFilter(t *testing.T) {
615615
scheme := runtime.NewScheme()
616-
g := NewWithT(t)
616+
g, _, _ := G(t)
617617
g.Expect(v1alpha1.AddToScheme(scheme)).To(Succeed())
618618
g.Expect(capiv2.AddToScheme(scheme)).To(Succeed())
619619

@@ -644,7 +644,7 @@ func TestHostedControlPlaneReconciler_ReconcileFilter(t *testing.T) {
644644
hasFinalizer := func(t *testing.T, fc client.Client) bool {
645645
t.Helper()
646646
updated := &v1alpha1.HostedControlPlane{}
647-
g := NewWithT(t)
647+
g, _, _ := G(t)
648648
g.Expect(fc.Get(t.Context(), req.NamespacedName, updated)).To(Succeed())
649649
return len(updated.Finalizers) > 0
650650
}
@@ -693,7 +693,7 @@ func TestHostedControlPlaneReconciler_ReconcileFilter(t *testing.T) {
693693

694694
for _, tt := range tests {
695695
t.Run(tt.name, func(t *testing.T) {
696-
g := NewWithT(t)
696+
g, _, _ := G(t)
697697
fc := buildClient()
698698
reconciler := createTestReconcilerWithFilter(fc, tt.filter)
699699

0 commit comments

Comments
 (0)