Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions controllers/cruisecontroltask_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"slices"
"strings"

"emperror.dev/errors"
Expand Down Expand Up @@ -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
Expand Down
83 changes: 81 additions & 2 deletions controllers/cruisecontroltask_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}
}