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

non-generic Watch for CRD's without Type #1413

Closed
mmercan opened this issue Sep 28, 2023 · 4 comments
Closed

non-generic Watch for CRD's without Type #1413

mmercan opened this issue Sep 28, 2023 · 4 comments

Comments

@mmercan
Copy link

mmercan commented Sep 28, 2023

Describe the bug

I have several CRDs which I don't have types. I have a simple function it lists them as JToken then, rest of the application validate the configuration over JsonPath base rule engine.

`

public async Task<List<JToken>> ListClusterCustomObject(string Group, string Version, string Plural)
{
    var result = await _client.CustomObjects.ListClusterCustomObjectWithHttpMessagesAsync(Group, Version, Plural);

    var jToken = JToken.Parse(result.Body.ToString());
    var res = jToken?.SelectToken("items")?.ToList();
    if (res == null)
    {
        res = new List<JToken>();
    }
    return res;
}

`

A clear and concise description of what the bug is.

I am trying to create a similar function over Watch I have tried several methods and I have failed.
is there any method I can use for Watch which can return Response Body then I can convert it to JToken ?
or do you have any suggestion on how to work with Watch without having POCO classes for the CRD types.

Kubernetes C# SDK Client Version
e.g. 10.0.31

Server Kubernetes Version
e.g. 1.26.3

Dotnet Runtime Version
e.g. net7

To Reproduce
Steps to reproduce the behavior:

Expected behavior
A clear and concise description of what you expected to happen.

KubeConfig
If applicable, add a KubeConfig file with secrets redacted.

Where do you run your app with Kubernetes SDK (please complete the following information):

  • OS: [Windows 10]
  • Environment [e.g. container]
  • Cloud [e.g. Azure]

Additional context
Add any other context about the problem here.

@IvanJosipovic
Copy link
Contributor

JToken is a Newtonsoft.Json object, this library uses System.Text.Json, you can try using JsonDocument and JsonElement.

Alternatively, you can generate types for the CRDs using https://github.com/IvanJosipovic/KubernetesCRDModelGen

@mmercan
Copy link
Author

mmercan commented Sep 28, 2023

If I could receive the result.Body (string) over the Watch I could convert it to JToken inside of the my method
JsonDocument and JsonElement still doesn't support JsonPath (dotnet/runtime#31068)
is there any way of working with Watch without Deserialization ?

@tg123
Copy link
Member

tg123 commented Sep 29, 2023

.ListClusterCustomObjectWithHttpMessagesAsync<dynamic>?

@mmercan mmercan changed the title non-generic Watch for CRD's with out Type non-generic Watch for CRD's without Type Sep 30, 2023
@mmercan
Copy link
Author

mmercan commented Oct 4, 2023

Thanks for everyone's help and suggestions, even if I have to take JsonElement and convert to JToken
this method solved my issues.

    Action<WatchEventType, object> onEvent = (a, b) =>
    {
        if (b is JsonElement content)
        {
            JToken token = JToken.Parse(content.ToString());
            Console.WriteLine(token.ToString());
        }
    };
    Action<Exception>? onError = null;
    Action? onClose = null;


    var result = await client.CustomObjects.ListClusterCustomObjectWithHttpMessagesAsync(Group, Version, Plural, watch: true);
    await Task.FromResult(
                 result.Watch(onEvent, onError, onClose)
     );

@mmercan mmercan closed this as completed Oct 4, 2023
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

3 participants