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

Question: How to setup the client to access cluster wide (custom) resources #4800

Closed
MartijnStraatman opened this issue Jan 27, 2023 · 6 comments · Fixed by #4816
Closed

Comments

@MartijnStraatman
Copy link

MartijnStraatman commented Jan 27, 2023

I added a controller to our Kubernetes cluster which operates at the cluster wide level. I want to create object with this client.
So I created java code from the controller's crd's.

When i try to create an object, the client seems to be namespace-scoped by default.
For example:
https://xxxx:xxxx/apis/capsule.clastix.io/v1beta1/namespaces/default/tenants

This returns a 404 because this path does not exist at the api-server.

I expect it to be:
https://xxxx:xxxx/apis/capsule.clastix.io/v1beta1/tenants

How can I instrument the client so it won't use the namespace while creating the url.

        KubernetesClient client = new KubernetesClientBuilder().build();

        MixedOperation<io.clastix.capsule.v1beta1.Tenant,
                KubernetesResourceList<io.clastix.capsule.v1beta1.Tenant>, Resource<io.clastix.capsule.v1beta1.Tenant>>
                tenantClient = client.resources(io.clastix.capsule.v1beta1.Tenant.class);
       
       .....
         
       Object bla = tenantClient.resource(t).create();

I noticed while creating the java classes with the java-generator-cli-6.4.0.sh the Tenant class implements the io.fabric8.kubernetes.api.model.Namespaced interface. If I remove the interface, things work as expected..

Thx

@MartijnStraatman MartijnStraatman changed the title Question: How to setup the client to access cluster wide resources Question: How to setup the client to access cluster wide (custom) resources Jan 27, 2023
@andreaTP
Copy link
Member

Hi @MartijnStraatman , sorry for the delay, I missed this issue.
With a script like the:

#! /bin/bash
set -x

CRD=$1
CRD_NAME="${2:-default}"

# .XXXXXXX makes it cross-compatible Mac/Linux
TMP_DIR=$(mktemp -d -t $CRD_NAME.XXXXXXX)

cp ${CRD} $TMP_DIR/$CRD_NAME.yaml
mkdir -p $TMP_DIR/src

jbang io.fabric8:java-generator-cli:6.4.1 -u https://raw.githubusercontent.com/clastix/capsule/master/charts/capsule/crds/tenant-crd.yaml --target=$TMP_DIR/src

cat <<EOF >> $TMP_DIR/$CRD_NAME.java
//DEPS io.fabric8:kubernetes-client:6.2.0
//DEPS io.fabric8:generator-annotations:6.2.0
//DEPS io.sundr:builder-annotations:0.90.4
//DEPS org.projectlombok:lombok:1.18.24
//DEPS io.sundr:builder-annotations:0.90.4
//DEPS javax.validation:validation-api:2.0.1.Final
//SOURCES src/**.java
EOF

(
  cd $TMP_DIR
  jbang export portable --force $TMP_DIR/$CRD_NAME.java
  jbang --class-path $TMP_DIR/$CRD_NAME.jar -i
)

rm -rf $TMP_DIR

you have access to a shell session where you can reproduce the issue:

import io.fabric8.kubernetes.client.*
var client = new KubernetesClientBuilder().build();
client.resources(io.clastix.capsule.v1beta1.Tenant.class).create(new io.clastix.capsule.v1beta1.Tenant())

You get the following StackTrace:

|  Exception io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: POST at: https://127.0.0.1:49635/apis/capsule.clastix.io/v1beta1/namespaces/default/tenants. Message: Not Found.
|        at KubernetesClientException.copyAsCause (KubernetesClientException.java:238)
|        at OperationSupport.waitForResult (OperationSupport.java:517)
|        at OperationSupport.handleResponse (OperationSupport.java:551)
|        at OperationSupport.handleResponse (OperationSupport.java:535)
|        at OperationSupport.handleCreate (OperationSupport.java:328)
|        at BaseOperation.handleCreate (BaseOperation.java:675)
|        at BaseOperation.handleCreate (BaseOperation.java:88)
|        at CreateOnlyResourceOperation.create (CreateOnlyResourceOperation.java:42)
|        at (#3:1)
|  Caused by: io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: POST at: https://127.0.0.1:49635/apis/capsule.clastix.io/v1beta1/namespaces/default/tenants. Message: Not Found.
|        at OperationSupport.requestFailure (OperationSupport.java:709)
|        at OperationSupport.requestFailure (OperationSupport.java:689)
|        at OperationSupport.assertResponseCode (OperationSupport.java:640)
|        at OperationSupport.lambda$handleResponse$0 (OperationSupport.java:576)
|        at CompletableFuture$UniApply.tryFire (CompletableFuture.java:642)
|        at CompletableFuture.postComplete (CompletableFuture.java:506)
|        at CompletableFuture.complete (CompletableFuture.java:2073)
|        at OperationSupport.lambda$retryWithExponentialBackoff$2 (OperationSupport.java:618)
|        at CompletableFuture.uniWhenComplete (CompletableFuture.java:859)
|        at CompletableFuture$UniWhenComplete.tryFire (CompletableFuture.java:837)
|        at CompletableFuture.postComplete (CompletableFuture.java:506)
|        at CompletableFuture.complete (CompletableFuture.java:2073)
|        at OkHttpClientImpl$4.onResponse (OkHttpClientImpl.java:277)
|        at RealCall$AsyncCall.execute (RealCall.java:203)
|        at NamedRunnable.run (NamedRunnable.java:32)
|        at ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1128)
|        at ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:628)
|        at Thread.run (Thread.java:829)

cc. @manusa @shawkins

@manusa manusa added the bug label Jan 31, 2023
@manusa
Copy link
Member

manusa commented Jan 31, 2023

clz.addImplementedType("io.fabric8.kubernetes.api.model.Namespaced");

Should not always be added only for those CRDs with scope Namespaced

@andreaTP
Copy link
Member

Thanks @manusa , yes the bug has been identified, will fix it in a while.

@MartijnStraatman
Copy link
Author

thx!!

@andreaTP
Copy link
Member

Thank you for reporting! Don't know how we could have missed it 😅

@MartijnStraatman
Copy link
Author

MartijnStraatman commented Jan 31, 2023

We are building some kind of self-service api for users to create tenants on multi-tenant k8s clusters. This is a clusterscoped resource, hence we stumbled upon it.

@manusa manusa added this to the 6.5.0 milestone Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants