private async Task ProcessStateAsync(Domain.Entities.Cluster cluster, k8s.Models.V1NodeList clusterNodes) { try { foreach (var clusterNode in clusterNodes.Items) { if (clusterNode.Metadata.Name.Contains("master")) { cluster.State = clusterNode.Status.Conditions.FirstOrDefault(c => c.Reason == "KubeletReady")?.Type; var _ = await clusterRepository.UpdateClusterAsync(cluster); } else { var node = cluster.Nodes.FirstOrDefault(n => n.Name == clusterNode.Metadata.Name); if (node != null) { node.State = clusterNode.Status.Conditions.FirstOrDefault(c => c.Reason == "KubeletReady")?.Type; var __ = await clusterNodeRepository.UpdateClusterNodeAsync(node); } } } } catch (Exception e) { logger.LogError(e, "Error on processing state"); } }
private async Task ProcessMetricsAsync(Domain.Entities.Cluster cluster, Kubernetes client) { try { var metrics = await client.GetKubernetesNodesMetricsAsync(); var metricsGathered = new List <Metric>(); foreach (var metric in metrics.Items) { var node = cluster.Nodes.FirstOrDefault(n => n.Name == metric.Metadata.Name); var extractItemId = new Guid(); if (node != null) { extractItemId = node.Id; } else if (metric.Metadata.Name.Contains("master")) { extractItemId = cluster.Id; } if (extractItemId != Guid.Empty) { metricsGathered.Add(new Metric() { EntityId = extractItemId, CpuValue = long.Parse(metric.Usage["cpu"].CanonicalizeString().Replace("n", "")), MemoryValue = long.Parse(metric.Usage["memory"].CanonicalizeString().Replace("Ki", "")) }); } } await metricRepository.InsertMetricsWithStrategyAsync(metricsGathered.ToArray()); } catch (Exception e) { logger.LogError(e, "Error on processing state"); } }
private async Task MonitorClusterAsync(Domain.Entities.Cluster cluster) { try { var filePath = $"./configs/{cluster.Id}/kubeconfig"; if (!File.Exists(filePath)) { await File.WriteAllTextAsync(filePath, cluster.KubeConfig); } var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(filePath); var client = new Kubernetes(config); var clusterNodes = await client.ListNodeAsync(); await ProcessStateAsync(cluster, clusterNodes); await ProcessMetricsAsync(cluster, client); } catch (Exception e) { logger.LogError(e, "Fail to connect client."); } }