static void Main(string[] args) { var client = new Kubernetes(KubernetesClientConfiguration.InClusterConfig()); var pods = client.ListNamespacedPod("default"); foreach (var p in pods.Items) Console.WriteLine($"Pod = {p.Metadata.Name}"); //Create Pod var pod = new V1Pod { ApiVersion = "v1", Kind = "Pod", Metadata = new V1ObjectMeta { Name = "my-test-pod" }, Spec = new V1PodSpec { Containers = new List<V1Container>() } }; pod.Spec.Containers.Add(new V1Container { Name = "my-test-container", Image = "ngnix", }); //Create pod var result = client.CreateNamespacedPod(pod, "default"); foreach (var status in result.Status.ContainerStatuses) Console.WriteLine($"Image = {status.Image}, StartedAt = {status.State.Running.StartedAt}"); pods = client.ListNamespacedPod("default"); foreach (var p in pods.Items) Console.WriteLine($"Pod = {p.Metadata.Name}"); Console.ReadLine(); }
private static List <Server> GetServers() { List <Server> servers = new List <Server>(); var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); IKubernetes client = new Kubernetes(config); var listDeploy = client.ListNamespacedDeployment("default"); var listServices = client.ListNamespacedService("default"); foreach (var deploy in listDeploy.Items) { string ipExternal = ""; var service = listServices.Items.Where(a => a.Metadata.Name == deploy.Metadata.Name).FirstOrDefault(); if (service != null) { if (service.Status.LoadBalancer != null && service.Status.LoadBalancer.Ingress != null && service.Status.LoadBalancer.Ingress.Count() > 0) { ipExternal = service.Status.LoadBalancer.Ingress.First().Ip; } string exampleJson = null; exampleJson = "{\n \"endpoints\" : {\n \"minecraft\" : \"minecraft\",\n \"rcon\" : \"rcon\"\n },\n \"name\" : \"name\"\n}"; var server = exampleJson != null ? JsonConvert.DeserializeObject <Server>(exampleJson) : default(Server); server.Name = deploy.Metadata.Name; server.Endpoints.Minecraft = ipExternal + ":25565"; server.Endpoints.Rcon = ipExternal + ":25575"; servers.Add(server); } } return(servers); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); if ( string.IsNullOrWhiteSpace(Configuration["KUBERNETES_SERVICE_HOST"]) == false && string.IsNullOrWhiteSpace(Configuration["KUBERNETES_SERVICE_PORT"]) == false ) { services.AddTransient <IKubernetes>(serviceProvider => new Kubernetes(KubernetesClientConfiguration.InClusterConfig())); } else { services.AddTransient <IKubernetes>(serviceProvider => new Kubernetes(KubernetesClientConfiguration.BuildConfigFromConfigFile())); } services = AddPersistenceRepository(services); services.AddTransient <IConfigMapService, ConfigMapService>(); services.AddTransient <IAwsAuthConfigMapRepository, AwsAuthConfigMapRepository>(); services.AddTransient <IAddRoleRequestValidator, AddRoleRequestValidator>(); services.AddTransient <IAddNamespaceRequestValidator, AddNamespaceRequestValidator>(); services.AddTransient <INamespaceRepository, NamespaceRepository>(); services.AddTransient <IRoleRepository, RoleRepository>(); services.AddTransient <IRoleBindingRepository, RoleBindingRepository>(); services.AddTransient <IKubernetesWrapper, KubernetesWrapper>(); // Event handlers services.AddTransient <IEventHandler <CapabilityRegisteredEvent>, CapabilityRegisteredEventHandler>(); services.AddHostedService <MetricHostedService>(); services.AddHealthChecks() .AddCheck("self", () => HealthCheckResult.Healthy()) .AddCheck <S3BucketHealthCheck>("S3 bucket"); ConfigureDomainEvents(services); services.AddHostedService <KafkaConsumerHostedService>(); }
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(); } }
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()); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddSingleton <IKubernetes>(serviceProvider => { KubernetesClientConfiguration config = KubernetesClientConfiguration.InClusterConfig(); return(new Kubernetes(config)); }); services.AddSingleton <IKubernetesService, KubernetesService>(); services.AddMvc() .AddJsonOptions(setup => { setup.SerializerSettings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); } ); }
public async Task <string> GetClusterNameAsync() { try { logger.LogInformation("Getting cluster information - client - GetClusterNameAsync"); var stream = await storageWorker.DownloadFileAsync(kubekOptions.ConfigFileName); var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(stream); logger.LogInformation("Getting host name from config file"); return(config.Host); } catch (Exception e) { logger.LogError(e.Message); } return(string.Empty); }
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; }
public void LoadInCluster() { 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)) { var config = KubernetesClientConfiguration.InClusterConfig(); Assert.Equal("https://kubernetes.default.svc:443/", config.Host); Assert.Null(config.Namespace); } }
private V1ServiceList LoadKubernetesServices() { try { var config = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("KUBERNETES_PORT")) ? KubernetesClientConfiguration.BuildConfigFromConfigFile() : KubernetesClientConfiguration.InClusterConfig(); var client = new Kubernetes(config); return(client.ListNamespacedService("default")); } catch (KubeConfigException ex) { _logger.LogError(ex, "This is not a FATAL error. It only means that K8S service discovery is off."); return(null); } }
public DeployService() { // default configuration var config = new KubernetesClientConfiguration { Host = "http://127.0.0.1:8001" }; try { config = KubernetesClientConfiguration.InClusterConfig(); } catch (k8s.Exceptions.KubeConfigException) { Console.WriteLine("Running in Localhost mode"); } this._client = new Kubernetes(config); }
public async Task Crd_Equality_test() { var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); var kubernetes = new Kubernetes(config); var entity1 = await kubernetes.GetNamespacedCustomResource <ArgoApplicationResource>( "argo", "azl-dev-elysium-api", CancellationToken.None ); var entity2 = await kubernetes.GetNamespacedCustomResource <ArgoApplicationResource>( "argo", "azl-dev-elysium-api", CancellationToken.None ); entity1.Should().BeEquivalentTo(entity2); }
private static void Main(string[] args) { var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); IKubernetes client = new Kubernetes(config); Console.WriteLine("Starting Request!"); var list = client.ListNamespacedPod("default"); foreach (var item in list.Items) { Console.WriteLine(item.Metadata.Name); } if (list.Items.Count == 0) { Console.WriteLine("Empty!"); } }
public void DeletedConfigurationFile() { var assetFileInfo = new FileInfo("assets/kubeconfig.yml"); var tempFileInfo = new FileInfo(Path.GetTempFileName()); File.Copy(assetFileInfo.FullName, tempFileInfo.FullName, /* overwrite: */ true); KubernetesClientConfiguration config; try { config = KubernetesClientConfiguration.BuildConfigFromConfigFile(tempFileInfo, useRelativePaths: false); } finally { File.Delete(tempFileInfo.FullName); } }
public static async Task <KubernetesClientConfiguration> CreateClientConfigAsync(this IKubernetes kubernetes, V1ServiceAccount serviceAccount) { if (kubernetes is null) { throw new ArgumentNullException(nameof(kubernetes)); } if (serviceAccount is null) { throw new ArgumentNullException(nameof(serviceAccount)); } var clusterConfig = await kubernetes .CreateClusterConfigAsync(serviceAccount) .ConfigureAwait(false); return(KubernetesClientConfiguration.BuildConfigFromConfigObject(clusterConfig)); }
public static KubernetesClientConfiguration Configure() { if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("KUBECONFIG"))) { return(KubernetesClientConfiguration.BuildConfigFromConfigFile((string)null, Environment.GetEnvironmentVariable("KUBECONFIG"))); } if (File.Exists("minikube.config")) { // If you're using minikube, things can get akward if you import the root CA in the Trusted Root Certificate Authorities list // and re-create your cluster. Certificates issued will be rejected by Windows because the DN of the root CA doesn't change; // yet the certificate will have a different signature. return(KubernetesClientConfiguration.BuildConfigFromConfigFile((string)null, "minikube.config")); } else { throw new NotSupportedException(); } }
private static KubernetesClientConfiguration LocateKubeConfig() { // Attempt to dynamically determine between in-cluster and host (debug) development... var serviceHost = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST"); var servicePort = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_PORT"); // Locate from environment variables directly. Unlike IConfiguration, which could be overridden. if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("POD_NAME")) || string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("POD_NAMESPACE"))) { throw new InvalidOperationException("Failed to detect current pod information. Check POD_NAME and POD_NAMESPACE environment variables."); } if (!string.IsNullOrWhiteSpace(serviceHost) && !string.IsNullOrWhiteSpace(servicePort)) { return(KubernetesClientConfiguration.InClusterConfig()); } return(KubernetesClientConfiguration.BuildConfigFromConfigFile()); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "servers/{name}")] HttpRequest req, TraceWriter log, ExecutionContext context, string name) { IActionResult result = null; try { var config = KubernetesClientConfiguration.BuildConfigFromConfigFile($@"{context.FunctionAppDirectory}\config\config.txt"); new Minecraft(config).Delete(req.Query["name"]); result = new NoContentResult(); } catch (Exception e) { result = new OkObjectResult(new { name, error = e.Message }); } return(result as ActionResult); }
private KubernetesClientConfiguration GetKubernetesClient() { KubernetesClientConfiguration config = new KubernetesClientConfiguration(); config.AccessToken = appConfig.GetValue <string>("K8_ACCESS_TOKEN"); config.ClientCertificateData = appConfig.GetValue <string>("K8_CLIENT_CERTIFICATE_DATA"); config.ClientCertificateKeyData = appConfig.GetValue <string>("K8_CLIENT_CERTIFICATE_KEY_DATA"); config.ClientKeyFilePath = ""; config.ClientCertificateFilePath = ""; config.Host = appConfig.GetValue <string>("K8_TARGET_HOST"); config.Namespace = ""; config.Password = ""; config.SkipTlsVerify = true; config.SslCaCert = null; config.UserAgent = null; config.Username = null; return(config); }
public Kub() { KubernetesClientConfiguration config = null; try { config = KubernetesClientConfiguration.InClusterConfig(); } catch { } if (config == null) { config = new KubernetesClientConfiguration { Host = "http://127.0.0.1:8001" }; } _client = new Kubernetes(config); }
private static void Main(string[] args) { var config = KubernetesClientConfiguration.BuildDefaultConfig(); var handler = new PrometheusHandler(); IKubernetes client = new Kubernetes(config, new DelegatingHandler[] { handler }); var server = new MetricServer(hostname: "localhost", port: 1234); server.Start(); Console.WriteLine("Making requests!"); while (true) { client.ListNamespacedPod("default"); client.ListNode(); client.ListNamespacedDeployment("default"); Thread.Sleep(1000); } }
public static async Task Main() { var crd = new CustomResourceDefinition() { ApiVersion = "engineerd.dev/v1alpha1", PluralName = "examples", Kind = "Example", Namespace = "kubecontroller" }; var controller = new Controller <ExampleCRD>( new Kubernetes(KubernetesClientConfiguration.BuildConfigFromConfigFile()), crd, (WatchEventType eventType, ExampleCRD example) => Console.WriteLine("Event type: {0} for {1}", eventType, example.Metadata.Name)); var cts = new CancellationTokenSource(); await controller.StartAsync(cts.Token).ConfigureAwait(false); }
public async Task GetSiloMembership_Kubernetes_ReturnsSiloInformation() { //Act var config = KubernetesClientConfiguration.BuildDefaultConfig(); config.AccessToken = KubernetesSiloOptions.ApiToken; using var client = new Kubernetes(config) { BaseUri = new Uri(KubernetesSiloOptions.ApiEndpoint.Remove(KubernetesSiloOptions.ApiEndpoint.LastIndexOf("/", StringComparison.Ordinal), 4)) }; var membershipTable = await client.GetNamespacedCustomObjectAsync(KubernetesSiloOptions.Group, "v1", "orleanstest", "silos", string.Empty); JsonDocument.Parse(membershipTable.ToString()).RootElement.TryGetProperty("items", out var siloElements); var siloElement = siloElements.EnumerateArray().ToList().FirstOrDefault(x => x.GetProperty("siloName").ToString() == SiloName); //Assert Assert.Equal(SiloStatus.Active.ToString(), siloElement.GetProperty("status").ToString(), true); }
public async Task <Kubernetes> LoadBasedOnConfigurationAsync() { try { logger.LogInformation("Getting config file from Azure Storage"); var stream = await storageWorker.DownloadFileAsync(kubekOptions.ConfigFileName); logger.LogInformation("Getting cluster information - client - LoadBasedOnConfigurationAsync"); var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(stream); var client = new Kubernetes(config); return(client); } catch (Exception e) { logger.LogError(e.Message); } return(null); }
public KubernetesClusterAgent( IClusterMembershipService clusterMembershipService, ILogger <KubernetesClusterAgent> logger, IOptionsMonitor <KubernetesHostingOptions> options, IOptions <ClusterOptions> clusterOptions, ILocalSiloDetails localSiloDetails) { _localSiloDetails = localSiloDetails; _logger = logger; _shutdownToken = new CancellationTokenSource(); _options = options; _clusterOptions = clusterOptions.Value; _clusterMembershipService = clusterMembershipService; _config = _options.CurrentValue.GetClientConfiguration?.Invoke() ?? throw new ArgumentNullException(nameof(KubernetesHostingOptions) + "." + nameof(KubernetesHostingOptions.GetClientConfiguration)); _client = new k8s.Kubernetes(_config); _podLabelSelector = $"{KubernetesHostingOptions.ServiceIdLabel}={_clusterOptions.ServiceId},{KubernetesHostingOptions.ClusterIdLabel}={_clusterOptions.ClusterId}"; _podNamespace = _options.CurrentValue.Namespace; _podName = _options.CurrentValue.PodName; }
public IEnumerable <string> Get() { // Load from in-cluster configuration: var config = KubernetesClientConfiguration.InClusterConfig(); // Use the config object to create a client. var client = new Kubernetes(config); var result = new List <string>(); var list = client.ListNamespacedService("default"); foreach (var item in list.Items) { result.Add("Pods for service: " + item.Metadata.Name); result.Add("=-=-=-=-=-=-=-=-=-=-="); if (item.Spec == null || item.Spec.Selector == null) { continue; } var labels = new List <string>(); foreach (var key in item.Spec.Selector) { labels.Add(key.Key + "=" + key.Value); } var labelStr = string.Join(",", labels.ToArray()); result.Add(labelStr); var podList = client.ListNamespacedPod("default", labelSelector: labelStr); foreach (var pod in podList.Items) { result.Add(pod.Metadata.Name); } if (podList.Items.Count == 0) { result.Add("Empty!"); } } return(result); }
private V1Pod FindFirstPod(string prefix) { var config = KubernetesClientConfiguration.BuildDefaultConfig(); IKubernetes client = new Kubernetes(config); Console.WriteLine("Starting Request!"); var list = client.ListNamespacedPod("default"); foreach (var item in list.Items) { if (item.Metadata.Name.StartsWith(prefix)) { return(item); } Console.WriteLine(item.Metadata.Name); } return(null); }
// 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 KubernetesEndpointWatcher(IWebHostEnvironment env, IOptions <BalancerOptions> options, ILogger <KubernetesEndpointWatcher> logger) { _options = options.Value; _logger = logger; _endpointEntries = Array.Empty <EndpointEntry>(); try { _k8sClient = new Kubernetes(KubernetesClientConfiguration.InClusterConfig()); InitializeWatcherAsync().Wait(); _logger.LogDebug("Connected to K8s, watcher initialized"); } catch (KubeConfigException x) when(PredefinedConfiguration.HasPredefinedConfiguration(x, env)) { _k8sClient = null; _endpointEntries = PredefinedConfiguration.GetEndpointEntries(env); _logger.LogDebug($"Can not connect to K8s, using predefined configuration"); } }
private async static Task Main(string[] args) { var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); IKubernetes client = new Kubernetes(config); var podlistResp = client.ListNamespacedPodWithHttpMessagesAsync("default", watch: true); // C# 8 required https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8 await foreach (var(type, item) in podlistResp.WatchAsync <V1Pod, V1PodList>()) { Console.WriteLine("==on watch event=="); Console.WriteLine(type); Console.WriteLine(item.Metadata.Name); Console.WriteLine("==on watch event=="); } // uncomment if you prefer callback api // WatchUsingCallback(client); }