Skip to content

Can you explain how to work this client? #78

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

Closed
RouR opened this issue Jan 7, 2018 · 7 comments · Fixed by #84
Closed

Can you explain how to work this client? #78

RouR opened this issue Jan 7, 2018 · 7 comments · Fixed by #84

Comments

@RouR
Copy link

RouR commented Jan 7, 2018

I have some config

apiVersion: apps/v1beta2 
kind: Deployment
metadata:
  name: web-deployment
  labels:
    app: web
spec:
  template:
    metadata:
      labels:
        app: web
        ci: somedataForOverride        

And I write this code, dokerize it and deploy to cluster with env KUBERNETES_SERVICE_HOST="kubernetes.default.svc.cluster.local" KUBERNETES_SERVICE_PORT="443"

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

            var list = client.ListNamespacedPod("default");
            foreach (var item in list.Items)
            {
                Console.WriteLine(item.Metadata.Name);
            }
            if (list.Items.Count == 0)
            {
                Console.WriteLine("Empty!");
            }

            Console.WriteLine("ListNamespacedDeployment!");
            foreach (var item in client.ListNamespacedDeployment("default").Items)
            {
                Console.WriteLine($"Name {item.Metadata.Name}");
                foreach (var label in item.Spec.Template.Metadata.Labels)
                {
                    Console.WriteLine($"Label {label.Key} = {label.Value}");
                    if (label.Key == "ci")
                    {
                        var newVal = "newdata";
                        Console.WriteLine($"\t\t\tFound! try set to {newVal}");
                        item.Spec.Template.Metadata.Labels.Add("ci2", "newVal");
                        client.PatchNamespacedDeployment(new
                        {
                            op = "replace",
                            path = "/spec/template/metadata/labels/"+ label.Key,
                            value = newVal
                        },item.Metadata.Name, "default"); //fail!
                    }
                }
                foreach (var container in item.Spec.Template.Spec.Containers)
                {
                    Console.WriteLine($"Image {container.Image}");
                }
            }

And I get

2018-01-07T21:03:26.886868069Z Starting Request!
2018-01-07T21:03:27.398606591Z tool-deployment-664848d469-fdfqp
2018-01-07T21:03:27.398636889Z web-deployment-7d87b9fb8-r24c2
2018-01-07T21:03:27.398642596Z web-deployment-7d87b9fb8-s96d6
2018-01-07T21:03:27.398647321Z ListNamespacedDeployment!
2018-01-07T21:03:27.464030335Z Name tool-deployment
2018-01-07T21:03:27.464060924Z Label app = tool
2018-01-07T21:03:27.464066364Z Label ddd = dd6rty6
2018-01-07T21:03:27.464430225Z Image skipeddata/skipeddata:dev
2018-01-07T21:03:27.464444583Z Name web-deployment
2018-01-07T21:03:27.46444943Z Label app = web
2018-01-07T21:03:27.464453986Z Label ci = somedataForOverride
2018-01-07T21:03:27.464458516Z Found! try set to newdata
2018-01-07T21:03:47.549478912Z
2018-01-07T21:03:47.577526422Z Unhandled Exception: Microsoft.Rest.HttpOperationException: Operation returned an invalid status code 'InternalServerError'
2018-01-07T21:03:47.57769817Z at k8s.Kubernetes.<PatchNamespacedDeploymentWithHttpMessagesAsync>d__321.MoveNext()
2018-01-07T21:03:47.577706886Z --- End of stack trace from previous location where exception was thrown ---

@tg123
Copy link
Member

tg123 commented Jan 8, 2018

seems PatchNamespacedDeployment is not working as except, I will have a look at that api.

@brendandburns
Copy link
Contributor

@tg123 I think this is the same underlying issue as this:

kubernetes-client/java#127

@tg123
Copy link
Member

tg123 commented Jan 12, 2018

@RouR sorry for the late reply, just another busy week

Your approach is right and there is a tricky thing in generated client:
"will serialize all anonymous into {}"
due to default SerializationSettings

SerializationSettings = new JsonSerializerSettings
            {
               .........
                ContractResolver = new ReadOnlyJsonContractResolver(),
               .......
            };

workarounds might be:

here is my tested using JsonPatch which is trying to add another label to pod.

                var newlables = new Dictionary<string, string>(p.Metadata.Labels)
                {
                    ["test"] = "test"
                };
                var patch = new JsonPatchDocument<V1Pod>();
                patch.Replace(e => e.Metadata.Labels, newlables);
                client.PatchNamespacedPod(patch, n, "default");

@brendanburns
We need to support all 3 kinds of PatchType in the client

  • JSONPatchType
  • MergePatchType
  • StrategicMergePatchType

howerver the generated client, like Java client, treat all body object as JSONPatchType.
I will try to find a better way to support Patch calls

@RouR
Copy link
Author

RouR commented Jan 13, 2018

Hmm,
As I see Headers is forced to "application/json-patch+json; charset=utf-8", but it`s work (I check in debugger headers)

_httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json-patch+json; charset=utf-8");

I use Microsoft.AspNetCore.JsonPatch instead of https://github.com/aaxelm/JsonPatch.NetStandard
with this code:

var newlables = new Dictionary<string, string>(item.Spec.Template.Metadata.Labels)
{
	[ciLabel.Key] = "newData" 
};
var patch = new JsonPatchDocument<Appsv1beta1Deployment>();
patch.Replace(e => e.Spec.Template.Metadata.Labels, newlables);
client.PatchNamespacedDeployment(patch, item.Metadata.Name, k8Namespace);

The type Appsv1beta1Deployment is not defined in NuGet-package KubernetesClient 0.3.0-beta
Please, update NuGet
Thank`s for the help!

RouR added a commit to RouR/ToDo-ToBuy that referenced this issue Jan 13, 2018
@RouR
Copy link
Author

RouR commented Mar 9, 2018

Hi!
I took the latest version today.
I see breaking changes - PatchNamespacedDeployment require V1Patch object.

Can you explain how to use it? Or provide some example - how to change label in deployment specification?

@tg123
Copy link
Member

tg123 commented Mar 10, 2018

using Microsoft.AspNetCore.JsonPatch;

var newlables = new Dictionary<string, string>(p.Metadata.Labels)
{
    ["test"] = "test"
};
var patch = new JsonPatchDocument<V1Pod>();
patch.Replace(e => e.Metadata.Labels, newlables);
client.PatchNamespacedPod(new V1Patch(patch), n, "default");

@RouR
Copy link
Author

RouR commented Mar 11, 2018

Thanks!

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

Successfully merging a pull request may close this issue.

3 participants