Exemplo n.º 1
0
        static IServiceCollection AddKubernetesDiscoveryService(this IServiceCollection services, KubernetesDiscoverySettings kubernetesDiscoverySettings)
        {
            KubernetesClientConfiguration kubernetesConfig;

            if (!string.IsNullOrEmpty(kubernetesDiscoverySettings.ClusterHost) && !string.IsNullOrEmpty(kubernetesDiscoverySettings.Token))
            {
                kubernetesConfig = new KubernetesClientConfiguration {
                    Host        = kubernetesDiscoverySettings.ClusterHost,
                    AccessToken = kubernetesDiscoverySettings.Token,
                    // Some cloud services like Azure AKS use self-signed certificates not valid for httpclient.
                    // With this method we allow invalid certificates
                    SkipTlsVerify = true
                };
            }
            else if (KubernetesClientConfiguration.IsInCluster())
            {
                kubernetesConfig = KubernetesClientConfiguration.InClusterConfig();
            }
            else
            {
                kubernetesConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
            }

            services.AddSingleton(kubernetesDiscoverySettings)
            .AddHostedService <KubernetesDiscoveryHostedService>()
            .AddSingleton <IKubernetes>(new Kubernetes(kubernetesConfig))
            .AddHttpClient(Keys.K8S_CLUSTER_SERVICE_HTTP_CLIENT_NAME);

            return(services);
        }
Exemplo n.º 2
0
        public void ConfigureServices(IServiceCollection services)
        {
            KubernetesClientConfiguration config;

            if (KubernetesClientConfiguration.IsInCluster())
            {
                config = KubernetesClientConfiguration.InClusterConfig();
                services.AddSingleton <ILeaderSelector, KubernetesLeaderSelector>();
            }
            else
            {
                config = new KubernetesClientConfiguration {
                    Host = "http://localhost:8001"
                };
                services.AddSingleton <ILeaderSelector, DummyLeaderSelector>();
            }

            services.AddHttpClient("K8s")
            .AddTypedClient <IKubernetes>((httpClient, serviceProvider) => new Kubernetes(config, httpClient))
            .ConfigurePrimaryHttpMessageHandler(config.CreateDefaultHttpClientHandler)
            .AddHttpMessageHandler(KubernetesClientConfiguration.CreateWatchHandler);

            services.AddSingleton <ResourceSet <Foo> >();
            services.AddHostedService <CrdControllerService>();
        }
Exemplo n.º 3
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);


            services.AddSingleton(s =>
            {
                var logger    = s.GetRequiredService <ILogger <Startup> >();
                var inCluster = KubernetesClientConfiguration.IsInCluster();
                logger.LogDebug("Building client configuration. Client is in cluster: '{inCluster}'", inCluster);

                return(inCluster
                    ? KubernetesClientConfiguration.InClusterConfig()
                    : KubernetesClientConfiguration.BuildConfigFromConfigFile());
            });

            services.AddSingleton <IKubernetes>(s =>
            {
                var logger = s.GetRequiredService <ILogger <Startup> >();
                logger.LogDebug("Initializing Kubernetes client");
                return(new k8s.Kubernetes(s.GetRequiredService <KubernetesClientConfiguration>())
                {
                    HttpClient = { Timeout = TimeSpan.FromMinutes(60) }
                });
            });

            services.AddTransient <SecretsMonitor>();
            services.AddTransient <ConfigMapMonitor>();
            services.AddTransient <CustomResourceDefinitionMonitor>();
            services.AddTransient <CertManagerCertificatesMonitor>();

            services.AddHostedService <SecretsMirror>();
            services.AddHostedService <ConfigMapMirror>();
            services.AddHostedService <CertManagerMirror>();
        }
        public void ConfigureServices(IServiceCollection services)
        {
            KubernetesClientConfiguration config;

            if (KubernetesClientConfiguration.IsInCluster())
            {
                config = KubernetesClientConfiguration.InClusterConfig();
                services.AddSingleton <ILeaderSelector, KubernetesLeaderSelector>();
            }
            else
            {
                config = new KubernetesClientConfiguration {
                    Host = "http://localhost:8001"
                };
                services.AddSingleton <ILeaderSelector, DummyLeaderSelector>();
            }

            services.AddHttpClient("K8s")
            .AddTypedClient <IKubernetes>((httpClient, serviceProvider) => new Kubernetes(config, httpClient))
            .ConfigurePrimaryHttpMessageHandler(config.CreateDefaultHttpClientHandler)
            .AddHttpMessageHandler(KubernetesClientConfiguration.CreateWatchHandler);

            services.AddSingleton <ConcurrentDictionary <string, AzureDnsCredentialSpec> >();
            services.AddSingleton <IDnsClientFactory, AzureDnsClientFactory>();

            services.AddHostedService <AzureDnsCredentialsController>();
            services.AddHostedService <DnsRecordsetController>();
        }
