Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/watch/watch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'kubernetes'
require 'pp'

config = Kubernetes::Configuration.default_config()
client = Kubernetes::ApiClient.new(config)

watch = Kubernetes::Watch.new(client)

watch.connect('/api/v1/namespaces') do |obj|
pp obj
end
1 change: 1 addition & 0 deletions kubernetes/lib/kubernetes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require 'kubernetes/version'
require 'kubernetes/configuration'
require 'kubernetes/loader'
require 'kubernetes/watch'

# Configuration
require 'kubernetes/config/error'
Expand Down
36 changes: 36 additions & 0 deletions kubernetes/spec/watch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'spec_helper'
require 'kubernetes/watch'

describe 'WatchClient' do
it 'should construct correctly' do
Kubernetes::Watch.new(nil)
end

it 'should parse chunks correctly' do
client = Kubernetes::Watch.new(nil)
last = ''

last, data = client.split_lines(last, "foo\nbar\nba")

expect(last).to eq('ba')
expect(data).to eq(%w[foo bar])

last, data = client.split_lines(last, "z\nblah\n")
expect(last).to eq('')
expect(data).to eq(%w[baz blah])
end
end
51 changes: 51 additions & 0 deletions kubernetes/src/kubernetes/watch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'json'

# The Kubernetes module encapsulates the Kubernetes client for Ruby
module Kubernetes
# The Watch class provides the ability to watch a specific resource for
# updates.
class Watch
def initialize(client)
@client = client
end

def connect(path, &_block)
opts = { auth_names: ['BearerToken'] }
request = @client.build_request('GET', path + '?watch=true', opts)
last = ''
request.on_body do |chunk|
last, pieces = split_lines(last, chunk)
pieces.each do |part|
yield JSON.parse(part)
end
end
request.run
end

def split_lines(last, chunk)
data = chunk
data = last + '' + data

ix = data.rindex("\n")
return [data, []] unless ix

complete = data[0..ix]
last = data[(ix + 1)..data.length]
[last, complete.split(/\n/)]
end
end
end