Kubeclient

Gem Version Build Status Code Climate Dependency Status

A Ruby client for Kubernetes REST api. The client supports GET, POST, PUT, DELETE on nodes, pods, services, replication controllers and namespaces. The client currently supports Kubernetes REST api version v1beta3.

Installation

Add this line to your application's Gemfile:

gem 'kubeclient'

And then execute:

$ bundle

Or install it yourself as:

$ gem install kubeclient

Usage

Initialize the client:
client = Kubeclient::Client.new 'http://localhost:8080/api/' , "v1beta3"

Or without specifying version (it will be set by default to "v1beta3"

client = Kubeclient::Client.new 'http://localhost:8080/api/'

Another option is to initialize the client with URI object:

uri = URI::HTTP.build(host: "somehostname", port: 8080) client = Kubeclient::Client.new uri

It is also possible to use https and configure ssl with:

client = Kubeclient::Client.new 'https://localhost:8443/api/' , "v1beta3" client.ssl_options(
client_cert: OpenSSL::X509::Certificate.new(File.read('/path/to/client.crt')),
client_key: OpenSSL::PKey::RSA.new(File.read('/path/to/client.key')),
ca_file: '/path/to/ca.crt',
verify_ssl: OpenSSL::SSL::VERIFY_PEER
)

For testing and development purpose you can disable the ssl check with:

client.ssl_options(verify_ssl: OpenSSL::SSL::VERIFY_NONE)

Examples:

  1. Get all pods (and respectively: get_services, get_nodes, get_replication_controllers)
    pods = client.get_pods
    You can get entities which have specific labels by specifying input parameter named label-selector:
    pods = client.get_pods(label_selector: 'name=redis-master')
    You can specify multiple labels and that returns entities which have both labels:
    pods = client.get_pods(label_selector: 'name=redis-master,app=redis')

  2. Get a specific node (and respectively: get_service "service id" , get_pod "pod id" , get_replication_controller "rc id" )
    node1 = client.get_node "127.0.0.1"
    Note - Kubernetes doesn't work with the uid, but rather with the 'id' property. Querying with uid causes 404.

  3. Delete a service (and respectively delete_pod "pod id" , delete_replication_controller "rc id", delete node "node id")
    Input parameter - id (string) specifying service id, pod id, replication controller id.
    client.delete_service "redis-service"

  4. Create a service (and respectively: create_pod pod_object, create_replication_controller rc_obj)
    Input parameter - object of type Service, Pod, ReplicationController.
    The below example is for v1beta3
    service = Service.new
    service.metadata.name = "redis-master"
    service.spec.port = 6379
    service.spec.containerPort = "redis-server"
    service.spec.selector = {}
    service.spec.selector.name = "redis"
    service.spec.selector.role = "master"
    client.create_service service

  5. Update entity (update pod, service, replication controller)
    Input parameter - object of type Service, Pod, ReplicationController
    The below example is for v1beta3
    client.update_service service1

  6. all_entities - Returns a hash with 7 keys (node, service, pod, replication_controller, namespace, endpoint and event). Each key points to an EntityList of same type. This method is a convenience method instead of calling each entity's get method separately.
    client.all_entities

  7. Receive entity updates
    It is possible to receive live update notices watching the relevant entities:
    watcher = client.watch_pods
    watcher.each do |notice|
    # process notice data
    end
    It is possible to interrupt the watcher from another thread with:
    watcher.finish

Contributing

  1. Fork it ( https://github.com/[my-github-username]/kubeclient/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Test your changes with rake test rubocop, add new tests if needed.
  4. If you added a new functionality, add it to README
  5. Commit your changes (git commit -am 'Add some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create a new Pull Request

Tests

This client is tested with Minitest. Please run all tests before submitting a Pull Request, and add new tests for new functionality.

Running tests:
rake test