Asynchronous Operations
This outline details asynchronous operations within the Kubernetes client library for C#. It uses a consistent approach to define asynchronous methods for various operations.
Asynchronous Methods
The library offers several asynchronous methods for interacting with the Kubernetes API, enabling efficient and responsive applications. These methods are implemented using the Task
type, which represents an asynchronous operation.
Async Methods with Task
Return Type:
Task
is used as the return type for methods that perform operations without returning any specific result.- Example:
operations.CreateNamespacedPodAsync(namespace, body)
- Source:
src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template
- Example:
Async Methods with Task<T>
Return Type:
Task<T>
is used as the return type for methods that return a result of typeT
.- Example:
operations.ReadNamespacedPodAsync(name, namespace)
- Source:
src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template
- Example:
Async Methods with Task<Stream>
Return Type:
Task<Stream>
is used as the return type for methods that return a stream of data.- Example:
operations.ReadNamespacedPodAsync(name, namespace)
- Source:
src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template
- Example:
Async Methods with Task<HttpOperationResponse>
Return Type:
Task<HttpOperationResponse>
is used as the return type for methods that return the entire HTTP response.- Example:
operations.ListNamespacedPodWithHttpMessagesAsync(namespace, watch: true)
- Source:
src/LibKubernetesGenerator/templates/Operations.cs.template
- Example:
Cancellation Tokens
The library supports cancellation tokens for asynchronous operations. Cancellation tokens allow you to cancel an operation gracefully.
- Example:
operations.CreateNamespacedPodAsync(namespace, body, cancellationToken)
- Source:
src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template
Examples
Here are some examples of how to use asynchronous operations in the library:
- Create a Pod asynchronously:
// Create a new pod asynchronously var pod = new V1Pod { ... }; await operations.CreateNamespacedPodAsync("default", pod);
- Retrieve a Pod asynchronously:
// Retrieve a pod asynchronously var pod = await operations.ReadNamespacedPodAsync("my-pod", "default");
- List Pods asynchronously:
// List all pods asynchronously var pods = await operations.ListNamespacedPodAsync("default");
- Watch for changes to Pods asynchronously:
// Watch for changes to pods asynchronously await operations.ListNamespacedPodWithHttpMessagesAsync("default", watch: true).WatchAsync( (type, item) => { // Process the watch event } );
- Execute a command in a Pod asynchronously:
// Execute a command in a pod asynchronously await operations.NamespacedPodExecAsync("my-pod", "default", "my-container", new[] { "ls", "-l" }, tty: false, async (stdIn, stdOut, stdErr) => { // Process the standard input, output, and error streams } );
Documentation
For detailed information on each operation and its parameters, please refer to the generated documentation or source code in the Kubernetes Client for C# repository.
Conclusion
These asynchronous operations provide a powerful way to interact with the Kubernetes API in a non-blocking manner, enhancing the responsiveness and efficiency of your applications.
## Top-Level Directory Explanations
<a class='local-link directory-link' data-ref="examples/" href="#examples/">examples/</a> - This directory contains example projects and usage scenarios for the Kubernetes client library.
<a class='local-link directory-link' data-ref="examples/exec/" href="#examples/exec/">examples/exec/</a> - This subdirectory contains examples of executing commands on Kubernetes clusters.
<a class='local-link directory-link' data-ref="examples/metrics/" href="#examples/metrics/">examples/metrics/</a> - This subdirectory contains examples of working with metrics in Kubernetes.
<a class='local-link directory-link' data-ref="examples/namespace/" href="#examples/namespace/">examples/namespace/</a> - This subdirectory contains examples of working with namespaces in Kubernetes.
<a class='local-link directory-link' data-ref="examples/portforward/" href="#examples/portforward/">examples/portforward/</a> - This subdirectory contains examples of using port forwarding with the Kubernetes client library.
<a class='local-link directory-link' data-ref="examples/restart/" href="#examples/restart/">examples/restart/</a> - This subdirectory contains examples of restarting Kubernetes pods.
<a class='local-link directory-link' data-ref="examples/watch/" href="#examples/watch/">examples/watch/</a> - This subdirectory contains examples of watching Kubernetes resources for changes.
<a class='local-link directory-link' data-ref="examples/workerServiceDependencyInjection/" href="#examples/workerServiceDependencyInjection/">examples/workerServiceDependencyInjection/</a> - This subdirectory contains an example of using dependency injection in a worker service using the Kubernetes client library.
<a class='local-link directory-link' data-ref="src/" href="#src/">src/</a> - This directory contains the source code for the project.
<a class='local-link directory-link' data-ref="src/KubernetesClient.Aot/" href="#src/KubernetesClient.Aot/">src/KubernetesClient.Aot/</a> - This subdirectory contains ahead-of-time (AOT) compiled code for the Kubernetes client library.
<a class='local-link directory-link' data-ref="src/KubernetesClient.Kubectl/" href="#src/KubernetesClient.Kubectl/">src/KubernetesClient.Kubectl/</a> - This subdirectory contains code for implementing kubectl functionality in the Kubernetes client library.
<a class='local-link directory-link' data-ref="src/KubernetesClient.Kubectl/Beta/" href="#src/KubernetesClient.Kubectl/Beta/">src/KubernetesClient.Kubectl/Beta/</a> - This subdirectory contains beta (experimental) functionality for the kubectl implementation.
<a class='local-link directory-link' data-ref="src/KubernetesClient/" href="#src/KubernetesClient/">src/KubernetesClient/</a> - This subdirectory contains the main Kubernetes client library source code.
<a class='local-link directory-link' data-ref="src/KubernetesClient/LeaderElection/" href="#src/KubernetesClient/LeaderElection/">src/KubernetesClient/LeaderElection/</a> - This subdirectory contains code for implementing leader election in the Kubernetes client library.
<a class='local-link directory-link' data-ref="src/LibKubernetesGenerator/" href="#src/LibKubernetesGenerator/">src/LibKubernetesGenerator/</a> - This subdirectory contains code for generating C# code from OpenAPI definitions.
<a class='local-link directory-link' data-ref="tests/" href="#tests/">tests/</a> - This directory contains test code for the project.
<a class='local-link directory-link' data-ref="tests/E2E.Aot.Tests/" href="#tests/E2E.Aot.Tests/">tests/E2E.Aot.Tests/</a> - This subdirectory contains end-to-end tests for the AOT compiled code.
<a class='local-link directory-link' data-ref="tests/E2E.Tests/" href="#tests/E2E.Tests/">tests/E2E.Tests/</a> - This subdirectory contains end-to-end tests for the non-AOT code.
<a class='local-link directory-link' data-ref="tests/KubernetesClient.Tests/" href="#tests/KubernetesClient.Tests/">tests/KubernetesClient.Tests/</a> - This subdirectory contains tests for the main implementation of the Kubernetes client library.
<a class='local-link directory-link' data-ref="tests/KubernetesClient.Tests/LeaderElection/" href="#tests/KubernetesClient.Tests/LeaderElection/">tests/KubernetesClient.Tests/LeaderElection/</a> - This subdirectory contains tests for the leader election functionality.
<a class='local-link directory-link' data-ref="tests/KubernetesClient.Tests/Mock/" href="#tests/KubernetesClient.Tests/Mock/">tests/KubernetesClient.Tests/Mock/</a> - This subdirectory contains tests using mocks.