golang get kubernetes resources(30000+ configmaps) failed

5/8/2020

I want to use client-go to get resources in Kubernetes cluster. Due to a large amount of data, when I get the configmap connection is closed.

stream error when reading response body, may be caused by closed connection. Please retry. Original error: stream error: stream ID 695; INTERNAL_ERROR

configmaps:

$ kubectl -n kube-system get cm |wc -l
   35937

code:

cms, err := client.CoreV1().ConfigMaps(kube-system).List(context.TODO(), v1.ListOptions{})

I try to use Limit parameter, I can get some data, but I don’t know how to get all.

cms, err := client.CoreV1().ConfigMaps(kube-system).List(context.TODO(), v1.ListOptions{Limit: 1000 })

I'm new to Go. Any pointers as to how to go about it would be greatly appreciated.

-- cainiaohhh
go
kubernetes

1 Answer

5/8/2020

The documentation for v1.ListOptions describes how it works:

limit is a maximum number of responses to return for a list call. If more items exist, the server will set the continue field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results.

This means that you should examine the response, save the value of the continue field (as well as the actual results), then reissue the same command but with continue set to the just seen value. Repeat until the returned continue field is empty (or an error occurs).

See the API concepts page for details on handling chunking of large results.

-- Marc
Source: StackOverflow