Skip to content

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

Open
@josephaw1022

Description

@josephaw1022

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    lifecycle/staleDenotes an issue or PR has remained open with no activity and has become stale.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions