Class: KubernetesDeploy::KubernetesResource
- Inherits:
-
Object
- Object
- KubernetesDeploy::KubernetesResource
- Defined in:
- lib/kubernetes-deploy/kubernetes_resource.rb
Direct Known Subclasses
Cloudsql, ConfigMap, Deployment, Ingress, PersistentVolumeClaim, Pod, Redis, Service
Constant Summary collapse
- TIMEOUT =
5.minutes
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#deploy_started ⇒ Object
writeonly
Sets the attribute deploy_started.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
- #type ⇒ Object
Class Method Summary collapse
- .for_type(type, name, namespace, context, file) ⇒ Object
- .logger ⇒ Object
- .logger=(value) ⇒ Object
- .timeout ⇒ Object
Instance Method Summary collapse
- #deploy_failed? ⇒ Boolean
- #deploy_finished? ⇒ Boolean
- #deploy_succeeded? ⇒ Boolean
- #deploy_timed_out? ⇒ Boolean
- #exists? ⇒ Boolean
- #group_name ⇒ Object
- #id ⇒ Object
-
#initialize(name, namespace, context, file) ⇒ KubernetesResource
constructor
A new instance of KubernetesResource.
- #log_status ⇒ Object
- #run_kubectl(*args) ⇒ Object
- #status ⇒ Object
- #status_data ⇒ Object
- #sync ⇒ Object
- #timeout ⇒ Object
- #tpr? ⇒ Boolean
Constructor Details
#initialize(name, namespace, context, file) ⇒ KubernetesResource
Returns a new instance of KubernetesResource.
48 49 50 51 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 48 def initialize(name, namespace, context, file) # subclasses must also set these @name, @namespace, @context, @file = name, namespace, context, file end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
21 22 23 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 21 def context @context end |
#deploy_started=(value) ⇒ Object (writeonly)
Sets the attribute deploy_started
22 23 24 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 22 def deploy_started=(value) @deploy_started = value end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
21 22 23 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 21 def file @file end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
21 22 23 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 21 def name @name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
21 22 23 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 21 def namespace @namespace end |
#type ⇒ Object
82 83 84 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 82 def type @type || self.class.name.split('::').last end |
Class Method Details
.for_type(type, name, namespace, context, file) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 26 def self.for_type(type, name, namespace, context, file) case type when 'cloudsql' then Cloudsql.new(name, namespace, context, file) when 'configmap' then ConfigMap.new(name, namespace, context, file) when 'deployment' then Deployment.new(name, namespace, context, file) when 'pod' then Pod.new(name, namespace, context, file) when 'redis' then Redis.new(name, namespace, context, file) when 'ingress' then Ingress.new(name, namespace, context, file) when 'persistentvolumeclaim' then PersistentVolumeClaim.new(name, namespace, context, file) when 'service' then Service.new(name, namespace, context, file) else self.new(name, namespace, context, file).tap { |r| r.type = type } end end |
.logger ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 11 def self.logger @logger ||= begin l = Logger.new($stderr) l.formatter = proc do |_severity, _datetime, _progname, msg| "#{msg}\n" end l end end |
.logger=(value) ⇒ Object
7 8 9 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 7 def self.logger=(value) @logger = value end |
.timeout ⇒ Object
40 41 42 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 40 def self.timeout self::TIMEOUT end |
Instance Method Details
#deploy_failed? ⇒ Boolean
61 62 63 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 61 def deploy_failed? false end |
#deploy_finished? ⇒ Boolean
86 87 88 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 86 def deploy_finished? deploy_failed? || deploy_succeeded? || deploy_timed_out? end |
#deploy_succeeded? ⇒ Boolean
65 66 67 68 69 70 71 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 65 def deploy_succeeded? if @deploy_started && !@success_assumption_warning_shown KubernetesDeploy.logger.warn("Don't know how to monitor resources of type #{type}. Assuming #{id} deployed successfully.") @success_assumption_warning_shown = true end true end |
#deploy_timed_out? ⇒ Boolean
90 91 92 93 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 90 def deploy_timed_out? return false unless @deploy_started !deploy_succeeded? && !deploy_failed? && (Time.now.utc - @deploy_started > timeout) end |
#exists? ⇒ Boolean
73 74 75 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 73 def exists? nil end |
#group_name ⇒ Object
111 112 113 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 111 def group_name type.downcase.pluralize end |
#id ⇒ Object
53 54 55 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 53 def id "#{type}/#{name}" end |
#log_status ⇒ Object
127 128 129 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 127 def log_status KubernetesResource.logger.info("[KUBESTATUS][#{@context}][#{@namespace}] #{JSON.dump(status_data)}") end |
#run_kubectl(*args) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 115 def run_kubectl(*args) raise FatalDeploymentError, "Namespace missing for namespaced command" if namespace.blank? raise FatalDeploymentError, "Explicit context is required to run this command" if context.blank? args = args.unshift("kubectl").push("--namespace=#{namespace}").push("--context=#{context}") KubernetesDeploy.logger.debug Shellwords.join(args) out, err, st = Open3.capture3(*args) KubernetesDeploy.logger.debug(out.shellescape) KubernetesDeploy.logger.debug("[ERROR] #{err.shellescape}") unless st.success? [out.chomp, st] end |
#status ⇒ Object
77 78 79 80 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 77 def status @status ||= "Unknown" deploy_timed_out? ? "Timed out with status #{@status}" : @status end |
#status_data ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 99 def status_data { group: group_name, name: name, status_string: status, exists: exists?, succeeded: deploy_succeeded?, failed: deploy_failed?, timed_out: deploy_timed_out? } end |
#sync ⇒ Object
57 58 59 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 57 def sync log_status end |
#timeout ⇒ Object
44 45 46 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 44 def timeout self.class.timeout end |
#tpr? ⇒ Boolean
95 96 97 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 95 def tpr? false end |