How to create k8s deployment using kubernetes-client in c#?

5/9/2019

I'm getting Microsoft.Rest.HttpOperationException: 'Operation returned an invalid status code 'BadRequest'' on this line.

var result = client.CreateNamespacedDeployment(deployment, namespace);

Kubernetes-client has a small number of good resources and most of them is written in other language such as java and python. So i'm referring to these documentations.

this is my implementation so far.

        V1Deployment deployment = new V1Deployment()
        {
            ApiVersion = "extensions/v1beta1",
            Kind = "Deployment",
            Metadata = new V1ObjectMeta()
            {
                Name = "...",
                NamespaceProperty = env,
                Labels = new Dictionary<string, string>()
                {
                    { "app", "..." }
                }
            },
            Spec = new V1DeploymentSpec
            {
                Replicas = 1,
                Selector = new V1LabelSelector()
                {
                    MatchLabels = new Dictionary<string, string>
                    {
                        { "app", "..." }
                    }
                },
                Template = new V1PodTemplateSpec()
                {
                    Metadata = new V1ObjectMeta()
                    {
                        CreationTimestamp = null,
                        Labels = new Dictionary<string, string>
                        {
                             { "app", "..." }
                        }
                    },
                    Spec = new V1PodSpec
                    {
                        Containers = new List<V1Container>()
                        {
                            new V1Container()
                            {
                                Name = "...",
                                Image = "...",
                                ImagePullPolicy = "Always",
                                Ports = new List<V1ContainerPort> { new V1ContainerPort(80) }
                            }
                        }
                    }
                }
            },
            Status = new V1DeploymentStatus()
            {
                Replicas = 1    
            }
        };

        var result = client.CreateNamespacedDeployment(deployment, namespace);

I want to know the proper way on how to create kubernetes deployment using kubernetes-client, and also i want to know the cause of this issue.

-- PANDA MAN
.net
.net-core
asp.net
c#
kubernetes

2 Answers

5/10/2019

Closing this issue (Resolved)

Reference: https://github.com/Azure/autorest/issues/931

Cause of issue: incorrect version of Kubernetes ApiVersion.

Solution: get and replace ApiVersion from kubernetes api.

Can also handle the exception using:

        try
        {
            var result = client.CreateNamespacedDeployment(deployment, namespace);

        }
        catch (Microsoft.Rest.HttpOperationException httpOperationException)
        {
            var phase = httpOperationException.Response.ReasonPhrase;
            var content = httpOperationException.Response.Content;
        }
-- PANDA MAN
Source: StackOverflow

5/17/2019

For the full clarity and future visitors, it's worth to mention, what is exactly behind this bad request error (code: 400) returned from API server, when using your code sample:

"the API version in the data (extensions/v1beta1) does not match the expected API version (apps/v1)"

Solution:

 ApiVersion = "extensions/v1beta1" -> ApiVersion = "apps/v1"

Full code sample:

 private static void Main(string[] args)
        {
            var k8SClientConfig = new KubernetesClientConfiguration {  Host = "http://127.0.0.1:8080" };
            IKubernetes client = new Kubernetes(k8SClientConfig);

            ListDeployments(client);

            V1Deployment deployment = new V1Deployment()
            {
                ApiVersion = "apps/v1",
                Kind = "Deployment",
                Metadata = new V1ObjectMeta()
                {
                    Name = "nepomucen",
                    NamespaceProperty = null,
                    Labels = new Dictionary<string, string>()
                {
                    { "app", "nepomucen" }
                }
                },
                Spec = new V1DeploymentSpec
                {
                    Replicas = 1,
                    Selector = new V1LabelSelector()
                    {
                        MatchLabels = new Dictionary<string, string>
                    {
                        { "app", "nepomucen" }
                    }
                    },
                    Template = new V1PodTemplateSpec()
                    {
                        Metadata = new V1ObjectMeta()
                        {
                            CreationTimestamp = null,
                            Labels = new Dictionary<string, string>
                        {
                             { "app", "nepomucen" }
                        }
                        },
                        Spec = new V1PodSpec
                        {
                            Containers = new List<V1Container>()
                        {
                            new V1Container()
                            {
                                Name = "nginx",
                                Image = "nginx:1.7.9",
                                ImagePullPolicy = "Always",
                                Ports = new List<V1ContainerPort> { new V1ContainerPort(80) }
                            }
                        }
                        }
                    }
                },
                Status = new V1DeploymentStatus()
                {
                    Replicas = 1
                }
            };
-- Nepomucen
Source: StackOverflow