Class: KubernetesDeploy::KubernetesResource

Inherits:
Object
  • Object
show all
Defined in:
lib/kubernetes-deploy/kubernetes_resource.rb

Constant Summary collapse

TIMEOUT =
5.minutes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#contextObject (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

Parameters:

  • value

    the value to set the attribute deploy_started to.



9
10
11
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 9

def deploy_started=(value)
  @deploy_started = value
end

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 8

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 8

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



8
9
10
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 8

def namespace
  @namespace
end

#typeObject



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

.timeoutObject



27
28
29
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 27

def self.timeout
  self::TIMEOUT
end

Instance Method Details

#deploy_failed?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 48

def deploy_failed?
  false
end

#deploy_finished?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


60
61
62
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 60

def exists?
  nil
end

#group_nameObject



98
99
100
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 98

def group_name
  type.downcase.pluralize
end

#idObject



40
41
42
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 40

def id
  "#{type}/#{name}"
end

#log_statusObject



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

#statusObject



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_dataObject



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

#syncObject



44
45
46
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 44

def sync
  log_status
end

#timeoutObject



31
32
33
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 31

def timeout
  self.class.timeout
end

#tpr?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 82

def tpr?
  false
end