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
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.
35 36 37 38 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 35 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.
8 9 10 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 8 def context @context end |
#deploy_started=(value) ⇒ Object (writeonly)
Sets the attribute deploy_started
9 10 11 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 9 def deploy_started=(value) @deploy_started = value end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
8 9 10 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 8 def file @file end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 8 def name @name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
8 9 10 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 8 def namespace @namespace end |
#type ⇒ Object
69 70 71 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 69 def type @type || self.class.name.split('::').last end |
Class Method Details
.for_type(type, name, namespace, context, file) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 13 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 |
.timeout ⇒ Object
27 28 29 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 27 def self.timeout self::TIMEOUT end |
Instance Method Details
#deploy_failed? ⇒ Boolean
48 49 50 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 48 def deploy_failed? false end |
#deploy_finished? ⇒ Boolean
73 74 75 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 73 def deploy_finished? deploy_failed? || deploy_succeeded? || deploy_timed_out? end |
#deploy_succeeded? ⇒ Boolean
52 53 54 55 56 57 58 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 52 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
77 78 79 80 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 77 def deploy_timed_out? return false unless @deploy_started !deploy_succeeded? && !deploy_failed? && (Time.now.utc - @deploy_started > timeout) end |
#exists? ⇒ Boolean
60 61 62 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 60 def exists? nil end |
#group_name ⇒ Object
98 99 100 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 98 def group_name type.downcase.pluralize end |
#id ⇒ Object
40 41 42 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 40 def id "#{type}/#{name}" end |
#log_status ⇒ Object
114 115 116 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 114 def log_status STDOUT.puts "[KUBESTATUS] #{JSON.dump(status_data)}" end |
#run_kubectl(*args) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 102 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
64 65 66 67 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 64 def status @status ||= "Unknown" deploy_timed_out? ? "Timed out with status #{@status}" : @status end |
#status_data ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 86 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
44 45 46 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 44 def sync log_status end |
#timeout ⇒ Object
31 32 33 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 31 def timeout self.class.timeout end |
#tpr? ⇒ Boolean
82 83 84 |
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 82 def tpr? false end |