Exemplo n.º 5
0
        static async Task Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .AddJsonFile("config/appsettings.json", optional: true)
                         .Build();

            var kubeConfig = KubernetesClientConfiguration.IsInCluster() ?
                             KubernetesClientConfiguration.InClusterConfig() :
                             new KubernetesClientConfiguration {
                Host = "http://localhost:8001"
            };

            var kubernetes = new Kubernetes(kubeConfig);

            var serviceProvider = new ServiceCollection()
                                  .AddSingleton(config)
                                  .AddSingleton(kubernetes)
                                  .AddTransient <IInstaller, WebPingerInstaller>()
                                  .AddTransient <IInstaller, WebPingerArchiveInstaller>()
                                  .BuildServiceProvider();

            var installers = serviceProvider.GetServices <IInstaller>();

            foreach (var installer in installers)
            {
                await installer.InstallAsync();
            }

            Console.WriteLine("* Done.");
        }
        public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureServices((hostContext, services) =>
        {
            services.AddHostedService <HealthChecksOperator>()
            .AddSingleton <IKubernetes>(sp =>
            {
                var config = KubernetesClientConfiguration.IsInCluster() ?
                             KubernetesClientConfiguration.InClusterConfig() :
                             KubernetesClientConfiguration.BuildConfigFromConfigFile();

                return(new Kubernetes(config));
            })
            .AddTransient <IHealthChecksController, HealthChecksController>()
            .AddSingleton <OperatorDiagnostics>()
            .AddSingleton <DeploymentHandler>()
            .AddSingleton <ServiceHandler>()
            .AddSingleton <SecretHandler>()
            .AddSingleton <ConfigMaphandler>()
            .AddSingleton <HealthCheckServiceWatcher>();
        }).ConfigureLogging((context, builder) =>
        {
            var logger = new LoggerConfiguration()
                         .MinimumLevel.Information()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                         .Enrich.WithProperty("Application", nameof(K8sOperator))
                         .Enrich.FromLogContext()
                         .WriteTo.ColoredConsole(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception:lj}")
                         .CreateLogger();

            builder.ClearProviders();
            builder.AddSerilog(logger, dispose: true);
        });
        private IKubernetes InitializeKubernetesClient()
        {
            KubernetesClientConfiguration kubernetesConfig;

            if (!string.IsNullOrEmpty(_discoveryOptions.ClusterHost) && !string.IsNullOrEmpty(_discoveryOptions.Token))
            {
                kubernetesConfig = new KubernetesClientConfiguration
                {
                    Host        = _discoveryOptions.ClusterHost,
                    AccessToken = _discoveryOptions.Token,
                    // Some cloud services like Azure AKS use self-signed certificates not valid for httpclient.
                    // With this method we allow invalid certificates
                    SkipTlsVerify = true
                };
            }
            else if (KubernetesClientConfiguration.IsInCluster())
            {
                kubernetesConfig = KubernetesClientConfiguration.InClusterConfig();
            }
            else
            {
                kubernetesConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
            }

            return(new Kubernetes(kubernetesConfig));
        }
