Module: Util::KubectlOps
- Extended by:
- Logging
- Defined in:
- lib/util/kubectl_ops.rb
Constant Summary collapse
- CONFIG_MAP_RESOURCE_NAME =
'config_map'.freeze
- RETRYABLE_STATUS_CODE =
Status codes for which retry is attempted (To represent 5xx, use 5)
[429, 5].freeze
- MAX_ATTEMPTS =
5
- INITIAL_DELAY =
seconds
1
- EXPONENT_CONSTANT =
āeā in exponential function (e^x) where x is number of retries
2
Constants included from Logging
Logging::SEV_LABEL, Logging::TRACE
Class Method Summary collapse
- .create_configmap(cm_name, namespace, data_hash) ⇒ Object
- .create_object(object_type, kubeclient_resource_object, client) ⇒ Object
- .get_configmap(cm_name, namespace) ⇒ Object
- .get_object(object_type, object_name, namespace, client, options = {}) ⇒ Object
- .get_objects(object_type, client = nil, options = {}) ⇒ Object
- .kube_apiserver_request(method:, object_type:, client: nil, object_name: nil, namespace: nil, object: nil, patch: nil, opts: {}) ⇒ Object
- .patch_configmap(cm_name, namespace, data_hash) ⇒ Object
- .patch_object(object_type, object_name, patch_hash, namespace, client) ⇒ Object
- .set_chunk_limit(num) ⇒ Object
- .should_retry?(error, retry_count, object_type) ⇒ Boolean
Methods included from Logging
Class Method Details
.create_configmap(cm_name, namespace, data_hash) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/util/kubectl_ops.rb', line 197 def create_configmap(cm_name, namespace, data_hash) # Create resource object for config map cm = Kubeclient::Resource.new cm. = {} cm..name = cm_name cm..namespace = namespace cm.data = {} cm.data = data_hash object_type = CONFIG_MAP_RESOURCE_NAME client = Util::KubeClient.get_clients[:core_client] begin create_object(object_type, cm, client) rescue StandardError => e logger.error("Unable to create '#{object_type}/#{cm_name}'") raise e end end |
.create_object(object_type, kubeclient_resource_object, client) ⇒ Object
168 169 170 171 172 173 |
# File 'lib/util/kubectl_ops.rb', line 168 def create_object(object_type, kubeclient_resource_object, client) kube_apiserver_request(method: :create_object, object_type: object_type, client: client, object: kubeclient_resource_object) end |
.get_configmap(cm_name, namespace) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/util/kubectl_ops.rb', line 184 def get_configmap(cm_name, namespace) object_type = CONFIG_MAP_RESOURCE_NAME client = Util::KubeClient.get_clients[:core_client] begin response = get_object(object_type, cm_name, namespace, client) rescue StandardError => e return nil if e.error_code == 404 raise e end response end |
.get_object(object_type, object_name, namespace, client, options = {}) ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/util/kubectl_ops.rb', line 159 def get_object(object_type, object_name, namespace, client, = {}) kube_apiserver_request(method: :get_object, object_type: object_type, client: client, object_name: object_name, namespace: namespace, opts: ) end |
.get_objects(object_type, client = nil, options = {}) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/util/kubectl_ops.rb', line 151 def get_objects(object_type, client = nil, = {}) .merge!({ limit: @chunk_limit, continue: nil }) kube_apiserver_request(method: :get_objects, object_type: object_type, client: client, opts: ) end |
.kube_apiserver_request(method:, object_type:, client: nil, object_name: nil, namespace: nil, object: nil, patch: nil, opts: {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 107 108 109 110 111 112 113 114 115 116 117 118 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 |
# File 'lib/util/kubectl_ops.rb', line 44 def kube_apiserver_request(method:, object_type:, client: nil, object_name: nil, namespace: nil, object: nil, patch: nil, opts: {}) response = nil retry_count = 0 begin case method when :get_objects if client.nil? client = Util::KubeClient.get_clients[Enum::ObjectClientMapingEnum.const_get(object_type.upcase.to_s)] end logger.debug("Fetching '#{object_type}' details from all namespaces") method_verb = 'GET' opts.merge!({ as: :parsed_symbolized }) response = {} loop do entities = client.public_send("get_#{object_type}", opts) continue = entities[:metadata][:continue] if response.empty? # when all objects are fetched via single API request if continue.nil? response = entities break end response = entities.clone else response[:items] += entities[:items] end break if continue.nil? logger.debug("remainingItemCount - #{entities[:metadata][:remainingItemCount]}") opts.merge!({ continue: continue }) end if response.nil? || response.empty? logger.debug("Empty or null response recieved from Kubernetes API endpoint '#{client.rest_client.url}'.") return { parsed_symbolized: [], raw: '' } else return { parsed_symbolized: response, raw: JSON.dump(response) } end when :get_object method_verb = 'GET' logger.debug("Fetching '#{object_type}/#{object_name}' details") opts.merge!(as: :parsed_symbolized) response = client.public_send("get_#{object_type}", object_name, namespace, opts) when :create_object method_verb = 'CREATE' logger.debug("Creating '#{object_type}' - #{object}") response = client.public_send("create_#{object_type}", object) when :patch_object method_verb = 'PATCH' logger.debug("Patching '#{object_type}/#{object_name}' - #{patch}") response = client.public_send("patch_#{object_type}", object_name, patch, namespace) else raise SyntaxError, "Unsupported method - #{method} passed." end rescue KubeException => e if should_retry?(e, retry_count, object_type) retry_count += 1 retry end if e.error_code.nil? logger.error("Unexpected error occurred while calling #{method_verb} '#{object_type}'. Error details: #{e}") raise e elsif e.error_code == 302 logger.error("Max re-direct attempts while calling #{method_verb} '#{object_type}'. Error details: #{e}") raise StandardError, "Max re-direct attempts while calling #{method_verb} '#{object_type}'." elsif e.error_code == 401 # In such cases, client should be re-created in order to refresh token logger.error("Not authorized to #{method_verb} '#{object_type}'. Error details: #{e}") raise StandardError, "Not authorized to #{method_verb} '#{object_type}'." elsif e.error_code == 403 logger.error("Forbidden to #{method_verb} '#{object_type}'. Error details: #{e}") raise StandardError, "Forbidden to #{method_verb} '#{object_type}'." elsif e.error_code == 404 logger.debug("Resource '#{object_type}' not found. Error details: #{e}") raise e elsif e.error_code == 410 # ref - https://kubernetes.io/docs/reference/using-api/api-concepts/#retrieving-large-results-sets-in-chunks logger.error("Too many '#{object_type}' objects. Consider increasing CHUNK limit.") raise StandardError, "Too many '#{object_type}' objects. Consider increasing CHUNK limit." elsif e.error_code == 429 logger.error("Encountered throttling while calling #{method_verb} '#{object_type}'. Error details: #{e}") raise StandardError, "Encountered throttling while #{method_verb} '#{object_type}'." elsif (e.error_code / 100) == 5 logger.error("Unexpected error occurred while #{method_verb} '#{object_type}'. Error details: #{e}") raise StandardError, "Unexpected error occurred while #{method_verb} '#{object_type}'." else logger.error("Error '#{e}' encountered while #{method_verb} '#{object_type}' from Kubernetes API endpoint '#{client.rest_client.url}'") raise StandardError, "Error '#{e}' encountered while #{method_verb} '#{object_type}'." end rescue NoMethodError => e logger.error('Unexpected Error - Method not found.') raise e rescue StandardError => e logger.error("Error while #{method_verb} '#{object_type}'. Error: #{e}") if should_retry?(e, retry_count, object_type) retry_count += 1 retry end error_msg = "Maximum retry attempts ('#{MAX_ATTEMPTS}') exceeded. Aborting fetch for '#{object_type}'." logger.error(error_msg) raise error error_msg end response end |
.patch_configmap(cm_name, namespace, data_hash) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/util/kubectl_ops.rb', line 217 def patch_configmap(cm_name, namespace, data_hash) patch_hash = { data: data_hash } object_type = CONFIG_MAP_RESOURCE_NAME client = Util::KubeClient.get_clients[:core_client] begin patch_object(object_type, cm_name, patch_hash, namespace, client) rescue StandardError => e logger.error("Unable to patch '#{object_type}/#{cm_name}'") raise e end end |
.patch_object(object_type, object_name, patch_hash, namespace, client) ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/util/kubectl_ops.rb', line 175 def patch_object(object_type, object_name, patch_hash, namespace, client) kube_apiserver_request(method: :patch_object, object_type: object_type, client: client, object_name: object_name, patch: patch_hash, namespace: namespace) end |
.set_chunk_limit(num) ⇒ Object
25 26 27 |
# File 'lib/util/kubectl_ops.rb', line 25 def set_chunk_limit(num) @chunk_limit = num end |
.should_retry?(error, retry_count, object_type) ⇒ Boolean
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/util/kubectl_ops.rb', line 29 def should_retry?(error, retry_count, object_type) return false if error.error_code.nil? # for handling timeouts if (RETRYABLE_STATUS_CODE.include? error.error_code) || RETRYABLE_STATUS_CODE.include?(error.error_code / 100) if retry_count < MAX_ATTEMPTS # If e = 2, and initial delay is 10, intervals will be (1, 2, 4, 8, 16) sleep_interval = EXPONENT_CONSTANT.pow(retry_count) * INITIAL_DELAY logger.warn("Error while making API Server call - '#{object_type}'. Retrying in #{sleep_interval} seconds. HTTP status code: #{error.error_code}") sleep(sleep_interval) return true end end false end |