kubernetes-client/java

`Kubectl.get(V1EndpointSlice.class)` failed due to `endpoints: null`

fanyang01 opened this issue · 3 comments

Describe the bug

It is possible that a service does not have any endpoint, for example, when the corresponding deployment was scaled to zero. In this case, the EndpointSlice API returns endpoints: null. However, it will be rejected by the following validation rule:

// ensure the json data is an array
if (!jsonObj.get("endpoints").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `endpoints` to be an array in the JSON string but got `%s`", jsonObj.get("endpoints").toString()));
}

Client Version
20.0.1

Kubernetes Version
1.29.0-eks-c417bb3

Java Version
Java 21

To Reproduce
Steps to reproduce the behavior:

  • Create a service that cannot select any pod:
kubectl apply -f - <<EOF
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app.kubernetes.io/name: proxy
  ports:
  - name: name-of-service-port
    protocol: TCP
    port: 80
    targetPort: http-web-svc
EOF
  • The endpoints field will be null:
k get endpointslice nginx-service-t786l -o yaml
addressType: IPv4
apiVersion: discovery.k8s.io/v1
endpoints: null
kind: EndpointSlice
...

Expected behavior

Kubectl.get(V1EndpointSlice.class) would not throw IllegalArgumentException.

Server (please complete the following information):

  • OS: Bottlerocket
  • Environment: container
  • Cloud: AWS

Personally, I think this is an issue with how Go handles encoding empty arrays (https://medium.com/swlh/arrays-and-json-in-go-98540f2fa74e)

In practice, if you want to fix this, it needs to be in the upstream code generator, since this parsing is generated by that code generator.

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@brendandburns Thanks for your comment. I am closing this issue. Here is my workaround:

            List<V1EndpointSlice> endpointSlices;
            try {
                endpointSlices = Kubectl.get(V1EndpointSlice.class)
                        .apiClient(client.apiClient())
                        .namespace(namespace)
                        .options(options)
                        .execute();
            } catch (IllegalArgumentException e) {
                String message = e.getMessage();
                if (message != null && message.contains("`endpoints`")) {
                    endpointSlices = null;
                } else {
                    throw e;
                }
            }