diff --git a/examples/watch/watch.rb b/examples/watch/watch.rb new file mode 100644 index 00000000..f996d8a7 --- /dev/null +++ b/examples/watch/watch.rb @@ -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 \ No newline at end of file diff --git a/kubernetes/lib/kubernetes.rb b/kubernetes/lib/kubernetes.rb index 8f841c3f..b1f78d93 100644 --- a/kubernetes/lib/kubernetes.rb +++ b/kubernetes/lib/kubernetes.rb @@ -16,6 +16,7 @@ require 'kubernetes/version' require 'kubernetes/configuration' require 'kubernetes/loader' +require 'kubernetes/watch' # Configuration require 'kubernetes/config/error' diff --git a/kubernetes/spec/watch_spec.rb b/kubernetes/spec/watch_spec.rb new file mode 100644 index 00000000..7c3500ef --- /dev/null +++ b/kubernetes/spec/watch_spec.rb @@ -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 diff --git a/kubernetes/src/kubernetes/watch.rb b/kubernetes/src/kubernetes/watch.rb new file mode 100644 index 00000000..80e616a3 --- /dev/null +++ b/kubernetes/src/kubernetes/watch.rb @@ -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