|
| 1 | +package multiclustertraffic |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/linkerd/linkerd2/pkg/k8s" |
| 12 | + "github.com/linkerd/linkerd2/testutil" |
| 13 | + kerrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | +) |
| 15 | + |
| 16 | +// TestFederatedService deploys emojivoto to two clusters and has the web-svc |
| 17 | +// in both clusters join a federated service. It creates a vote-bot in the |
| 18 | +// source cluster which sends traffic to the federated service and then checks |
| 19 | +// the logs of the web-svc in both clusters. If it has successfully issued |
| 20 | +// requests, then we'll see log messages. |
| 21 | +// |
| 22 | +// We verify that the federated service exists and has no endpoints in the |
| 23 | +// source cluster. |
| 24 | +func TestFederatedService(t *testing.T) { |
| 25 | + if err := TestHelper.SwitchContext(contexts[testutil.TargetContextKey]); err != nil { |
| 26 | + testutil.AnnotatedFatalf(t, |
| 27 | + "failed to rebuild helper clientset with new context", |
| 28 | + "failed to rebuild helper clientset with new context [%s]: %v", |
| 29 | + contexts[testutil.TargetContextKey], err) |
| 30 | + } |
| 31 | + ctx := context.Background() |
| 32 | + // Create emojivoto in target cluster, to be deleted at the end of the test. |
| 33 | + annotations := map[string]string{ |
| 34 | + // "config.linkerd.io/proxy-log-level": "linkerd=debug,info", |
| 35 | + } |
| 36 | + TestHelper.WithDataPlaneNamespace(ctx, "emojivoto-federated", annotations, t, func(t *testing.T, ns string) { |
| 37 | + t.Run("Deploy resources in source and target clusters", func(t *testing.T) { |
| 38 | + // Deploy federated-client in source-cluster |
| 39 | + o, err := TestHelper.KubectlWithContext("", contexts[testutil.SourceContextKey], "create", "ns", ns) |
| 40 | + if err != nil { |
| 41 | + testutil.AnnotatedFatalf(t, "failed to create ns", "failed to create ns: %s\n%s", err, o) |
| 42 | + } |
| 43 | + o, err = TestHelper.KubectlApplyWithContext("", contexts[testutil.SourceContextKey], "--namespace", ns, "-f", "testdata/federated-client.yml") |
| 44 | + if err != nil { |
| 45 | + testutil.AnnotatedFatalf(t, "failed to install federated-client", "failed to installfederated-client: %s\n%s", err, o) |
| 46 | + } |
| 47 | + |
| 48 | + // Deploy emojivoto in both clusters |
| 49 | + for _, ctx := range contexts { |
| 50 | + out, err := TestHelper.KubectlApplyWithContext("", ctx, "--namespace", ns, "-f", "testdata/emojivoto-no-bot.yml") |
| 51 | + if err != nil { |
| 52 | + testutil.AnnotatedFatalf(t, "failed to install emojivoto", "failed to install emojivoto: %s\n%s", err, out) |
| 53 | + } |
| 54 | + |
| 55 | + // Label the service to join the federated service. |
| 56 | + timeout := time.Minute |
| 57 | + err = testutil.RetryFor(timeout, func() error { |
| 58 | + out, err = TestHelper.KubectlWithContext("", ctx, "--namespace", ns, "label", "service/web-svc", "mirror.linkerd.io/federated=member") |
| 59 | + return err |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + testutil.AnnotatedFatalf(t, "failed to label web-svc", "%s\n%s", err, out) |
| 63 | + } |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("Wait until target workloads are ready", func(t *testing.T) { |
| 68 | + // Wait until client is up and running in source cluster |
| 69 | + voteBotDeployReplica := map[string]testutil.DeploySpec{"vote-bot": {Namespace: ns, Replicas: 1}} |
| 70 | + TestHelper.WaitRolloutWithContext(t, voteBotDeployReplica, contexts[testutil.SourceContextKey]) |
| 71 | + |
| 72 | + // Wait until services and replicas are up and running. |
| 73 | + emojiDeployReplicas := map[string]testutil.DeploySpec{ |
| 74 | + "web": {Namespace: ns, Replicas: 1}, |
| 75 | + "emoji": {Namespace: ns, Replicas: 1}, |
| 76 | + "voting": {Namespace: ns, Replicas: 1}, |
| 77 | + } |
| 78 | + for _, ctx := range contexts { |
| 79 | + TestHelper.WaitRolloutWithContext(t, emojiDeployReplicas, ctx) |
| 80 | + } |
| 81 | + |
| 82 | + }) |
| 83 | + |
| 84 | + timeout := time.Minute |
| 85 | + t.Run("Ensure federated service exists and has no endpoints", func(t *testing.T) { |
| 86 | + err := TestHelper.SwitchContext(contexts[testutil.SourceContextKey]) |
| 87 | + if err != nil { |
| 88 | + testutil.AnnotatedFatal(t, "failed to switch contexts", err) |
| 89 | + } |
| 90 | + err = testutil.RetryFor(timeout, func() error { |
| 91 | + svc, err := TestHelper.GetService(ctx, ns, "web-svc-federated") |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + remoteDiscovery, found := svc.Annotations[k8s.RemoteDiscoveryAnnotation] |
| 96 | + if !found { |
| 97 | + return fmt.Errorf("federated service missing annotation: %s", k8s.RemoteDiscoveryLabel) |
| 98 | + } |
| 99 | + if remoteDiscovery != "web-svc@target" { |
| 100 | + return fmt.Errorf("federated service remote discovery was %s, expected %s", remoteDiscovery, "web-svc@target") |
| 101 | + } |
| 102 | + localDiscovery, found := svc.Annotations[k8s.LocalDiscoveryAnnotation] |
| 103 | + if !found { |
| 104 | + return fmt.Errorf("federated service missing annotation: %s", k8s.LocalDiscoveryAnnotation) |
| 105 | + } |
| 106 | + if localDiscovery != "web-svc" { |
| 107 | + return fmt.Errorf("federated service local discovery was %s, expected %s", localDiscovery, "web-svc") |
| 108 | + } |
| 109 | + |
| 110 | + _, err = TestHelper.GetEndpoints(ctx, ns, "web-svc-federated") |
| 111 | + if err == nil { |
| 112 | + return errors.New("federated service should not have endpoints") |
| 113 | + } |
| 114 | + if !kerrors.IsNotFound(err) { |
| 115 | + return fmt.Errorf("failed to retrieve federated service endpoints: %w", err) |
| 116 | + } |
| 117 | + return nil |
| 118 | + }) |
| 119 | + if err != nil { |
| 120 | + testutil.AnnotatedFatal(t, "timed-out verifying federated service", err) |
| 121 | + } |
| 122 | + }) |
| 123 | + |
| 124 | + for _, ctx := range contexts { |
| 125 | + err := testutil.RetryFor(timeout, func() error { |
| 126 | + out, err := TestHelper.KubectlWithContext("", |
| 127 | + ctx, |
| 128 | + "--namespace", ns, |
| 129 | + "logs", |
| 130 | + "--selector", "app=web-svc", |
| 131 | + "--container", "web-svc", |
| 132 | + ) |
| 133 | + if err != nil { |
| 134 | + return fmt.Errorf("%w\n%s", err, out) |
| 135 | + } |
| 136 | + // Check for expected error messages |
| 137 | + for _, row := range strings.Split(out, "\n") { |
| 138 | + if strings.Contains(row, " /api/vote?choice=:doughnut: ") { |
| 139 | + return nil |
| 140 | + } |
| 141 | + } |
| 142 | + return fmt.Errorf("web-svc logs in %s cluster do not include voting errors\n%s", ctx, out) |
| 143 | + }) |
| 144 | + if err != nil { |
| 145 | + testutil.AnnotatedFatal(t, fmt.Sprintf("timed-out waiting for traffic in %s cluster (%s)", ctx, timeout), err) |
| 146 | + } |
| 147 | + } |
| 148 | + }) |
| 149 | +} |
0 commit comments