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.



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

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

Parameters:

  • value

    the value to set the attribute deploy_started to.



22
23
24
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 22

def deploy_started=(value)
  @deploy_started = value
end

#fileObject (readonly)

Returns the value of attribute file.



21
22
23
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 21

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 21

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



21
22
23
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 21

def namespace
  @namespace
end

#typeObject



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

.loggerObject



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

.timeoutObject



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

def self.timeout
  self::TIMEOUT
end

Instance Method Details

#deploy_failed?Boolean

Returns:

  • (Boolean)


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

def deploy_failed?
  false
end

#deploy_finished?Boolean

Returns:

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

Returns:

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

Returns:

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 73

def exists?
  nil
end

#group_nameObject



111
112
113
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 111

def group_name
  type.downcase.pluralize
end

#idObject



53
54
55
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 53

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

#log_statusObject



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

#statusObject



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_dataObject



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

#syncObject



57
58
59
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 57

def sync
  log_status
end

#timeoutObject



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

def timeout
  self.class.timeout
end

#tpr?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/kubernetes-deploy/kubernetes_resource.rb', line 95

def tpr?
  false
end