6 Commits

11 changed files with 390 additions and 167 deletions

View File

@@ -17,21 +17,21 @@
<StreamGeometry x:Key="arrow_download_regular">M12.25,39.5 L35.75,39.5 C36.4403559,39.5 37,40.0596441 37,40.75 C37,41.3972087 36.5081253,41.9295339 35.8778052,41.9935464 L35.75,42 L12.25,42 C11.5596441,42 11,41.4403559 11,40.75 C11,40.1027913 11.4918747,39.5704661 12.1221948,39.5064536 L12.25,39.5 L35.75,39.5 L12.25,39.5 Z M23.6221948,6.00645361 L23.75,6 C24.3972087,6 24.9295339,6.49187466 24.9935464,7.12219476 L25,7.25 L25,31.54 L30.6466793,25.8942911 C31.1348346,25.4061358 31.9262909,25.4061358 32.4144462,25.8942911 C32.9026016,26.3824465 32.9026016,27.1739027 32.4144462,27.6620581 L24.6362716,35.4402327 C24.1481163,35.928388 23.35666,35.928388 22.8685047,35.4402327 L15.0903301,27.6620581 C14.6021747,27.1739027 14.6021747,26.3824465 15.0903301,25.8942911 C15.5784855,25.4061358 16.3699417,25.4061358 16.858097,25.8942911 L22.5,31.536 L22.5,7.25 C22.5,6.60279131 22.9918747,6.0704661 23.6221948,6.00645361 L23.75,6 L23.6221948,6.00645361 Z</StreamGeometry>
<StreamGeometry x:Key="arrow_right_regular">M15.2685,4.20949 C14.97,3.92233 14.4952,3.93153 14.208,4.23005 C13.9209,4.52857 13.9301,5.00335 14.2286,5.29051 L22.5028,13.25 L3.75,13.25 C3.33579,13.25 3,13.5858 3,13.9999982 C3,14.4142 3.33579,14.75 3.75,14.75 L22.5018,14.75 L14.2286,22.7085 C13.9301,22.9957 13.9209,23.4705 14.208,23.769 C14.4952,24.0675 14.97,24.0767 15.2685,23.7896 L24.6965,14.7202 C25.1054,14.3268 25.1054,13.6723 24.6965,13.2788 L15.2685,4.20949 Z</StreamGeometry>
<StreamGeometry x:Key="arrow_sync_circle_regular">M12,2 C17.5228,2 22,6.47715 22,12 C22,17.5228 17.5228,22 12,22 C6.47715,22 2,17.5228 2,12 C2,6.47715 6.47715,2 12,2 Z M12,3.5 C7.30558,3.5 3.5,7.30558 3.5,12 C3.5,16.6944 7.30558,20.5 12,20.5 C16.6944,20.5 20.5,16.6944 20.5,12 C20.5,7.30558 16.6944,3.5 12,3.5 Z M16.75,12 C17.1296833,12 17.4434889,12.2821653 17.4931531,12.6482323 L17.5,12.75 L17.5,15.75 C17.5,16.1642 17.1642,16.5 16.75,16.5 C16.3703167,16.5 16.0565111,16.2178347 16.0068469,15.8517677 L16,15.75 L16,15 C15.0881,16.2143 13.6362,17 11.9999,17 C10.4748,17 9.09587,16.316 8.17857,15.237 C7.91028,14.9214 7.94862,14.4481 8.2642,14.1798 C8.57979,13.9115 9.05311,13.9499 9.3214,14.2655 C9.96322,15.0204 10.9293,15.5 11.9999,15.5 C13.32553,15.5 14.4803167,14.7625672 15.0742404,13.6746351 L15.1633,13.5 L14,13.5 C13.5858,13.5 13.25,13.1642 13.25,12.75 C13.25,12.3703167 13.5321653,12.0565111 13.8982323,12.0068469 L14,12 L16.75,12 Z M11.9999,7 C13.5368,7 14.9041,7.66036 15.8268,8.77062 C16.0915,9.08918 16.0479,9.56205 15.7294,9.8268 C15.4108,10.0916 14.9379,10.0479 14.6732,9.72938 C14.0368,8.96361 13.093,8.5 11.9999,8.5 C10.5754318,8.5 9.34895806,9.35140335 8.80281957,10.5730172 L8.72948,10.75 L10,10.75 C10.4142,10.75 10.75,11.0858 10.75,11.5 C10.75,11.8796833 10.4678347,12.1934889 10.1017677,12.2431531 L10,12.25 L7.25,12.25 C6.8703075,12.25 6.55650958,11.9678347 6.50684668,11.6017677 L6.5,11.5 L6.5,8.25 C6.5,7.83579 6.83579,7.5 7.25,7.5 C7.6296925,7.5 7.94349042,7.78215688 7.99315332,8.14823019 L8,8.25 L8,8.99955 C8.9121,7.78531 10.364,7 11.9999,7 Z</StreamGeometry>
<StreamGeometry x:Key="document_one_page_regular">M17.7499 2.00097C18.9408 2.00097 19.9156 2.92613 19.9947 4.09692L19.9999 4.25097V19.749C19.9999 20.9399 19.0748 21.9147 17.904 21.9938L17.7499 21.999H6.25006C5.0592 21.999 4.08442 21.0739 4.00525 19.9031L4.00006 19.749V4.25097C4.00006 3.0601 4.92522 2.08532 6.09601 2.00616L6.25006 2.00097H17.7499ZM17.7499 3.50097H6.25006C5.87037 3.50097 5.55657 3.78312 5.50691 4.1492L5.50006 4.25097V19.749C5.50006 20.1287 5.78222 20.4425 6.14829 20.4922L6.25006 20.499H17.7499C18.1296 20.499 18.4434 20.2169 18.4931 19.8508L18.4999 19.749V4.25097C18.4999 3.87127 18.2178 3.55748 17.8517 3.50782L17.7499 3.50097Z M7 15.75C7 15.3358 7.33578 15 7.75 15H16.25C16.6642 15 17 15.3358 17 15.75C17 16.1642 16.6642 16.5 16.25 16.5H7.75C7.33578 16.5 7 16.1642 7 15.75Z M7 7.75C7 7.33579 7.33578 7 7.75 7H16.25C16.6642 7 17 7.33579 17 7.75C17 8.16422 16.6642 8.5 16.25 8.5H7.75C7.33578 8.5 7 8.16422 7 7.75Z M7 11.75C7 11.3358 7.33578 11 7.75 11H16.25C16.6642 11 17 11.3358 17 11.75C17 12.1642 16.6642 12.5 16.25 12.5H7.75C7.33578 12.5 7 12.1642 7 11.75Z</StreamGeometry>
<StreamGeometry x:Key="arrow_sync_regular">M7.74944331,5.18010908 C8.0006303,5.50946902 7.93725859,5.9800953 7.60789865,6.23128229 C5.81957892,7.59514774 4.75,9.70820889 4.75,12 C4.75,15.7359812 7.57583716,18.8119527 11.2066921,19.2070952 L10.5303301,18.5303301 C10.2374369,18.2374369 10.2374369,17.7625631 10.5303301,17.4696699 C10.7965966,17.2034034 11.2132603,17.1791973 11.5068718,17.3970518 L11.5909903,17.4696699 L13.5909903,19.4696699 C13.8572568,19.7359365 13.8814629,20.1526002 13.6636084,20.4462117 L13.5909903,20.5303301 L11.5909903,22.5303301 C11.298097,22.8232233 10.8232233,22.8232233 10.5303301,22.5303301 C10.2640635,22.2640635 10.2398575,21.8473998 10.4577119,21.5537883 L10.5303301,21.4696699 L11.280567,20.7208479 C6.78460951,20.3549586 3.25,16.5902554 3.25,12 C3.25,9.23526399 4.54178532,6.68321165 6.6982701,5.03856442 C7.02763004,4.78737743 7.49825632,4.85074914 7.74944331,5.18010908 Z M13.4696699,1.46966991 C13.7625631,1.76256313 13.7625631,2.23743687 13.4696699,2.53033009 L12.7204313,3.27923335 C17.2159137,3.64559867 20.75,7.4100843 20.75,12 C20.75,14.6444569 19.5687435,17.0974104 17.5691913,18.7491089 C17.2498402,19.0129038 16.7771069,18.9678666 16.513312,18.6485156 C16.2495171,18.3291645 16.2945543,17.8564312 16.6139054,17.5926363 C18.2720693,16.2229363 19.25,14.1922015 19.25,12 C19.25,8.26436254 16.4246828,5.18861329 12.7943099,4.7930139 L13.4696699,5.46966991 C13.7625631,5.76256313 13.7625631,6.23743687 13.4696699,6.53033009 C13.1767767,6.8232233 12.701903,6.8232233 12.4090097,6.53033009 L10.4090097,4.53033009 C10.1161165,4.23743687 10.1161165,3.76256313 10.4090097,3.46966991 L12.4090097,1.46966991 C12.701903,1.1767767 13.1767767,1.1767767 13.4696699,1.46966991 Z</StreamGeometry>
<StreamGeometry x:Key="arrow_rotate_right_regular">M8.98489 5.00002C8.90135 5.00019 8.81821 5.00178 8.7355 5.00477C8.7297 5.00498 8.7239 5.0052 8.7181 5.00542C7.55763 5.05015 6.48324 5.36959 5.58643 5.88751C4.02356 6.79009 3 8.29543 3 10C3 11.7078 4.02751 13.2157 5.59546 14.1177C6.56274 14.6741 7.73571 15 9 15H9.5C9.81933 15 10.092 14.8004 10.2002 14.5192C10.2324 14.4357 10.25 14.3449 10.25 14.25C10.25 13.8358 9.91421 13.5 9.5 13.5H9C8.03665 13.5 7.14401 13.2646 6.412 12.8636C5.25554 12.2302 4.5 11.1837 4.5 10C4.5 8.81627 5.25554 7.76979 6.41201 7.13637C7.08679 6.76678 7.89807 6.53782 8.7754 6.50428L9 6.5H15.939L14.2197 8.21967C14.2193 8.21999 14.219 8.22032 14.2187 8.22064C13.9268 8.51361 13.9271 8.98776 14.2197 9.28033C14.4859 9.5466 14.9026 9.5708 15.1962 9.35295L15.2803 9.28033C15.2804 9.2803 15.2803 9.28037 15.2803 9.28033L15.2812 9.27945L18.2803 6.28033C18.2863 6.27432 18.2922 6.26823 18.298 6.26208C18.5616 5.98033 18.5672 5.54487 18.3149 5.25657C18.3038 5.24398 18.2923 5.23167 18.2803 5.21967L15.2813 2.22064C15.2811 2.22046 15.2815 2.22083 15.2813 2.22064C15.2812 2.2205 15.2805 2.21981 15.2803 2.21967L15.1962 2.14705C14.9026 1.9292 14.4859 1.9534 14.2197 2.21967C13.9271 2.51224 13.9268 2.98639 14.2187 3.27936C14.219 3.27968 14.2193 3.28001 14.2197 3.28033L15.938 5H9C8.99496 5 8.98992 5.00001 8.98489 5.00002ZM3.61101 20.0673C3.37203 20.2056 3.29046 20.5115 3.42882 20.7505C3.51821 20.9049 3.68311 21 3.86153 21H20C20.5522 21 21 20.5523 21 20V10.8672C21 10.5911 20.7761 10.3672 20.5 10.3672C20.412 10.3672 20.3256 10.3904 20.2494 10.4345L3.61101 20.0673ZM7.58467 19.5L19.5 12.6017V19.5H7.58467Z</StreamGeometry>
<StreamGeometry x:Key="arrow_rotate_clockwise_regular">M12 3C16.9706 3 21 7.02944 21 12C21 15.0777 19.4407 17.865 16.9769 19.5009L18.75 19.5C19.1642 19.5 19.5 19.8358 19.5 20.25C19.5 20.6297 19.2178 20.9435 18.8518 20.9932L18.75 21H14.75C14.3703 21 14.0565 20.7178 14.0068 20.3518L14 20.25V16.25C14 15.8358 14.3358 15.5 14.75 15.5C15.1297 15.5 15.4435 15.7822 15.4932 16.1482L15.5 16.25L15.501 18.635C17.9241 17.3557 19.5 14.8247 19.5 12C19.5 7.85786 16.1421 4.5 12 4.5C7.85786 4.5 4.5 7.85786 4.5 12C4.5 12.4142 4.16421 12.75 3.75 12.75C3.33579 12.75 3 12.4142 3 12C3 7.02944 7.02944 3 12 3ZM12 9.25C13.5188 9.25 14.75 10.4812 14.75 12C14.75 13.5188 13.5188 14.75 12 14.75C10.4812 14.75 9.25 13.5188 9.25 12C9.25 10.4812 10.4812 9.25 12 9.25ZM12 10.75C11.3096 10.75 10.75 11.3096 10.75 12C10.75 12.6904 11.3096 13.25 12 13.25C12.6904 13.25 13.25 12.6904 13.25 12C13.25 11.3096 12.6904 10.75 12 10.75Z</StreamGeometry>
<StreamGeometry x:Key="warning_regular">M10.9093922,2.78216375 C11.9491636,2.20625071 13.2471955,2.54089334 13.8850247,3.52240345 L13.9678229,3.66023048 L21.7267791,17.6684928 C21.9115773,18.0021332 22.0085303,18.3772743 22.0085303,18.7586748 C22.0085303,19.9495388 21.0833687,20.9243197 19.9125791,21.003484 L19.7585303,21.0086748 L4.24277801,21.0086748 C3.86146742,21.0086748 3.48641186,20.9117674 3.15282824,20.7270522 C2.11298886,20.1512618 1.7079483,18.8734454 2.20150311,17.8120352 L2.27440063,17.668725 L10.0311968,3.66046274 C10.2357246,3.291099 10.5400526,2.98673515 10.9093922,2.78216375 Z M20.4146132,18.3952808 L12.6556571,4.3870185 C12.4549601,4.02467391 11.9985248,3.89363262 11.6361802,4.09432959 C11.5438453,4.14547244 11.4637001,4.21532637 11.4006367,4.29899869 L11.3434484,4.38709592 L3.58665221,18.3953582 C3.385998,18.7577265 3.51709315,19.2141464 3.87946142,19.4148006 C3.96285732,19.4609794 4.05402922,19.4906942 4.14802472,19.5026655 L4.24277801,19.5086748 L19.7585303,19.5086748 C20.1727439,19.5086748 20.5085303,19.1728883 20.5085303,18.7586748 C20.5085303,18.6633247 20.4903516,18.5691482 20.455275,18.4811011 L20.4146132,18.3952808 L12.6556571,4.3870185 L20.4146132,18.3952808 Z M12.0004478,16.0017852 C12.5519939,16.0017852 12.9991104,16.4489016 12.9991104,17.0004478 C12.9991104,17.5519939 12.5519939,17.9991104 12.0004478,17.9991104 C11.4489016,17.9991104 11.0017852,17.5519939 11.0017852,17.0004478 C11.0017852,16.4489016 11.4489016,16.0017852 12.0004478,16.0017852 Z M11.9962476,8.49954934 C12.3759432,8.49924613 12.689964,8.78114897 12.7399193,9.14718469 L12.7468472,9.24894974 L12.750448,13.7505438 C12.7507788,14.1647572 12.4152611,14.5008121 12.0010476,14.5011439 C11.621352,14.5014471 11.3073312,14.2195442 11.257376,13.8535085 L11.250448,13.7517435 L11.2468472,9.25014944 C11.2465164,8.83593601 11.5820341,8.49988112 11.9962476,8.49954934 Z</StreamGeometry>
</Style.Resources>
<Style Selector="PathIcon.loading">
<Style.Animations>
<Animation Duration="0:0:1" IterationCount="INFINITE">
<Animation Duration="0:0:2" IterationCount="INFINITE">
<KeyFrame Cue="0%">
<Setter Property="RotateTransform.Angle" Value="0"/>
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="RotateTransform.Angle" Value="179"/>
<Setter Property="RotateTransform.Angle" Value="359"/>
</KeyFrame>
</Animation>
</Style.Animations>

