Ejemplo n.º 1
0
        public static async Task PushNotification(
            WatchEventType eventType,
            HealthCheckResource resource,
            V1Service uiService,
            V1Service notificationService,
            V1Secret endpointSecret,
            ILogger <K8sOperator> logger,
            IHttpClientFactory httpClientFactory)
        {
            var address   = KubernetesAddressFactory.CreateHealthAddress(notificationService, resource);
            var uiAddress = KubernetesAddressFactory.CreateAddress(uiService, resource);

            dynamic healthCheck = new
            {
                Type = eventType,
                notificationService.Metadata.Name,
                Uri = address
            };

            var client = httpClientFactory.CreateClient();

            try
            {
                string type = healthCheck.Type.ToString();
                string name = healthCheck.Name;
                string uri  = healthCheck.Uri;

                logger.LogInformation("[PushService] Namespace {Namespace} - Sending Type: {type} - Service {name} with uri : {uri} to ui endpoint: {address}", resource.Metadata.NamespaceProperty, type, name, uri, uiAddress);

                var key = Encoding.UTF8.GetString(endpointSecret.Data["key"]);

                var response = await client.PostAsync($"{uiAddress}{Constants.PushServicePath}?{Constants.PushServiceAuthKey}={key}",

                                                      new StringContent(JsonSerializer.Serialize(healthCheck, new JsonSerializerOptions
                {
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                }), Encoding.UTF8, "application/json"));


                logger.LogInformation("[PushService] Notification result for {name} - status code: {statuscode}", notificationService.Metadata.Name, response.StatusCode);
            }
            catch (Exception ex)
            {
                logger.LogError("Error notifying healthcheck service: {message}", ex.Message);
            }
        }
        public void parse_properly_the_k8s_api_discovered_services_for_a_remote_cluster()
        {
            var healthPath  = "healthz";
            var apiResponse = File.ReadAllText("UI/Kubernetes/SampleData/remote-cluster-discovery-sample.json");

            var services = JsonConvert.DeserializeObject <ServiceList>(apiResponse);

            var addressFactory = new KubernetesAddressFactory(healthPath);

            List <string> serviceAddresses = new List <string>();

            foreach (var item in services.Items)
            {
                serviceAddresses.Add(addressFactory.CreateAddress(item));
            }

            serviceAddresses[0].Should().Be("http://13.73.139.23:80/healthz");
            serviceAddresses[1].Should().Be("http://13.80.181.10:51000/healthz");
            serviceAddresses[2].Should().Be("http://12.0.0.190:5672/healthz");
            serviceAddresses[3].Should().Be("http://12.0.0.168:30478/healthz");
        }
        public void parse_properly_the_k8s_api_discovered_services_for_a_local_cluster()
        {
            var healthPath  = "healthz";
            var apiResponse = File.ReadAllText("UI/Kubernetes/SampleData/local-cluster-discovery-sample.json");

            var services = JsonConvert.DeserializeObject <ServiceList>(apiResponse);

            var addressFactory = new KubernetesAddressFactory(healthPath);

            List <string> serviceAddresses = new List <string>();

            foreach (var item in services.Items)
            {
                serviceAddresses.Add(addressFactory.CreateAddress(item));
            }

            serviceAddresses[0].Should().Be("http://localhost:10000/healthz");
            serviceAddresses[1].Should().Be("http://localhost:9000/healthz");
            serviceAddresses[2].Should().Be("http://localhost:30000/healthz");
            serviceAddresses[3].Should().Be("http://10.97.1.153:80/healthz");
        }
        public void parse_properly_the_k8s_api_discovered_services_for_a_remote_cluster()
        {
            var healthPath  = "healthz";
            var apiResponse = File.ReadAllText("UI/Kubernetes/SampleData/remote-cluster-discovery-sample.json");

            var services = JsonConvert.DeserializeObject <V1ServiceList>(apiResponse);

            var addressFactory = new KubernetesAddressFactory(new KubernetesDiscoverySettings
            {
                HealthPath               = healthPath,
                ServicesPathAnnotation   = Keys.HEALTHCHECKS_DEFAULT_DISCOVERY_PATH_ANNOTATION,
                ServicesPortAnnotation   = Keys.HEALTHCHECKS_DEFAULT_DISCOVERY_PORT_ANNOTATION,
                ServicesSchemeAnnotation = Keys.HEALTHCHECKS_DEFAULT_DISCOVERY_SCHEME_ANNOTATION
            });

            IReadOnlyList <string> serviceAddresses = services.Items.Select(service => addressFactory.CreateAddress(service)).ToList();

            serviceAddresses[0].Should().Be("http://13.73.139.23:80/healthz");
            serviceAddresses[1].Should().Be("http://13.80.181.10:51000/healthz");
            serviceAddresses[2].Should().Be("http://12.0.0.190:5672/healthz");
            serviceAddresses[3].Should().Be("http://12.0.0.168:30478/healthz");
            serviceAddresses[4].Should().Be("https://10.152.183.35:8080/custom/health/path");
        }
        public void parse_properly_the_k8s_api_discovered_services_for_a_local_cluster()
        {
            var healthPath  = "healthz";
            var apiResponse = File.ReadAllText("UI/Kubernetes/SampleData/local-cluster-discovery-sample.json");

            var services = JsonConvert.DeserializeObject <V1ServiceList>(apiResponse);


            var addressFactory = new KubernetesAddressFactory(new KubernetesDiscoverySettings
            {
                HealthPath               = healthPath,
                ServicesPathAnnotation   = Keys.HEALTHCHECKS_DEFAULT_DISCOVERY_PATH_ANNOTATION,
                ServicesPortAnnotation   = Keys.HEALTHCHECKS_DEFAULT_DISCOVERY_PORT_ANNOTATION,
                ServicesSchemeAnnotation = Keys.HEALTHCHECKS_DEFAULT_DISCOVERY_SCHEME_ANNOTATION
            });

            IReadOnlyList <string> serviceAddresses = services.Items.Select(service => addressFactory.CreateAddress(service)).ToList();

            serviceAddresses[0].Should().Be("http://localhost:10000/healthz");
            serviceAddresses[1].Should().Be("http://localhost:9000/healthz");
            serviceAddresses[2].Should().Be("http://localhost:30000/healthz");
            serviceAddresses[3].Should().Be("http://10.97.1.153:80/healthz");
            serviceAddresses[4].Should().Be("http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:7070/custom/health/path");
            serviceAddresses[5].Should().Be("https://some.external-site.com:443/custom/health/path");
        }