Exemplo n.º 8
0
        public static IClientBuilder UseKubeGatewayListProvider(this IClientBuilder builder,
                                                                Action <KubeGatewayOptions> configureOptions = null)
        {
            return(builder.ConfigureServices((ctx, services) =>
            {
                services.AddOptions <KubeGatewayOptions>();
                services.Configure <KubeGatewayOptions>(configureOptions);

                KubernetesClientConfiguration config = default;

                if (KubernetesClientConfiguration.IsInCluster())
                {
                    config = KubernetesClientConfiguration.InClusterConfig();
                }
                else
                {
                    config = KubernetesClientConfiguration.BuildDefaultConfig();
                }

                services.AddSingleton <IGatewayListProvider>(sp => new KubeGatewayListProvider(
                                                                 sp.GetRequiredService <ILoggerFactory>(),
                                                                 sp.GetRequiredService <IOptions <ClusterOptions> >(),
                                                                 sp.GetRequiredService <IOptions <GatewayOptions> >(),
                                                                 sp.GetRequiredService <IOptions <KubeGatewayOptions> >(),
                                                                 new k8s.Kubernetes(config)
                                                                 ));
            }));
        }
Exemplo n.º 9
0
        static async Task Main(string[] args)
        {
            KubernetesClientConfiguration config;

            if (KubernetesClientConfiguration.IsInCluster())
            {
                config = KubernetesClientConfiguration.InClusterConfig();
            }
            else
            {
                config = new KubernetesClientConfiguration {
                    Host = "http://localhost:8001"
                };
            }

            _Client = new Kubernetes(config);

            var result = await _Client.ListNamespacedCustomObjectWithHttpMessagesAsync(
                group : User.Definition.Group,
                version : User.Definition.Version,
                plural : User.Definition.Plural,
                namespaceParameter : "default",
                watch : true);

            using (result.Watch <User, object>((type, item) => Handle(type, item)))
            {
                Console.WriteLine("* Watching for custom object events");
                _ResetEvent.WaitOne();
            }
        }
        private static IKubernetes GetKubernetesClient()
        {
            var config = KubernetesClientConfiguration.IsInCluster() ?
                         KubernetesClientConfiguration.InClusterConfig() :
                         KubernetesClientConfiguration.BuildConfigFromConfigFile();

            return(new Kubernetes(config));
        }
Exemplo n.º 11
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync(context.Request.Host.ToString());
                    await context.Response.WriteAsync(Environment.NewLine);
                    await context.Response.WriteAsync("Hello from backend!!!");
                });
                endpoints.MapGet("/env", async context =>
                {
                    context.Response.ContentType = "application/json";
                    var configuration            = context.RequestServices.GetRequiredService <IConfiguration>() as IConfigurationRoot;
                    var vars = Environment.GetEnvironmentVariables()
                               .Cast <DictionaryEntry>()
                               .OrderBy(e => (string)e.Key)
                               .ToDictionary(e => (string)e.Key, e => (string)e.Value);
                    var data = new
                    {
                        version           = Environment.Version.ToString(),
                        env               = vars,
                        configuration     = configuration.AsEnumerable().ToDictionary(c => c.Key, c => c.Value),
                        configurtionDebug = configuration.GetDebugView(),
                    };
                    await JsonSerializer.SerializeAsync(context.Response.Body, data);
                });

                endpoints.MapGet("/replicas", async context =>
                {
                    context.Response.ContentType = "application/json";

                    if (!KubernetesClientConfiguration.IsInCluster())
                    {
                        await JsonSerializer.SerializeAsync(context.Response.Body, new { message = "Not running in k8s" });
                        return;
                    }

                    var config        = KubernetesClientConfiguration.InClusterConfig();
                    var klient        = new Kubernetes(config);
                    var endpointsList = await klient.ListNamespacedEndpointsAsync("default");

                    await JsonSerializer.SerializeAsync(context.Response.Body, endpointsList.Items);
                });
            });
        }
