<commit_before>﻿// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license.

using k8s;
using k8s.Models;

namespace SparkTodo.API.Services;

public class KubernetesEnvironment
{
    public string Namespace { get; set; }
    public string PodName { get; set; }
    public V1Pod PodInfo { get; set; }
    public string DeploymentName { get; set; }
    public V1Deployment DeploymentInfo { get; set; }
    public string ServiceName { get; set; }
    public V1Service ServiceInfo { get; set; }
}

public interface IKubernetesService
{
    Task<KubernetesEnvironment> GetKubernetesEnvironment();
}

public class KubernetesService : IKubernetesService
{
    private static readonly bool IsInK8sClsuter = KubernetesClientConfiguration.IsInCluster();
    private volatile KubernetesEnvironment _environment = null;
    private readonly AsyncLock _lock = new();

    public async Task<KubernetesEnvironment> GetKubernetesEnvironment()
    {
        if (_environment is null)
        {
            using (await _lock.LockAsync())
            {
                if (_environment is null)
                {
                    var host = Environment.MachineName;
                    if (IsInK8sClsuter)
                    {
                        var config = KubernetesClientConfiguration.InClusterConfig();
                        _environment = new KubernetesEnvironment()
                        {
                            Namespace = config.Namespace,
                            PodName = host,
                        };

                        var client = new Kubernetes(config);
                        var podListResult = await client.ListNamespacedPodAsync(config.Namespace);
                        var podInfo = podListResult.Items.First(x => x.Metadata.Name == host);
                        _environment.PodInfo = podInfo;

                        // deployment
                        var deploymentListResult = await client.ListNamespacedDeploymentAsync(config.Namespace);
                        foreach (var item in deploymentListResult.Items)
                        {
                            if (item.Spec.Selector.MatchLabels.Count > 0)
                            {
                                var labelMatch = item.Spec.Selector.MatchLabels
                                    .All(p => podInfo.GetLabel(p.Key) == p.Value);
                                if (labelMatch)
                                {
                                    _environment.DeploymentName = item.Name();
                                    _environment.DeploymentInfo = item;
                                    break;
                                }
                            }
                            else
                            {
                                // TODO: match pod according to Selector.MatchExpressions
                            }
                        }

                        // service
                        var serviceListResult = await client.ListNamespacedServiceAsync(config.Namespace);
                        foreach (var item in serviceListResult.Items)
                        {
                            if (item.Spec.Selector.Count > 0)
                            {
                                var labelMatch = item.Spec.Selector
                                    .All(p => podInfo.GetLabel(p.Key) == p.Value);
                                if (labelMatch)
                                {
                                    _environment.ServiceName = item.Name();
                                    _environment.ServiceInfo = item;
                                    break;
                                }
                            }
                            else
                            {
                                // TODO: match pod according to Selector.MatchExpressions
                            }
                        }
                    }
                    else
                    {
                        _environment = new KubernetesEnvironment() { PodName = host };
                    }
                }
            }
        }
        return _environment;
    }<commit_msg>Update KubernetesService<commit_after>﻿// Copyright (c) <NAME>. All rights reserved.
// Licensed under the MIT license.

using k8s;
using k8s.Models;

namespace SparkTodo.API.Services;

public class KubernetesEnvironment
{
    public string Namespace { get; set; }
    public string PodName { get; set; }
    public V1Pod PodInfo { get; set; }
    public string DeploymentName { get; set; }
    public V1Deployment DeploymentInfo { get; set; }
    public string ServiceName { get; set; }
    public V1Service ServiceInfo { get; set; }
}

public interface IKubernetesService
{
    Task<KubernetesEnvironment> GetKubernetesEnvironment();
}

public class KubernetesService : IKubernetesService
{
    private static readonly bool IsInK8sCluster = KubernetesClientConfiguration.IsInCluster();
    private volatile KubernetesEnvironment _environment = null;
    private readonly AsyncLock _lock = new();

    public async Task<KubernetesEnvironment> GetKubernetesEnvironment()
    {
        if (_environment is null)
        {
            using (await _lock.LockAsync())
            {
                if (_environment is null)
                {
                    var host = Environment.MachineName;
                    if (IsInK8sCluster)
                    {
                        var config = KubernetesClientConfiguration.InClusterConfig();
                        _environment = new KubernetesEnvironment()
                        {
                            Namespace = config.Namespace,
                            PodName = host,
                        };

                        var client = new Kubernetes(config);
                        var podListResult = await client.ListNamespacedPodAsync(config.Namespace);
                        foreach(var item in podListResult.Items)
                        {
                            System.Console.WriteLine("Pod: {item.Name()}");
                        }
                        var podInfo = podListResult.Items.First(x => x.Name() == host);
                        _environment.PodInfo = podInfo;

                        // deployment
                        var deploymentListResult = await client.ListNamespacedDeploymentAsync(config.Namespace);
                        foreach (var item in deploymentListResult.Items)
                        {
                            System.Console.WriteLine("Deployment: {item.Name()}");
                            if (item.Spec.Selector.MatchLabels.Count > 0)
                            {
                                var labelMatch = item.Spec.Selector.MatchLabels
                                    .All(p => podInfo.GetLabel(p.Key) == p.Value);
                                if (labelMatch)
                                {
                                    _environment.DeploymentName = item.Name();
                                    _environment.DeploymentInfo = item;
                                    break;
                                }
                            }
                            else
                            {
                                // TODO: match pod according to Selector.MatchExpressions
                            }
                        }

                        // service
                        var serviceListResult = await client.ListNamespacedServiceAsync(config.Namespace);
                        foreach (var item in serviceListResult.Items)
                        {
                            System.Console.WriteLine("Service: {item.Name()}");
                            var labelMatch = item.Spec.Selector
                                .All(p => podInfo.GetLabel(p.Key) == p.Value);
                            if (labelMatch)
                            {
                                _environment.ServiceName = item.Name();
                                _environment.ServiceInfo = item;
                                break;
                            }
                        }
                    }
                    else
                    {
                        _environment = new KubernetesEnvironment() { PodName = host };
                    }
                }
            }
        }
        return _environment;
    }
{'fraction_non_alphanumeric': 0.06665088196993016, 'fraction_numerical': 0.0020125488339055287, 'mean_word_length': 1.0215362526920315, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7578403', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1687, 'n_words': 401}