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

Load any Submit resources without mentioning kind types and api group/versions #3336 #3744

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

vaidikcode
Copy link

#3336 As described I tried to follow your approach and created a method to load and submit resources and then passed it on to the Api handler
I don't know where to mention the use case example-

public static void main(String[] args) throws Exception {

String yamlContent = "apiVersion: v1\n"
        + "kind: Pod\n"
        + "metadata:\n"
        + "  name: my-pod\n";


Object createdResource = Yaml.loadAndSubmitResource(yamlContent);


System.out.println(createdResource);

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: vaidikcode
Once this PR has been reviewed and has the lgtm label, please assign yue9944882 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

linux-foundation-easycla bot commented Oct 17, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Oct 17, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @vaidikcode!

It looks like this is your first PR to kubernetes-client/java 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/java has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 17, 2024
@yue9944882
Copy link
Member

@vaidikcode thank you for the contribution. the Yaml.java file where the change is proposed is designed/dedicated for yaml content [de]serialization. i think a better place to bring this enhancement in is the Kubectl related utilities under the dynamic package e.g.:

https://github.com/kubernetes-client/java/blob/master/util/src/main/java/io/kubernetes/client/util/generic/dynamic/Dynamics.java#L44-L54

We can add a short-cut util function under Dynamics probably named createFromYaml to achieve that functionality, wdyt?

@vaidikcode
Copy link
Author

@yue9944882 Yes, I think it would be much better. Can you elaborate on how I can do this? Can you also tell me if I need to include an example code to use this method, and if yes, please tell me where to include the example code.

import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove wildcard imports.

* @return An instantiation of the object.
* @throws IOException If an error occurs while reading the YAML.
*/
public static Object loadAndSubmitResource(String content) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more flexible to load from a Reader rather than a String you can always create a StringReader

//loading yaml content as unstructured object
Object unstructuredObject = Yaml.load(new StringReader(content));

if (!(unstructuredObject instanceof Map)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, this will only work for single-instance YAML files, if people put multiple objects in a single file, it will fail. That's fine for a starting point, but you may want to document it somewhere.

Object unstructuredObject = Yaml.load(new StringReader(content));

if (!(unstructuredObject instanceof Map)) {
throw new IllegalArgumentException("Invalid YAML");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ParseException might be more appropriate? (here and below)

String version;

// If apiVersion contains '/', then it means there is a specified group for it and we need to get that else it is a default group
if (apiVersion.contains("/")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is already present here:

public static GroupVersion parse(String apiVersion) {

Let's re-use that instead of reimplementing.

version = apiVersion;
}

//creating resource plurals from kind
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the Discovery class here instead

String resourcePlural = kind.toLowerCase(Locale.ROOT) + "s"; // Simplistic pluralization logic

//getting the strongly typed object from model mapper
Class<?> modelClass = ModelMapper.getApiTypeClass(group, version, kind);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can say Class<? implements KubernetesObject> here?

@brendandburns
Copy link
Contributor

Some style comments. Generally this code looks good to me.

Please add unit tests.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Oct 19, 2024
@vaidikcode
Copy link
Author

@brendandburns I've made the changes to the code and added unit tests, just as you suggested.

@brendandburns
Copy link
Contributor

@vaidikcode looks good to me. I agree with @yue9944882 that this is a better fit for KubectlCreate.java but otherwise ready to merge once you move it there.

@brendandburns
Copy link
Contributor

Alternately, feel free to keep the loader code in Yaml.java but move the create code to KubectlCreate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants