Skip to content

Commit

Permalink
CLI updates for public-api/viz split
Browse files Browse the repository at this point in the history
This is based on the branch `alpeb/linkerd-web-public-viz` from #5559.

Changes to command files under `cli/cmd`:
- Updated `endpoints.go` according to new API interface name.
- Updated `version.go`, `dashboard` and `uninstall.go` to pull the viz namespace from `viz.pkg.GetVizNamespace()`.

Changes to command files under `viz/cmd`:
- `edges.go`, `routes.go`, `stat.go` and `top.go`: point to dependencies that were moved from public-api to viz.

Tests are not expected to pass on this one either.
  • Loading branch information
alpeb committed Jan 18, 2021
1 parent 7f6cc2f commit ec908e4
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 60 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ destination.`,
return cmd
}

func requestEndpointsFromAPI(client public.PublicAPIClient, authorities []string) (endpointsInfo, error) {
func requestEndpointsFromAPI(client public.Client, authorities []string) (endpointsInfo, error) {
info := make(endpointsInfo)
// buffered channels to avoid blocking
events := make(chan *destinationPb.Update, len(authorities))
Expand Down
14 changes: 10 additions & 4 deletions cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
api "github.com/linkerd/linkerd2/pkg/public"
"github.com/linkerd/linkerd2/pkg/version"
pb "github.com/linkerd/linkerd2/viz/metrics-api/gen/viz"
"github.com/linkerd/linkerd2/viz/pkg"
vizApi "github.com/linkerd/linkerd2/viz/pkg/api"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -51,7 +53,7 @@ func newCmdVersion() *cobra.Command {
}
}

configureAndRunVersion(cmd.Context(), k8sAPI, options, os.Stdout, api.RawPublicAPIClient, api.RawVizAPIClient)
configureAndRunVersion(cmd.Context(), k8sAPI, options, os.Stdout, api.RawPublicAPIClient, vizApi.RawClient)
return nil
},
}
Expand All @@ -70,7 +72,7 @@ func configureAndRunVersion(
options *versionOptions,
stdout io.Writer,
mkPublicClient func(ctx context.Context, k8sAPI *k8s.KubernetesAPI, controlPlaneNamespace, apiAddr string) (publicPb.ApiClient, error),
mkVizClient func(ctx context.Context, k8sAPI *k8s.KubernetesAPI, controlPlaneNamespace, apiAddr string) (pb.ApiClient, error),
mkVizClient func(ctx context.Context, k8sAPI *k8s.KubernetesAPI, controlPlaneNamespace string) (pb.ApiClient, error),
) {
clientVersion := version.Version
if options.shortVersion {
Expand Down Expand Up @@ -99,8 +101,12 @@ func configureAndRunVersion(
}

if options.proxy {
vizClient, clientErr := mkVizClient(ctx, k8sAPI, controlPlaneNamespace, apiAddr)
if clientErr != nil {
var vizClient pb.ApiClient
vizNamespace, err := pkg.GetVizNamespace(ctx, k8sAPI)
if err == nil {
vizClient, err = mkVizClient(ctx, k8sAPI, vizNamespace)
}
if err != nil {
fmt.Fprintln(stdout, "Proxy versions: unavailable")
} else {
req := &pb.ListPodsRequest{}
Expand Down
3 changes: 2 additions & 1 deletion viz/cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/linkerd/linkerd2/pkg/healthcheck"
"github.com/linkerd/linkerd2/pkg/k8s"
api "github.com/linkerd/linkerd2/pkg/public"
vizPkg "github.com/linkerd/linkerd2/viz/pkg"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -97,7 +98,7 @@ func NewCmdDashboard() *cobra.Command {
return err
}

vizNamespace, err := getVizNamespace(cmd.Context(), k8sAPI)
vizNamespace, err := vizPkg.GetVizNamespace(cmd.Context(), k8sAPI)
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions viz/cmd/edges.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"text/tabwriter"

"github.com/fatih/color"
"github.com/linkerd/linkerd2/controller/api/util"
coreUtil "github.com/linkerd/linkerd2/controller/api/util"
pkgcmd "github.com/linkerd/linkerd2/pkg/cmd"
"github.com/linkerd/linkerd2/pkg/healthcheck"
api "github.com/linkerd/linkerd2/pkg/public"
pb "github.com/linkerd/linkerd2/viz/metrics-api/gen/viz"
"github.com/linkerd/linkerd2/viz/metrics-api/util"
"github.com/linkerd/linkerd2/viz/pkg/api"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -82,7 +83,7 @@ func NewCmdEdges() *cobra.Command {
# Get all edges between pods in all namespaces.
linkerd viz edges po --all-namespaces`,
Args: cobra.ExactArgs(1),
ValidArgs: util.ValidTargets,
ValidArgs: coreUtil.ValidTargets,
RunE: func(cmd *cobra.Command, args []string) error {
if options.namespace == "" {
options.namespace = pkgcmd.GetDefaultNamespace(kubeconfigPath, kubeContext)
Expand All @@ -95,7 +96,7 @@ func NewCmdEdges() *cobra.Command {

// The gRPC client is concurrency-safe, so we can reuse it in all the following goroutines
// /~https://github.com/grpc/grpc-go/issues/682
client := api.CheckVizAPIClientOrExit(healthcheck.Options{
client := api.CheckClientOrExit(healthcheck.Options{
ControlPlaneNamespace: controlPlaneNamespace,
KubeConfig: kubeconfigPath,
Impersonate: impersonate,
Expand Down Expand Up @@ -159,7 +160,7 @@ func validateEdgesRequestInputs(targets []*pb.Resource, options *edgesOptions) e
}

func buildEdgesRequests(resources []string, options *edgesOptions) ([]*pb.EdgesRequest, error) {
targets, err := util.BuildResources(options.namespace, resources)
targets, err := coreUtil.BuildResources(options.namespace, resources)

if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions viz/cmd/edges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"testing"

"github.com/linkerd/linkerd2/controller/api/public"
api "github.com/linkerd/linkerd2/viz/metrics-api"
)

type edgesParamsExp struct {
Expand Down Expand Up @@ -99,8 +99,8 @@ func TestEdges(t *testing.T) {
}

func testEdgesCall(exp edgesParamsExp, t *testing.T) {
mockClient := &public.MockAPIClient{}
response := public.GenEdgesResponse(exp.resourceType, "all")
mockClient := &api.MockAPIClient{}
response := api.GenEdgesResponse(exp.resourceType, "all")

mockClient.EdgesResponseToReturn = response

Expand Down
16 changes: 0 additions & 16 deletions viz/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package cmd

import (
"context"
"errors"
"fmt"
"regexp"

"github.com/fatih/color"
"github.com/linkerd/linkerd2/pkg/k8s"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand All @@ -24,7 +20,6 @@ const (
)

var (

// special handling for Windows, on all other platforms these resolve to
// os.Stdout and os.Stderr, thanks to /~https://github.com/mattn/go-colorable
stdout = color.Output
Expand Down Expand Up @@ -84,14 +79,3 @@ func NewCmdViz() *cobra.Command {

return vizCmd
}

func getVizNamespace(ctx context.Context, k8sAPI *k8s.KubernetesAPI) (string, error) {
ns, err := k8sAPI.CoreV1().Namespaces().List(ctx, metav1.ListOptions{LabelSelector: "linkerd.io/extension=linkerd-viz"})
if err != nil {
return "", err
}
if len(ns.Items) == 0 {
return "", errors.New("linkerd-viz extension not found")
}
return ns.Items[0].Name, nil
}
13 changes: 7 additions & 6 deletions viz/cmd/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"text/tabwriter"
"time"

"github.com/linkerd/linkerd2/controller/api/util"
coreUtil "github.com/linkerd/linkerd2/controller/api/util"
pkgcmd "github.com/linkerd/linkerd2/pkg/cmd"
"github.com/linkerd/linkerd2/pkg/healthcheck"
"github.com/linkerd/linkerd2/pkg/k8s"
api "github.com/linkerd/linkerd2/pkg/public"
pb "github.com/linkerd/linkerd2/viz/metrics-api/gen/viz"
"github.com/linkerd/linkerd2/viz/metrics-api/util"
"github.com/linkerd/linkerd2/viz/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ This command will only display traffic which is sent to a service that has a Ser
# Routes for calls from the traffic deployment to the webapp service in the test namespace.
linkerd viz routes deploy/traffic -n test --to svc/webapp`,
Args: cobra.ExactArgs(1),
ValidArgs: util.ValidTargets,
ValidArgs: coreUtil.ValidTargets,
RunE: func(cmd *cobra.Command, args []string) error {
if options.namespace == "" {
options.namespace = pkgcmd.GetDefaultNamespace(kubeconfigPath, kubeContext)
Expand All @@ -73,7 +74,7 @@ This command will only display traffic which is sent to a service that has a Ser
}

output, err := requestRouteStatsFromAPI(
api.CheckVizAPIClientOrExit(healthcheck.Options{
api.CheckClientOrExit(healthcheck.Options{
ControlPlaneNamespace: controlPlaneNamespace,
KubeConfig: kubeconfigPath,
Impersonate: impersonate,
Expand Down Expand Up @@ -353,7 +354,7 @@ func buildTopRoutesRequest(resource string, options *routesOptions) (*pb.TopRout
return nil, err
}

target, err := util.BuildResource(options.namespace, resource)
target, err := coreUtil.BuildResource(options.namespace, resource)
if err != nil {
return nil, err
}
Expand All @@ -374,7 +375,7 @@ func buildTopRoutesRequest(resource string, options *routesOptions) (*pb.TopRout
if options.toNamespace == "" {
options.toNamespace = options.namespace
}
toRes, err := util.BuildResource(options.toNamespace, options.toResource)
toRes, err := coreUtil.BuildResource(options.toNamespace, options.toResource)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions viz/cmd/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"testing"

"github.com/linkerd/linkerd2/controller/api/public"
api "github.com/linkerd/linkerd2/viz/metrics-api"
)

type routesParamsExp struct {
Expand Down Expand Up @@ -48,9 +48,9 @@ func TestRoutes(t *testing.T) {
}

func testRoutesCall(exp routesParamsExp, t *testing.T) {
mockClient := &public.MockAPIClient{}
mockClient := &api.MockAPIClient{}

response := public.GenTopRoutesResponse(exp.routes, exp.counts, exp.options.toResource != "", "foobar")
response := api.GenTopRoutesResponse(exp.routes, exp.counts, exp.options.toResource != "", "foobar")

mockClient.TopRoutesResponseToReturn = response

Expand Down
15 changes: 8 additions & 7 deletions viz/cmd/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"text/tabwriter"
"time"

"github.com/linkerd/linkerd2/controller/api/util"
coreUtil "github.com/linkerd/linkerd2/controller/api/util"
"github.com/linkerd/linkerd2/pkg/cmd"
pkgcmd "github.com/linkerd/linkerd2/pkg/cmd"
"github.com/linkerd/linkerd2/pkg/healthcheck"
"github.com/linkerd/linkerd2/pkg/k8s"
api "github.com/linkerd/linkerd2/pkg/public"
pb "github.com/linkerd/linkerd2/viz/metrics-api/gen/viz"
"github.com/linkerd/linkerd2/viz/metrics-api/util"
"github.com/linkerd/linkerd2/viz/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -166,7 +167,7 @@ If no resource name is specified, displays stats about all resources of the spec
# Get all inbound stats to the test namespace.
linkerd viz stat ns/test`,
Args: cobra.MinimumNArgs(1),
ValidArgs: util.ValidTargets,
ValidArgs: coreUtil.ValidTargets,
RunE: func(cmd *cobra.Command, args []string) error {
if options.namespace == "" {
options.namespace = pkgcmd.GetDefaultNamespace(kubeconfigPath, kubeContext)
Expand All @@ -179,7 +180,7 @@ If no resource name is specified, displays stats about all resources of the spec

// The gRPC client is concurrency-safe, so we can reuse it in all the following goroutines
// /~https://github.com/grpc/grpc-go/issues/682
client := api.CheckVizAPIClientOrExit(healthcheck.Options{
client := api.CheckClientOrExit(healthcheck.Options{
ControlPlaneNamespace: controlPlaneNamespace,
KubeConfig: kubeconfigPath,
Impersonate: impersonate,
Expand Down Expand Up @@ -685,20 +686,20 @@ func getNamePrefix(resourceType string) string {
}

func buildStatSummaryRequests(resources []string, options *statOptions) ([]*pb.StatSummaryRequest, error) {
targets, err := util.BuildResources(options.namespace, resources)
targets, err := coreUtil.BuildResources(options.namespace, resources)
if err != nil {
return nil, err
}

var toRes, fromRes *pb.Resource
if options.toResource != "" {
toRes, err = util.BuildResource(options.toNamespace, options.toResource)
toRes, err = coreUtil.BuildResource(options.toNamespace, options.toResource)
if err != nil {
return nil, err
}
}
if options.fromResource != "" {
fromRes, err = util.BuildResource(options.fromNamespace, options.fromResource)
fromRes, err = coreUtil.BuildResource(options.fromNamespace, options.fromResource)
if err != nil {
return nil, err
}
Expand Down
22 changes: 11 additions & 11 deletions viz/cmd/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package cmd
import (
"testing"

"github.com/linkerd/linkerd2/controller/api/public"
pkgcmd "github.com/linkerd/linkerd2/pkg/cmd"
"github.com/linkerd/linkerd2/pkg/k8s"
api "github.com/linkerd/linkerd2/viz/metrics-api"
)

type paramsExp struct {
counts *public.PodCounts
counts *api.PodCounts
options *statOptions
resNs []string
file string
Expand All @@ -19,7 +19,7 @@ func TestStat(t *testing.T) {
options := newStatOptions()
t.Run("Returns namespace stats", func(t *testing.T) {
testStatCall(paramsExp{
counts: &public.PodCounts{
counts: &api.PodCounts{
MeshedPods: 1,
RunningPods: 2,
FailedPods: 0,
Expand All @@ -32,7 +32,7 @@ func TestStat(t *testing.T) {

t.Run("Returns pod stats", func(t *testing.T) {
testStatCall(paramsExp{
counts: &public.PodCounts{
counts: &api.PodCounts{
Status: "Running",
MeshedPods: 1,
RunningPods: 1,
Expand All @@ -55,7 +55,7 @@ func TestStat(t *testing.T) {
options.outputFormat = jsonOutput
t.Run("Returns namespace stats (json)", func(t *testing.T) {
testStatCall(paramsExp{
counts: &public.PodCounts{
counts: &api.PodCounts{
MeshedPods: 1,
RunningPods: 2,
FailedPods: 0,
Expand All @@ -78,7 +78,7 @@ func TestStat(t *testing.T) {
options.allNamespaces = true
t.Run("Returns all namespace stats", func(t *testing.T) {
testStatCall(paramsExp{
counts: &public.PodCounts{
counts: &api.PodCounts{
MeshedPods: 1,
RunningPods: 2,
FailedPods: 0,
Expand All @@ -92,7 +92,7 @@ func TestStat(t *testing.T) {
options.outputFormat = jsonOutput
t.Run("Returns all namespace stats (json)", func(t *testing.T) {
testStatCall(paramsExp{
counts: &public.PodCounts{
counts: &api.PodCounts{
MeshedPods: 1,
RunningPods: 2,
FailedPods: 0,
Expand All @@ -107,7 +107,7 @@ func TestStat(t *testing.T) {
options.outputFormat = "wide"
t.Run("Returns TCP stats", func(t *testing.T) {
testStatCall(paramsExp{
counts: &public.PodCounts{
counts: &api.PodCounts{
MeshedPods: 1,
RunningPods: 2,
FailedPods: 0,
Expand Down Expand Up @@ -228,10 +228,10 @@ func TestStat(t *testing.T) {
}

func testStatCall(exp paramsExp, resourceType string, t *testing.T) {
mockClient := &public.MockAPIClient{}
response := public.GenStatSummaryResponse("emoji", resourceType, exp.resNs, exp.counts, true, true)
mockClient := &api.MockAPIClient{}
response := api.GenStatSummaryResponse("emoji", resourceType, exp.resNs, exp.counts, true, true)
if resourceType == k8s.TrafficSplit {
response = public.GenStatTsResponse("foo-split", resourceType, exp.resNs, true, true)
response = api.GenStatTsResponse("foo-split", resourceType, exp.resNs, true, true)
}

mockClient.StatSummaryResponseToReturn = response
Expand Down
Loading

0 comments on commit ec908e4

Please sign in to comment.