Exemplo n.º 12
0
        private static void Main(string[] args)
        {
            var appConfig = new ConfigurationBuilder()
                            .AddEnvironmentVariables()
                            .Build();

            var loadBalancerTarget = appConfig["LOAD_BALANCER_TARGET"];

            var channel = new Channel(loadBalancerTarget, ChannelCredentials.Insecure);

            _client = new ConfigRemoteClient(new ConfigRemote.ConfigRemoteClient(channel));

            var config = KubernetesClientConfiguration.IsInCluster()
                ? KubernetesClientConfiguration.InClusterConfig()
                : KubernetesClientConfiguration.BuildDefaultConfig();

            _k8sClient = new Kubernetes(config);

            Console.WriteLine($"Current context {config.CurrentContext}");
            Console.WriteLine($"Connecting to {_k8sClient.BaseUri}");

            var listNodeAsync = _k8sClient.ListNodeAsync();

            var nodesInfo = new NodesInfo(listNodeAsync.Result);

            var ingressLister = _k8sClient.ListIngressForAllNamespacesWithHttpMessagesAsync(watch: true);

            using (ingressLister.Watch <Extensionsv1beta1Ingress, Extensionsv1beta1IngressList>((type, item) =>
            {
                switch (type)
                {
                case WatchEventType.Added:
                    HandleIngressAdded(item, nodesInfo);
                    break;

                default:
                    Console.WriteLine($"Unhandled event for ingress {item.Metadata.Name}");
                    break;
                }
            }))
            {
                Console.WriteLine("Watching for ingress changes...");
                Console.ReadLine();
            }
        }
Exemplo n.º 13
0
        public void IsInCluster()
        {
            Assert.False(KubernetesClientConfiguration.IsInCluster());

            var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
            var certPath  = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);

            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { tokenPath, new MockFileData("foo") },
                { certPath, new MockFileData("bar") },
            });

            using (new FileUtils.InjectedFileSystem(fileSystem))
            {
                Assert.True(KubernetesClientConfiguration.IsInCluster());
            }
        }
        public Controller(T crd, IOperationHandler <T> handler, string k8sNamespace = "")
        {
            KubernetesClientConfiguration config;

            if (KubernetesClientConfiguration.IsInCluster())
            {
                config = KubernetesClientConfiguration.InClusterConfig();
            }
            else
            {
                config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
            }

            Kubernetes     = new Kubernetes(config);
            m_k8sNamespace = k8sNamespace;
            m_crd          = crd;
            m_handler      = handler;
        }
Exemplo n.º 15
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            services.AddTransient <IKubernetes>(ser =>
            {
                KubernetesClientConfiguration config;

                if (!KubernetesClientConfiguration.IsInCluster())
                {
                    config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
                }
                else
                {
                    config = KubernetesClientConfiguration.InClusterConfig();
                }

                return(new Kubernetes(config));
            });
        }
        public void IsInCluster()
        {
            Assert.False(KubernetesClientConfiguration.IsInCluster());

            Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_HOST", "kubernetes");
            Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_PORT", "443");

            var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
            var certPath  = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);

            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { tokenPath, new MockFileData("foo") },
                { certPath, new MockFileData("bar") },
            });

            using (new FileUtils.InjectedFileSystem(fileSystem))
            {
                Assert.True(KubernetesClientConfiguration.IsInCluster());
            }
        }
Exemplo n.º 17
0
        public static ISiloBuilder UseKubeMembership(this ISiloBuilder builder)
        {
            return(builder.ConfigureServices((ctx, services) =>
            {
                KubernetesClientConfiguration config = default;

                if (KubernetesClientConfiguration.IsInCluster())
                {
                    config = KubernetesClientConfiguration.InClusterConfig();
                }
                else
                {
                    config = KubernetesClientConfiguration.BuildDefaultConfig();
                }

                services.AddSingleton <IMembershipTable>(sp => new KubeMembershipTable(
                                                             sp.GetRequiredService <ILoggerFactory>(),
                                                             sp.GetRequiredService <IOptions <ClusterOptions> >(),
                                                             new k8s.Kubernetes(config)
                                                             ));
            }));
        }
