This documentation provides a step-by-step guide on how to build and start the kubernetes-client/csharp project. Follow these instructions carefully to ensure a successful setup.

Prerequisites

  • A Linux machine with Docker installed.
  • .NET SDK version 8.0.100 must be installed.

Ensure your environment is ready and configured before you proceed with the steps below.

Step-by-Step Guide

1. Clone the Repository

Begin by checking out the generator project. It is recommended to clone the repository into a separate directory, referred to as $GEN_DIR.

cd $GEN_DIR/..
git clone https://github.com/kubernetes-client/gen

2. Prepare the Environment

Ensure that your global.json file is configured correctly. It should specify the SDK version and any build SDKs necessary for the project.

Here is an example of what your global.json might look like:

{
    "sdk": {
        "version": "8.0.100",
        "rollForward": "latestMajor"
    },
    "msbuild-sdks": {
        "Microsoft.Build.Traversal": "4.1.0"
    }
}

3. Navigate to the Project Directory

Locate and navigate to the examples/simple directory within the cloned repository. This directory contains the example projects you will work with.

cd csharp/examples/simple

4. Build the Project

Before running the project, build it using the .NET CLI. This step compiles the C# code and prepares it for execution.

dotnet build

5. Run the Example Application

Once the build is complete, you can run the example application using the following command. This will start the execution of the Program.cs file located in the examples/simple directory.

dotnet run

Detailed Code Example

Here is a concise code snippet for your reference, especially demonstrating how to create a Kubernetes client and list the pods in the default namespace:

using k8s;
using k8s.Models;

var config = KubernetesClientConfiguration.BuildConfigFromConfigFile("your_kubeconfig_path");
IKubernetes client = new Kubernetes(config);
Console.WriteLine("Starting Request!");

var list = client.CoreV1.ListNamespacedPod("default");
foreach (var pod in list.Items)
{
    Console.WriteLine($"Pod Name: {pod.Metadata.Name}");
}

This snippet enables you to connect to the Kubernetes cluster and retrieve the list of pods in the default namespace.

Final Notes

Ensure you have your Kubernetes configuration file (kubeconfig) correctly set up and accessible from the directory from which you’re running the commands.

The examples directory contains extensive code examples that might be beneficial as you work through building and starting your project.

By adhering to these steps, you will successfully build and start the kubernetes-client/csharp project on your local machine.

Sources