From 5c65466f084521a8b8cfc05dc1b2b5d86a2af16f Mon Sep 17 00:00:00 2001 From: Adi Muraru Date: Tue, 24 Nov 2026 14:38:34 +0100 Subject: [PATCH] fix(cruisecontrol): match disk removal log dirs exactly --- controllers/cruisecontroltask_controller.go | 21 ++--- .../cruisecontroltask_controller_test.go | 83 ++++++++++++++++++- 2 files changed, 86 insertions(+), 18 deletions(-) diff --git a/controllers/cruisecontroltask_controller.go b/controllers/cruisecontroltask_controller.go index d2092b9b6..2fae5c77d 100644 --- a/controllers/cruisecontroltask_controller.go +++ b/controllers/cruisecontroltask_controller.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "reflect" + "slices" "strings" "emperror.dev/errors" @@ -217,24 +218,12 @@ func (r *CruiseControlTaskReconciler) Reconcile(ctx context.Context, request ctr brokerID := task.BrokerID volume := task.Volume - if _, ok := brokerLogDirsToRemove[brokerID]; !ok { - brokerLogDirsToRemove[brokerID] = []string{} - } - - found := false - if onlineDirs, ok := logDirsByBroker[brokerID][scale.LogDirStateOnline]; ok { - for _, dir := range onlineDirs { - if strings.HasPrefix(strings.TrimSpace(dir), strings.TrimSpace(volume)) { - brokerLogDirsToRemove[brokerID] = append(brokerLogDirsToRemove[brokerID], dir) - found = true - break - } - } - } - - if !found { + // Kafka reports a single separator even when the mount path ends in a slash. + logDir := util.StorageConfigKafkaMountPath(strings.TrimRight(volume, "/")) + if !slices.Contains(logDirsByBroker[brokerID][scale.LogDirStateOnline], logDir) { 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")) } + brokerLogDirsToRemove[brokerID] = append(brokerLogDirsToRemove[brokerID], logDir) } // create the cruise control operation diff --git a/controllers/cruisecontroltask_controller_test.go b/controllers/cruisecontroltask_controller_test.go index 1b467fdfe..81e3637ca 100644 --- a/controllers/cruisecontroltask_controller_test.go +++ b/controllers/cruisecontroltask_controller_test.go @@ -20,17 +20,20 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - - "github.com/banzaicloud/koperator/pkg/scale" + "sigs.k8s.io/controller-runtime/pkg/client/fake" banzaiv1alpha1 "github.com/banzaicloud/koperator/api/v1alpha1" "github.com/banzaicloud/koperator/api/v1beta1" + scalemocks "github.com/banzaicloud/koperator/controllers/tests/mocks" "github.com/banzaicloud/koperator/pkg/resources/kafka/mocks" + "github.com/banzaicloud/koperator/pkg/scale" ) func TestBrokersJBODSelector(t *testing.T) { @@ -426,3 +429,79 @@ func TestCreateCCOperation(t *testing.T) { testCase.parameterCheck(t, createdOperation.Status.CurrentTask.Parameters) } } + +func TestReconcileDiskRemovalMatchesExactLogDir(t *testing.T) { + const brokerID = "1" + testCases := []struct { + name string + mountPath string + onlineDirs []string + wantTarget string + }{ + { + name: "sibling path reported first", + mountPath: "/kafka-logs1", + onlineDirs: []string{"/kafka-logs10/kafka", "/kafka-logs1/kafka"}, + wantTarget: "1-/kafka-logs1/kafka", + }, + { + name: "Kafka normalizes a trailing separator", + mountPath: "/kafka-logs1/", + onlineDirs: []string{"/kafka-logs10/kafka", "/kafka-logs1/kafka"}, + wantTarget: "1-/kafka-logs1/kafka", + }, + { + name: "requested path not online on this broker", + mountPath: "/kafka-logs1", + onlineDirs: []string{"/kafka-logs10/kafka"}, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + ctx := context.Background() + scheme := runtime.NewScheme() + require.NoError(t, v1beta1.AddToScheme(scheme)) + require.NoError(t, banzaiv1alpha1.AddToScheme(scheme)) + cluster := &v1beta1.KafkaCluster{ + ObjectMeta: metav1.ObjectMeta{Name: "kafka", Namespace: "kafka"}, + Status: v1beta1.KafkaClusterStatus{BrokersState: map[string]v1beta1.BrokerState{ + brokerID: {GracefulActionState: v1beta1.GracefulActionState{VolumeStates: map[string]v1beta1.VolumeState{ + tc.mountPath: {CruiseControlVolumeState: v1beta1.GracefulDiskRemovalRequired}, + }}}, + }}, + } + kube := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cluster). + WithStatusSubresource(cluster, &banzaiv1alpha1.CruiseControlOperation{}).Build() + scaler := scalemocks.NewMockCruiseControlScaler(gomock.NewController(t)) + scaler.EXPECT().LogDirsByBroker(ctx).Return(map[string]map[scale.LogDirState][]string{ + brokerID: {scale.LogDirStateOnline: tc.onlineDirs}, + "2": {scale.LogDirStateOnline: {"/kafka-logs1/kafka"}}, + }, nil) + r := &CruiseControlTaskReconciler{ + Client: kube, DirectClient: kube, Scheme: scheme, ScaleFactory: scalemocks.NewMockScaleFactory(scaler), + } + + key := client.ObjectKeyFromObject(cluster) + _, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: key}) + operations := &banzaiv1alpha1.CruiseControlOperationList{} + require.NoError(t, kube.List(ctx, operations, client.InNamespace(cluster.Namespace))) + require.NoError(t, kube.Get(ctx, key, cluster)) + state := cluster.Status.BrokersState[brokerID].GracefulActionState.VolumeStates[tc.mountPath] + if tc.wantTarget == "" { + assert.Error(t, err) + assert.Empty(t, operations.Items) + assert.Equal(t, v1beta1.GracefulDiskRemovalRequired, state.CruiseControlVolumeState) + assert.Nil(t, state.CruiseControlOperationReference) + return + } + require.NoError(t, err) + require.Len(t, operations.Items, 1) + operation := &operations.Items[0] + assert.Equal(t, banzaiv1alpha1.OperationRemoveDisks, operation.CurrentTaskOperation()) + assert.Equal(t, tc.wantTarget, operation.CurrentTaskParameters()[scale.ParamBrokerIDAndLogDirs]) + assert.Equal(t, v1beta1.GracefulDiskRemovalScheduled, state.CruiseControlVolumeState) + require.NotNil(t, state.CruiseControlOperationReference) + assert.Equal(t, operation.Name, state.CruiseControlOperationReference.Name) + }) + } +}