Exemplo n.º 18
0
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterGeneric(typeof(ManagedWatcher <,>));
            builder.RegisterGeneric(typeof(ManagedWatcher <, ,>));


            builder.Register(s =>
            {
                var logger    = s.Resolve <ILogger <CoreModule> >();
                var inCluster = KubernetesClientConfiguration.IsInCluster();
                logger.LogDebug("Building client configuration. Client is in cluster: '{inCluster}'", inCluster);

                return(inCluster
                    ? KubernetesClientConfiguration.InClusterConfig()
                    : KubernetesClientConfiguration.BuildConfigFromConfigFile());
            }).As <KubernetesClientConfiguration>().SingleInstance();

            builder.Register(s => new k8s.Kubernetes(s.Resolve <KubernetesClientConfiguration>())
            {
                HttpClient = { Timeout = TimeSpan.FromMinutes(60) }
            }).As <k8s.Kubernetes>().As <IKubernetes>();
        }
Exemplo n.º 19
0
        /// <summary>
        /// Uses a Kubernetes-base <see cref="IWorkflowRuntime"/>
        /// </summary>
        /// <param name="app">The <see cref="ISynapseApplicationBuilder"/> to configure</param>
        /// <returns>The configured <see cref="ISynapseApplicationBuilder"/></returns>
        public static ISynapseApplicationBuilder UseKubernetesRuntimeHost(this ISynapseApplicationBuilder app)
        {
            var runtimeHostOptions = new KubernetesRuntimeOptions();

            app.Configuration.Bind("kubernetes", runtimeHostOptions);
            app.Services.AddSingleton <KubernetesRuntimeHost>();
            app.Services.AddSingleton <IWorkflowRuntime>(provider => provider.GetRequiredService <KubernetesRuntimeHost>());
            app.Services.AddSingleton <IHostedService>(provider => provider.GetRequiredService <KubernetesRuntimeHost>());
            var config = null as KubernetesClientConfiguration;

            if (KubernetesClientConfiguration.IsInCluster())
            {
                config = KubernetesClientConfiguration.InClusterConfig();
            }
            else
            {
                config = KubernetesClientConfiguration.BuildDefaultConfig();
            }
            app.Services.AddKubernetesClient(config);
            app.Services.TryAddSingleton(Options.Create(runtimeHostOptions));
            return(app);
        }
Exemplo n.º 20
0
        static async Task Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .AddJsonFile("config/appsettings.json", optional: true)
                         .Build();

            var kubeConfig = KubernetesClientConfiguration.IsInCluster() ?
                             KubernetesClientConfiguration.InClusterConfig() :
                             new KubernetesClientConfiguration {
                Host = "http://localhost:8001"
            };

            var kubernetes = new Kubernetes(kubeConfig);

            _ServiceProvider = new ServiceCollection()
                               .AddSingleton(config)
                               .AddSingleton(kubernetes)
                               .AddTransient <WebPingerArchiveAddedHandler>()
                               .AddTransient <WebPingerArchiveDeletedHandler>()
                               .BuildServiceProvider();

            // TODO - this hangs and then fails if there are no objects
            var result = await kubernetes.ListNamespacedCustomObjectWithHttpMessagesAsync(
                group : WebPingerArchive.Definition.Group,
                version : WebPingerArchive.Definition.Version,
                plural : WebPingerArchive.Definition.Plural,
                namespaceParameter : "default",
                watch : true);

            using (result.Watch <WebPingerArchive, object>(async(type, item) => await Handle(type, item)))
            {
                Console.WriteLine("* Watching for WebPingerArchive events");

                var ctrlc = new ManualResetEventSlim(false);
                Console.CancelKeyPress += (sender, eventArgs) => ctrlc.Set();
                ctrlc.Wait();
            }
        }
Exemplo n.º 21
0
        public static IConfigurationBuilder AddJsonKubeConfigMap(this IConfigurationBuilder builder, string name, string @namespace, string key, bool onlyInCluster = true)
        {
            var isInCluster = KubernetesClientConfiguration.IsInCluster();

            if (onlyInCluster && !isInCluster)
            {
                return(builder);
            }

            var config = isInCluster ?
                         KubernetesClientConfiguration.InClusterConfig() :
                         KubernetesClientConfiguration.BuildDefaultConfig();

            IKubernetes client = new Kubernetes(config);
            var         json   = client.ReadNamespacedConfigMap(name, @namespace)?.Data[key];

            if (!string.IsNullOrEmpty(json))
            {
                builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json)));
            }

            return(builder);
        }
