Skip to content

Commit

Permalink
Add HTTP Route Graph View
Browse files Browse the repository at this point in the history
  • Loading branch information
weibaohui committed Aug 5, 2024
1 parent d1b0122 commit bee031a
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@
}
}


@if (GrpcRoute.Status is not null)
{
<Divider Orientation="left" Style="font-weight:bold">@L["Status"]</Divider>
Expand Down
8 changes: 8 additions & 0 deletions BlazorApp/Pages/Gateway/HttpRoute/HttpRouteDetailView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@if (HttpRoute.Spec.Rules is { Count: > 0 })
{
<Divider Orientation="left" Style="font-weight:bold">@L["Rules"]</Divider>

@foreach (var rule in HttpRoute.Spec.Rules)
{
<HTable Bordered Column="1">
Expand Down Expand Up @@ -278,6 +279,13 @@
}
}

@if (HttpRoute.Spec.Rules is { Count: > 0 })
{
<Divider Orientation="left" Style="font-weight:bold">@L["Graph"]</Divider>

<RulesDetailView Rules="@HttpRoute.Spec.Rules"></RulesDetailView>
}

@if (HttpRoute.Status is not null)
{
<Divider Orientation="left" Style="font-weight:bold">@L["Status"]</Divider>
Expand Down
5 changes: 5 additions & 0 deletions BlazorApp/Pages/Gateway/RulesDetailView.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@inherits BlazorApp.Pages.Common.PageBase
@if (_diagramDef is { Length: > 0 })
{
<MermaidDiagram Definition=@_diagramDef></MermaidDiagram>
}
109 changes: 109 additions & 0 deletions BlazorApp/Pages/Gateway/RulesDetailView.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BlazorApp.Pages.Common;
using Entity.Crd.Gateway;
using Mapster;
using Microsoft.AspNetCore.Components;

namespace BlazorApp.Pages.Gateway;

public partial class RulesDetailView : PageBase
{
[Parameter] public IList<HTTPRouteRule> Rules { get; set; }

string _diagramDef = "";

// const string template = @"
// flowchart LR
// A[HTTPRouteRule]
// A-- text -->B
// A-- text -->C
// ";

protected override async Task OnInitializedAsync()
{
if (Rules is { Count: > 0 })
{
_diagramDef = "flowchart LR";


foreach (var rule in Rules)
{
if (rule.Matches is { Count: > 0 })
{
foreach (var match in rule.Matches)
{
_diagramDef += $"\r\n Gateway --> {match.Path?.Value ?? ""}\r\n";

var matchValue = match.Path?.Value ?? "";

if (match.Headers is { Count: > 0 })
{
foreach (var header in match.Headers)
{
var backends = rule.BackendRefs?.Adapt<IList<BackendRefWithWeight>>();
if (backends is { Count: > 0 })
{
foreach (var backend in backends)
{
_diagramDef +=
$"\r\n {matchValue} -- {header.Name}={header.Value} --> {backend.Name}:{backend.Port}\r\n";
}
}
}
}
else
{
var backends = rule.BackendRefs?.Adapt<IList<BackendRefWithWeight>>();
if (backends is { Count: > 0 })
{
foreach (var backend in backends)
{
_diagramDef +=
$"\r\n {matchValue} -- {backend.Weight} --> {backend.Name}:{backend.Port}\r\n";
}
}
}


if (rule.Filters is { Count: > 0 })
{
var mirrorList = rule.Filters.Where(x => x.Type == HTTPRouteFilterType.RequestMirror)
.ToList();
foreach (var item in mirrorList)
{
var backend = item.RequestMirror?.BackendRef;
if (backend is not null)
{
_diagramDef +=
$"\r\n {matchValue} -- Mirror --> {backend.Name}:{backend.Port}\r\n";
}
}

var redirectList = rule.Filters.Where(x => x.Type == HTTPRouteFilterType.RequestRedirect)
.ToList();
foreach (var item in redirectList)
{
var backend = item.RequestRedirect;
if (backend?.Hostname != null || backend?.Scheme != null)
{
_diagramDef +=
$"\r\n {matchValue} -- Redirect --> {backend.Scheme}://{backend.Hostname}\r\n";
}

if (backend?.Path != null)
{
_diagramDef +=
$"\r\n {matchValue} -- Redirect --> {backend.Path.ReplaceFullPath}\r\n";
}
}
}
}
}
}
}

await base.OnInitializedAsync();
}
}
7 changes: 3 additions & 4 deletions BlazorApp/Pages/Mermaid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

const string def1 = @"
flowchart LR
A --> B
B --> C
C --> A
A-- text -->B
A-- text -->C
";

}
}
3 changes: 2 additions & 1 deletion BlazorApp/Service/k8s/impl/HttpRouteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public IList<V1Service> GetBackendServices(V1HTTPRoute httpRoute)
IList<V1Service> svcList = new List<V1Service>();

var backendRefs = httpRoute?.Spec?.Rules?
.Where(x => x.BackendRefs is { Count: > 0 })
.SelectMany(x => x.BackendRefs)?
.Where(x => x.Kind == "Service")?
.Where(x => x is { Kind: "Service" })?
.ToList();
if (backendRefs is { Count: > 0 })
{
Expand Down

0 comments on commit bee031a

Please sign in to comment.