Class: KubeDeployTools::KubernetesResource

Inherits:
Object
  • Object
show all
Defined in:
lib/kube_deploy_tools/kubernetes_resource.rb

Direct Known Subclasses

Deployment

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath:, definition:, kubectl:) ⇒ KubernetesResource

Returns a new instance of KubernetesResource.



26
27
28
29
30
31
32
33
34
35
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 26

def initialize(filepath:, definition:, kubectl:)
  @filepath = filepath
  @definition = definition
  @kubectl = kubectl

  @namespace = definition.dig('metadata', 'namespace')
  @name = definition.dig('metadata', 'name')
  @kind = definition['kind']
  @annotations = definition.dig('metadata', 'annotations')
end

Instance Attribute Details

#annotationsObject

Returns the value of attribute annotations.



5
6
7
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 5

def annotations
  @annotations
end

#definitionObject

Returns the value of attribute definition.



5
6
7
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 5

def definition
  @definition
end

#kindObject

Returns the value of attribute kind.



5
6
7
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 5

def kind
  @kind
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 5

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



5
6
7
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 5

def namespace
  @namespace
end

Class Method Details

.build(filepath: nil, definition:, kubectl:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 11

def self.build(filepath: nil, definition:, kubectl:)
  opts = { filepath: filepath, definition: definition, kubectl: kubectl }
  # Find the corresponding class for the Kubernetes resource, if available
  if ["Deployment"].include?(definition["kind"])
    klass = KubeDeployTools.const_get(definition["kind"])
    klass.new(**opts)
  else
    # Otherwise initialize here if no class exists for this Kubernetes
    # resource kind
    inst = new(**opts)
    inst.kind = definition["kind"]
    inst
  end
end

Instance Method Details

#create_definition_tempfileObject



45
46
47
48
49
50
51
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 45

def create_definition_tempfile
  file = Tempfile.new(["#{@namespace}-#{@kind}-#{@name}", ".yaml"])
  file.write(YAML.dump(@definition))
  file
ensure
  file&.close
end

#fileObject



41
42
43
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 41

def file
  @file ||= create_definition_tempfile
end

#filepathObject



37
38
39
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 37

def filepath
  @filepath ||= file.path
end

#syncObject



53
54
55
# File 'lib/kube_deploy_tools/kubernetes_resource.rb', line 53

def sync
  # unimplemented
end