View File

@@ -0,0 +1,6 @@
This favicon was generated using the following font:
- Font Title: Genos
- Font Author: Copyright 2011 The Genos Project Authors (https://github.com/googlefonts/genos)
- Font Source: http://fonts.gstatic.com/s/genos/v10/SlGNmQqPqpUOYTYjacb0Hc91fTwVA0_orUK6K7ZsAg.ttf
- Font License: SIL Open Font License, 1.1 (http://scripts.sil.org/OFL))

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 172 KiB

View File

@@ -8,6 +8,8 @@
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
<Configurations>Debug;Release</Configurations>
<Platforms>AnyCPU</Platforms>
<ApplicationIcon>Assets/app.ico</ApplicationIcon>
<Version>0.0.3</Version>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DefineConstants>TRACE</DefineConstants>
@@ -28,7 +30,9 @@
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.1" />
<PackageReference Include="KubernetesClient" Version="11.0.44" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,12 @@
using Avalonia.Media;
namespace K8sFileBrowser.Models;
public class Message
{
public bool IsVisible { get; set; }
public string Text { get; set; } = string.Empty;
public bool IsError { get; set; }
public IBrush Color => IsError ? new SolidColorBrush(Avalonia.Media.Color.FromRgb(74, 7, 2)) : Brushes.Black;
public double Opacity => 0.7;
}

View File

@@ -16,8 +16,8 @@ class Program
{
Log.Logger = new LoggerConfiguration()
//.Filter.ByIncludingOnly(Matching.WithProperty("Area", LogArea.Control))
.MinimumLevel.Verbose()
.WriteTo.Console()
.MinimumLevel.Information()
.WriteTo.Async(a => a.File("app.log"))
.CreateLogger();
BuildAvaloniaApp()

View File

@@ -0,0 +1,23 @@
// // Copyright (c) Vector Informatik GmbH. All rights reserved.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using K8sFileBrowser.Models;
namespace K8sFileBrowser.Services;
public interface IKubernetesService
{
IEnumerable<ClusterContext> GetClusterContexts();
string GetCurrentContext();
void SwitchClusterContext(ClusterContext clusterContext);
Task<IEnumerable<Namespace>> GetNamespacesAsync();
Task<IEnumerable<Pod>> GetPodsAsync(string namespaceName, CancellationToken cancellationToken = default);
IList<FileInformation> GetFiles(string namespaceName, string podName, string containerName, string path);
Task DownloadFile(Namespace? selectedNamespace, Pod? selectedPod, FileInformation selectedFile,
string? saveFileName, CancellationToken cancellationToken = default);
Task DownloadLog(Namespace? selectedNamespace, Pod? selectedPod,
string? saveFileName, CancellationToken cancellationToken = default);
}

View File

@@ -14,7 +14,7 @@ using static ICSharpCode.SharpZipLib.Core.StreamUtils;
namespace K8sFileBrowser.Services;
public class KubernetesService
public class KubernetesService : IKubernetesService
{
private readonly K8SConfiguration _k8SConfiguration;
private IKubernetes _kubernetesClient = null!;
@@ -40,7 +40,7 @@ public class KubernetesService
CreateKubernetesClient(clusterContext);
}
public IEnumerable<Namespace> GetNamespaces()
public async Task<IEnumerable<Namespace>> GetNamespacesAsync()
{
var namespaces = _kubernetesClient.CoreV1.ListNamespace();
var namespaceList = namespaces != null
@@ -49,9 +49,9 @@ public class KubernetesService
return namespaceList;
}
public IEnumerable<Pod> GetPods(string namespaceName)
public async Task<IEnumerable<Pod>> GetPodsAsync(string namespaceName, CancellationToken cancellationToken = default)
{
var pods = _kubernetesClient.CoreV1.ListNamespacedPod(namespaceName);
var pods = await _kubernetesClient.CoreV1.ListNamespacedPodAsync(namespaceName, cancellationToken: cancellationToken);
var podList = pods != null
? pods.Items.Select(n =>
new Pod
@@ -73,7 +73,7 @@ public class KubernetesService
podName, namespaceName, containerName,
new[] { "find", path, "-maxdepth", "1", "-exec", "stat", "-c", "%F|%n|%s|%Y", "{}", ";" },
true,
execResult.ParseFileInformationCallback, CancellationToken.None)
(@in, @out, err) => execResult.ParseFileInformationCallback(@in, @out, err), CancellationToken.None)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
@@ -98,11 +98,10 @@ public class KubernetesService
_kubernetesClient = kubernetesClient;
}
public async Task DownloadFile(Namespace? selectedNamespace, Pod? selectedPod, FileInformation selectedFile,
string? saveFileName)
string? saveFileName, CancellationToken cancellationToken = default)
{
Log.Information("{SelectedNamespace} - {SelectedPod} - {SelectedFile} - {SaveFileName}",
Log.Information("{SelectedNamespace} - {SelectedPod} - {@SelectedFile} - {SaveFileName}",
selectedNamespace, selectedPod, selectedFile, saveFileName);
var handler = new ExecAsyncCallback(async (_, stdOut, stdError) =>
{
@@ -111,37 +110,57 @@ public class KubernetesService
await using var outputFileStream = File.OpenWrite(saveFileName!);
await using var tarInputStream = new TarInputStream(stdOut, Encoding.Default);
var entry = await tarInputStream.GetNextEntryAsync(default);
var entry = await tarInputStream.GetNextEntryAsync(cancellationToken);
if (entry == null)
{
Log.Error("Copy command failed: no files found");
throw new IOException("Copy command failed: no files found");
}
var bytes = new byte[entry.Size];
ReadFully( tarInputStream, bytes );
await outputFileStream.WriteAsync(bytes, default);
await outputFileStream.WriteAsync(bytes, cancellationToken);
}
catch (Exception ex)
{
Log.Error(ex, "Copy command failed");
throw new IOException($"Copy command failed: {ex.Message}");
}
using var streamReader = new StreamReader(stdError);
while (streamReader.EndOfStream == false)
{
var error = await streamReader.ReadToEndAsync(default);
Log.Error(error);
var error = await streamReader.ReadToEndAsync(cancellationToken);
Log.Error("Remote error: {Error}",error);
}
});
// the kubectl uses also tar for copying files
await _kubernetesClient.NamespacedPodExecAsync(
selectedPod.Name,
selectedNamespace.Name,
selectedPod.Containers.First(),
selectedPod?.Name,
selectedNamespace?.Name,
selectedPod?.Containers.First(),
new[] { "sh", "-c", $"tar cf - {selectedFile.Name}" },
false,
handler,
default);
cancellationToken);
}
public async Task DownloadLog(Namespace? selectedNamespace, Pod? selectedPod,
string? saveFileName, CancellationToken cancellationToken = default)
{
Log.Information("{SelectedNamespace} - {SelectedPod} - {SaveFileName}",
selectedNamespace, selectedPod, saveFileName);
// the kubectl uses also tar for copying files
var response = await _kubernetesClient.CoreV1.ReadNamespacedPodLogWithHttpMessagesAsync(
selectedPod?.Name,
selectedNamespace?.Name,
container: selectedPod?.Containers.First(),
follow: false , cancellationToken: cancellationToken)
.ConfigureAwait(false);
await using var outputFileStream = File.OpenWrite(saveFileName!);
var stream = response.Body;
await stream.CopyToAsync(outputFileStream, cancellationToken);
}
}

