Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to add ApplyInlineYaml and DeleteInlineYaml methods to the C# Kubernetes client #1608

Open
josephaw1022 opened this issue Jan 17, 2025 · 1 comment

Comments

@josephaw1022
Copy link

Background

I have experience working with Helm charts, where I frequently read and write YAML files to define Kubernetes resources. Recently, I’ve been working on creating an operator with KubeOps, and in that process, I realized that having a method like ApplyInlineYaml and DeleteInlineYaml in the C# Kubernetes client would make it much easier to follow exactly what is being created and deleted.

While it is not difficult to read C# code and understand how it is converted into YAML, it would be much more intuitive (at least for me) if I could define my resources using inline YAML. This would simplify the process of defining and managing resources directly in the code, reducing the mental overhead of mapping between C# objects and their YAML representations.

Proposal

Adding two new methods to the IKubernetes interface:

ApplyInlineYaml: This method would allow users to apply Kubernetes resources defined as YAML strings directly to a cluster.
DeleteInlineYaml: This method would allow users to delete Kubernetes resources defined as YAML strings directly from a cluster.

Example Usage

// ... omitted dependencies

public class Program
{
    private static async Task Main(string[] args)
    {
        var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
        IKubernetes client = new Kubernetes(config);

        var inlineResources = """
          # Namespace Definition
          apiVersion: v1
          kind: Namespace
          metadata:
            name: simple-namespace

          ---

          # Deployment Definition
          apiVersion: apps/v1
          kind: Deployment
          metadata:
            name: simple-deployment
            namespace: simple-namespace
          spec:
            replicas: 1
            selector:
              matchLabels:
                app: simple-app
            template:
              metadata:
                labels:
                  app: simple-app
              spec:
                containers:
                - name: simple-container
                  image: nginx:latest
                  ports:
                  - containerPort: 80
        """;

        // To create or update the resources
        client.ApplyInlineYaml(inlineResources);

        // To delete the resources
        client.DeleteInlineYaml(inlineResources);
    }
}

Existing Example:

There is already an example in the official C# Kubernetes client repository (Program.cs) that demonstrates how to load and apply Kubernetes resources from YAML. This approach works, but it is not ideal for operator development, where the simplicity of applying or deleting resources from inline YAML would be more efficient. Simplifying this process into a single method call for each action (apply and delete) would streamline workflows, especially in operator development.

@tg123
Copy link
Member

tg123 commented Jan 17, 2025

thank you
this is planned in kubectl sdk

it will provide same experience as kubectl cli

however, we don't have enough contributors for it
i was a bit busy recently and don't have enough time on it

pr to kubectl project is welcomed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants