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
3 changes: 3 additions & 0 deletions pkg/resource-handler/controller/shard/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ func buildMultiorchContainer(shard *multigresv1alpha1.Shard, cellName string) co
watchTarget := fmt.Sprintf("%s/%s/%s",
shard.Spec.DatabaseName, shard.Spec.TableGroupName, shard.Spec.ShardName)

// Advertise the Service DNS name; unlike multipooler (per-pod), Multiorch is a Deployment behind one Service.
serviceName := buildMultiorchNameWithCell(shard, cellName, nameutil.ServiceConstraints)
args := []string{
"multiorch", // Subcommand
"--http-port=15300",
Expand All @@ -697,6 +699,7 @@ func buildMultiorchContainer(shard *multigresv1alpha1.Shard, cellName string) co
"--cell=" + cellName,
"--watch-targets=" + watchTarget,
"--log-level=" + string(shard.Spec.LogLevels.Multiorch),
fmt.Sprintf("--hostname=%s.%s.svc.cluster.local", serviceName, shard.Namespace),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have something advertise a per-pod address here? With multiorch.replicas > 1, each replica registers a distinct topology ID, but this gives them all the same load-balanced hostname. A request targeting orch A can therefore reach orch B, affecting /proxy/orch/{cell}/{id} and instance-specific RPCs such as DisableRecovery.

The new e2e test uses one replica. Can we add a two-replica case that verifies each advertised address reaches the corresponding instance? This is just to match the current setup we have for other tests.

}
if shardTLSConfigured(shard) {
args = append(args,
Expand Down
15 changes: 15 additions & 0 deletions pkg/resource-handler/controller/shard/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"k8s.io/utils/ptr"

multigresv1alpha1 "github.com/multigres/multigres-operator/api/v1alpha1"
nameutil "github.com/multigres/multigres-operator/pkg/util/name"
)

func TestBuildMultipoolerContainer(t *testing.T) {
Expand Down Expand Up @@ -705,6 +706,10 @@ func TestBuildMultiorchContainer(t *testing.T) {
}{
"default multiorch container": {
shard: &multigresv1alpha1.Shard{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Labels: map[string]string{"multigres.com/cluster": "test-cluster"},
},
Spec: multigresv1alpha1.ShardSpec{
DatabaseName: "testdb",
TableGroupName: "default",
Expand Down Expand Up @@ -736,6 +741,16 @@ func TestBuildMultiorchContainer(t *testing.T) {
"--cell=zone1",
"--watch-targets=testdb/default/0",
"--log-level=info",
"--hostname=" + buildMultiorchNameWithCell(&multigresv1alpha1.Shard{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"multigres.com/cluster": "test-cluster"},
},
Spec: multigresv1alpha1.ShardSpec{
DatabaseName: "testdb",
TableGroupName: "default",
ShardName: "0",
},
}, "zone1", nameutil.ServiceConstraints) + ".default.svc.cluster.local",
},
Ports: buildMultiorchContainerPorts(),
Resources: corev1.ResourceRequirements{},
Expand Down
10 changes: 10 additions & 0 deletions pkg/resource-handler/controller/shard/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,16 @@ func TestShardReconciliation(t *testing.T) {
obj.SetName(hashedDeployName)
deploy.Spec.Selector.MatchLabels["app.kubernetes.io/instance"] = clusterName
deploy.Spec.Template.ObjectMeta.Labels["app.kubernetes.io/instance"] = clusterName

// multiorch advertises the Service DNS name via --hostname.
for i := range deploy.Spec.Template.Spec.Containers {
c := &deploy.Spec.Template.Spec.Containers[i]
if c.Name == "multiorch" {
c.Args = append(c.Args, fmt.Sprintf(
"--hostname=%s.%s.svc.cluster.local", hashedSvcName, tc.shard.Namespace,
))
}
}
}
if svc, ok := obj.(*corev1.Service); ok {
obj.SetName(hashedSvcName)
Expand Down
30 changes: 30 additions & 0 deletions pkg/resource-handler/controller/shard/multiorch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func TestBuildMultiorchDeployment(t *testing.T) {
Spec: corev1.PodSpec{
Containers: []corev1.Container{
buildMultiorchContainer(&multigresv1alpha1.Shard{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Labels: map[string]string{
"multigres.com/cluster": "test-cluster",
},
},
Spec: multigresv1alpha1.ShardSpec{
DatabaseName: "testdb",
TableGroupName: "default",
Expand Down Expand Up @@ -205,6 +211,12 @@ func TestBuildMultiorchDeployment(t *testing.T) {
Spec: corev1.PodSpec{
Containers: []corev1.Container{
buildMultiorchContainer(&multigresv1alpha1.Shard{
ObjectMeta: metav1.ObjectMeta{
Namespace: "prod-ns",
Labels: map[string]string{
"multigres.com/cluster": "prod-cluster",
},
},
Spec: multigresv1alpha1.ShardSpec{
DatabaseName: "proddb",
TableGroupName: "prod-tg",
Expand Down Expand Up @@ -304,6 +316,12 @@ func TestBuildMultiorchDeployment(t *testing.T) {
Spec: corev1.PodSpec{
Containers: []corev1.Container{
buildMultiorchContainer(&multigresv1alpha1.Shard{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Labels: map[string]string{
"multigres.com/cluster": "repl-cluster",
},
},
Spec: multigresv1alpha1.ShardSpec{
DatabaseName: "testdb",
TableGroupName: "default",
Expand Down Expand Up @@ -438,6 +456,12 @@ func TestBuildMultiorchDeployment(t *testing.T) {
Spec: corev1.PodSpec{
Containers: []corev1.Container{
buildMultiorchContainer(&multigresv1alpha1.Shard{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Labels: map[string]string{
"multigres.com/cluster": "labels-cluster",
},
},
Spec: multigresv1alpha1.ShardSpec{
DatabaseName: "testdb",
TableGroupName: "default",
Expand Down Expand Up @@ -562,6 +586,12 @@ func TestBuildMultiorchDeployment(t *testing.T) {
Spec: corev1.PodSpec{
Containers: []corev1.Container{
buildMultiorchContainer(&multigresv1alpha1.Shard{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Labels: map[string]string{
"multigres.com/cluster": "override-cluster",
},
},
Spec: multigresv1alpha1.ShardSpec{
DatabaseName: "testdb",
TableGroupName: "default",
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/shared/rulechange/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build e2e

package rulechange_test

import (
"fmt"
"os"
"testing"

"github.com/multigres/multigres-operator/test/e2e/framework"
)

var cluster *framework.Cluster

func TestMain(m *testing.M) {
var err error
cluster, err = framework.EnsureSharedCluster()
if err != nil {
fmt.Fprintf(os.Stderr, "e2e setup: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
205 changes: 205 additions & 0 deletions test/e2e/shared/rulechange/rulechange_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
//go:build e2e

package rulechange_test

import (
"context"
"os/exec"
"regexp"
"strconv"
"strings"
"testing"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"

multigresv1alpha1 "github.com/multigres/multigres-operator/api/v1alpha1"
"github.com/multigres/multigres-operator/pkg/util/metadata"
"github.com/multigres/multigres-operator/test/e2e/framework"
)

// TestApplyCertifiedRuleChange is a regression test for a bug where
// multiadmin could not reach multiorch: multiorch advertised a pod-local
// hostname (via failed hostname discovery) that was unresolvable from
// multiadmin's pod. It exercises ApplyCertifiedRuleChange, the one RPC path
// where multiadmin dials multiorch directly using multiorch's
// self-registered topology hostname, by re-proposing the shard's current
// (already-installed) leader/cohort with --unsafe-derive-cert-from-reachable
// so no external cert material has to be computed by the test.
//
// The CLI runs from inside the multiadmin pod via kubectl exec, dialing
// "localhost" for the admin server, so only the multiadmin -> multiorch hop
// crosses real cluster DNS -- the hop that was broken.
func TestApplyCertifiedRuleChange(t *testing.T) {
t.Parallel()
ns := cluster.CreateNamespace(t)
c, err := cluster.CRClient()
if err != nil {
t.Fatalf("create CR client: %v", err)
}
ctx := context.Background()

cr := framework.MustLoadCluster("test/e2e/fixtures/base.yaml", ns)
cr.Name = "rule-change"
if err := c.Create(ctx, cr); err != nil {
t.Fatalf("create MultigresCluster: %v", err)
}

t.Log("waiting for the shard to become healthy")
cluster.WaitForAllPodsReady(t, ns)
shard := waitForHealthyShard(t, c, ns, cr.Name)

leader, cohort := currentLeaderAndCohort(t, c, ns, cr.Name, shard)
multiadminPod := multiadminPodName(t, c, ns, cr.Name)

args := []string{
"--kubeconfig", cluster.Kubeconfig,
"exec", "-n", ns, multiadminPod, "--",
"multigres", "cluster", "apply-rule-change",
"--admin-server=localhost:18070",
"--database=" + string(shard.Spec.DatabaseName),
"--table-group=" + string(shard.Spec.TableGroupName),
"--shard=" + string(shard.Spec.ShardName),
"--leader=" + leader,
"--cohort=" + strings.Join(cohort, ","),
"--durability=" + shard.Spec.DurabilityPolicy,
"--unsafe-derive-cert-from-reachable",
"--yes",
"--reason=e2e regression test for multiadmin->multiorch hostname resolution",
}
execCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
out, err := exec.CommandContext(execCtx, "kubectl", args...).CombinedOutput()
if err != nil {
t.Fatalf("apply-rule-change failed: %v: %s", err, strings.TrimSpace(string(out)))
}
t.Logf("apply-rule-change output: %s", strings.TrimSpace(string(out)))

// Confirm multiorch actually installed the proposed rule rather than the
// CLI merely exiting 0 on some other no-op path: the reported leader must
// match what we proposed, and the term must have advanced past the
// shard's prior rule (term=1, installed by the operator's own bootstrap).
installedTerm, installedLeader := parseInstalledRule(t, string(out))
if installedLeader != leader {
t.Fatalf("installed rule leader = %q, want %q", installedLeader, leader)
}
if installedTerm <= 1 {
t.Fatalf("installed rule term = %d, want > 1 (a genuinely new rule, not the initial appointment)", installedTerm)
}
}

var installedRuleRE = regexp.MustCompile(`Installed rule term=(\d+), leader=(\S+)/(\S+)`)

// parseInstalledRule extracts the term and "cell_name" leader encoding from
// apply-rule-change's stdout ("Installed rule term=%d, leader=%s/%s").
func parseInstalledRule(t testing.TB, out string) (term int, leader string) {
t.Helper()
m := installedRuleRE.FindStringSubmatch(out)
if m == nil {
t.Fatalf("apply-rule-change did not report an installed rule: %s", out)
}
term, err := strconv.Atoi(m[1])
if err != nil {
t.Fatalf("parse installed rule term %q: %v", m[1], err)
}
return term, m[2] + "_" + m[3]
}

func waitForHealthyShard(
t testing.TB,
c client.Client,
namespace, clusterName string,
) *multigresv1alpha1.Shard {
t.Helper()
var found *multigresv1alpha1.Shard
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
err := wait.PollUntilContextCancel(ctx, 3*time.Second, true, func(ctx context.Context) (bool, error) {
shards := &multigresv1alpha1.ShardList{}
if err := c.List(ctx, shards,
client.InNamespace(namespace),
client.MatchingLabels{metadata.LabelMultigresCluster: clusterName},
); err != nil || len(shards.Items) != 1 {
return false, nil
}
shard := &shards.Items[0]
if shard.Status.Phase != multigresv1alpha1.PhaseHealthy || !shard.Status.OrchReady {
return false, nil
}
found = shard
return true, nil
})
if err != nil {
t.Fatalf("timed out waiting for healthy shard: %v", err)
}
return found
}

// currentLeaderAndCohort returns the shard's already-installed leader and
// full cohort, encoded as "cell_name" pooler IDs (the format
// apply-rule-change's --leader/--cohort flags expect).
func currentLeaderAndCohort(
t testing.TB,
c client.Client,
namespace, clusterName string,
shard *multigresv1alpha1.Shard,
) (leader string, cohort []string) {
t.Helper()
pods := &corev1.PodList{}
if err := c.List(context.Background(), pods,
client.InNamespace(namespace),
client.MatchingLabels{
metadata.LabelMultigresCluster: clusterName,
metadata.LabelMultigresPool: "default",
},
); err != nil {
t.Fatalf("list pool pods: %v", err)
}
for i := range pods.Items {
pod := &pods.Items[i]
cell := pod.Labels[metadata.LabelMultigresCell]
id := poolerServiceID(t, pod)
clusterID := cell + "_" + id
cohort = append(cohort, clusterID)
if shard.Status.PodRoles[pod.Name] == "PRIMARY" {
leader = clusterID
}
}
if leader == "" {
t.Fatalf("no PRIMARY pod found among pool pods")
}
if len(cohort) == 0 {
t.Fatalf("no pool pods found")
}
return leader, cohort
}

func poolerServiceID(t testing.TB, pod *corev1.Pod) string {
t.Helper()
for _, container := range pod.Spec.Containers {
for _, arg := range container.Args {
if serviceID, ok := strings.CutPrefix(arg, "--service-id="); ok {
return serviceID
}
}
}
t.Fatalf("pooler pod %q has no service ID", pod.Name)
return ""
}

func multiadminPodName(t testing.TB, c client.Client, namespace, clusterName string) string {
t.Helper()
pods := &corev1.PodList{}
if err := c.List(context.Background(), pods,
client.InNamespace(namespace),
client.MatchingLabels{
metadata.LabelAppComponent: metadata.ComponentMultiadmin,
metadata.LabelMultigresCluster: clusterName,
},
); err != nil || len(pods.Items) != 1 {
t.Fatalf("get multiadmin pod: %v", err)
}
return pods.Items[0].Name
}
Loading