public IActionResult Delete() { IKubernetes client = GetKubeConfig.GetKubernetes(); V1PodList list = client.ListNamespacedPod("default"); var testeDelete = new V1DeleteOptions(); client.DeleteNamespacedPod(testeDelete, "teste-api", "default"); return(Ok()); }
private void ParsePod(string ns, V1PodList list, bool observe = false) { // lets try to find a worker foreach (var item in list.Items) { // it's weird but some pods does not have annotations so lets figure out if (item.Metadata?.Annotations?.Count > 0) { // those labels indicates that this pod is actually a worker so we should consider to add it to the hashing ring item.Metadata.Annotations.TryGetValue(Annotations.CachingFasterEnabled, out string isEnabled); item.Metadata.Annotations.TryGetValue(Annotations.CachingFasterScrapePort, out string listenPort); // we need both values to proceed if (isEnabled is null || listenPort is null) { continue; } // lets keep an eye on this namespace if (observe) { ObservedNamespaces(ns); } // may be the pod is market as disabled so lets validate it if (isEnabled.Equals("true", StringComparison.InvariantCultureIgnoreCase)) { var status = default(V1ContainerStatus); // lets check if ready if ((status = item.Status?.ContainerStatuses?.FirstOrDefault()) != null) { // so add or refresh the worker with current status workers.Join(item.Metadata?.Name, item.Status?.PodIP, Convert.ToInt32(listenPort), status.Ready); } else { // we could not retrieve status metadata, so lets mark as inactive workers.SetStatus(item.Status?.PodIP, false, false); } } else { // we could not retrieve status metadata, so lets mark as inactive the pod is marked as disabled so lets refresh our ring if neccessary workers.SetStatus(item.Status?.PodIP, false, true); } } } }
/// <summary> /// fixme!!! /// </summary> private IEnumerable <string> FindWorkers() { List <string> podNames = new List <string>(); V1PodList pods = client.ListNamespacedPod(podNamespace); foreach (V1Pod pod in pods.Items) { string podName = pod.Name(); if (!podName.Contains("job-manager")) { podNames.Add(podName); } } return(podNames); }
/// <summary> /// fixme!!! /// </summary> private IEnumerable <string> FindWorkers() { List <string> podNames = new List <string>(); string labelSelector = $"{podTypeLabelName}={workerPodType}"; V1PodList pods = client.ListNamespacedPod(podNamespace, labelSelector: labelSelector); foreach (V1Pod pod in pods.Items) { string podName = pod.Name(); if (!podName.Contains("job-manager")) { podNames.Add(podName); } } return(podNames); }
public async Task ListPodAsync_ForwardRequests_Async() { var list = new V1PodList(); var response = new HttpOperationResponse <V1PodList>() { Body = list }; var protocol = new Mock <IKubernetesProtocol>(MockBehavior.Strict); protocol .Setup(p => p.ListNamespacedPodWithHttpMessagesAsync("default", null, "continue", "fieldSelector", "labelSelector", 1, null, null, null, null, null, null, default)) .ReturnsAsync(response); protocol.Setup(p => p.Dispose()).Verifiable(); using (var client = new KubernetesClient(protocol.Object, KubernetesOptions.Default, NullLogger <KubernetesClient> .Instance, NullLoggerFactory.Instance)) { Assert.Same(list, await client.ListPodAsync("continue", "fieldSelector", "labelSelector", 1, default).ConfigureAwait(false)); } protocol.Verify(); }
public IActionResult Post() { IKubernetes client = GetKubeConfig.GetKubernetes(); V1PodList list = client.ListNamespacedPod("default"); var pod = new V1Pod { Metadata = new V1ObjectMeta { Name = "teste-api", }, Spec = new V1PodSpec { Containers = new List <V1Container>() { new V1Container { Image = "containersopenhackteam10.azurecr.io/minecraft-server:v1.0", Name = "teste-api", Ports = new List <V1ContainerPort> { new V1ContainerPort { ContainerPort = 25565 }, new V1ContainerPort { ContainerPort = 25575 } } } }, ImagePullSecrets = new List <V1LocalObjectReference> { new V1LocalObjectReference { Name = "acr-auth" } } } }; client.CreateNamespacedPod(pod, "default"); return(Ok()); }
public V1Pod GetPodByNameAndNamespace(string _PodName, string _Namespace) { try { V1PodList PodList = KClient.ListNamespacedPod(_Namespace); for (int i = 0; i < PodList.Items.Count; ++i) { if (PodList.Items[i].Name() == _PodName) { return(PodList.Items[i]); } } return(null); } catch (Exception ex) { //Return null if pod could not be found and throw exception when connection error occurs throw ex; } }
public IActionResult Get() { IKubernetes client = GetKubeConfig.GetKubernetes(); V1PodList list = client.ListNamespacedPod("default"); List <RootObject> pods = new List <RootObject>(); foreach (var item in list.Items) { pods.Add(new RootObject { Name = item?.Metadata?.Name, Endpoints = new Endpoints { Minecraft = $"{item?.Status?.PodIP}: {item?.Spec?.Containers?.FirstOrDefault().Ports?.FirstOrDefault(p => p.ContainerPort == 25565).ContainerPort}", Rcon = $"{item?.Status?.PodIP}: {item?.Spec?.Containers?.FirstOrDefault().Ports?.FirstOrDefault(p => p.ContainerPort == 25575).ContainerPort}" } }); } //return Ok(list.Items.FirstOrDefault()); return(Ok(pods)); }
protected override int OnExecute(CommandLineApplication app) { var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); var podList = new V1PodList(); try { IKubernetes client = new Kubernetes(config); if (Parent.AllNamespaces) { podList = client.ListPodForAllNamespaces(); } else { podList = client.ListNamespacedPod("default"); } } catch (HttpRequestException ex) { Reporter.Output.WriteError(ex.InnerException.Message); return(1); } var getOutputList = new List <GetPodOutput>(); foreach (var item in podList.Items) { var totalContainers = item.Status.ContainerStatuses.Count; var readyContainers = item.Status.ContainerStatuses.Count(cs => cs.Ready); var restarts = item.Status.ContainerStatuses.Sum(cs => cs.RestartCount); var readyString = $"{readyContainers}/{totalContainers}"; var getPodOutput = new GetPodOutput { Name = item.Metadata.Name, Ready = readyString, Status = item.Status.Phase, Restarts = restarts, Age = DateTime.UtcNow - item.Metadata.CreationTimestamp, Namespace = item.Metadata.NamespaceProperty }; getOutputList.Add(getPodOutput); } var outputDict = new Dictionary <string, Func <GetPodOutput, object> >(); if (Parent.AllNamespaces) { outputDict.Add("NAMESPACE", x => x.Namespace); } outputDict.Add("NAME", x => x.Name); outputDict.Add("READY", x => x.Ready); outputDict.Add("STATUS", x => x.Status); outputDict.Add("RESTARTS", x => x.Restarts); outputDict.Add("AGE", x => x.AgeString); TableFormatter.Print(getOutputList, "No resources found", null, outputDict); return(0); }