From 13aa5b228f1dfc55453a2ee507d3c544f5e62df1 Mon Sep 17 00:00:00 2001 From: weibaohui Date: Thu, 23 Nov 2023 14:48:55 +0800 Subject: [PATCH] watch --- BlazorApp/Chat/ChatHub.cs | 9 +- BlazorApp/Chat/IChatHub.cs | 9 +- BlazorApp/Chat/SignalSendService.cs | 22 ++++ BlazorApp/Pages/ChatHubPage.razor.cs | 9 +- .../Pages/Deployment/DeploymentIndex.razor.cs | 4 +- BlazorApp/Pages/Pod/PodIndex.razor | 16 +-- BlazorApp/Pages/Pod/PodIndex.razor.cs | 24 +++-- .../Pages/ReplicaSet/ReplicaSetIndex.razor.cs | 4 +- BlazorApp/Program.cs | 7 +- BlazorApp/Service/impl/NamespaceService.cs | 16 +-- BlazorApp/Service/impl/PodService.cs | 8 +- BlazorApp/Service/impl/TablePagedService.cs | 100 ------------------ BlazorApp/Service/impl/WatchService.cs | 13 ++- .../{Service/impl => Utils}/ResourceCache.cs | 4 +- BlazorApp/Utils/TablePagedService.cs | 98 +++++++++++++++++ Entity/IChatClient.cs | 5 +- Entity/ResourceWatchEntity.cs | 12 +++ 17 files changed, 210 insertions(+), 150 deletions(-) create mode 100644 BlazorApp/Chat/SignalSendService.cs delete mode 100644 BlazorApp/Service/impl/TablePagedService.cs rename BlazorApp/{Service/impl => Utils}/ResourceCache.cs (98%) create mode 100644 BlazorApp/Utils/TablePagedService.cs create mode 100644 Entity/ResourceWatchEntity.cs diff --git a/BlazorApp/Chat/ChatHub.cs b/BlazorApp/Chat/ChatHub.cs index 54a5cdb2..50c0b38f 100644 --- a/BlazorApp/Chat/ChatHub.cs +++ b/BlazorApp/Chat/ChatHub.cs @@ -1,14 +1,21 @@ using System; using System.Threading.Tasks; +using k8s; +using k8s.Models; using Microsoft.AspNetCore.SignalR; namespace BlazorApp.Chat; -public class ChatHub : Hub, IChatHub +public class ChatHub : Hub { public async Task SendMessage(string message) { Console.WriteLine("收到" + message); await Clients.All.SendAsync("ReceiveMessage", message); } + + public Task SendWatchEvent(string message) + { + throw new NotImplementedException(); + } } diff --git a/BlazorApp/Chat/IChatHub.cs b/BlazorApp/Chat/IChatHub.cs index 75c433fc..aa70fab9 100644 --- a/BlazorApp/Chat/IChatHub.cs +++ b/BlazorApp/Chat/IChatHub.cs @@ -1,8 +1,11 @@ using System.Threading.Tasks; +using k8s; +using k8s.Models; namespace BlazorApp.Chat; -public interface IChatHub +public interface IChatHub where T : IKubernetesObject { - public Task SendMessage(string message); -} + public Task SendMessage(string message); + public Task SendWatchEvent(string message); +} \ No newline at end of file diff --git a/BlazorApp/Chat/SignalSendService.cs b/BlazorApp/Chat/SignalSendService.cs new file mode 100644 index 00000000..8b7159d4 --- /dev/null +++ b/BlazorApp/Chat/SignalSendService.cs @@ -0,0 +1,22 @@ +using BlazorApp.Service; +using k8s; +using k8s.Models; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.DependencyInjection; + +namespace BlazorApp.Chat; + +public class SignalSendService where T : IKubernetesObject +{ + private readonly IHubContext _ctx; + + public SignalSendService(IHubContext ctx) + { + _ctx = ctx; + } + + public void Send(WatchEventType type, T item) + { + _ctx.Clients.All.SendAsync("Resource", (type, item)); + } +} \ No newline at end of file diff --git a/BlazorApp/Pages/ChatHubPage.razor.cs b/BlazorApp/Pages/ChatHubPage.razor.cs index 407198a1..834cc8f1 100644 --- a/BlazorApp/Pages/ChatHubPage.razor.cs +++ b/BlazorApp/Pages/ChatHubPage.razor.cs @@ -1,5 +1,7 @@ using System; using System.Threading.Tasks; +using Entity; +using k8s.Models; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.SignalR.Client; @@ -22,10 +24,11 @@ protected override async Task OnInitializedAsync() .Build(); //receive event - _hubConnection.On("ReceiveMessage", async (message) => + _hubConnection.On>("ReceiveMessage", async (data) => { - Console.WriteLine("page收到消息" + message); - MessageReceived += message; + Console.WriteLine("page收到消息" + data.Message); + Console.WriteLine("page收到消息" + data.Item.Metadata.Name); + MessageReceived += data.Item.Metadata.Name; await InvokeAsync(StateHasChanged); }); diff --git a/BlazorApp/Pages/Deployment/DeploymentIndex.razor.cs b/BlazorApp/Pages/Deployment/DeploymentIndex.razor.cs index 2e9a1cfd..550b4e24 100644 --- a/BlazorApp/Pages/Deployment/DeploymentIndex.razor.cs +++ b/BlazorApp/Pages/Deployment/DeploymentIndex.razor.cs @@ -3,6 +3,8 @@ using System.Threading.Tasks; using AntDesign.TableModels; using BlazorApp.Service; +using BlazorApp.Service.impl; +using BlazorApp.Utils; using k8s.Models; using Microsoft.AspNetCore.Components; @@ -71,4 +73,4 @@ private async Task OnDeployClick(V1Deployment deploy) var options = PageDrawerService.DefaultOptions($"{deploy.Kind ?? "Deployment"}:{deploy.Name()}"); await PageDrawerService.ShowDrawerAsync(options, deploy); } -} +} \ No newline at end of file diff --git a/BlazorApp/Pages/Pod/PodIndex.razor b/BlazorApp/Pages/Pod/PodIndex.razor index 98a3ae86..b126a826 100644 --- a/BlazorApp/Pages/Pod/PodIndex.razor +++ b/BlazorApp/Pages/Pod/PodIndex.razor @@ -4,11 +4,11 @@ @using Extension @using BlazorApp.Pages.Workload @using BlazorApp.Pages.Container -@if (tps.PagedItems != null) +@if (_tps.PagedItems != null) { - @@ -17,13 +17,13 @@ diff --git a/BlazorApp/Pages/Pod/PodIndex.razor.cs b/BlazorApp/Pages/Pod/PodIndex.razor.cs index c315ec8f..f36a8f9c 100644 --- a/BlazorApp/Pages/Pod/PodIndex.razor.cs +++ b/BlazorApp/Pages/Pod/PodIndex.razor.cs @@ -5,6 +5,8 @@ using AntDesign.TableModels; using BlazorApp.Pages.Node; using BlazorApp.Service; +using BlazorApp.Service.impl; +using BlazorApp.Utils; using Entity; using k8s.Models; using Microsoft.AspNetCore.Components; @@ -23,10 +25,10 @@ public partial class PodIndex : ComponentBase private IPageDrawerService PageDrawerService { get; set; } [Inject] - private IWatchService WatchServicee { get; set; } + private IWatchService WatchService { get; set; } - private TablePagedService tps; + private TablePagedService _tps; private string _selectedNs = ""; @@ -34,15 +36,15 @@ public partial class PodIndex : ComponentBase protected override async Task OnInitializedAsync() { - tps = new TablePagedService(PodService); - await tps.GetData(_selectedNs); + _tps = new TablePagedService(PodService); + await _tps.GetData(_selectedNs); //TODO 改为接收watch var timer = new Timer(1000); timer.Enabled = true; timer.Elapsed += async (o, args) => { if (!PodService.Changed()) return; - await tps.GetData(_selectedNs); + await _tps.GetData(_selectedNs); await InvokeAsync(StateHasChanged); // Console.WriteLine("refreshPods"); }; @@ -52,19 +54,19 @@ protected override async Task OnInitializedAsync() private async Task OnNsSelectedHandler(string ns) { _selectedNs = ns; - await tps.OnNsSelectedHandler(ns); + await _tps.OnNsSelectedHandler(ns); await InvokeAsync(StateHasChanged); } public void RemoveSelection(string uid) { - tps.SelectedRows = tps.SelectedRows.Where(x => x.Metadata.Uid != uid); + _tps.SelectedRows = _tps.SelectedRows.Where(x => x.Metadata.Uid != uid); } private async Task OnChange(QueryModel queryModel) { - tps.OnChange(queryModel); + _tps.OnChange(queryModel); await InvokeAsync(StateHasChanged); } @@ -72,11 +74,11 @@ private async Task OnSearchHandler(string key) { if (string.IsNullOrEmpty(key)) { - await tps.GetData(_selectedNs); + await _tps.GetData(_selectedNs); } else { - tps.OnSearch(tps.OriginItems.Where(w => w.Name().Contains(key)).ToList()); + _tps.OnSearch(_tps.OriginItems.Where(w => w.Name().Contains(key)).ToList()); } await InvokeAsync(StateHasChanged); @@ -103,7 +105,7 @@ private async Task PodDeleteHandler(V1Pod pod) private async Task OnPodChanged(string obj) { - await tps.GetData(_selectedNs); + await _tps.GetData(_selectedNs); await InvokeAsync(StateHasChanged); } } diff --git a/BlazorApp/Pages/ReplicaSet/ReplicaSetIndex.razor.cs b/BlazorApp/Pages/ReplicaSet/ReplicaSetIndex.razor.cs index a32acf79..f84234e8 100644 --- a/BlazorApp/Pages/ReplicaSet/ReplicaSetIndex.razor.cs +++ b/BlazorApp/Pages/ReplicaSet/ReplicaSetIndex.razor.cs @@ -3,6 +3,8 @@ using System.Threading.Tasks; using AntDesign.TableModels; using BlazorApp.Service; +using BlazorApp.Service.impl; +using BlazorApp.Utils; using k8s.Models; using Microsoft.AspNetCore.Components; @@ -72,4 +74,4 @@ private async Task OnRsClick(V1ReplicaSet rs) await PageDrawerService.ShowDrawerAsync(options, rs); } } -} +} \ No newline at end of file diff --git a/BlazorApp/Program.cs b/BlazorApp/Program.cs index 9ca95102..fcf0b44b 100644 --- a/BlazorApp/Program.cs +++ b/BlazorApp/Program.cs @@ -16,10 +16,9 @@ builder.Services.AddAntDesign(); builder.Services.Configure(builder.Configuration.GetSection("ProSettings")); builder.Services.AddSingleton(); -builder.Services.AddHttpClient(); - -builder.Services.AddScoped(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -27,10 +26,10 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddHttpClient(); var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/BlazorApp/Service/impl/NamespaceService.cs b/BlazorApp/Service/impl/NamespaceService.cs index 4c2a4210..cc76f20b 100644 --- a/BlazorApp/Service/impl/NamespaceService.cs +++ b/BlazorApp/Service/impl/NamespaceService.cs @@ -19,25 +19,25 @@ public NamespaceService(IBaseService baseService) /// /// 缓存当前NS列表 /// - private IList ns; + private IList _ns; + public async Task> GetNamespaces() { - if (ns != null) + if (_ns != null) { - return ns; + return _ns; } _ = await List(); - return ns; + return _ns; } public async Task List() { - - var list = await _baseService.Client().ListNamespaceAsync(); + var list = await _baseService.Client().ListNamespaceAsync(); //缓存当前获取到的NS,每次获取都做强制更新 - ns = list.Items.ToList(); + _ns = list.Items.ToList(); return list; } } -} +} \ No newline at end of file diff --git a/BlazorApp/Service/impl/PodService.cs b/BlazorApp/Service/impl/PodService.cs index c9b3be02..11cb80ea 100644 --- a/BlazorApp/Service/impl/PodService.cs +++ b/BlazorApp/Service/impl/PodService.cs @@ -4,8 +4,12 @@ using System.Linq; using System.Net.WebSockets; using System.Threading.Tasks; +using BlazorApp.Chat; +using BlazorApp.Utils; +using Entity; using k8s; using k8s.Models; +using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.DependencyInjection; namespace BlazorApp.Service.impl @@ -14,7 +18,7 @@ public class PodService : IPodService { private readonly IBaseService _baseService; private readonly IWatchService _watchService; - private readonly IServiceScope? _scope; + private readonly IServiceScope _scope; private ResourceCache _cache = ResourceCache.Instance(); public PodService(IBaseService baseService, IServiceScopeFactory serviceScopeFactory) @@ -22,10 +26,10 @@ public PodService(IBaseService baseService, IServiceScopeFactory serviceScopeFac _baseService = baseService; _scope = serviceScopeFactory.CreateScope(); _watchService = _scope.ServiceProvider.GetService(); - // Console.WriteLine("PodService 初始化"); } + public bool Changed() { return _cache.Changed(); diff --git a/BlazorApp/Service/impl/TablePagedService.cs b/BlazorApp/Service/impl/TablePagedService.cs deleted file mode 100644 index d16ac0c5..00000000 --- a/BlazorApp/Service/impl/TablePagedService.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using AntDesign.TableModels; - -namespace BlazorApp.Service -{ - public class TablePagedService - { - private readonly INamespaceAction _listService; - - public TablePagedService(INamespaceAction listService) - { - _listService = listService; - } - - - /// - /// 页面显示的条目 - /// - public IList PagedItems; - - /// - /// 获取的原始条目 - /// - public IList OriginItems; - - private string _selectedNs; - - public IEnumerable SelectedRows; - - public int PageIndex = 1; - public int PageSize = 10; - public int Total = 100; - public bool Loading = false; - - void ChangePageSize(int pageSize) - { - if (PageSize != pageSize) - { - PageSize = pageSize; - PageIndex = 1; - } - } - - public async Task GetData(string ns) - { - Loading = true; - OriginItems = await _listService.ListItemsByNamespaceAsync(ns); - PagedItems = OriginItems; - Total = OriginItems.Count; - PageIndex = 1; - Loading = false; - } - - /// - /// 命名空间切换事件 - /// - /// - /// - public async Task OnNsSelectedHandler(string ns) - { - if (_selectedNs == ns) - { - return; - } - - Loading = true; - PageIndex = 1; - _selectedNs = ns; - await GetData(ns); - Loading = false; - } - - - /// - /// 变更事件 - /// - /// - public void OnChange(QueryModel queryModel) - { - PageIndex = queryModel.PageIndex; - PageSize = queryModel.PageSize; - Loading = true; - var query = OriginItems.Skip((PageIndex - 1) * PageSize).Take(PageSize); - PagedItems = query.ToList(); - Loading = false; - } - - public void OnSearch(IList predicate) - { - Loading = true; - PageIndex = 1; - OriginItems = predicate; - PagedItems = OriginItems.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList(); - Total = OriginItems.Count(); - Loading = false; - } - } -} diff --git a/BlazorApp/Service/impl/WatchService.cs b/BlazorApp/Service/impl/WatchService.cs index 1d705cf1..b7df0043 100644 --- a/BlazorApp/Service/impl/WatchService.cs +++ b/BlazorApp/Service/impl/WatchService.cs @@ -3,6 +3,8 @@ using System.Threading; using System.Threading.Tasks; using BlazorApp.Chat; +using BlazorApp.Utils; +using Entity; using k8s; using k8s.Models; using Microsoft.AspNetCore.SignalR; @@ -14,8 +16,8 @@ public class WatchService : IWatchService { private readonly IBaseService _baseService; - private readonly IHubContext _ctx; private ResourceCache _cache = ResourceCache.Instance(); + private readonly IHubContext _ctx; public WatchService(IBaseService baseService, IHubContext ctx) { @@ -25,7 +27,6 @@ public WatchService(IBaseService baseService, IHubContext ctx) WatchAllPod(); } - public async Task WatchAllPod() { var podListResp = _baseService.Client().CoreV1 @@ -33,12 +34,18 @@ public async Task WatchAllPod() await foreach (var (type, item) in podListResp.WatchAsync()) { + var data = new ResourceWatchEntity + { + Message = $"{type}:{item.Metadata.Name}", + Type = type, + Item = item + }; Console.WriteLine("==on watch event start =="); Console.WriteLine($"{type}:{item.Kind}:{item.Metadata.Name}"); _cache.Update(type, item); Console.WriteLine("==on watch event end =="); Console.WriteLine("==on SendAsync event start =="); - await _ctx.Clients.All.SendAsync("ReceiveMessage", $"{type}:{item.Metadata.Name}"); + await _ctx.Clients.All.SendAsync("ReceiveMessage", data); Console.WriteLine("==on SendAsync event end =="); } } diff --git a/BlazorApp/Service/impl/ResourceCache.cs b/BlazorApp/Utils/ResourceCache.cs similarity index 98% rename from BlazorApp/Service/impl/ResourceCache.cs rename to BlazorApp/Utils/ResourceCache.cs index 06e966b5..c831ace9 100644 --- a/BlazorApp/Service/impl/ResourceCache.cs +++ b/BlazorApp/Utils/ResourceCache.cs @@ -3,7 +3,7 @@ using k8s; using k8s.Models; -namespace BlazorApp.Service.impl; +namespace BlazorApp.Utils; public class ResourceCache where T : IKubernetesObject { @@ -72,4 +72,4 @@ public bool Changed() { return _listChangedByWatch; } -} +} \ No newline at end of file diff --git a/BlazorApp/Utils/TablePagedService.cs b/BlazorApp/Utils/TablePagedService.cs new file mode 100644 index 00000000..8b9c2c43 --- /dev/null +++ b/BlazorApp/Utils/TablePagedService.cs @@ -0,0 +1,98 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AntDesign.TableModels; +using BlazorApp.Service; + +namespace BlazorApp.Utils; + +public class TablePagedService +{ + private readonly INamespaceAction _listService; + + public TablePagedService(INamespaceAction listService) + { + _listService = listService; + } + + + /// + /// 页面显示的条目 + /// + public IList PagedItems; + + /// + /// 获取的原始条目 + /// + public IList OriginItems; + + private string _selectedNs; + + public IEnumerable SelectedRows; + + public int PageIndex = 1; + public int PageSize = 10; + public int Total = 100; + public bool Loading = false; + + void ChangePageSize(int pageSize) + { + if (PageSize == pageSize) return; + PageSize = pageSize; + PageIndex = 1; + } + + public async Task GetData(string ns) + { + Loading = true; + OriginItems = await _listService.ListItemsByNamespaceAsync(ns); + PagedItems = OriginItems; + Total = OriginItems.Count; + PageIndex = 1; + Loading = false; + } + + /// + /// 命名空间切换事件 + /// + /// + /// + public async Task OnNsSelectedHandler(string ns) + { + if (_selectedNs == ns) + { + return; + } + + Loading = true; + PageIndex = 1; + _selectedNs = ns; + await GetData(ns); + Loading = false; + } + + + /// + /// 变更事件 + /// + /// + public void OnChange(QueryModel queryModel) + { + PageIndex = queryModel.PageIndex; + PageSize = queryModel.PageSize; + Loading = true; + var query = OriginItems.Skip((PageIndex - 1) * PageSize).Take(PageSize); + PagedItems = query.ToList(); + Loading = false; + } + + public void OnSearch(IList predicate) + { + Loading = true; + PageIndex = 1; + OriginItems = predicate; + PagedItems = OriginItems.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList(); + Total = OriginItems.Count(); + Loading = false; + } +} diff --git a/Entity/IChatClient.cs b/Entity/IChatClient.cs index 4024d349..67e1b857 100644 --- a/Entity/IChatClient.cs +++ b/Entity/IChatClient.cs @@ -4,9 +4,8 @@ namespace Entity { - public interface IChatClient + public interface IChatClient where T : IKubernetesObject { - Task ReceiveMessage(string user, string message); - Task PodWatch(WatchEventType type, V1Pod item); + Task ResourceWatch(WatchEventType type, T item); } } diff --git a/Entity/ResourceWatchEntity.cs b/Entity/ResourceWatchEntity.cs new file mode 100644 index 00000000..cc548266 --- /dev/null +++ b/Entity/ResourceWatchEntity.cs @@ -0,0 +1,12 @@ +using System; +using k8s; +using k8s.Models; + +namespace Entity; + +public class ResourceWatchEntity where T : IKubernetesObject +{ + public string Message { get; set; } + public WatchEventType Type { get; set; } + public T Item { get; set; } +}