Create GenericKubernetesResource from file without ResourceDefinitionContext #3941
-
Hi, I'm looking for a simple way to resemble something like Is there any way to let fabric8 Kubernetes client create that context under the hood? If not, how can this be done programmatically? How does kubectl manage this? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
This is supported by 5.11+. See the issues referenced with #3547. |
Beta Was this translation helpful? Give feedback.
-
Just to provide an example: kubernetesClient.resource("you-resource-as-yaml").createOrReplace();
kubernetesClient.load(yourResourceAsInputStream).createOrReplace(); The client will infer your context by querying the cluster for the APIs responsible of the provided resource Kind+apiVersion. Edit (2022-03-12): As suggested by @shawkins later in this discussion, in case you are testing this implementation with our KubernetesMockServer, then you'd need to setup some expectations for your API (if your YAML contains an entry for a non-core resource): ResourceDefinitionContext rdc = new ResourceDefinitionContext.Builder()
.withGroup("test.fabric8.io")
.withVersion("v1alpha1")
.withKind("hello")
.withNamespaced(true)
.withPlural("hellos")
.build();
server.expect()
.get()
.withPath(String.format("/apis/%s/%s", rdc.getGroup(), rdc.getVersion()))
.andReturn(HttpURLConnection.HTTP_OK,
new APIResourceListBuilder()
.withResources(
new APIResourceBuilder().withKind(rdc.getKind())
.withNamespaced(rdc.isNamespaceScoped())
.withName(rdc.getPlural())
.build())
.build())
.once(); |
Beta Was this translation helpful? Give feedback.
-
I'm going to edit my answer to include Steven's snippet so you can get a complete answer without going through the discussion. |
Beta Was this translation helpful? Give feedback.
-
Thanks! This answer is pretty helpful. Could we add this to the document? |
Beta Was this translation helpful? Give feedback.
This is supported by 5.11+. See the issues referenced with #3547.