Skip to content

Commit ac7e101

Browse files
committed
fix code smells
1 parent 125ed1d commit ac7e101

File tree

7 files changed

+94
-77
lines changed

7 files changed

+94
-77
lines changed

AzureDevOpsTeamMembersVelocity/Pages/KubernetesDashboard.razor.cs

Lines changed: 78 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -334,64 +334,7 @@ await podChannel.OnMessageRecieved(async pod =>
334334
Logger.LogError(JsonSerializer.Serialize(pod.Item2));
335335
break;
336336
case WatchEventType.Modified:
337-
if (Pods.TryGetValue(key, out var oldValue) && Pods.TryUpdate(key, pod.Item2, oldValue))
338-
{
339-
var taskKey = PodLogsTaskKey(pod.Item2.Metadata.NamespaceProperty, pod.Item2.Metadata.Name);
340-
341-
if (TokensBag.TryGetValue(taskKey, out var token))
342-
{
343-
if (pod.Item2.Status.Phase == "Succeeded" ||
344-
pod.Item2.Status.Phase == "Failed")
345-
{
346-
token.Cancel();
347-
348-
TokensBag.Remove(taskKey);
349-
}
350-
else if (pod.Item2.Status.Phase == "Running" ||
351-
pod.Item2.Status.Phase == "Pending")
352-
{
353-
Logger.LogInformation("Get metadata update on a pod that listen to logs");
354-
}
355-
else
356-
{
357-
Logger.LogWarning("Unknow state recieved from the hub");
358-
Logger.LogWarning(WatchEventType.Modified.ToString());
359-
Logger.LogWarning(JsonSerializer.Serialize(pod.Item2));
360-
}
361-
}
362-
else
363-
{
364-
if (pod.Item2.Status.Phase == "Succeeded" ||
365-
pod.Item2.Status.Phase == "Failed")
366-
{
367-
Logger.LogInformation("Get metadata update on a pod that listen to logs, but the pod is not ready for listening to logs");
368-
}
369-
else if (pod.Item2.Status.Phase == "Running" || pod.Item2.Status.Phase == "Pending")
370-
{
371-
if (DeployementCheckedForLogs.Any(d => pod.Item2.Metadata.Name.StartsWith(d)))
372-
{
373-
Logger.LogInformation("Start listen to pod log after reveived a ready state");
374-
taskList.Add(ListenTopPodLogs(PodLogsTaskKey(@namespace, podName), @namespace, podName));
375-
}
376-
else
377-
{
378-
Logger.LogInformation($"Recived {WatchEventType.Modified} from the hub for pod {podName} in namespace {@namespace}");
379-
}
380-
}
381-
else
382-
{
383-
Logger.LogWarning("Unknow state recieved from the hub");
384-
Logger.LogWarning(WatchEventType.Modified.ToString());
385-
Logger.LogWarning(JsonSerializer.Serialize(pod.Item2));
386-
}
387-
}
388-
}
389-
else
390-
{
391-
var message = $"Failed to update pod {pod.Item2.Metadata.Name}";
392-
Logger.LogError(message);
393-
Error = message;
394-
}
337+
OnUpdatePodInfo(key, pod.Item2, taskList);
395338
break;
396339
default:
397340
throw new InvalidOperationException($"Unknow {nameof(WatchEventType)} with value {pod.Item1}");
@@ -407,6 +350,68 @@ await InvokeAsync(() =>
407350
await Task.WhenAll(taskList);
408351
}
409352

353+
private void OnUpdatePodInfo(string key, V1Pod pod, List<Task> taskList)
354+
{
355+
if (Pods.TryGetValue(key, out var oldValue) && Pods.TryUpdate(key, pod, oldValue))
356+
{
357+
var taskKey = PodLogsTaskKey(pod.Metadata.NamespaceProperty, pod.Metadata.Name);
358+
359+
if (TokensBag.TryGetValue(taskKey, out var token))
360+
{
361+
if (pod.Status.Phase == "Succeeded" ||
362+
pod.Status.Phase == "Failed")
363+
{
364+
token.Cancel();
365+
366+
TokensBag.Remove(taskKey);
367+
}
368+
else if (pod.Status.Phase == "Running" ||
369+
pod.Status.Phase == "Pending")
370+
{
371+
Logger.LogInformation("Get metadata update on a pod that listen to logs");
372+
}
373+
else
374+
{
375+
Logger.LogWarning("Unknow state recieved from the hub");
376+
Logger.LogWarning(WatchEventType.Modified.ToString());
377+
Logger.LogWarning(JsonSerializer.Serialize(pod));
378+
}
379+
}
380+
else
381+
{
382+
if (pod.Status.Phase == "Succeeded" ||
383+
pod.Status.Phase == "Failed")
384+
{
385+
Logger.LogInformation("Get metadata update on a pod that listen to logs, but the pod is not ready for listening to logs");
386+
}
387+
else if (pod.Status.Phase == "Running" || pod.Status.Phase == "Pending")
388+
{
389+
if (DeployementCheckedForLogs.Any(d => pod.Metadata.Name.StartsWith(d)))
390+
{
391+
Logger.LogInformation("Start listen to pod log after reveived a ready state");
392+
taskList.Add(ListenTopPodLogs(PodLogsTaskKey(pod.Namespace(), pod.Name()), pod.Namespace(), pod.Name()));
393+
}
394+
else
395+
{
396+
Logger.LogInformation($"Recived {WatchEventType.Modified} from the hub for pod {pod.Name()} in namespace {pod.Namespace()}");
397+
}
398+
}
399+
else
400+
{
401+
Logger.LogWarning("Unknow state recieved from the hub");
402+
Logger.LogWarning(WatchEventType.Modified.ToString());
403+
Logger.LogWarning(JsonSerializer.Serialize(pod));
404+
}
405+
}
406+
}
407+
else
408+
{
409+
var message = $"Failed to update pod {pod.Metadata.Name}";
410+
Logger.LogError(message);
411+
Error = message;
412+
}
413+
}
414+
410415
private async Task OnPodLogClick(ChangeEventArgs args, string podNamespace, string podName)
411416
{
412417
var taskKey = PodLogsTaskKey(podNamespace, podName);
@@ -522,7 +527,21 @@ private async Task DeleteNamespace(V1Namespace ns)
522527

523528
private void ClearLogs()
524529
{
525-
while (PodLogs.TryTake(out _)) ;
530+
try
531+
{
532+
bool trySucceeded = false;
533+
534+
do
535+
{
536+
trySucceeded = PodLogs.TryTake(out _);
537+
538+
} while (trySucceeded);
539+
}
540+
catch (Exception e)
541+
{
542+
Error = $"An error append when clearing logs: {e.Message}";
543+
Logger.LogWarning(e, e.Message);
544+
}
526545
}
527546

528547
private bool ScrollToBottom { get; set; }

AzureDevOpsTeamMembersVelocity/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void ConfigureServices(IServiceCollection services)
9696
{
9797
if (string.Equals(Configuration["MockK8s"], bool.TrueString, StringComparison.OrdinalIgnoreCase))
9898
{
99-
return K8sClientFactory.CreateClientForIntegrationTest();
99+
return K8SClientFactory.CreateClientForIntegrationTest();
100100
}
101101

102102
if (KubernetesClientConfiguration.IsInCluster())

IntegrationTest/ApplicationFactory/TeamVelocityWebAppFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
3030
// Mock kubernetes
3131
services.RemoveAll(typeof(IKubernetes));
3232

33-
var k8sClient = K8sClientFactory.CreateClientForIntegrationTest(new TestOutputHelper());
33+
var k8sClient = K8SClientFactory.CreateClientForIntegrationTest(new TestOutputHelper());
3434

3535
services.AddSingleton(sp => k8sClient);
3636

IntegrationTest/Hubs/HubCommunicationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task SignlaRTestSetup()
2828

2929
services.AddTeamMemberVelocityAutorisation(context.Configuration);
3030

31-
services.AddSingleton<IKubernetes>(K8sClientFactory.CreateClientForIntegrationTest(new TestOutputHelper()));
31+
services.AddSingleton<IKubernetes>(K8SClientFactory.CreateClientForIntegrationTest(new TestOutputHelper()));
3232

3333
services.AddSignalR();
3434
})

MockK8s/K8sClientFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace MockK8s
1212
{
13-
public static class K8sClientFactory
13+
public static class K8SClientFactory
1414
{
1515
private static readonly string AddedNamespaceStreamLine = BuildWatchEventStreamLine(WatchEventType.Added, MockKubeApiServer.MockNamespaceResponse);
1616
private static readonly string AddedDeploymentStreamLine = BuildWatchEventStreamLine(WatchEventType.Added, MockKubeApiServer.MockDeploymentReponse);
@@ -19,7 +19,6 @@ public static class K8sClientFactory
1919
public static IKubernetes CreateClientForIntegrationTest(ITestOutputHelper? testOutput = null)
2020
{
2121
var created = new AsyncManualResetEvent(false);
22-
var eventsReceived = new AsyncManualResetEvent(false);
2322

2423
var server = new MockKubeApiServer(testOutput,
2524
async httpContext =>

MockK8s/MockWebSocket.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void SetState(WebSocketState state)
2727
this.state = state;
2828
}
2929

30-
public EventHandler<MessageDataEventArgs> MessageSent { get; set; }
30+
public EventHandler<MessageDataEventArgs>? MessageSent { get; set; }
3131

3232
public Task InvokeReceiveAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage)
3333
{
@@ -54,8 +54,7 @@ public override void Abort()
5454
throw new NotImplementedException();
5555
}
5656

57-
public override Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription,
58-
CancellationToken cancellationToken)
57+
public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
5958
{
6059
this.closeStatus = closeStatus;
6160
closeStatusDescription = statusDescription;
@@ -69,15 +68,12 @@ public override Task CloseAsync(WebSocketCloseStatus closeStatus, string statusD
6968
return Task.CompletedTask;
7069
}
7170

72-
public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription,
73-
CancellationToken cancellationToken)
71+
public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
7472
{
7573
throw new NotImplementedException();
7674
}
7775

78-
public override async Task<WebSocketReceiveResult> ReceiveAsync(
79-
ArraySegment<byte> buffer,
80-
CancellationToken cancellationToken)
76+
public override async Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buffer, CancellationToken cancellationToken)
8177
{
8278
if (receiveBuffers.IsEmpty)
8379
{
@@ -88,15 +84,17 @@ public override async Task<WebSocketReceiveResult> ReceiveAsync(
8884
var endOfMessage = true;
8985
var messageType = WebSocketMessageType.Close;
9086

91-
if (receiveBuffers.TryPeek(out MessageData received))
87+
if (receiveBuffers.TryPeek(out MessageData? received))
9288
{
9389
messageType = received.MessageType;
9490
if (received.Buffer.Count <= buffer.Count)
9591
{
96-
receiveBuffers.TryDequeue(out received);
97-
received.Buffer.CopyTo(buffer);
98-
bytesReceived = received.Buffer.Count;
99-
endOfMessage = received.EndOfMessage;
92+
if (receiveBuffers.TryDequeue(out received))
93+
{
94+
received.Buffer.CopyTo(buffer);
95+
bytesReceived = received.Buffer.Count;
96+
endOfMessage = received.EndOfMessage;
97+
}
10098
}
10199
else
102100
{

MockK8s/TestOutputLoggerProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public TestOutputLoggerProvider(ITestOutputHelper testOutput, LogLevel minLogLev
3131
/// </summary>
3232
public void Dispose()
3333
{
34+
// Nothing to dispose
3435
}
3536

3637
/// <summary>

0 commit comments

Comments
 (0)