/// <summary>
        /// Async factory method to build the instance of a K8sEnvironment.
        /// </summary>
        /// <returns></returns>
        public async Task <K8sEnvironment> CreateAsync(TimeSpan timeout)
        {
            K8sEnvironment           instance = null;
            ILogger <K8sEnvironment> logger   = null;

            try
            {
                using (IKubeHttpClient httpClient = _httpClientFactory.Create(_httpClientSettings))
                    using (K8sQueryClient queryClient = _k8sQueryClientFactory.Create(httpClient))
                    {
                        string containerId = _httpClientSettings.ContainerId;
                        if (await SpinWaitContainerReady(timeout, queryClient, containerId).ConfigureAwait(false))
                        {
                            instance = new K8sEnvironment()
                            {
                                ContainerID = containerId
                            };

                            K8sPod myPod = await queryClient.GetMyPodAsync().ConfigureAwait(false);

                            instance.myPod = myPod;
                            logger?.LogDebug(Invariant($"Getting container status of container-id: {containerId}"));
                            instance.myContainerStatus = myPod.GetContainerStatus(containerId);

                            IEnumerable <K8sReplicaSet> replicaSetList = await queryClient.GetReplicasAsync().ConfigureAwait(false);

                            instance.myReplicaSet = myPod.GetMyReplicaSet(replicaSetList);

                            if (instance.myReplicaSet != null)
                            {
                                IEnumerable <K8sDeployment> deploymentList = await queryClient.GetDeploymentsAsync().ConfigureAwait(false);

                                instance.myDeployment = instance.myReplicaSet.GetMyDeployment(deploymentList);
                            }

                            if (instance.myPod != null)
                            {
                                IEnumerable <K8sNode> nodeList = await queryClient.GetNodesAsync().ConfigureAwait(false);

                                string nodeName = instance.myPod.Spec.NodeName;
                                if (!string.IsNullOrEmpty(nodeName))
                                {
                                    instance.myNode = nodeList.FirstOrDefault(node => !string.IsNullOrEmpty(node.Metadata?.Name) && node.Metadata.Name.Equals(nodeName, StringComparison.OrdinalIgnoreCase));
                                }
                            }
                        }
                        else
                        {
                            logger?.LogError(Invariant($"Kubernetes info is not available within given time of {timeout.TotalMilliseconds} ms."));
                        }
                    }
                return(instance);
            }
            catch (Exception ex)
            {
                logger?.LogCritical(ex.ToString());
                return(null);
            }
        }
コード例 #2
0
        /// <summary>
        /// Async factory method to build the instance of a K8sEnvironment.
        /// </summary>
        /// <returns></returns>
        public async Task <IK8sEnvironment> CreateAsync(DateTime timeoutAt)
        {
            K8sEnvironment instance = null;

            try
            {
                using (IKubeHttpClient httpClient = _httpClientFactory.Create(_httpClientSettings))
                    using (K8sQueryClient queryClient = _k8sQueryClientFactory.Create(httpClient))
                    {
                        // TODO: See if there's better way to fetch the container id
                        K8sPod myPod = await SpinWaitUntilGetPod(timeoutAt, queryClient).ConfigureAwait(false);

                        if (myPod != null)
                        {
                            string containerId = null;
                            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                            {
                                // For Windows, is there a way to fetch the current container id from within the container?
                                containerId = myPod.Status.ContainerStatuses.First().ContainerID;
                            }
                            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                            {
                                // For Linux, container id could be fetched directly from cGroup.
                                containerId = _httpClientSettings.ContainerId;
                            }
                            // ~

                            if (await SpinWaitContainerReady(timeoutAt, queryClient, containerId).ConfigureAwait(false))
                            {
                                instance = new K8sEnvironment()
                                {
                                    ContainerID = containerId
                                };

                                instance.myPod = myPod;
                                _logger.LogDebug(Invariant($"Getting container status of container-id: {containerId}"));
                                instance.myContainerStatus = myPod.GetContainerStatus(containerId);

                                IEnumerable <K8sReplicaSet> replicaSetList = await queryClient.GetReplicasAsync().ConfigureAwait(false);

                                instance.myReplicaSet = myPod.GetMyReplicaSet(replicaSetList);

                                if (instance.myReplicaSet != null)
                                {
                                    IEnumerable <K8sDeployment> deploymentList = await queryClient.GetDeploymentsAsync().ConfigureAwait(false);

                                    instance.myDeployment = instance.myReplicaSet.GetMyDeployment(deploymentList);
                                }

                                if (instance.myPod != null)
                                {
                                    IEnumerable <K8sNode> nodeList = await queryClient.GetNodesAsync().ConfigureAwait(false);

                                    string nodeName = instance.myPod.Spec.NodeName;
                                    if (!string.IsNullOrEmpty(nodeName))
                                    {
                                        instance.myNode = nodeList.FirstOrDefault(node => !string.IsNullOrEmpty(node.Metadata?.Name) && node.Metadata.Name.Equals(nodeName, StringComparison.OrdinalIgnoreCase));
                                    }
                                }
                            }
                            else
                            {
                                _logger.LogError(Invariant($"Kubernetes info is not available before the timeout at {timeoutAt}."));
                            }
                        }
                        else
                        {
                            // MyPod is null, meaning query timed out.
                            _logger.LogCritical("Fail to fetch the pod information in time. Kubernetes info will not be available for the telemetry.");
                            return(null);
                        }
                    }
                return(instance);
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex.ToString());
                return(null);
            }
        }