From 34af86c2309b589b6a30fd2e5ba4b011a2bdb4f1 Mon Sep 17 00:00:00 2001 From: Alejandro Pedraza Date: Thu, 17 Aug 2023 13:55:36 -0500 Subject: [PATCH] `linkerd mc link|gateways` small improvements When issuing `linkerd mc gateways` with no links, the command waited till it timed out (30s by default) before showing an empty table. This change refactors the concurrency code in `getGatewayMetrics()` to avoid that. Also the `linkerd mc link -h` help text for the `--gateway` flag was showing a dupe default value: ``` If false, allows a link to be created against a cluster that does not have a gateway service (default true) (default true) ``` --- multicluster/cmd/gateways.go | 23 ++++++++++++++++------- multicluster/cmd/link.go | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/multicluster/cmd/gateways.go b/multicluster/cmd/gateways.go index e259adff2ef5a..aecfa16c2f0d9 100644 --- a/multicluster/cmd/gateways.go +++ b/multicluster/cmd/gateways.go @@ -8,7 +8,7 @@ import ( "io" "os" "strings" - "sync/atomic" + "sync" "time" "github.com/linkerd/linkerd2/cli/table" @@ -198,15 +198,15 @@ func newGatewaysCommand() *cobra.Command { func getGatewayMetrics(k8sAPI *k8s.KubernetesAPI, pods []corev1.Pod, leaders map[string]struct{}, wait time.Duration) []gatewayMetrics { var metrics []gatewayMetrics metricsChan := make(chan gatewayMetrics) - var activeRoutines int32 + var wg sync.WaitGroup for _, pod := range pods { if _, found := leaders[pod.Name]; !found { continue } - atomic.AddInt32(&activeRoutines, 1) + wg.Add(1) go func(p corev1.Pod) { - defer atomic.AddInt32(&activeRoutines, -1) + defer wg.Done() name := p.Labels[k8s.RemoteClusterNameLabel] container, err := getServiceMirrorContainer(p) if err != nil { @@ -224,20 +224,29 @@ func getGatewayMetrics(k8sAPI *k8s.KubernetesAPI, pods []corev1.Pod, leaders map } }(pod) } + + go func() { + wg.Wait() + close(metricsChan) + }() + timeout := time.NewTimer(wait) defer timeout.Stop() + wait: for { select { case metric := <-metricsChan: + if metric.clusterName == "" { + // channel closed + break wait + } metrics = append(metrics, metric) case <-timeout.C: break wait } - if atomic.LoadInt32(&activeRoutines) == 0 { - break - } } + return metrics } diff --git a/multicluster/cmd/link.go b/multicluster/cmd/link.go index 2d77dd026d49f..991c0795bc998 100644 --- a/multicluster/cmd/link.go +++ b/multicluster/cmd/link.go @@ -351,7 +351,7 @@ A full list of configurable values can be found at https://github.com/linkerd/li cmd.Flags().StringVar(&opts.gatewayAddresses, "gateway-addresses", opts.gatewayAddresses, "If specified, overwrites gateway addresses when gateway service is not type LoadBalancer (comma separated list)") cmd.Flags().Uint32Var(&opts.gatewayPort, "gateway-port", opts.gatewayPort, "If specified, overwrites gateway port when gateway service is not type LoadBalancer") cmd.Flags().BoolVar(&opts.ha, "ha", opts.ha, "Enable HA configuration for the service-mirror deployment (default false)") - cmd.Flags().BoolVar(&opts.enableGateway, "gateway", opts.enableGateway, "If false, allows a link to be created against a cluster that does not have a gateway service (default true)") + cmd.Flags().BoolVar(&opts.enableGateway, "gateway", opts.enableGateway, "If false, allows a link to be created against a cluster that does not have a gateway service") pkgcmd.ConfigureNamespaceFlagCompletion( cmd, []string{"namespace", "gateway-namespace"},