Class: Biosphere::Kube::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/biosphere/kube.rb

Instance Method Summary collapse

Constructor Details

#initialize(hostname, ssl_options) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
# File 'lib/biosphere/kube.rb', line 30

def initialize(hostname, ssl_options)
    @clients = []

    @clients << ::Kubeclient::Client.new("#{hostname}/api" , "v1", ssl_options: ssl_options)
    @clients << ::Kubeclient::Client.new("#{hostname}/apis/extensions/" , "v1beta1", ssl_options: ssl_options)
    @clients << ::Kubeclient::Client.new("#{hostname}/apis/batch/" , "v2alpha1", ssl_options: ssl_options)

    @clients.each { |c| c.discover }
end

Instance Method Details

#apply_resource(resource) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/biosphere/kube.rb', line 119

def apply_resource(resource)
    name = resource[:metadata][:name]
    responses = []
    not_found = false
    begin
        response = get(resource)
    rescue RestClient::NotFound => e
        not_found = true
    end

    if not_found
        begin
            response = post(resource)
            puts "Created resource #{response[:resource]}"
            responses << response
        rescue RestClient::UnprocessableEntity => e
            pp e
            pp JSON.parse(e.response.body)
        end
    else
        puts "Updating resource #{response[:resource]}"

        # Get the current full resource from apiserver
        current_resource = response[:body]

        update_resource = Kube.kube_merge_resource_for_put!(current_resource, resource)

        begin
            responses << put(update_resource)
        rescue RestClient::Exception => e
            puts "Error updating resource: #{e} #{e.class}"
            pp JSON.parse(e.response)
        rescue RestClient::Exception => e
            puts "Misc exception: #{e}, #{e.class}, #{e.response}"
        end
        
        return responses
    end
end

#get(resource) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/biosphere/kube.rb', line 80

def get(resource)
    name = resource[:metadata][:name]
    client = get_client(resource)
    resource_name = get_resource_name(resource)

    if !client
        raise ArgumentError, "Unknown resource #{resource[:kind]} of #{name} for kubernetes. Maybe this is in a new extension api?"
    end

    ns_prefix = client.build_namespace_prefix(resource[:metadata][:namespace])
    key = ns_prefix + resource_name + "/#{name}"
    ret = client.rest_client[key].get(client.instance_variable_get("@headers"))
    return {
        action: :get,
        resource: key,
        body: JSON.parse(ret.body, :symbolize_names => true)
    }
end

#get_client(resource) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/biosphere/kube.rb', line 51

def get_client(resource)
    kind = resource[:kind].underscore_case
    @clients.each do |c|
        if c.instance_variable_get("@entities")[kind]
            return c
        end
    end
    return nil
end

#get_resource_name(resource) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/biosphere/kube.rb', line 40

def get_resource_name(resource)
    resource_name = nil
    kind = resource[:kind].underscore_case
    @clients.each do |c|
        if c.instance_variable_get("@entities")[kind]
            return c.instance_variable_get("@entities")[kind].resource_name
        end
    end
    return nil
end

#post(resource) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/biosphere/kube.rb', line 61

def post(resource)
    name = resource[:metadata][:name]
    client = get_client(resource)
    resource_name = get_resource_name(resource)

    if !client
        raise ArgumentError, "Unknown resource #{resource[:kind]} of #{name} for kubernetes. Maybe this is in a new extension api?"
    end

    ns_prefix = client.build_namespace_prefix(resource[:metadata][:namespace])
    ns_prefix = ns_prefix.empty? ? "namespaces/default/" : ns_prefix
    ret =  client.rest_client[ns_prefix + resource_name].post(resource.to_h.to_json, { 'Content-Type' => 'application/json' }.merge(client.instance_variable_get("@headers")))
    return {
        action: :post,
        resource: ns_prefix + resource_name + "/#{name}",
        body: JSON.parse(ret.body, :symbolize_names => true)
    }
end

#put(resource) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/biosphere/kube.rb', line 99

def put(resource)
    name = resource[:metadata][:name]
    client = get_client(resource)
    resource_name = get_resource_name(resource)

    if !client
        raise ArgumentError, "Unknown resource #{resource[:kind]} of #{name} for kubernetes. Maybe this is in a new extension api?"
    end

    ns_prefix = client.build_namespace_prefix(resource[:metadata][:namespace])
    key = ns_prefix + resource_name + "/#{name}"
    ret = client.rest_client[key].put(resource.to_h.to_json, { 'Content-Type' => 'application/json' }.merge(client.instance_variable_get("@headers")))
    return {
        action: :put,
        resource: key,
        body: JSON.parse(ret.body, :symbolize_names => true)
    }

end