diff --git a/pkg/reconciliation/reconcile_racks.go b/pkg/reconciliation/reconcile_racks.go index 805a178d..d40012b2 100644 --- a/pkg/reconciliation/reconcile_racks.go +++ b/pkg/reconciliation/reconcile_racks.go @@ -2437,9 +2437,9 @@ func (rc *ReconciliationContext) datacenterPods() []*corev1.Pod { dcPods := FilterPodListByLabels(rc.clusterPods, dcSelector) if rc.Datacenter.Status.MetadataVersion < 1 && rc.Datacenter.Status.DatacenterName != nil && *rc.Datacenter.Status.DatacenterName == rc.Datacenter.Spec.DatacenterName { - rc.ReqLogger.Info("Fetching with the old metadata version also") + rc.ReqLogger.Info("Fetching datacenter pods with the old metadata version labels") dcSelector[api.DatacenterLabel] = api.CleanLabelValue(rc.Datacenter.Spec.DatacenterName) - rc.dcPods = append(rc.dcPods, FilterPodListByLabels(rc.clusterPods, dcSelector)...) + dcPods = append(dcPods, FilterPodListByLabels(rc.clusterPods, dcSelector)...) } return dcPods diff --git a/pkg/reconciliation/reconcile_racks_test.go b/pkg/reconciliation/reconcile_racks_test.go index bb53bd67..89cc2eee 100644 --- a/pkg/reconciliation/reconcile_racks_test.go +++ b/pkg/reconciliation/reconcile_racks_test.go @@ -6,7 +6,6 @@ package reconciliation import ( "context" "fmt" - "github.com/pkg/errors" "io" "net/http" "reflect" @@ -16,6 +15,8 @@ import ( "testing" "time" + "github.com/pkg/errors" + "k8s.io/utils/ptr" api "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" @@ -2780,3 +2781,96 @@ func TestDatacenterStatus(t *testing.T) { assert.NoError(err) assert.Equal(float64(0), val) } + +func TestDatacenterPods(t *testing.T) { + rc, _, cleanupMockScr := setupTest() + defer cleanupMockScr() + assert := assert.New(t) + + desiredStatefulSet, err := newStatefulSetForCassandraDatacenter( + nil, + "default", + rc.Datacenter, + 3) + assert.NoErrorf(err, "error occurred creating statefulset") + + desiredStatefulSet.Status.ReadyReplicas = *desiredStatefulSet.Spec.Replicas + + trackObjects := []runtime.Object{ + desiredStatefulSet, + rc.Datacenter, + } + + mockPods := mockReadyPodsForStatefulSet(desiredStatefulSet, rc.Datacenter.Spec.ClusterName, rc.Datacenter.Name) + for idx := range mockPods { + mp := mockPods[idx] + trackObjects = append(trackObjects, mp) + } + + rc.Client = fake.NewClientBuilder().WithStatusSubresource(rc.Datacenter).WithRuntimeObjects(trackObjects...).Build() + + nextRack := &RackInformation{} + nextRack.RackName = "default" + nextRack.NodeCount = 1 + nextRack.SeedCount = 1 + + rackInfo := []*RackInformation{nextRack} + + rc.desiredRackInformation = rackInfo + rc.statefulSets = make([]*appsv1.StatefulSet, len(rackInfo)) + + rc.clusterPods = mockPods + assert.Equal(int(*desiredStatefulSet.Spec.Replicas), len(rc.datacenterPods())) +} + +func TestDatacenterPodsOldLabels(t *testing.T) { + rc, _, cleanupMockScr := setupTest() + defer cleanupMockScr() + assert := assert.New(t) + + // We fake the process a bit to get old style naming and labels + rc.Datacenter.Name = "overrideMe" + + desiredStatefulSet, err := newStatefulSetForCassandraDatacenter( + nil, + "default", + rc.Datacenter, + 3) + assert.NoErrorf(err, "error occurred creating statefulset") + + desiredStatefulSet.Status.ReadyReplicas = *desiredStatefulSet.Spec.Replicas + + trackObjects := []runtime.Object{ + desiredStatefulSet, + rc.Datacenter, + } + + mockPods := mockReadyPodsForStatefulSet(desiredStatefulSet, rc.Datacenter.Spec.ClusterName, rc.Datacenter.Name) + for idx := range mockPods { + mp := mockPods[idx] + trackObjects = append(trackObjects, mp) + } + + rc.Client = fake.NewClientBuilder().WithStatusSubresource(rc.Datacenter).WithRuntimeObjects(trackObjects...).Build() + + nextRack := &RackInformation{} + nextRack.RackName = "default" + nextRack.NodeCount = 1 + nextRack.SeedCount = 1 + + rackInfo := []*RackInformation{nextRack} + + rc.desiredRackInformation = rackInfo + rc.statefulSets = make([]*appsv1.StatefulSet, len(rackInfo)) + + rc.clusterPods = mockPods + + // Lets modify the Datacenter names and set the status like it used to be in some older versions + rc.Datacenter.Spec.DatacenterName = "overrideMe" + rc.Datacenter.Name = "dc1" + rc.Datacenter.Status.DatacenterName = ptr.To[string]("overrideMe") + rc.Datacenter.Status.MetadataVersion = 0 + + // We should still find the pods + assert.Equal(int(*desiredStatefulSet.Spec.Replicas), len(rc.datacenterPods())) +} diff --git a/tests/upgrade_operator/upgrade_operator_suite_test.go b/tests/upgrade_operator/upgrade_operator_suite_test.go index d2a0b151..126ded2a 100644 --- a/tests/upgrade_operator/upgrade_operator_suite_test.go +++ b/tests/upgrade_operator/upgrade_operator_suite_test.go @@ -80,7 +80,7 @@ var _ = Describe(testName, func() { k := kubectl.ApplyFiles(dcYaml) ns.ExecAndLog(step, k) - ns.WaitForDatacenterReady(dcName) + ns.WaitForDatacenterOperatorProgress(dcName, "Ready", 1800) // Get UID of the cluster pod step = "get Cassandra pods UID"