Exemplo n.º 22
0
        private static IServiceProvider ConfigureServices()
        {
            var configuration = BuildConfiguration();

            var services = new ServiceCollection();

            services.AddSingleton(configuration);

            services.AddHttpClient("k8s");

            services.AddLogging(configure => configure.AddConsole())
            .Configure <LoggerFilterOptions>(configure => configure.MinLevel = LogLevel.Debug);

            services.AddMetrics(builder => {
                new MetricsReporterBuilder().Chain(builder.Report, configuration.GetSection("Metrics:Reporting"));
            });

            services.AddScoped <IKubernetes, Kubernetes>(provider => {
                var factory        = provider.GetRequiredService <ILoggerFactory>();
                var logger         = factory.CreateLogger <HttpClient>();
                var loggingHandler = new LoggingHttpMessageHandler(logger);
                var config         = KubernetesClientConfiguration.IsInCluster() ? KubernetesClientConfiguration.InClusterConfig() : KubernetesClientConfiguration.BuildDefaultConfig();
                return(new Kubernetes(config, loggingHandler));
            });

            services.AddScoped <IKubernetesService, KubernetesService>();
            services.AddScoped <ISqlManagementService, SqlManagementService>();
            services.AddScoped <SecretSourceRehydrator>();
            services.AddScoped <DatabaseServerRehydrator>();
            services.AddOperator <DatabaseResource, DatabaseOperator>();
            services.AddOperator <DatabaseServerResource, DatabaseServerOperator>();
            services.AddOperator <DeploymentScriptResource, DeploymentScriptOperator>();
            services.AddOperator <CredentialsResource, CredentialsOperator>();

            return(services.BuildServiceProvider());
        }
Exemplo n.º 23
0
        /// <summary>
        /// Handles purging of old <b>logstash</b> and <b>metricbeat</b> Elasticsearch indexes.
        /// </summary>
        /// <returns>The tracking <see cref="Task"/>.</returns>
        public async Task LogPurgerAsync(TimeSpan logPurgerInterval, int retentionDays)
        {
            using (var jsonClient = new JsonClient())
            {
                jsonClient.BaseAddress = KubernetesClientConfiguration.IsInCluster()
                    ? this.ServiceMap[NeonServices.Elasticsearch].Endpoints.Default.Uri
                    : new Uri($"http://localhost:{this.ServiceMap[NeonServices.Elasticsearch].Endpoints.Default.Port}");

                var periodicTask =
                    new AsyncPeriodicTask(
                        logPurgerInterval,
                        onTaskAsync:
                        async() =>
                {
                    // We're going to list the indexes and look for [logstash]
                    // and [metricbeat] indexes that encode the index date like:
                    //
                    //      logstash-2018.06.06
                    //      metricbeat-6.1.1-2018.06.06
                    //
                    // The date is simply encodes the day covered by the index.

                    var utcNow           = DateTime.UtcNow;
                    var deleteBeforeDate = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day) - TimeSpan.FromDays(retentionDays);

                    var indexList = await jsonClient.GetAsync <JObject>("_aliases");

                    foreach (var indexProperty in indexList.Properties())
                    {
                        var indexName = indexProperty.Name;

                        // We're only purging [logstash] and [metricbeat] indexes.

                        if (!indexName.StartsWith("logstash-") && !indexName.StartsWith("metricbeat-"))
                        {
                            continue;
                        }


                        // Extract the date from the index name.

                        var pos = indexName.LastIndexOf('-');

                        if (pos == -1)
                        {
                            Log.LogWarn(() => $"LOG-PURGER: Cannot extract date from index named [{indexName}].");
                            continue;
                        }

                        var date      = indexName.Substring(pos + 1);
                        var fields    = date.Split('.');
                        var indexDate = default(DateTime);

                        try
                        {
                            indexDate = new DateTime(int.Parse(fields[0]), int.Parse(fields[1]), int.Parse(fields[2]));
                        }
                        catch
                        {
                            Log.LogWarn(() => $"LOG-PURGER: Cannot extract date from index named [{indexName}].");
                            continue;
                        }

                        if (indexDate < deleteBeforeDate)
                        {
                            Log.LogInfo(() => $"LOG-PURGER: Deleting index [{indexName}].");
                            await jsonClient.DeleteAsync <JObject>(indexName);
                            Log.LogInfo(() => $"LOG-PURGER: [{indexName}] was deleted.");
                        }
                    }

                    Log.LogDebug("LOG-PURGER: Scan finished.");
                    return(await Task.FromResult(false));
                },
                        onExceptionAsync:
                        async e =>
                {
                    Log.LogError("LOG-PURGER", e);
                    return(await Task.FromResult(false));
                },
                        onTerminateAsync:
                        async() =>
                {
                    Log.LogInfo(() => "LOG-PURGER: Terminating");
                    await Task.CompletedTask;
                });

                await periodicTask.Run();
            }
        }
