Skip to content

Commit e1d69f8

Browse files
controller: Add migration label to pause reconciliation
To support migrating CockroachDB clusters from the public operator to the CockroachDB operator through coexistence, we need to prevent the operator from reverting manual changes made during the migration process. This includes preventing the scale-up of StatefulSet pods after manual scale-down and allowing modifications to Service labels (e.g., public service) which would otherwise be reset by the operator. This commit introduces a new migration label (`crdb.io/migrating`) in the v1alpha1 API. When this label is set to "true" on the CrdbCluster resource, the controller stops the entire reconciliation loop. This ensures the operator does not interfere with any resources during the migration phase.
1 parent eb6510b commit e1d69f8

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,3 @@ publish-operator-openshift:
373373
.PHONY: release/publish-openshift-bundle
374374
release/publish-openshift-bundle:
375375
./build/release/teamcity-publish-openshift-bundle.sh
376-

apis/v1alpha1/cluster_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ import (
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323
)
2424

25+
const (
26+
// CrdbOperatorMigrationLabel is the label used to indicate that the cluster is migrating.
27+
// If this label is set to "true", the operator will stop reconciling the
28+
// entire cluster.
29+
CrdbOperatorMigrationLabel = "crdb.io/skip-reconcile"
30+
)
31+
2532
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
2633
// Important: Run "make dev/generate" to regenerate code after modifying this file
2734

pkg/controller/cluster_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request
142142
}
143143
}
144144

145+
// If the cluster is migrating, we stop reconciliation
146+
if val, ok := cluster.Unwrap().Labels[api.CrdbOperatorMigrationLabel]; ok && val == "true" {
147+
log.Info("cluster is migrating, stopping reconciliation")
148+
return noRequeue()
149+
}
150+
145151
actorToExecute, err := r.Director.GetActorToExecute(ctx, &cluster, log)
146152
if err != nil {
147153
return requeueAfter(30*time.Second, nil)

pkg/controller/cluster_controller_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,44 @@ func TestReconcile(t *testing.T) {
147147
assert.Equal(t, tt.want, actual)
148148
})
149149
}
150+
}
151+
152+
func TestReconcileMigrationLabel(t *testing.T) {
153+
scheme := testutil.InitScheme(t)
154+
155+
ns := &corev1.Namespace{
156+
ObjectMeta: metav1.ObjectMeta{
157+
Name: "test-namespace",
158+
},
159+
}
160+
161+
cluster := testutil.NewBuilder("cluster").Namespaced(ns.Name).WithNodeCount(1).Cr()
162+
// Set status so we skip the "first reconcile" block
163+
cluster.Status.ClusterStatus = "Running"
164+
// Set the migration label
165+
cluster.Labels = map[string]string{
166+
api.CrdbOperatorMigrationLabel: "true",
167+
}
168+
169+
objs := []runtime.Object{
170+
ns,
171+
cluster,
172+
}
173+
174+
cl := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).WithStatusSubresource(cluster).Build()
175+
log := zapr.NewLogger(zaptest.NewLogger(t)).WithName("cluster-controller-test")
176+
req := ctrl.Request{NamespacedName: types.NamespacedName{Namespace: cluster.Namespace, Name: cluster.Name}}
177+
178+
r := &controller.ClusterReconciler{
179+
Client: cl,
180+
Log: log,
181+
Scheme: scheme,
182+
Director: &fakeDirector{
183+
actorToExecute: &fakeActor{},
184+
},
185+
}
150186

187+
actual, err := r.Reconcile(context.TODO(), req)
188+
require.NoError(t, err)
189+
assert.Equal(t, ctrl.Result{}, actual)
151190
}

pkg/resource/statefulset_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424
"path/filepath"
25+
"testing"
2526

2627
api "github.com/cockroachdb/cockroach-operator/apis/v1alpha1"
2728
"github.com/cockroachdb/cockroach-operator/pkg/labels"
@@ -32,8 +33,6 @@ import (
3233
"github.com/stretchr/testify/assert"
3334
"github.com/stretchr/testify/require"
3435
appsv1 "k8s.io/api/apps/v1"
35-
36-
"testing"
3736
)
3837

3938
var update = flag.Bool("update", false, "update the golden files of this test")

0 commit comments

Comments
 (0)