Class: Keel::GCloud::Kubernetes::Pod

Inherits:
Object
  • Object
show all
Defined in:
lib/keel/gcloud/kubernetes/pod.rb

Overview

A class to represent a Kubernetes Pod. It is a simplified view of what Kubernetes returns with only the necessary information required to perform the operations needed.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ Pod

Returns a new instance of Pod.



13
14
15
16
17
18
19
20
21
22
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 13

def initialize **params
  @app        = params[:app]
  @name       = params[:name]
  @namespace  = params[:namespace]
  @status     = params[:status]
  @uid        = params[:uid]

  @cli        = Cli.new
  @prompter   = Prompter.new
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



11
12
13
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 11

def app
  @app
end

#cliObject

Returns the value of attribute cli.



11
12
13
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 11

def cli
  @cli
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 11

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



11
12
13
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 11

def namespace
  @namespace
end

#statusObject

Returns the value of attribute status.



11
12
13
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 11

def status
  @status
end

#uidObject

Returns the value of attribute uid.



11
12
13
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 11

def uid
  @uid
end

Class Method Details

.fetch_all(env, selector) ⇒ Hash

Fetches all the pods from Kubernetes.

Parameters:

  • env (String)

    the namespace/environment for which to fetch the pods

  • app (String)

    the app for which to fetch the pods. [optional] Hash with multiple selectors

Returns:

  • (Hash)

    the parsed result of the API call



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 51

def self.fetch_all env, selector
  if selector.is_a? Hash
    selector = selector.map{|k,v| "#{k}=#{v}"}.join(',')
  else
    selector = "app=#{selector}"
  end
  
  command   = "kubectl get po --namespace=#{env} -l #{selector} -o yaml"
  rcs_yaml  = YAML.load Cli.new.execute(command)
  return false unless rcs_yaml

  self.from_yaml rcs_yaml
end

.from_yaml(yaml) ⇒ Array<Pod>

Parses the returned YAML into objects of the Pod class.

Parameters:

  • yaml (Hash)

    the parsed result of the API call

Returns:

  • (Array<Pod>)

    an array of Pod objects



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 30

def self.from_yaml yaml
  yaml['items'].map do |item|
    params = {
      app:        item['metadata']['labels']['app'],
      name:       item['metadata']['name'],
      namespace:  item['metadata']['namespace'],
      status:     item['status']['phase'],
      uid:        item['metadata']['uid'],
    }

    self.new params
  end
end

Instance Method Details

#deleteBoolean

Deletes the pod.

Returns:

  • (Boolean)

    whether the call succeeded or not



79
80
81
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 79

def delete
  @cli.system_call "kubectl delete po #{self.name} --namespace=#{self.namespace}"
end

#logs(tail = nil) ⇒ Boolean

Fetches the logs for the pod. If the param tail is set to true, it tails the logs.

Parameters:

  • tail (Boolean, nil) (defaults to: nil)

    flag whether to tail the logs or not

Returns:

  • (Boolean)

    whether the call succeeded or not



90
91
92
93
94
95
96
97
98
99
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 90

def logs tail=nil
  f = tail ? '-f ' : ''

  if tail
    @prompter.print 'Fetching logs...'
    @prompter.print 'Use Ctrl-C to stop'
  end

  @cli.system_call "kubectl logs #{f}#{self.name} --namespace=#{self.namespace} -c=#{self.app}"
end

#running?Boolean

Checks if the namespace is running by comparing the status attribute.

Returns:

  • (Boolean)


70
71
72
# File 'lib/keel/gcloud/kubernetes/pod.rb', line 70

def running?
  'Running' == self.status
end