Exemplo n.º 24
0
        static async Task <int> Main(string[] args)
        {
            IOperator basicOperator = null;

            // Setup logging
            using var loggerFactory = SetupLogging(args);
            var logger = loggerFactory.CreateLogger <Program>();

            try
            {
                logger.LogDebug($"Environment variables: {JsonConvert.SerializeObject(Environment.GetEnvironmentVariables())}");

                // Setup termination handlers
                SetupSignalHandlers();

                // Setup the Kubernetes client
                using var client = SetupClient(args);

                // Setup the operator
                var configuration = GetOperatorConfiguration();
                basicOperator = new Operator(configuration, client, loggerFactory);
                basicOperator.AddControllerOfType <MyResourceController>();

                // Start the operator
                return(await basicOperator.StartAsync());
            }
            catch (Exception exception)
            {
                logger.LogError(exception, "Operator error");
                return(1);
            }

            IKubernetes SetupClient(string[] args)
            {
                // Load the Kubernetes configuration
                KubernetesClientConfiguration config = null;

                if (KubernetesClientConfiguration.IsInCluster())
                {
                    logger.LogDebug("Loading cluster configuration");
                    config = KubernetesClientConfiguration.InClusterConfig();
                }
                else
                {
                    logger.LogDebug("Loading local configuration");
                    config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
                }

                if (logger.IsEnabled(LogLevel.Debug))
                {
                    logger.LogDebug($"Client configuration: {JsonConvert.SerializeObject(config)}");
                }

                return(new Kubernetes(config));
            }

            ILoggerFactory SetupLogging(string[] args)
            {
                if (!System.Enum.TryParse <LogLevel>(Environment.GetEnvironmentVariable("LOG_LEVEL"), true, out LogLevel logLevel))
                {
                    logLevel = LogLevel.Debug;
                }

                var loggerFactory = LoggerFactory.Create(builder => builder
                                                         .AddConsole(options => options.Format = ConsoleLoggerFormat.Systemd)
                                                         .SetMinimumLevel(logLevel)
                                                         );

                // Enable Kubernetes client logging if level = DEBUG
                ServiceClientTracing.IsEnabled = logLevel <= LogLevel.Debug;
                ServiceClientTracing.AddTracingInterceptor(new ConsoleTracingInterceptor());

                return(loggerFactory);
            }

            void SetupSignalHandlers()
            {
                // SIGTERM: signal the operator to shut down gracefully
                AppDomain.CurrentDomain.ProcessExit += (s, e) =>
                {
                    logger.LogDebug("Received SIGTERM");
                    basicOperator?.Stop();
                };

                // SIGINT: try to shut down gracefully on the first attempt
                Console.CancelKeyPress += (s, e) =>
                {
                    bool isFirstSignal = !basicOperator.IsDisposing;
                    logger.LogDebug($"Received SIGINT, first signal: {isFirstSignal}");
                    if (isFirstSignal)
                    {
                        e.Cancel = true;
                        Environment.Exit(0);
                    }
                };
            }
        }
