private static async Task Main(string[] args) { var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); var generic = new GenericClient(config, "", "v1", "nodes"); var node = await generic.ReadAsync <V1Node>("kube0").ConfigureAwait(false); Console.WriteLine(node.Metadata.Name); var genericPods = new GenericClient(config, "", "v1", "pods"); var pods = await genericPods.ListNamespacedAsync <V1PodList>("default").ConfigureAwait(false); foreach (var pod in pods.Items) { Console.WriteLine(pod.Metadata.Name); } }
public async Task <IEnumerable> GetReplicaSetByNamespace(string resourceNamespace) { List <ReplicaSetModel> resourceList = new List <ReplicaSetModel>(); var apiGenericClient = new GenericClient(_config, "apps", "v1", "replicasets"); var returnedList = await apiGenericClient.ListNamespacedAsync <V1ReplicaSetList>(resourceNamespace).ConfigureAwait(false); foreach (var item in returnedList.Items) { ReplicaSetModel r = new ReplicaSetModel { Name = item.Name() }; resourceList.Add(r); } return(resourceList); }
public async Task <IEnumerable> GetDeploymentsByNamespace(string resourceNamespace) { List <DeploymentModel> resourceList = new List <DeploymentModel>(); var apiGenericClient = new GenericClient(_config, "apps", "v1", "deployments"); var returnedList = await apiGenericClient.ListNamespacedAsync <V1DeploymentList>(resourceNamespace).ConfigureAwait(false); foreach (var item in returnedList.Items) { DeploymentModel d = new DeploymentModel { Name = item.Name() }; resourceList.Add(d); } return(resourceList); }
public async Task <IEnumerable> GetServicesByNamespace(string resourceNamespace) { List <ServiceModel> resourceList = new List <ServiceModel>(); var apiGenericClient = new GenericClient(_config, "", "v1", "services"); var returnedList = await apiGenericClient.ListNamespacedAsync <V1ServiceList>(resourceNamespace).ConfigureAwait(false); foreach (var item in returnedList.Items) { ServiceModel s = new ServiceModel { Name = item.Name() }; resourceList.Add(s); } return(resourceList); }
private static async Task Main(string[] args) { Console.WriteLine("starting main()..."); // creating the k8s client var k8SClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile(); IKubernetes client = new Kubernetes(k8SClientConfig); // creating a K8s client for the CRD var myCRD = Utils.MakeCRD(); Console.WriteLine("working with CRD: {0}.{1}", myCRD.PluralName, myCRD.Group); var generic = new GenericClient(client, myCRD.Group, myCRD.Version, myCRD.PluralName); // creating a sample custom resource content var myCr = Utils.MakeCResource(); try { Console.WriteLine("creating CR {0}", myCr.Metadata.Name); var response = await client.CreateNamespacedCustomObjectWithHttpMessagesAsync( myCr, myCRD.Group, myCRD.Version, myCr.Metadata.NamespaceProperty ?? "default", myCRD.PluralName).ConfigureAwait(false); } catch (HttpOperationException httpOperationException) when(httpOperationException.Message.Contains("422")) { var phase = httpOperationException.Response.ReasonPhrase; var content = httpOperationException.Response.Content; Console.WriteLine("response content: {0}", content); Console.WriteLine("response phase: {0}", phase); } catch (HttpOperationException) { } // listing the cr instances Console.WriteLine("CR list:"); var crs = await generic.ListNamespacedAsync <CustomResourceList <CResource> >(myCr.Metadata.NamespaceProperty ?? "default").ConfigureAwait(false); foreach (var cr in crs.Items) { Console.WriteLine("- CR Item {0} = {1}", crs.Items.IndexOf(cr), cr.Metadata.Name); } var old = JsonSerializer.SerializeToDocument(myCr); myCr.Metadata.Labels.TryAdd("newKey", "newValue"); var expected = JsonSerializer.SerializeToDocument(myCr); var patch = old.CreatePatch(expected); // updating the custom resource var crPatch = new V1Patch(patch, V1Patch.PatchType.JsonPatch); try { var patchResponse = await client.PatchNamespacedCustomObjectAsync( crPatch, myCRD.Group, myCRD.Version, myCr.Metadata.NamespaceProperty ?? "default", myCRD.PluralName, myCr.Metadata.Name).ConfigureAwait(false); } catch (HttpOperationException httpOperationException) { var phase = httpOperationException.Response.ReasonPhrase; var content = httpOperationException.Response.Content; Console.WriteLine("response content: {0}", content); Console.WriteLine("response phase: {0}", phase); } // getting the updated custom resource var fetchedCR = await generic.ReadNamespacedAsync <CResource>( myCr.Metadata.NamespaceProperty ?? "default", myCr.Metadata.Name).ConfigureAwait(false); Console.WriteLine("fetchedCR = {0}", fetchedCR.ToString()); // deleting the custom resource try { myCr = await generic.DeleteNamespacedAsync <CResource>( myCr.Metadata.NamespaceProperty ?? "default", myCr.Metadata.Name).ConfigureAwait(false); Console.WriteLine("Deleted the CR"); } catch (Exception exception) { Console.WriteLine("Exception type {0}", exception); } }
private static async Task Main(string[] args) { var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); //var generic = new GenericClient(config, "", "v1", "nodes"); // var node = await generic.ReadAsync<V1Node>("kube0").ConfigureAwait(false); // Console.WriteLine(node.Metadata.Name); var genericPods = new GenericClient(config, "", "v1", "pods"); var pods = await genericPods.ListNamespacedAsync <V1PodList>("default").ConfigureAwait(false); foreach (var pod in pods.Items) { Console.WriteLine(pod.Metadata.Name); } var genericServices = new GenericClient(config, "", "v1", "services"); var services = await genericServices.ListNamespacedAsync <V1ServiceList>("default").ConfigureAwait(false); foreach (var svc in services.Items) { Console.WriteLine(svc.Metadata.Name); } var genericDeployments = new GenericClient(config, "apps", "v1", "deployments"); var deployments = await genericDeployments.ListNamespacedAsync <V1DeploymentList>("default").ConfigureAwait(false); foreach (var dep in deployments.Items) { Console.WriteLine(dep.Metadata.Name); } var genericDaemonSets = new GenericClient(config, "apps", "v1", "daemonsets"); var daemonsets = await genericDaemonSets.ListNamespacedAsync <V1DaemonSetList>("kube-system").ConfigureAwait(false); foreach (var d in daemonsets.Items) { Console.WriteLine(d.Metadata.Name); } // var genericIngress = new GenericClient(config, "apps", "v1", "ingress"); // var ingresses = await genericIngress.ListNamespacedAsync<V1IngressList>("default").ConfigureAwait(false); // foreach (var i in ingresses.Items) // { // Console.WriteLine(i.Metadata.Name); // } var genericReplicaset = new GenericClient(config, "apps", "v1", "replicasets"); var replicasets = await genericReplicaset.ListNamespacedAsync <V1ReplicaSetList>("default").ConfigureAwait(false); foreach (var r in replicasets.Items) { Console.WriteLine(r.Metadata.Name); } // var genericJob = new GenericClient(config, "", "v1", "jobs"); // var jobs = await genericJob.ListNamespacedAsync<V1JobList>("default").ConfigureAwait(false); // foreach (var r in jobs.Items) // { // Console.WriteLine(r.Metadata.Name); // } }
public async void GenericTest() { var namespaceParameter = "default"; var podName = "k8scsharp-e2e-generic-pod"; var client = CreateClient(); var genericPods = new GenericClient(client, "", "v1", "pods"); void Cleanup() { var pods = client.ListNamespacedPod(namespaceParameter); while (pods.Items.Any(p => p.Metadata.Name == podName)) { try { client.DeleteNamespacedPod(podName, namespaceParameter); } catch (HttpOperationException e) { if (e.Response.StatusCode == System.Net.HttpStatusCode.NotFound) { return; } } } } try { Cleanup(); await genericPods.CreateNamespacedAsync( new V1Pod() { Metadata = new V1ObjectMeta { Name = podName, }, Spec = new V1PodSpec { Containers = new[] { new V1Container() { Name = "k8scsharp-e2e", Image = "nginx", }, }, }, }, namespaceParameter).ConfigureAwait(false); var pods = await genericPods.ListNamespacedAsync <V1PodList>(namespaceParameter).ConfigureAwait(false); Assert.Contains(pods.Items, p => p.Metadata.Name == podName); int retry = 5; while (retry-- > 0) { try { await genericPods.DeleteNamespacedAsync <V1Pod>(namespaceParameter, podName).ConfigureAwait(false); } catch (HttpOperationException e) { if (e.Response.StatusCode == System.Net.HttpStatusCode.NotFound) { return; } } pods = await genericPods.ListNamespacedAsync <V1PodList>(namespaceParameter).ConfigureAwait(false); if (!pods.Items.Any(p => p.Metadata.Name == podName)) { break; } await Task.Delay(TimeSpan.FromSeconds(2.5)).ConfigureAwait(false); } Assert.DoesNotContain(pods.Items, p => p.Metadata.Name == podName); } finally { Cleanup(); } }