tintoy / dotnet-kube-client

A Kubernetes API client for .NET Standard / .NET Core
MIT License
192 stars 33 forks source link

Example of creating pod / applying yaml #93

Closed MortenMeisler closed 5 years ago

MortenMeisler commented 5 years ago

Can you show the equivalent of doing this: kubectl apply -f my-app.yaml using your library?

Or perhaps just a client.PodsV1().Create() example?

Thanks

tintoy commented 5 years ago

Hi.

Here's an example of using PodsV1().Create(). It creates a pod called "demo-pod" in namespace "demo-namespace" with a label of "app=demo-app", with a single container that runs /bin/sleep.

PodV1 pod = await client.PodsV1().Create(new PodV1
{
    Metadata = new ObjectMetaV1
    {
        Name = "demo-pod",
        Namespace = "demo-namespace",
        Labels =
        {
            ["app"] = "demo-app"
        }
    },
    Spec = new PodSpecV1
    {
        Containers =
        {
            new ContainerV1
            {
                Name = "demo-container",
                Image = "ubuntu:xenial",
                Command = { "/bin/sleep", "20m" }
            }
        }
    }
});

kubectl apply does a whole bunch of stuff behind the scenes, but the new server-side apply feature in K8s will enable you to replicate that behaviour when it's fully working.

If you already have YAML, and it only contains a single resource, you can use the static Yaml class to deserialise it into a PodV1 model (or whichever model type is appropriate).

tintoy commented 5 years ago

BTW, if you have an existing YAML file, the models will more-or-less map one-to-one with the YAML (or JSON) structure used by kubectl.

MortenMeisler commented 5 years ago

Ok thanks a bunch, I think you linked the worng url to the server-side apply, that sounds cool. But i'll give it a try with the yaml deseriaze also. Nice library

MortenMeisler commented 5 years ago

And what would be the best approach to do the deserialize? I tried this, but it's not deserializing. I thought the input would be string type, but it's textreader, so not sure i'm doing it right.

Thanks again!

DeploymentSpecV1Beta1 deploymentSpecV1Beta1 = new DeploymentSpecV1Beta1();
            try
            {   
                using (TextReader sr = new StreamReader("C:\\Users\\Morten\\helloworld.yaml"))
                {

                    deploymentSpecV1Beta1 = Yaml.Deserialize<DeploymentSpecV1Beta1>(sr);

                }
            }
            catch (IOException e)
            {

                Console.WriteLine(e.Message);
            }
MortenMeisler commented 5 years ago

NEVERMIND, i pasted in the wrong model, should have been deployment not deploymentspec

DeploymentV1Beta1

tintoy commented 5 years ago

There is also another project on github called PSKubectl that uses this library - you may find more useful examples in there (especially around dynamically working with models from YAML).

tintoy commented 5 years ago

Hi - it sounds like this issue has been resolved, so I'm going to close it. But feel free to reopen if you disagree. Thanks for using the library! :)