Exemplo n.º 25
0
 public static IKubeClient Create(KubernetesVersion kubernetesVersion)
 {
     return(KubernetesClientConfiguration.IsInCluster() ? ForInCluster(kubernetesVersion) : ForLocalProxy(kubernetesVersion));
 }
Exemplo n.º 26
0
        /// <summary>
        /// Handles purging of old <b>logstash</b> and <b>metricbeat</b> Elasticsearch indexes.
        /// </summary>
        /// <returns>The tracking <see cref="Task"/>.</returns>
        public void KibanaSetup()
        {
            Log.LogInfo("Setting up Kibana index patterns.");
            using (var jsonClient = new JsonClient())
            {
                jsonClient.BaseAddress = KubernetesClientConfiguration.IsInCluster() ?
                                         this.ServiceMap[NeonServices.Kibana].Endpoints.Default.Uri : new Uri($"http://*****:*****@timestamp";

                    indexPattern.attributes = attributes;

                    await jsonClient.PostAsync($"api/saved_objects/index-pattern/logstash-*?overwrite=true", indexPattern);
                }).Wait();

                // Now we need to save a Kibana config document so that [logstash-*] will be
                // the default index and the timestamp will be displayed as UTC and have a
                // more useful terse format.

                retry.InvokeAsync(
                    async() =>
                {
                    dynamic setting = new ExpandoObject();

                    setting.value = "logstash-*";
                    await jsonClient.PostAsync($"api/kibana/settings/defaultIndex", setting);

                    setting.value = "HH:mm:ss.SSS MM-DD-YYYY";
                    await jsonClient.PostAsync($"api/kibana/settings/dateFormat", setting);

                    setting.value = "UTC";
                    await jsonClient.PostAsync($"api/kibana/settings/dateFormat:tz", setting);
                }).Wait();
            }
            Log.LogInfo("Kibana index patterns configured.");
        }
Exemplo n.º 27
0
        static async Task Main(string[] args)
        {
            var cubePods = new List <string>();

            if (KubernetesClientConfiguration.IsInCluster())
            {
                var config = KubernetesClientConfiguration.BuildDefaultConfig();
                var client = new Kubernetes(config);
                pgqList = client.ListNamespacedPod("pgq").Items
                          .Where(i => i.Metadata.Name.StartsWith("pgq-"))
                          .OrderBy(i => i.Metadata.Name)
                          .Select(i => new IPEndPoint(IPAddress.Parse(i.Status.PodIP), 88))
                          .ToArray();
            }

            var pgqs = Environment.GetEnvironmentVariable("H2R_PGQ_LIST");

            if (!string.IsNullOrWhiteSpace(pgqs))
            {
                pgqList = pgqs
                          .Split(',', ';', ' ', '\r', '\n', '\t')
                          .Where(c => !string.IsNullOrWhiteSpace(c))
                          .Select(c => {
                    var parts = c.Split(':');
                    var host  = parts[0];
                    Console.WriteLine(host);
                    return(new IPEndPoint(Dns.GetHostAddresses(host)[0], int.Parse(parts[1])));
                })
                          .ToArray();
            }

            foreach (var ep in pgqList)
            {
                Console.WriteLine("connecting to " + ep);
            }

            var bGet = true;
            var bPut = true;

            if (args.Length > 0)
            {
                bGet = args[0].Contains("get");
                bPut = args[0].Contains("put");
            }

            if (args.Length > 1)
            {
                totalRps = int.Parse(args[1]);
            }

            Console.Write("warming up... ");
            var pgq = new AggPgqClient(pgqList.Select(ep => new PgqClient(ep)).ToArray());
            await pgq.Warmup();

            Console.WriteLine("started!");

            var t = new List <Task>();

            if (bGet)
            {
                t.Add(TestGet());
            }
            if (bPut)
            {
                t.Add(TestPut());
            }

            Console.ReadLine();
            go_on = false;
            await Task.WhenAll(t);
        }