Skip to content

Commit

Permalink
feat(kumactl): add gateway support to inspect dataplane and policy (#…
Browse files Browse the repository at this point in the history
…3973)

Signed-off-by: Mike Beaumont <mjboamail@gmail.com>
  • Loading branch information
michaelbeaumont authored Mar 7, 2022
1 parent b60734e commit 08cde76
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 97 deletions.
38 changes: 30 additions & 8 deletions app/kumactl/cmd/inspect/inspect_dataplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,44 @@ import (
"github.com/spf13/cobra"

"github.com/kumahq/kuma/app/kumactl/pkg/cmd"
api_server_types "github.com/kumahq/kuma/pkg/api-server/types"
"github.com/kumahq/kuma/pkg/core/resources/apis/mesh"
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
)

var dataplaneInspectTemplate = "{{ range .Items }}" +
"{{ .AttachmentEntry | FormatAttachment }}:\n" +
"{{ range $typ, $policies := .MatchedPolicies }}" +
" {{ $typ }}\n" +
" {{ range $policies }}{{ .Meta.Name }}\n{{ end }}" +
"{{ end }}" +
"\n" +
"{{ end }}"
var dataplaneInspectTemplate = `{{ with IsSidecar . }}{{ range $num, $item := .Items }}{{ .AttachmentEntry | FormatAttachment }}:
{{ range $typ, $policies := .MatchedPolicies }} {{ $typ }}
{{ range $policies }}{{ .Meta.Name }}
{{ end }}{{ end }}
{{ end }}{{ end }}{{ with IsGateway . }}GATEWAY:
{{ range $typ, $policy := .Policies }} {{ $typ }}
{{ .Meta.Name }}
{{ end }}
{{ range .Listeners }}LISTENER ({{ .Protocol }}:{{ .Port }}):
{{ range .Hosts }} {{ .HostName }}:
{{ range .Routes }} {{ .Route }}:
{{ range .Destinations }} {{ FormatTags .Tags }}:
{{ range $typ, $policy := .Policies }} {{ $typ }}
{{ .Meta.Name }}
{{ end }}
{{ end }}{{ end }}{{ end }}{{ end }}{{ end }}`

func newInspectDataplaneCmd(pctx *cmd.RootContext) *cobra.Command {
tmpl, err := template.New("dataplane_inspect").Funcs(template.FuncMap{
"IsSidecar": func(e api_server_types.DataplaneInspectResponse) *api_server_types.DataplaneInspectEntryList {
if concrete, ok := e.DataplaneInspectResponseKind.(*api_server_types.DataplaneInspectEntryList); ok {
return concrete
}
return nil
},
"IsGateway": func(e api_server_types.DataplaneInspectResponse) *api_server_types.GatewayDataplaneInspectResult {
if concrete, ok := e.DataplaneInspectResponseKind.(*api_server_types.GatewayDataplaneInspectResult); ok {
return concrete
}
return nil
},
"FormatAttachment": attachmentToStr(true),
"FormatTags": tagsToStr(true),
}).Parse(dataplaneInspectTemplate)
if err != nil {
panic("unable to parse template")
Expand Down
10 changes: 5 additions & 5 deletions app/kumactl/cmd/inspect/inspect_dataplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
)

type testDataplaneInspectClient struct {
response *api_server_types.DataplaneInspectEntryList
response api_server_types.DataplaneInspectResponse
}

func (t *testDataplaneInspectClient) InspectPolicies(ctx context.Context, mesh, name string) (*api_server_types.DataplaneInspectEntryList, error) {
func (t *testDataplaneInspectClient) InspectPolicies(ctx context.Context, mesh, name string) (api_server_types.DataplaneInspectResponse, error) {
return t.response, nil
}

Expand All @@ -51,11 +51,11 @@ var _ = Describe("kumactl inspect dataplane", func() {
rawResponse, err := os.ReadFile(path.Join("testdata", given.serverOutput))
Expect(err).ToNot(HaveOccurred())

list := api_server_types.DataplaneInspectEntryList{}
Expect(json.Unmarshal(rawResponse, &list)).To(Succeed())
response := api_server_types.DataplaneInspectResponse{}
Expect(json.Unmarshal(rawResponse, &response)).To(Succeed())

testClient := &testDataplaneInspectClient{
response: &list,
response: response,
}

rootCtx, err := test_kumactl.MakeRootContext(time.Now(), nil)
Expand Down
39 changes: 26 additions & 13 deletions app/kumactl/cmd/inspect/inspect_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
"github.com/kumahq/kuma/app/kumactl/pkg/cmd"
api_server_types "github.com/kumahq/kuma/pkg/api-server/types"
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
)

var policyInspectTemplate = "Affected data plane proxies:\n\n" +
"{{ range .Items }}" +
"{{ with IsSidecar . }}" +
" {{ .DataplaneKey.Name }}" +
"{{ if . | PrintAttachments }}" +
":\n" +
"{{ range .Attachments }}" +
" {{ . | FormatAttachment }}\n" +
"{{ end }}" +
"{{ end }}" +
"\n" +
"{{ end }}" +
"{{ end }}"
var policyInspectTemplate = `Affected data plane proxies:
{{ range .Items }}{{ with IsSidecar . }} {{ .DataplaneKey.Name }}{{ if . | PrintAttachments }}:
{{ range .Attachments }} {{ . | FormatAttachment }}
{{ end }}{{ end }}
{{ end }}{{ with IsGateway . }} {{ .Gateway.Name }}:
{{ range .Listeners }} listener ({{ .Protocol }}:{{ .Port }}):
{{ range .Hosts }} {{ .HostName }}:
{{ range .Routes }} {{ .Route }}:
{{ range .Destinations }} {{ FormatTags . }}
{{ end }}{{ end }}{{ end }}{{ end }}
{{ end }}{{ end }}`

func newInspectPolicyCmd(policyDesc core_model.ResourceTypeDescriptor, pctx *cmd.RootContext) *cobra.Command {
tmpl, err := template.New("policy_inspect").Funcs(template.FuncMap{
Expand All @@ -49,6 +49,7 @@ func newInspectPolicyCmd(policyDesc core_model.ResourceTypeDescriptor, pctx *cmd
}
return true
},
"FormatTags": tagsToStr(false),
}).Parse(policyInspectTemplate)
if err != nil {
panic("unable to parse template")
Expand Down Expand Up @@ -93,3 +94,15 @@ func attachmentToStr(upperCase bool) func(api_server_types.AttachmentEntry) stri
}
}
}

func tagsToStr(upperCase bool) func(map[string]string) string {
return func(destinationTags map[string]string) string {
service := destinationTags[mesh_proto.ServiceTag]

label := "service"
if upperCase {
label = strings.ToUpper(label)
}
return fmt.Sprintf("%s %s", label, service)
}
}
2 changes: 1 addition & 1 deletion app/kumactl/cmd/inspect/inspect_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var _ = Describe("kumactl inspect POLICY", func() {
cmdArgs: []string{"inspect", "healthcheck", "hc1"},
}),
Entry("service policy (no kind in response)", testCase{
goldenFile: "inspect-health-check.golden.txt",
goldenFile: "inspect-health-check-1.5.golden.txt",
serverResponseFile: "inspect-health-check-1.5.server-response.json",
cmdArgs: []string{"inspect", "healthcheck", "hc1"},
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
GATEWAY:
TrafficLog
all-traffic
TrafficTrace
all-traffic

LISTENER (HTTP:80):
*:
route-1:
SERVICE backend:
HealthCheck
hc-1

SERVICE redis:
Timeout
t-1

Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,45 @@
}
]
}
]
],
"policies": {
"TrafficLog": {
"type": "TrafficLog",
"mesh": "default",
"name": "all-traffic",
"creationTime": "2022-03-03T22:24:14Z",
"modificationTime": "2022-03-03T22:24:14Z",
"sources": [
{
"match": {
"kuma.io/service": "*"
}
}
],
"destinations": [
{
"match": {
"kuma.io/service": "*"
}
}
]
},
"TrafficTrace": {
"type": "TrafficTrace",
"mesh": "default",
"name": "all-traffic",
"creationTime": "2022-03-03T22:25:34Z",
"modificationTime": "2022-03-03T22:25:34Z",
"selectors": [
{
"match": {
"kuma.io/service": "*"
}
}
],
"conf": {
"backend": "go"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Affected data plane proxies:

web-1:
service backend

backend-1:
service redis
service external

Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,35 @@
"total": 1,
"items": [
{
"dataplane": {
"mesh": "default",
"name": "backend-1"
},
"attachments": [
{
"type": "service",
"name": "gateway",
"service": "gateway"
},
{
"type": "service",
"name": "web",
"service": "web"
}
]
},
{
"kind": "SidecarDataplane",
"dataplane": {
"mesh": "default",
"name": "web-1"
},
"attachments": [
{
"type": "service",
"name": "gateway",
"service": "gateway"
"name": "backend",
"service": "backend"
}
]
},
{
"kind": "SidecarDataplane",
"dataplane": {
"mesh": "default",
"name": "redis-1"
"name": "backend-1"
},
"attachments": [
{
"type": "service",
"name": "gateway",
"service": "gateway"
"name": "redis",
"service": "redis"
},
{
"type": "service",
"name": "web",
"service": "web"
"name": "external",
"service": "external"
}
]
}
Expand Down
16 changes: 9 additions & 7 deletions app/kumactl/cmd/inspect/testdata/inspect-health-check.golden.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
Affected data plane proxies:

backend-1:
service gateway
service web
meshgateway:
listener (HTTP:80):
*:
route-1:
service redis

web-1:
service gateway
service backend

redis-1:
service gateway
service web
backend-1:
service redis
service external

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"gateway": {
"mesh": "mesh-1",
"name": "gateway"
"name": "meshgateway"
},
"listeners": [
{
Expand All @@ -33,25 +33,6 @@
}
]
},
{
"kind": "SidecarDataplane",
"dataplane": {
"mesh": "default",
"name": "backend-1"
},
"attachments": [
{
"type": "service",
"name": "gateway",
"service": "gateway"
},
{
"type": "service",
"name": "web",
"service": "web"
}
]
},
{
"kind": "SidecarDataplane",
"dataplane": {
Expand All @@ -61,27 +42,27 @@
"attachments": [
{
"type": "service",
"name": "gateway",
"service": "gateway"
"name": "backend",
"service": "backend"
}
]
},
{
"kind": "SidecarDataplane",
"dataplane": {
"mesh": "default",
"name": "redis-1"
"name": "backend-1"
},
"attachments": [
{
"type": "service",
"name": "gateway",
"service": "gateway"
"name": "redis",
"service": "redis"
},
{
"type": "service",
"name": "web",
"service": "web"
"name": "external",
"service": "external"
}
]
}
Expand Down
Loading

0 comments on commit 08cde76

Please sign in to comment.