-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Conversation
|
Welcome @vaidikcode! |
@vaidikcode thank you for the contribution. the We can add a short-cut util function under |
@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.*; |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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("/")) { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
public class Discovery { |
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); |
There was a problem hiding this comment.
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?
Some style comments. Generally this code looks good to me. Please add unit tests. |
@brendandburns I've made the changes to the code and added unit tests, just as you suggested. |
@vaidikcode looks good to me. I agree with @yue9944882 that this is a better fit for |
Alternately, feel free to keep the loader code in |
// Finding the resource in the discovery class | ||
Set<Discovery.APIResource> resources = discovery.findAll(); | ||
Optional<Discovery.APIResource> apiResource = resources.stream() | ||
.filter(r -> r.getKind().equalsIgnoreCase(kind)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to check group/version here also. There are a few Kinds which are duplicates in several different api groups :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #3668 (comment)
Core.V1Event vs Events.V1Event
@@ -342,27 +342,27 @@ protected MappingNode representJavaBean(Set<Property> properties, Object javaBea | |||
node.setTag(Tag.MAP); | |||
// Sort the output of our map so that we put certain keys, such as apiVersion, first. | |||
Collections.sort( | |||
node.getValue(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these are all whitespace changes. please revert.
Thanks for the update. One change is required for places where kind is the same across multiple API types. Also request to revert whitespace only changes on yaml.java Thanks for the patience! |
4a0dde2
to
b218eb4
Compare
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: vaidikcode 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 |
#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 {