Skip to content

Commit b96ff85

Browse files
committed
Add initial support for watch.
1 parent ecfaf5b commit b96ff85

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

examples/watch/watch.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2019 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'kubernetes'
16+
require 'pp'
17+
18+
config = Kubernetes::Configuration.default_config()
19+
client = Kubernetes::ApiClient.new(config)
20+
21+
watch = Kubernetes::Watch.new(client)
22+
23+
watch.connect('/api/v1/namespaces') do |obj|
24+
pp obj
25+
end

kubernetes/lib/kubernetes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require 'kubernetes/version'
1717
require 'kubernetes/configuration'
1818
require 'kubernetes/loader'
19+
require 'kubernetes/watch'
1920

2021
# Configuration
2122
require 'kubernetes/config/error'

kubernetes/src/kubernetes/watch.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2019 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'json'
16+
17+
# The Kubernetes module encapsulates the Kubernetes client for Ruby
18+
module Kubernetes
19+
class Watch
20+
def initialize(client)
21+
@client = client
22+
end
23+
24+
def connect(path, &block)
25+
opts = {
26+
:auth_names => ['BearerToken']
27+
}
28+
request = @client.build_request('GET', path + '?watch=true', opts)
29+
request.on_body do |chunk|
30+
parts = chunk.split(/\n/)
31+
parts.each do |part|
32+
obj = JSON.parse(part)
33+
yield obj
34+
end
35+
end
36+
response = request.run
37+
38+
pp response
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)