Skip to content

Commit 1750610

Browse files
committed
fix(cruisecontrol): match disk removal log dirs exactly
1 parent a852d6b commit 1750610

2 files changed

Lines changed: 86 additions & 18 deletions

File tree

‎controllers/cruisecontroltask_controller.go‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"reflect"
23+
"slices"
2324
"strings"
2425

2526
"emperror.dev/errors"
@@ -217,24 +218,12 @@ func (r *CruiseControlTaskReconciler) Reconcile(ctx context.Context, request ctr
217218

218219
brokerID := task.BrokerID
219220
volume := task.Volume
220-
if _, ok := brokerLogDirsToRemove[brokerID]; !ok {
221-
brokerLogDirsToRemove[brokerID] = []string{}
222-
}
223-
224-
found := false
225-
if onlineDirs, ok := logDirsByBroker[brokerID][scale.LogDirStateOnline]; ok {
226-
for _, dir := range onlineDirs {
227-
if strings.HasPrefix(strings.TrimSpace(dir), strings.TrimSpace(volume)) {
228-
brokerLogDirsToRemove[brokerID] = append(brokerLogDirsToRemove[brokerID], dir)
229-
found = true
230-
break
231-
}
232-
}
233-
}
234-
235-
if !found {
221+
// Kafka reports a single separator even when the mount path ends in a slash.
222+
logDir := util.StorageConfigKafkaMountPath(strings.TrimRight(volume, "/"))
223+
if !slices.Contains(logDirsByBroker[brokerID][scale.LogDirStateOnline], logDir) {
236224
return requeueWithError(log, fmt.Sprintf("volume %s not found for broker %s in CC online log dirs", volume, brokerID), errors.New("log dir not found"))
237225
}
226+
brokerLogDirsToRemove[brokerID] = append(brokerLogDirsToRemove[brokerID], logDir)
238227
}
239228

240229
// create the cruise control operation

‎controllers/cruisecontroltask_controller_test.go‎

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@ import (
2020
"testing"
2121

2222
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
2324
"go.uber.org/mock/gomock"
2425
corev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/runtime"
28+
ctrl "sigs.k8s.io/controller-runtime"
2729
"sigs.k8s.io/controller-runtime/pkg/client"
28-
29-
"github.com/banzaicloud/koperator/pkg/scale"
30+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3031

3132
banzaiv1alpha1 "github.com/banzaicloud/koperator/api/v1alpha1"
3233
"github.com/banzaicloud/koperator/api/v1beta1"
34+
scalemocks "github.com/banzaicloud/koperator/controllers/tests/mocks"
3335
"github.com/banzaicloud/koperator/pkg/resources/kafka/mocks"
36+
"github.com/banzaicloud/koperator/pkg/scale"
3437
)
3538

3639
func TestBrokersJBODSelector(t *testing.T) {
@@ -426,3 +429,79 @@ func TestCreateCCOperation(t *testing.T) {
426429
testCase.parameterCheck(t, createdOperation.Status.CurrentTask.Parameters)
427430
}
428431
}
432+
433+
func TestReconcileDiskRemovalMatchesExactLogDir(t *testing.T) {
434+
const brokerID = "1"
435+
testCases := []struct {
436+
name string
437+
mountPath string
438+
onlineDirs []string
439+
wantTarget string
440+
}{
441+
{
442+
name: "sibling path reported first",
443+
mountPath: "/kafka-logs1",
444+
onlineDirs: []string{"/kafka-logs10/kafka", "/kafka-logs1/kafka"},
445+
wantTarget: "1-/kafka-logs1/kafka",
446+
},
447+
{
448+
name: "Kafka normalizes a trailing separator",
449+
mountPath: "/kafka-logs1/",
450+
onlineDirs: []string{"/kafka-logs10/kafka", "/kafka-logs1/kafka"},
451+
wantTarget: "1-/kafka-logs1/kafka",
452+
},
453+
{
454+
name: "requested path not online on this broker",
455+
mountPath: "/kafka-logs1",
456+
onlineDirs: []string{"/kafka-logs10/kafka"},
457+
},
458+
}
459+
for _, tc := range testCases {
460+
t.Run(tc.name, func(t *testing.T) {
461+
ctx := context.Background()
462+
scheme := runtime.NewScheme()
463+
require.NoError(t, v1beta1.AddToScheme(scheme))
464+
require.NoError(t, banzaiv1alpha1.AddToScheme(scheme))
465+
cluster := &v1beta1.KafkaCluster{
466+
ObjectMeta: metav1.ObjectMeta{Name: "kafka", Namespace: "kafka"},
467+
Status: v1beta1.KafkaClusterStatus{BrokersState: map[string]v1beta1.BrokerState{
468+
brokerID: {GracefulActionState: v1beta1.GracefulActionState{VolumeStates: map[string]v1beta1.VolumeState{
469+
tc.mountPath: {CruiseControlVolumeState: v1beta1.GracefulDiskRemovalRequired},
470+
}}},
471+
}},
472+
}
473+
kube := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cluster).
474+
WithStatusSubresource(cluster, &banzaiv1alpha1.CruiseControlOperation{}).Build()
475+
scaler := scalemocks.NewMockCruiseControlScaler(gomock.NewController(t))
476+
scaler.EXPECT().LogDirsByBroker(ctx).Return(map[string]map[scale.LogDirState][]string{
477+
brokerID: {scale.LogDirStateOnline: tc.onlineDirs},
478+
"2": {scale.LogDirStateOnline: {"/kafka-logs1/kafka"}},
479+
}, nil)
480+
r := &CruiseControlTaskReconciler{
481+
Client: kube, DirectClient: kube, Scheme: scheme, ScaleFactory: scalemocks.NewMockScaleFactory(scaler),
482+
}
483+
484+
key := client.ObjectKeyFromObject(cluster)
485+
_, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: key})
486+
operations := &banzaiv1alpha1.CruiseControlOperationList{}
487+
require.NoError(t, kube.List(ctx, operations, client.InNamespace(cluster.Namespace)))
488+
require.NoError(t, kube.Get(ctx, key, cluster))
489+
state := cluster.Status.BrokersState[brokerID].GracefulActionState.VolumeStates[tc.mountPath]
490+
if tc.wantTarget == "" {
491+
assert.Error(t, err)
492+
assert.Empty(t, operations.Items)
493+
assert.Equal(t, v1beta1.GracefulDiskRemovalRequired, state.CruiseControlVolumeState)
494+
assert.Nil(t, state.CruiseControlOperationReference)
495+
return
496+
}
497+
require.NoError(t, err)
498+
require.Len(t, operations.Items, 1)
499+
operation := &operations.Items[0]
500+
assert.Equal(t, banzaiv1alpha1.OperationRemoveDisks, operation.CurrentTaskOperation())
501+
assert.Equal(t, tc.wantTarget, operation.CurrentTaskParameters()[scale.ParamBrokerIDAndLogDirs])
502+
assert.Equal(t, v1beta1.GracefulDiskRemovalScheduled, state.CruiseControlVolumeState)
503+
require.NotNil(t, state.CruiseControlOperationReference)
504+
assert.Equal(t, operation.Name, state.CruiseControlOperationReference.Name)
505+
})
506+
}
507+
}

0 commit comments

Comments
 (0)