Skip to content
Open
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
41 changes: 38 additions & 3 deletions documentdb-kubectl-plugin/cmd/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import (
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

"github.com/documentdb/documentdb-operator/api/preview"
)

const (
Expand All @@ -31,6 +34,7 @@ type promoteOptions struct {
targetCluster string
targetContext string
skipWait bool
failover bool
waitTimeout time.Duration
pollInterval time.Duration
}
Expand All @@ -55,6 +59,7 @@ func newPromoteCommand() *cobra.Command {
cmd.Flags().StringVar(&opts.targetCluster, "target-cluster", opts.targetCluster, "Name of the cluster that should become primary (required)")
cmd.Flags().StringVar(&opts.targetContext, "cluster-context", opts.targetContext, "Kubeconfig context for verifying member status (defaults to current context)")
cmd.Flags().BoolVar(&opts.skipWait, "skip-wait", opts.skipWait, "Return immediately after submitting the promotion request")
cmd.Flags().BoolVar(&opts.failover, "failover", opts.failover, "Perform a failover promotion (may result in data loss)")
cmd.Flags().DurationVar(&opts.waitTimeout, "wait-timeout", 10*time.Minute, "Maximum time to wait for the promotion to complete")
cmd.Flags().DurationVar(&opts.pollInterval, "poll-interval", 10*time.Second, "Polling interval while waiting for the promotion to complete")

Expand Down Expand Up @@ -148,11 +153,41 @@ func (o *promoteOptions) run(ctx context.Context, cmd *cobra.Command) error {
func (o *promoteOptions) patchDocumentDB(ctx context.Context, dyn dynamic.Interface) error {
gvr := schema.GroupVersionResource{Group: documentDBGVRGroup, Version: documentDBGVRVersion, Resource: documentDBGVRResource}

clusterReplicationPatch := map[string]any{
"primary": o.targetCluster,
}

// If failover is true, remove the old primary from clusterList
if o.failover {
unstructuredDoc, err := dyn.Resource(gvr).Namespace(o.namespace).Get(ctx, o.documentDBName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get DocumentDB %q: %w", o.documentDBName, err)
}

var doc preview.DocumentDB
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredDoc.Object, &doc); err != nil {
return fmt.Errorf("failed to convert DocumentDB %q to typed object: %w", o.documentDBName, err)
}

if doc.Spec.ClusterReplication == nil {
return fmt.Errorf("DocumentDB %q does not have clusterReplication configured", o.documentDBName)
}

oldPrimary := doc.Spec.ClusterReplication.Primary
if oldPrimary != "" && len(doc.Spec.ClusterReplication.ClusterList) > 0 {
var newClusterList []preview.MemberCluster
for _, cluster := range doc.Spec.ClusterReplication.ClusterList {
if cluster.Name != oldPrimary {
newClusterList = append(newClusterList, cluster)
}
}
clusterReplicationPatch["clusterList"] = newClusterList
}
}

patch := map[string]any{
"spec": map[string]any{
"clusterReplication": map[string]any{
"primary": o.targetCluster,
},
"clusterReplication": clusterReplicationPatch,
},
}

Expand Down
82 changes: 82 additions & 0 deletions documentdb-kubectl-plugin/cmd/promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,88 @@ func TestPatchDocumentDB(t *testing.T) {
}
}

func TestPatchDocumentDBFailover(t *testing.T) {
t.Parallel()
gvr := documentDBGVR()

namespace := defaultDocumentDBNamespace
docName := "sample"

doc := newDocumentWithClusterList(docName, namespace, "cluster-a", "Ready", []string{"cluster-a", "cluster-b", "cluster-c"})

client := newFakeDynamicClient(doc.DeepCopy())

opts := &promoteOptions{
documentDBName: docName,
namespace: namespace,
targetCluster: "cluster-b",
failover: true,
}

if err := opts.patchDocumentDB(context.Background(), client); err != nil {
t.Fatalf("patchDocumentDB returned error: %v", err)
}

patched, err := client.Resource(gvr).Namespace(namespace).Get(context.Background(), docName, metav1.GetOptions{})
if err != nil {
t.Fatalf("failed to fetch patched document: %v", err)
}

primary, _, err := unstructured.NestedString(patched.Object, "spec", "clusterReplication", "primary")
if err != nil {
t.Fatalf("failed to read patched primary: %v", err)
}
if primary != "cluster-b" {
t.Fatalf("expected primary cluster-b, got %q", primary)
}

clusterList, _, err := unstructured.NestedSlice(patched.Object, "spec", "clusterReplication", "clusterList")
if err != nil {
t.Fatalf("failed to read patched clusterList: %v", err)
}

// Verify old primary (cluster-a) was removed from clusterList
for _, cluster := range clusterList {
clusterMap, ok := cluster.(map[string]any)
if !ok {
continue
}
name, _, _ := unstructured.NestedString(clusterMap, "name")
if name == "cluster-a" {
t.Fatal("expected old primary cluster-a to be removed from clusterList")
}
}

// Verify remaining clusters are still present
if len(clusterList) != 2 {
t.Fatalf("expected 2 clusters in clusterList after failover, got %d", len(clusterList))
}
}

func newDocumentWithClusterList(name, namespace, primary, phase string, clusters []string) *unstructured.Unstructured {
clusterList := make([]any, 0, len(clusters))
for _, c := range clusters {
clusterList = append(clusterList, map[string]any{"name": c})
}

doc := &unstructured.Unstructured{Object: map[string]any{
"spec": map[string]any{
"clusterReplication": map[string]any{
"primary": primary,
"clusterList": clusterList,
},
},
"status": map[string]any{
"status": phase,
},
}}
gvk := schema.GroupVersionKind{Group: documentDBGVRGroup, Version: documentDBGVRVersion, Kind: "DocumentDB"}
doc.SetGroupVersionKind(gvk)
doc.SetName(name)
doc.SetNamespace(namespace)
return doc
}

func setDocumentState(ctx context.Context, client dynamic.Interface, gvr schema.GroupVersionResource, namespace, name, primary, phase string) error {
for {
obj, err := client.Resource(gvr).Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
Expand Down
108 changes: 74 additions & 34 deletions documentdb-kubectl-plugin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,90 @@ module github.com/documentdb/documentdb-operator/documentdb-kubectl-plugin
go 1.25.7

require (
github.com/spf13/cobra v1.9.1
k8s.io/api v0.32.2
k8s.io/apimachinery v0.32.2
k8s.io/client-go v0.32.2
github.com/documentdb/documentdb-operator v0.0.0
github.com/spf13/cobra v1.10.2
k8s.io/api v0.35.0
k8s.io/apimachinery v0.35.0
k8s.io/client-go v0.35.0
)

replace github.com/documentdb/documentdb-operator => ../operator/src

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudnative-pg/barman-cloud v0.4.1-0.20260108104508-ced266c145f5 // indirect
github.com/cloudnative-pg/cloudnative-pg v1.28.1 // indirect
github.com/cloudnative-pg/cnpg-i v0.3.1 // indirect
github.com/cloudnative-pg/machinery v0.3.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.22.4 // indirect
github.com/go-openapi/jsonreference v0.21.4 // indirect
github.com/go-openapi/swag v0.25.4 // indirect
github.com/go-openapi/swag/cmdutils v0.25.4 // indirect
github.com/go-openapi/swag/conv v0.25.4 // indirect
github.com/go-openapi/swag/fileutils v0.25.4 // indirect
github.com/go-openapi/swag/jsonname v0.25.4 // indirect
github.com/go-openapi/swag/jsonutils v0.25.4 // indirect
github.com/go-openapi/swag/loading v0.25.4 // indirect
github.com/go-openapi/swag/mangling v0.25.4 // indirect
github.com/go-openapi/swag/netutils v0.25.4 // indirect
github.com/go-openapi/swag/stringutils v0.25.4 // indirect
github.com/go-openapi/swag/typeutils v0.25.4 // indirect
github.com/go-openapi/swag/yamlutils v0.25.4 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/gnostic-models v0.7.1 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/kubernetes-csi/external-snapshotter/client/v8 v8.4.0 // indirect
github.com/lib/pq v1.11.1 // indirect
github.com/moby/spdystream v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.87.1 // indirect
github.com/prometheus/client_golang v1.23.2 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.67.4 // indirect
github.com/prometheus/procfs v0.19.2 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/oauth2 v0.27.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/term v0.30.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.7.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.1 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/net v0.49.0 // indirect
golang.org/x/oauth2 v0.34.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.40.0 // indirect
golang.org/x/term v0.39.0 // indirect
golang.org/x/text v0.33.0 // indirect
golang.org/x/time v0.14.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.5.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20251029180050-ab9386a59fda // indirect
google.golang.org/grpc v1.78.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.35.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
k8s.io/kube-openapi v0.0.0-20251125145642-4e65d59e963e // indirect
k8s.io/utils v0.0.0-20260108192941-914a6e750570 // indirect
sigs.k8s.io/controller-runtime v0.22.4 // indirect
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
sigs.k8s.io/randfill v1.0.0 // indirect
sigs.k8s.io/structured-merge-diff/v6 v6.3.1 // indirect
sigs.k8s.io/yaml v1.6.0 // indirect
)
Loading
Loading