Skip to content

Commit

Permalink
linkerd mc link|gateways small improvements (#11265)
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
alpeb authored Sep 5, 2023
1 parent e07b0bb commit 9b8d6e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions multicluster/cmd/gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"io"
"os"
"strings"
"sync/atomic"
"sync"
"time"

"github.com/linkerd/linkerd2/cli/table"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion multicluster/cmd/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit 9b8d6e9

Please sign in to comment.