Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 21, 2025

Implement YAML resource creation without type differentiation

This PR addresses the issue of submitting arbitrary YAML resources to Kubernetes without having to differentiate between Kind types, similar to kubectl create -f file.yaml.

Implementation Complete

  • Add new Yaml.createResource() methods that accept arbitrary YAML and creates resources using the generic client
  • Extract apiVersion and kind from unstructured YAML
  • Use ModelMapper to determine the strongly typed object class
  • Load YAML into the strongly typed object using the same content (vs dump/reload)
  • Use GenericKubernetesApi to create the resource in the cluster
  • Add comprehensive tests for the new functionality
  • Run full test suite to ensure backward compatibility

Technical Implementation

The solution implements three new public methods in the Yaml class:

  • Yaml.createResource(ApiClient, String) - Create from YAML string
  • Yaml.createResource(ApiClient, File) - Create from YAML file
  • Yaml.createResource(ApiClient, Reader) - Create from YAML stream

These methods:

  1. Read the YAML content into a string buffer once
  2. Parse it first as a Map to extract apiVersion/kind
  3. Use ModelMapper to determine the right strongly typed object class
  4. Parse the same content again into the strongly-typed object
  5. Use GenericKubernetesApi configured with the group/version/kind to create the resource
  6. Handle both namespaced and cluster-scoped resources appropriately

Usage Example

ApiClient client = Config.defaultClient();

String yaml = 
    "apiVersion: v1\n" +
    "kind: ConfigMap\n" +
    "metadata:\n" +
    "  name: my-config\n" +
    "  namespace: default\n" +
    "data:\n" +
    "  key: value\n";

// Automatically determines type and creates resource
Object resource = Yaml.createResource(client, yaml);
Original prompt

This section details on the original issue you should resolve

<issue_title>how to submit a yaml like kubectl create -f file.yaml ?</issue_title>
<issue_description>Is there a method to submit YAML resources of any type to Kubernetes ?
What I mean is not having to differentiate between Kind types.</issue_description>

<agent_instructions>Extend the kubectl create implementation to take arbitrary yaml and use the generic client to create that resource</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@brendandburns This is not currently possible. You would need to modify the `Yaml` class here:

https://github.com/kubernetes-client/java/blob/master/util/src/main/java/io/kubernetes/client/util/Yaml.java

To load unstructured YAML and then pick out the API group/version/kind and then use the ModelMapper here:
https://github.com/kubernetes-client/java/blob/master/util/src/main/java/io/kubernetes/client/util/ModelMapper.java

To determine the right strongly typed object, and then use Yaml.load again to reload the YAML into that strongly typed object.

Then you would need to pass that object to a GenericApi handler that was configured with the group/version/kind that you found above.

So it's possible, but it will require you to write a bunch of code. If you do, do that, feel free to send a PR.</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 21, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Copilot
Once this PR has been reviewed and has the lgtm label, please ask for approval from brendandburns. For more information see the 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

1 similar comment
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Copilot
Once this PR has been reviewed and has the lgtm label, please ask for approval from brendandburns. For more information see the 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

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 21, 2025
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 21, 2025
Copilot AI changed the title [WIP] Extend kubectl create to submit arbitrary YAML resources Add Yaml.createResource() for type-agnostic resource creation from YAML Nov 21, 2025
Copilot AI requested a review from brendandburns November 21, 2025 04:41
Copilot finished work on behalf of brendandburns November 21, 2025 04:41
Copy link
Contributor

@brendandburns brendandburns left a comment

Choose a reason for hiding this comment

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

@copilot Generally looks good. Please remove the markdown doc which is unnecessary and address this comment.

// Load the YAML into the strongly typed object
// Note: This double-loading approach (first as Map, then as typed object) follows the
// design recommended in the issue discussion to properly handle type determination
Object resource = loadAs(new StringReader(getSnakeYaml(clazz).dump(data)), clazz);
Copy link
Contributor

Choose a reason for hiding this comment

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

Load the Reader into a byte array at the beginning of this function and use the same byte array in both cases vs dumping it back out to YAML.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 1caa15d. Now reads the Reader into a string buffer first and uses the same content for both parsing (to extract apiVersion/kind) and loading into the typed object.

Copy link
Contributor Author

Copilot AI commented Nov 26, 2025

@copilot Generally looks good. Please remove the markdown doc which is unnecessary and address this comment.

Done in 1caa15d - removed the markdown doc and addressed the code review comment.

Copilot AI requested a review from brendandburns November 26, 2025 22:44
Copilot finished work on behalf of brendandburns November 26, 2025 22:44
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. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

how to submit a yaml like kubectl create -f file.yaml ?

3 participants