Class: Biosphere::Kube::Client
- Inherits:
-
Object
- Object
- Biosphere::Kube::Client
- Defined in:
- lib/biosphere/kube.rb
Instance Method Summary collapse
- #apply_resource(resource) ⇒ Object
- #get(resource) ⇒ Object
- #get_client(resource) ⇒ Object
- #get_resource_name(resource) ⇒ Object
-
#initialize(hostname, ssl_options) ⇒ Client
constructor
A new instance of Client.
- #post(resource) ⇒ Object
- #put(resource) ⇒ Object
Constructor Details
#initialize(hostname, ssl_options) ⇒ Client
Returns a new instance of Client.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/biosphere/kube.rb', line 30 def initialize(hostname, ) @clients = [] @clients << ::Kubeclient::Client.new("#{hostname}/api" , "v1", ssl_options: ) @clients << ::Kubeclient::Client.new("#{hostname}/apis/apps/" , "v1beta1", ssl_options: ) @clients << ::Kubeclient::Client.new("#{hostname}/apis/extensions/" , "v1beta1", ssl_options: ) @clients << ::Kubeclient::Client.new("#{hostname}/apis/batch/" , "v2alpha1", ssl_options: ) @clients << ::Kubeclient::Client.new("#{hostname}/apis/storage.k8s.io/" , "v1", ssl_options: ) @clients << ::Kubeclient::Client.new("#{hostname}/apis/autoscaling/" , "v1", ssl_options: ) @clients.each do |c| begin c.discover rescue KubeException => e puts "Could not discover api #{c.api_endpoint} - maybe this kube version is too old." end end end |
Instance Method Details
#apply_resource(resource) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/biosphere/kube.rb', line 147 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::NotFound => e pp e pp JSON.parse(e.response.body) puts "404 when applying resources might mean one of the following:" puts "\t * You're trying to apply a non-namespaced manifest." puts "\t Confirm if your manifests metadata should contain a namespace field or not" 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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/biosphere/kube.rb', line 108 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
60 61 62 63 64 65 66 67 68 |
# File 'lib/biosphere/kube.rb', line 60 def get_client(resource) kind = resource[:kind].underscore_case @clients.each do |c| if c.instance_variable_get("@api_group") + c.instance_variable_get("@api_version") == resource[:apiVersion] return c end end return nil end |
#get_resource_name(resource) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/biosphere/kube.rb', line 49 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/biosphere/kube.rb', line 70 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]) begin ret = client.rest_client[ns_prefix + resource_name].post(resource.to_h.to_json, { 'Content-Type' => 'application/json' }.merge(client.instance_variable_get("@headers"))) rescue RestClient::MethodNotAllowed => e if !resource[:metadata][:namespace] puts "Error doing api call: #{e}".colorize(:red) puts "This might be because you did not specify namespace in your resource: #{resource[:metadata]}".colorize(:yellow) else puts "Error calling API (on RestClient::MethodNotAllowed): #{e}" end puts "rest_client: #{ns_prefix + resource_name}, client: #{client.rest_client[ns_prefix + resource_name]}" puts "Dumpin resource request:" pp resource.to_h.to_json raise e rescue RestClient::Exception => e puts "Error calling API (on RestClient::Exception rescue): #{e}" puts "rest_client: #{ns_prefix + resource_name}, client: #{client.rest_client[ns_prefix + resource_name]}" puts "Dumpin resource request:" pp resource.to_h.to_json raise e end return { action: :post, resource: ns_prefix + resource_name + "/#{name}", body: JSON.parse(ret.body, :symbolize_names => true) } end |
#put(resource) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/biosphere/kube.rb', line 127 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 |