Skip to content

Commit

Permalink
Add support for API paging with limit and continue
Browse files Browse the repository at this point in the history
Implement API chunking support as specified in
https://kubernetes.io/docs/reference/using-api/api-concepts/#retrieving-large-results-sets-in-chunks

Add limit and continue as parameters to get_entities and get_entities
and add continue as an optional attribute (default to nil) to
EntityList.

Fixes: ManageIQ#283
  • Loading branch information
agrare committed Oct 17, 2018
1 parent 0b2c243 commit 79ef893
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/kubeclient/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ module ClientMixin

SEARCH_ARGUMENTS = {
'labelSelector' => :label_selector,
'fieldSelector' => :field_selector
'fieldSelector' => :field_selector,
'limit' => :limit,
'continue' => :continue,
}.freeze

WATCH_ARGUMENTS = { 'resourceVersion' => :resource_version }.merge!(SEARCH_ARGUMENTS).freeze
Expand Down Expand Up @@ -256,6 +258,8 @@ def watch_entities(resource_name, options = {})
# :namespace (string) - the namespace of the entity.
# :label_selector (string) - a selector to restrict the list of returned objects by labels.
# :field_selector (string) - a selector to restrict the list of returned objects by fields.
# :limit (integer) - a maximum number of items to return in each response
# :continue (string) - a token used to retrieve the next chunk of entities
# :as (:raw|:ros) - defaults to :ros
# :raw - return the raw response body as a string
# :ros - return a collection of RecursiveOpenStruct objects
Expand Down Expand Up @@ -456,10 +460,14 @@ def format_response(as, body, list_type = nil)
result.fetch('metadata', {}).fetch('resourceVersion', nil)
end

# If 'limit' was passed save the continue token
# see https://kubernetes.io/docs/reference/using-api/api-concepts/#retrieving-large-results-sets-in-chunks
continue = result.fetch('metadata', {}).fetch('continue', nil)

# result['items'] might be nil due to https://github.com/kubernetes/kubernetes/issues/13096
collection = result['items'].to_a.map { |item| Kubeclient::Resource.new(item) }

Kubeclient::Common::EntityList.new(list_type, resource_version, collection)
Kubeclient::Common::EntityList.new(list_type, resource_version, collection, continue)
else
Kubeclient::Resource.new(result)
end
Expand Down
5 changes: 3 additions & 2 deletions lib/kubeclient/entity_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module Kubeclient
module Common
# Kubernetes Entity List
class EntityList < DelegateClass(Array)
attr_reader :kind, :resourceVersion
attr_reader :continue, :kind, :resourceVersion

def initialize(kind, resource_version, list)
def initialize(kind, resource_version, list, continue = nil)
@kind = kind
# rubocop:disable Style/VariableName
@resourceVersion = resource_version
@continue = continue
super(list)
end
end
Expand Down

0 comments on commit 79ef893

Please sign in to comment.