View File

@@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Threading.Tasks;
using K8sFileBrowser.Models;
using K8sFileBrowser.Services;
using ReactiveUI;
@@ -11,40 +13,48 @@ namespace K8sFileBrowser.ViewModels;
public class MainWindowViewModel : ViewModelBase
{
private readonly ObservableAsPropertyHelper<IEnumerable<ClusterContext>> _clusterContexts;
private ObservableAsPropertyHelper<IEnumerable<ClusterContext>> _clusterContexts;
public IEnumerable<ClusterContext> ClusterContexts => _clusterContexts.Value;
private ClusterContext? _selectedClusterContext;
public ClusterContext? SelectedClusterContext
{
get => _selectedClusterContext;
set => this.RaiseAndSetIfChanged(ref _selectedClusterContext, value);
}
private readonly ObservableAsPropertyHelper<IEnumerable<Namespace>> _namespaces;
public IEnumerable<Namespace> Namespaces => _namespaces.Value;
private IEnumerable<Namespace> _namespaces;
public IEnumerable<Namespace> Namespaces
{
get => _namespaces;
set => this.RaiseAndSetIfChanged(ref _namespaces, value);
}
private Namespace? _selectedNamespace;
public Namespace? SelectedNamespace
{
get => _selectedNamespace;
set => this.RaiseAndSetIfChanged(ref _selectedNamespace, value);
}
private readonly ObservableAsPropertyHelper<IEnumerable<Pod>> _pods;
private ObservableAsPropertyHelper<IEnumerable<Pod>> _pods;
public IEnumerable<Pod> Pods => _pods.Value;
private Pod? _selectedPod;
public Pod? SelectedPod
{
get => _selectedPod;
set => this.RaiseAndSetIfChanged(ref _selectedPod, value);
}
private readonly ObservableAsPropertyHelper<IEnumerable<FileInformation>> _fileInformation;
private ObservableAsPropertyHelper<IEnumerable<FileInformation>> _fileInformation;
public IEnumerable<FileInformation> FileInformation => _fileInformation.Value;
private FileInformation? _selectedFile;
public FileInformation? SelectedFile
{
get => _selectedFile;
@@ -52,105 +62,52 @@ public class MainWindowViewModel : ViewModelBase
}
private string? _selectedPath;
public string? SelectedPath
{
get => _selectedPath;
set => this.RaiseAndSetIfChanged(ref _selectedPath, value);
}
private bool _isDownloadActive;
public bool IsDownloadActive
private Message _message;
public Message Message
{
get => _isDownloadActive;
set => this.RaiseAndSetIfChanged(ref _isDownloadActive, value);
get => _message;
set => this.RaiseAndSetIfChanged(ref _message, value);
}
public ReactiveCommand<Unit, Unit> DownloadCommand { get; }
public ReactiveCommand<Unit, Unit> ParentCommand { get; }
public ReactiveCommand<Unit, Unit> OpenCommand { get; }
public ReactiveCommand<Unit, Unit> DownloadCommand { get; private set; }
public ReactiveCommand<Unit, Unit> DownloadLogCommand { get; private set; }
public ReactiveCommand<Unit, Unit> ParentCommand { get; private set; }
public ReactiveCommand<Unit, Unit> OpenCommand { get; private set; }
private ReactiveCommand<Namespace, IEnumerable<Pod>> GetPodsForNamespace { get; set; }
public MainWindowViewModel()
{
var kubernetesService = new KubernetesService();
//TODO: use dependency injection to get the kubernetes service
IKubernetesService kubernetesService = new KubernetesService();
var isFile = this
.WhenAnyValue(x => x.SelectedFile, x => x.IsDownloadActive)
.Select(x => x is { Item1.Type: FileType.File, Item2: false });
// commands
ConfigureOpenDirectoryCommand();
ConfigureDownloadFileCommand(kubernetesService);
ConfigureDownloadLogCommand(kubernetesService);
ConfigureParentDirectoryCommand();
ConfigureGetPodsForNamespaceCommand(kubernetesService);
var isDirectory = this
.WhenAnyValue(x => x.SelectedFile, x => x.IsDownloadActive)
.Select(x => x is { Item1.Type: FileType.Directory, Item2: false });
// register the listeners
RegisterReadNamespaces(kubernetesService);
RegisterReadPods();
RegisterReadFiles(kubernetesService);
RegisterResetPath();
var isNotRoot = this
.WhenAnyValue(x => x.SelectedPath, x => x.IsDownloadActive)
.Select(x => x.Item1 is not "/" && !x.Item2);
OpenCommand = ReactiveCommand.Create(() =>
{
SelectedPath = SelectedFile != null ? SelectedFile!.Name : "/";
}, isDirectory, RxApp.MainThreadScheduler);
DownloadCommand = ReactiveCommand.CreateFromTask(async () =>
{
await Observable.StartAsync(async () => {
var fileName = SelectedFile!.Name.Substring(SelectedFile!.Name.LastIndexOf('/') + 1, SelectedFile!.Name.Length - SelectedFile!.Name.LastIndexOf('/') - 1);
var saveFileName = await ApplicationHelper.SaveFile(".", fileName);
if (saveFileName != null)
{
IsDownloadActive = true;
await kubernetesService.DownloadFile(SelectedNamespace, SelectedPod, SelectedFile, saveFileName);
IsDownloadActive = false;
// load the cluster contexts
InitiallyLoadContexts(kubernetesService);
}
}, RxApp.TaskpoolScheduler);
}, isFile, RxApp.MainThreadScheduler);
ParentCommand = ReactiveCommand.Create(() =>
private void InitiallyLoadContexts(IKubernetesService kubernetesService)
{
SelectedPath = SelectedPath![..SelectedPath!.LastIndexOf('/')];
if (SelectedPath!.Length == 0)
{
SelectedPath = "/";
}
}, isNotRoot, RxApp.MainThreadScheduler);
// read the cluster contexts
_namespaces = this
.WhenAnyValue(c => c.SelectedClusterContext)
.Throttle(TimeSpan.FromMilliseconds(10))
.Where(context => context != null)
.Select(context =>
{
kubernetesService.SwitchClusterContext(context!);
return kubernetesService.GetNamespaces();
})
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.Namespaces);
// read the pods when the namespace changes
_pods = this
.WhenAnyValue(c => c.SelectedNamespace)
.Throttle(TimeSpan.FromMilliseconds(10))
.Where(ns => ns != null)
.Select(ns => kubernetesService.GetPods(ns!.Name))
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.Pods);
// read the file information when the path changes
_fileInformation = this
.WhenAnyValue(c => c.SelectedPath, c => c.SelectedPod, c => c.SelectedNamespace)
.Throttle(TimeSpan.FromMilliseconds(10))
.Select(x => x.Item3 == null || x.Item2 == null
? new List<FileInformation>()
: kubernetesService.GetFiles(x.Item3!.Name, x.Item2!.Name, x.Item2!.Containers.First(),
x.Item1))
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.FileInformation);
// reset the path when the pod or namespace changes
this.WhenAnyValue(c => c.SelectedPod, c => c.SelectedNamespace)
.Subscribe(x => SelectedPath = "/");
// load the cluster contexts when the view model is created
var loadContexts = ReactiveCommand
.Create<Unit, IEnumerable<ClusterContext>>(_ => kubernetesService.GetClusterContexts());
@@ -161,4 +118,202 @@ public class MainWindowViewModel : ViewModelBase
SelectedClusterContext = ClusterContexts
.FirstOrDefault(x => x.Name == kubernetesService.GetCurrentContext());
}
private void RegisterResetPath()
{
// reset the path when the pod or namespace changes
this.WhenAnyValue(c => c.SelectedPod, c => c.SelectedNamespace)
.Throttle(new TimeSpan(10))
.Subscribe(x => SelectedPath = "/");
}
private void RegisterReadFiles(IKubernetesService kubernetesService)
{
// read the file information when the path changes
_fileInformation = this
.WhenAnyValue(c => c.SelectedPath, c => c.SelectedPod, c => c.SelectedNamespace)
.Throttle(new TimeSpan(10))
.Select(x => x.Item3 == null || x.Item2 == null
? new List<FileInformation>()
: kubernetesService.GetFiles(x.Item3!.Name, x.Item2!.Name, x.Item2!.Containers.First(),
x.Item1))
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.FileInformation);
}
private void RegisterReadPods()
{
// read the pods when the namespace changes
_pods = this
.WhenAnyValue(c => c.SelectedNamespace)
.Throttle(new TimeSpan(10))
.SelectMany(ns => GetPodsForNamespace.Execute(ns))
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.Pods);
}
private void RegisterReadNamespaces(IKubernetesService kubernetesService)
{
// read the cluster contexts
this
.WhenAnyValue(c => c.SelectedClusterContext)
.Throttle(new TimeSpan(10))
.SelectMany(context => GetClusterContextAsync(context, kubernetesService))
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(ns => Namespaces = ns);
}
private void ConfigureGetPodsForNamespaceCommand(IKubernetesService kubernetesService)
{
GetPodsForNamespace = ReactiveCommand.CreateFromObservable<Namespace, IEnumerable<Pod>>(ns =>
Observable.StartAsync(_ => PodsAsync(ns, kubernetesService), RxApp.TaskpoolScheduler));
GetPodsForNamespace.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(ex => ShowErrorMessage(ex.Message).ConfigureAwait(false).GetAwaiter().GetResult());
}
private void ConfigureParentDirectoryCommand()
{
var isNotRoot = this
.WhenAnyValue(x => x.SelectedPath, x => x.Message.IsVisible)
.Select(x => x.Item1 is not "/" && !x.Item2);
ParentCommand = ReactiveCommand.Create(() =>
{
SelectedPath = SelectedPath![..SelectedPath!.LastIndexOf('/')];
if (SelectedPath!.Length == 0)
{
SelectedPath = "/";
}
}, isNotRoot, RxApp.MainThreadScheduler);
ParentCommand.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(ex => ShowErrorMessage(ex.Message).ConfigureAwait(false).GetAwaiter().GetResult());
}
private void ConfigureDownloadLogCommand(IKubernetesService kubernetesService)
{
var isSelectedPod = this
.WhenAnyValue(x => x.SelectedPod)
.Select(x => x != null);
DownloadLogCommand = ReactiveCommand.CreateFromTask(async () =>
{
await Observable.StartAsync(async () =>
{
var fileName = SelectedPod?.Name + ".log";
var saveFileName = await ApplicationHelper.SaveFile(".", fileName);
if (saveFileName != null)
{
ShowWorkingMessage("Downloading Log...");
await kubernetesService.DownloadLog(SelectedNamespace, SelectedPod, saveFileName);
HideWorkingMessage();
}
}, RxApp.TaskpoolScheduler);
}, isSelectedPod, RxApp.MainThreadScheduler);
DownloadLogCommand.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(ex => ShowErrorMessage(ex.Message).ConfigureAwait(false).GetAwaiter().GetResult());
}
private void ConfigureDownloadFileCommand(IKubernetesService kubernetesService)
{
var isFile = this
.WhenAnyValue(x => x.SelectedFile, x => x.Message.IsVisible)
.Select(x => x is { Item1.Type: FileType.File, Item2: false });
DownloadCommand = ReactiveCommand.CreateFromTask(async () =>
{
await Observable.StartAsync(async () =>
{
var fileName = SelectedFile!.Name.Substring(SelectedFile!.Name.LastIndexOf('/') + 1,
SelectedFile!.Name.Length - SelectedFile!.Name.LastIndexOf('/') - 1);
var saveFileName = await ApplicationHelper.SaveFile(".", fileName);
if (saveFileName != null)
{
ShowWorkingMessage("Downloading File...");
await kubernetesService.DownloadFile(SelectedNamespace, SelectedPod, SelectedFile, saveFileName);
HideWorkingMessage();
}
}, RxApp.TaskpoolScheduler);
}, isFile, RxApp.MainThreadScheduler);
DownloadCommand.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(ex => ShowErrorMessage(ex.Message).ConfigureAwait(false).GetAwaiter().GetResult());
}
private void ConfigureOpenDirectoryCommand()
{
var isDirectory = this
.WhenAnyValue(x => x.SelectedFile, x => x.Message.IsVisible)
.Select(x => x is { Item1.Type: FileType.Directory, Item2: false });
OpenCommand = ReactiveCommand.Create(() => { SelectedPath = SelectedFile != null ? SelectedFile!.Name : "/"; },
isDirectory, RxApp.MainThreadScheduler);
OpenCommand.ThrownExceptions.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(ex => ShowErrorMessage(ex.Message).ConfigureAwait(false).GetAwaiter().GetResult());
}
private static async Task<IEnumerable<Pod>> PodsAsync(Namespace? ns, IKubernetesService kubernetesService)
{
if (ns == null)
return new List<Pod>();
return await kubernetesService.GetPodsAsync(ns.Name);
}
private async Task<IEnumerable<Namespace>> GetClusterContextAsync(ClusterContext? context, IKubernetesService kubernetesService)
{
if (context == null)
return new List<Namespace>();
try
{
ShowWorkingMessage("Switching context...");
Namespaces = new List<Namespace>();
kubernetesService.SwitchClusterContext(context!);
var namespaces = await kubernetesService.GetNamespacesAsync();
HideWorkingMessage();
return namespaces;
}
catch (Exception e)
{
RxApp.MainThreadScheduler.Schedule(Action);
return new List<Namespace>();
async void Action() => await ShowErrorMessage(e.Message);
}
}
private void ShowWorkingMessage(string message)
{
Message = new Message
{
IsVisible = true,
Text = message,
IsError = false
};
}
private async Task ShowErrorMessage(string message)
{
Message = new Message
{
IsVisible = true,
Text = message,
IsError = true
};
await Task.Delay(7000);
HideWorkingMessage();
}
private void HideWorkingMessage()
{
Message = new Message
{
IsVisible = false,
Text = "",
IsError = false
};
}
}

