Esempio n. 1
0
        public void CtorShouldSetSettingsProperty()
        {
            var settingsMock = new Mock <IKubeHttpClientSettingsProvider>();

            settingsMock.Setup(p => p.CreateMessageHandler()).Returns(new HttpClientHandler());
            KubeHttpClient client = new KubeHttpClient(settingsMock.Object, GetLogger <KubeHttpClient>());

            Assert.NotNull(client.Settings);
            Assert.Equal(settingsMock.Object, client.Settings);
        }
        public void CtorSetsKubeHttpClient()
        {
            var settingsMock = new Mock <IKubeHttpClientSettingsProvider>();

            settingsMock.Setup(p => p.CreateMessageHandler()).Returns(new HttpClientHandler());
            using (KubeHttpClient httpClient = new KubeHttpClient(settingsMock.Object))
                using (K8sQueryClient target = new K8sQueryClient(httpClient))
                {
                    Assert.Same(httpClient, target.KubeHttpClient);
                }
        }
Esempio n. 3
0
        public void CreateMessageHandlerShouldNotReturnNull()
        {
            var settingsMock = new Mock <IKubeHttpClientSettingsProvider>();

            settingsMock.Setup(p => p.CreateMessageHandler()).Returns(() => null);

            Exception ex = Assert.Throws <ArgumentNullException>(() =>
            {
                KubeHttpClient client = new KubeHttpClient(settingsMock.Object, GetLogger <KubeHttpClient>());
            });

            Assert.Equal("Value cannot be null.\r\nParameter name: handler", ex.Message);
        }
Esempio n. 4
0
        public void CtorShouldSetTheBaseAddress()
        {
            var settingsMock = new Mock <IKubeHttpClientSettingsProvider>();

            settingsMock.Setup(p => p.CreateMessageHandler()).Returns(new HttpClientHandler());
            Uri targetBaseUri = new Uri("https://k8stest/");

            settingsMock.Setup(p => p.ServiceBaseAddress).Returns(targetBaseUri);

            KubeHttpClient client = new KubeHttpClient(settingsMock.Object, GetLogger <KubeHttpClient>());

            Assert.NotNull(client.BaseAddress);
            Assert.Equal(targetBaseUri, client.BaseAddress);
        }
Esempio n. 5
0
        /// <summary>
        /// Async factory method to build the instance of this class.
        /// </summary>
        /// <returns></returns>
        public static async Task <K8sEnvironment> CreateAsync(TimeSpan timeout, ILoggerFactory loggerFactory)
        {
            K8sEnvironment           instance = null;
            ILogger <K8sEnvironment> logger   = null;

            try
            {
                logger = loggerFactory?.CreateLogger <K8sEnvironment>();

                KubeHttpClientSettingsProvider settings = new KubeHttpClientSettingsProvider(loggerFactory);
                using (KubeHttpClient httpClient = new KubeHttpClient(settings))
                    using (K8sQueryClient queryClient = new K8sQueryClient(httpClient))
                    {
                        if (await SpinWaitContainerReady(timeout, queryClient, settings.ContainerId, logger).ConfigureAwait(false))
                        {
                            instance = new K8sEnvironment()
                            {
                                ContainerID = settings.ContainerId
                            };
                            instance.logger = logger;

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

                            instance.myPod = myPod;
                            logger?.LogDebug(Invariant($"Getting container status of container-id: {settings.ContainerId}"));
                            instance.myContainerStatus = myPod.GetContainerStatus(settings.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);
            }
            finally
            {
                // Signal that initialization is done.
                instance?.InitializationWaiter.Set();
            }
        }