ruby-kubernetes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db8cdb80bed9318ec62f482b334c588f325f0ec1
4
+ data.tar.gz: 8c381395c992ab57695d6e61df91827d21c7ceec
5
+ SHA512:
6
+ metadata.gz: 0b50d5c382f1a06bf7cd156c935968464ad744799b7dfdea17c55f36e7b3c43259ba18c0602be1f36ef1db1b9f56b11e87ad67697317c3eb6745892b12a503b1
7
+ data.tar.gz: b6dde0435990b7990ceaf99442bc797fc362816ab0f8b84da6140395e6b036ed1c2affcabab1097a51871fb8e564ca5a2eeabec110cb5de5f53277ae41ffa0c4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kubernetes.gemspec
4
+ gemspec
@@ -0,0 +1,59 @@
1
+ # Kubernetes
2
+
3
+ A client library for [Kubernetes](http://kubernetes.io/) in Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'kubernetes'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install kubernetes
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'kubernetes'
25
+
26
+ # Assumes you're running a local proxy or SSH tunnel.
27
+ client = Kubernetes.new
28
+
29
+ # Connect to server from within a pod:
30
+ token = File.read('/var/run/secrets/kubernetes.io/serviceaccount/token')
31
+ client = Kubernetes.new({
32
+ connection: {
33
+ host: 'https://kubernetes',
34
+ headers: { Authorization: "Bearer #{token}"},
35
+ ssl_verify_peer: false }})
36
+
37
+ # Lists all pods:
38
+ client.get_pods
39
+
40
+ # Lists all replication controllers:
41
+ client.get_replication_controllers
42
+
43
+ # Get the logs for a specific pod:
44
+ client.logs("some-pod")
45
+ ```
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it ( https://github.com/[my-github-username]/kubernetes/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kubernetes"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kubernetes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-kubernetes"
8
+ spec.version = Kubernetes::VERSION
9
+ spec.authors = ["Daniel Schierbeck"]
10
+ spec.email = ["dasch@zendesk.com"]
11
+
12
+ spec.summary = %q{A basic K8s interface gem}
13
+ spec.description = %q{A basic K8s interface gem}
14
+ spec.homepage = "https://github.com/dasch/ruby-kubernetes"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,9 @@
1
+ require 'kubernetes/version'
2
+
3
+ module Kubernetes
4
+ def self.new(*args)
5
+ Client.new(*args)
6
+ end
7
+ end
8
+
9
+ require 'kubernetes/client'
@@ -0,0 +1,117 @@
1
+ require 'json'
2
+ require 'kubernetes/connection'
3
+ require 'kubernetes/pod'
4
+ require 'kubernetes/replication_controller'
5
+ require 'kubernetes/namespace'
6
+ require 'kubernetes/stream'
7
+ require 'kubernetes/watch_event'
8
+ require 'kubernetes/status'
9
+
10
+ module Kubernetes
11
+ class Error < StandardError
12
+ attr_reader :status
13
+
14
+ def initialize(status:)
15
+ super(status.message)
16
+
17
+ @status = status
18
+ end
19
+ end
20
+
21
+ class Client
22
+ DEFAULT_NAMESPACE = "default".freeze
23
+
24
+ def initialize(namespace: DEFAULT_NAMESPACE, options: {})
25
+ connection_options = options[:connection] || {}
26
+ @connection = Connection.new(namespace: namespace, options: connection_options)
27
+ end
28
+
29
+ def create_pod(pod)
30
+ data = post("pods", body: pod.to_json)
31
+ Pod.new(data)
32
+ end
33
+
34
+ def delete_pod(name)
35
+ delete("pods/#{name}")
36
+ end
37
+
38
+ def get_pod(name)
39
+ data = get("pods/#{name}")
40
+ Pod.new(data)
41
+ end
42
+
43
+ def get_pods
44
+ get("pods").
45
+ fetch("items").
46
+ map {|item| Pod.new(item) }
47
+ end
48
+
49
+ def watch_pods(name = nil)
50
+ stream = Stream.new(@connection)
51
+
52
+ stream.each("watch/#{namespace_prefix}/pods/#{name}") do |line|
53
+ yield WatchEvent.new(JSON.parse(line))
54
+ end
55
+ end
56
+
57
+ def create_replication_controller(rc)
58
+ data = post("replicationcontrollers", body: rc.to_json)
59
+ ReplicationController.new(data)
60
+ end
61
+
62
+ def get_replication_controller(name)
63
+ data = get("replicationcontrollers/#{name}")
64
+ ReplicationController.new(data)
65
+ end
66
+
67
+ def get_replication_controllers
68
+ get("replicationcontrollers").
69
+ fetch("items").
70
+ map {|item| ReplicationController.new(item) }
71
+ end
72
+
73
+ def create_namespace(namespace)
74
+ post("namespaces", prefix: nil, body: namespace.to_json)
75
+ end
76
+
77
+ def delete_namespace(name)
78
+ delete("namespaces/#{name}", prefix: nil)
79
+ end
80
+
81
+ def logs(pod_name)
82
+ get("pods/#{pod_name}/log")
83
+ end
84
+
85
+ private
86
+
87
+ def get(*args)
88
+ request(:get, *args)
89
+ end
90
+
91
+ def post(*args)
92
+ request(:post, *args)
93
+ end
94
+
95
+ def delete(*args)
96
+ request(:delete, *args)
97
+ end
98
+
99
+ def request(*args)
100
+ response = @connection.request(*args)
101
+
102
+ body = response.headers["Content-Type"] =~ /json/ ?
103
+ JSON.parse(response.body) :
104
+ response.body
105
+
106
+ if response.status == 200 || response.status == 201
107
+ body
108
+ else
109
+ raise Error, status: Status.new(body)
110
+ end
111
+ end
112
+
113
+ def namespace_prefix
114
+ "namespaces/#{@connection.namespace}"
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,32 @@
1
+ require 'excon'
2
+
3
+ module Kubernetes
4
+ class Connection
5
+ KUBERNETES_HOST = ENV.fetch("KUBERNETES_HOST", "http://localhost:8080").freeze
6
+
7
+ attr_reader :namespace
8
+
9
+ def initialize(host: KUBERNETES_HOST, namespace:, options: {})
10
+ @connection = Excon.new(host, options)
11
+ @namespace = namespace
12
+ end
13
+
14
+ def request(method, path, prefix: "/namespaces/#{namespace}", **options)
15
+ qualified_path = File.join("/api/v1", prefix || "", path)
16
+
17
+ @connection.request(method: method, path: qualified_path, **options)
18
+ end
19
+
20
+ def stream(path, &block)
21
+ handler = lambda {|chunk, remaining_bytes, total_bytes|
22
+ block.call(chunk)
23
+ }
24
+
25
+ request(:get, path, prefix: nil, response_block: handler)
26
+ ensure
27
+ # If the stream is stopped by the client there may be stuff still lingering
28
+ # in the socket.
29
+ @connection.reset
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,16 @@
1
+ module Kubernetes
2
+ class Container
3
+ attr_reader :name, :image, :command, :args, :working_dir
4
+ attr_reader :image_pull_policy, :termination_message_path
5
+
6
+ def initialize(data)
7
+ @name = data.fetch("name")
8
+ @image = data.fetch("image")
9
+ @command = data.fetch("command", [])
10
+ @args = data.fetch("args", [])
11
+ @working_dir = data.fetch("workingDir", nil)
12
+ @image_pull_policy = data.fetch("imagePullPolicy")
13
+ @termination_message_path = data.fetch("terminationMessagePath")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ require 'time'
2
+
3
+ module Kubernetes
4
+ class Metadata
5
+ attr_reader :name, :link, :uid, :resource_version, :creation_timestamp
6
+
7
+ def initialize(data)
8
+ @name = data.fetch("name", nil)
9
+ @namespace = data.fetch("namespace", nil)
10
+ @link = data.fetch("selfLink", nil)
11
+ @uid = data.fetch("uid", nil)
12
+ @resource_version = data.fetch("resourceVersion", nil)
13
+ @creation_timestamp = data.fetch("creationTimestamp") && Time.iso8601(data.fetch("creationTimestamp"))
14
+ @generate_name = data.fetch("generateName", nil)
15
+ @labels = data.fetch("labels", {})
16
+ @annotations = data.fetch("annotations", {})
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'kubernetes/metadata'
2
+ require 'kubernetes/namespace_spec'
3
+ require 'kubernetes/namespace_status'
4
+
5
+ module Kubernetes
6
+ class Namespace
7
+ attr_reader :metadata, :spec, :status
8
+
9
+ def initialize(data)
10
+ @metadata = Metadata.new(data.fetch("metadata"))
11
+ @spec = NamespaceSpec.new(data.fetch("spec"))
12
+ @status = NamespaceStatus.new(data.fetch("status"))
13
+ end
14
+
15
+ def name
16
+ metadata.name
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module Kubernetes
2
+ class NamespaceSpec
3
+ attr_reader :finalizers
4
+
5
+ def initialize(data)
6
+ @finalizers = data.fetch("finalizers")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Kubernetes
2
+ class NamespaceStatus
3
+ attr_reader :phase
4
+
5
+ def initialize(data)
6
+ @phase = data.fetch("phase")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ require 'kubernetes/metadata'
2
+ require 'kubernetes/pod_spec'
3
+ require 'kubernetes/pod_status'
4
+
5
+ module Kubernetes
6
+ class Pod
7
+ attr_reader :metadata, :spec, :status
8
+
9
+ def initialize(data)
10
+ @metadata = Metadata.new(data.fetch("metadata"))
11
+ @spec = PodSpec.new(data.fetch("spec"))
12
+ @status = PodStatus.new(data.fetch("status"))
13
+ end
14
+
15
+ def ==(other)
16
+ metadata.uid == other.metadata.uid
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ require 'kubernetes/container'
2
+
3
+ module Kubernetes
4
+ class PodSpec
5
+ def initialize(data)
6
+ @containers = data.fetch("containers", []).map {|item|
7
+ Container.new(item)
8
+ }
9
+
10
+ @restart_policy = data.fetch("restartPolicy")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ require 'time'
2
+
3
+ module Kubernetes
4
+ class PodStatus
5
+ attr_reader :phase, :reason, :message
6
+
7
+ def initialize(data)
8
+ @phase = data.fetch("phase")
9
+ @message = data.fetch("message", nil)
10
+ @reason = data.fetch("reason", nil)
11
+ @host_ip = data.fetch("hostIP", nil)
12
+ @pod_ip = data.fetch("podIP", nil)
13
+ @start_time = data.key?("startTime") && Time.iso8601(data.fetch("startTime"))
14
+ end
15
+
16
+ def pending?
17
+ @phase == "Pending"
18
+ end
19
+
20
+ def running?
21
+ @phase == "Running"
22
+ end
23
+
24
+ def succeeded?
25
+ @phase == "Succeeded"
26
+ end
27
+
28
+ def failed?
29
+ @phase == "Failed"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ require 'kubernetes/metadata'
2
+ require 'kubernetes/pod_spec'
3
+
4
+ module Kubernetes
5
+ class PodTemplateSpec
6
+ def initialize(data)
7
+ @metadata = Metadata.new(data.fetch("metadata"))
8
+ @spec = PodSpec.new(data.fetch("spec"))
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ require 'kubernetes/metadata'
2
+ require 'kubernetes/replication_controller_spec'
3
+ require 'kubernetes/replication_controller_status'
4
+
5
+ module Kubernetes
6
+ class ReplicationController
7
+ attr_reader :metadata, :spec, :status
8
+
9
+ def initialize(data)
10
+ @metadata = Metadata.new(data.fetch("metadata"))
11
+ @spec = ReplicationControllerSpec.new(data.fetch("spec"))
12
+ @status = ReplicationControllerStatus.new(data.fetch("status"))
13
+ end
14
+
15
+ def ==(other)
16
+ metadata.uid == other.metadata.uid
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ require 'kubernetes/pod_template_spec'
2
+
3
+ module Kubernetes
4
+ class ReplicationControllerSpec
5
+ def initialize(data)
6
+ @replicas = data.fetch("replicas")
7
+ @selector = data.fetch("selector")
8
+ @template = PodTemplateSpec.new(data.fetch("template"))
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Kubernetes
2
+ class ReplicationControllerStatus
3
+ def initialize(data)
4
+ @replicas = data.fetch("replicas")
5
+ @observed_generation = data.fetch("observedGeneration", nil)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ module Kubernetes
2
+ class Status
3
+ attr_reader :code, :message, :reason
4
+
5
+ def initialize(data)
6
+ @code = data.fetch("code")
7
+ @status = data.fetch("status")
8
+ @message = data.fetch("message")
9
+ @reason = data.fetch("reason")
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module Kubernetes
2
+ class Stream
3
+ def initialize(connection)
4
+ @connection = connection
5
+ end
6
+
7
+ def each(*args)
8
+ buffer = ""
9
+
10
+ @connection.stream(*args) do |chunk|
11
+ buffer << chunk
12
+
13
+ while line = buffer.slice!(/\A.+\n/)
14
+ yield line
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Kubernetes
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ module Kubernetes
2
+ class WatchEvent
3
+ attr_reader :type, :object
4
+
5
+ def initialize(data)
6
+ @type = data.fetch("type")
7
+ @object = map_object(data.fetch("object"))
8
+ end
9
+
10
+ private
11
+
12
+ def map_object(data)
13
+ case data.fetch("kind")
14
+ when "Pod" then Pod.new(data)
15
+ else raise "unknown kind `#{data.fetch('kind')}`"
16
+ end
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-kubernetes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Schierbeck
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A basic K8s interface gem
42
+ email:
43
+ - dasch@zendesk.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - kubernetes.gemspec
58
+ - lib/kubernetes.rb
59
+ - lib/kubernetes/client.rb
60
+ - lib/kubernetes/connection.rb
61
+ - lib/kubernetes/container.rb
62
+ - lib/kubernetes/metadata.rb
63
+ - lib/kubernetes/namespace.rb
64
+ - lib/kubernetes/namespace_spec.rb
65
+ - lib/kubernetes/namespace_status.rb
66
+ - lib/kubernetes/pod.rb
67
+ - lib/kubernetes/pod_spec.rb
68
+ - lib/kubernetes/pod_status.rb
69
+ - lib/kubernetes/pod_template_spec.rb
70
+ - lib/kubernetes/replication_controller.rb
71
+ - lib/kubernetes/replication_controller_spec.rb
72
+ - lib/kubernetes/replication_controller_status.rb
73
+ - lib/kubernetes/status.rb
74
+ - lib/kubernetes/stream.rb
75
+ - lib/kubernetes/version.rb
76
+ - lib/kubernetes/watch_event.rb
77
+ homepage: https://github.com/dasch/ruby-kubernetes
78
+ licenses: []
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A basic K8s interface gem
100
+ test_files: []