View File

@@ -7,7 +7,7 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="K8sFileBrowser.Views.MainWindow"
x:DataType="vm:MainWindowViewModel"
Icon="/Assets/avalonia-logo.ico"
Icon="/Assets/app.ico"
Title="K8sFileBrowser">
<Design.DataContext>
@@ -15,10 +15,11 @@
</Design.DataContext>
<Grid>
<Border ZIndex="1" IsVisible="{Binding IsDownloadActive}" Background="Black" Opacity="0.7">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<PathIcon Classes="loading" Data="{StaticResource arrow_sync_regular}" Width="100" Height="100"></PathIcon>
<TextBlock>Download File...</TextBlock>
<Border ZIndex="1" IsVisible="{Binding Message.IsVisible}" Background="{Binding Message.Color}" Opacity="{Binding Message.Opacity}">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Spacing="20" MaxWidth="500">
<PathIcon Classes="loading" Data="{StaticResource arrow_rotate_clockwise_regular}" Width="100" Height="100" IsVisible="{Binding !Message.IsError}"></PathIcon>
<PathIcon Data="{StaticResource warning_regular}" Width="100" Height="100" IsVisible="{Binding Message.IsError}"></PathIcon>
<TextBlock TextWrapping="Wrap" Text="{Binding Message.Text}">.</TextBlock>
</StackPanel>
</Border>
<Grid RowDefinitions="Auto, *">
@@ -87,6 +88,9 @@
<Button Command="{Binding DownloadCommand}" VerticalAlignment="Center" ToolTip.Tip="Download File">
<PathIcon Data="{StaticResource arrow_download_regular}"></PathIcon>
</Button>
<Button Command="{Binding DownloadLogCommand}" VerticalAlignment="Center" ToolTip.Tip="Download Pod Log">
<PathIcon Data="{StaticResource document_one_page_regular}"></PathIcon>
</Button>
</StackPanel>
</Grid>
<DataGrid Grid.Row="1"