Class: Metatron::Templates::Service

Inherits:
Metatron::Template show all
Includes:
Concerns::Annotated, Concerns::Namespaced
Defined in:
lib/metatron/templates/service.rb

Overview

The Service Kubernetes resource

Instance Attribute Summary collapse

Attributes inherited from Metatron::Template

#api_version, #kind, #label_namespace, #name

Instance Method Summary collapse

Methods included from Concerns::Namespaced

#formatted_namespace, included, #namespaced_initialize

Methods included from Concerns::Annotated

#annotated_initialize, #formatted_annotations, included

Methods inherited from Metatron::Template

initializer, initializers, metatron_template_class?, nearest_metatron_ancestor

Constructor Details

#initialize(name, port = nil) ⇒ Service

Returns a new instance of Service.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/metatron/templates/service.rb', line 13

def initialize(name, port = nil)
  super(name)
  @type = "ClusterIP"
  @selector = { "#{label_namespace}/name": name }
  @additional_labels = {}
  @additional_selector_labels = {}
  @publish_not_ready_addresses = false
  return unless port

  @ports = [
    {
      port: port.to_i,
      targetPort: port.to_i,
      protocol: "TCP",
      name:
    }
  ]
end

Instance Attribute Details

#additional_labelsObject

Returns the value of attribute additional_labels.



10
11
12
# File 'lib/metatron/templates/service.rb', line 10

def additional_labels
  @additional_labels
end

#additional_selector_labelsObject

Returns the value of attribute additional_selector_labels.



10
11
12
# File 'lib/metatron/templates/service.rb', line 10

def additional_selector_labels
  @additional_selector_labels
end

#portsObject

Returns the value of attribute ports.



10
11
12
# File 'lib/metatron/templates/service.rb', line 10

def ports
  @ports
end

#publish_not_ready_addressesObject Also known as: publishNotReadyAddresses

Returns the value of attribute publish_not_ready_addresses.



10
11
12
# File 'lib/metatron/templates/service.rb', line 10

def publish_not_ready_addresses
  @publish_not_ready_addresses
end

#selectorObject

Returns the value of attribute selector.



10
11
12
# File 'lib/metatron/templates/service.rb', line 10

def selector
  @selector
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/metatron/templates/service.rb', line 10

def type
  @type
end

Instance Method Details

#formatted_portsObject



34
35
36
# File 'lib/metatron/templates/service.rb', line 34

def formatted_ports
  ports&.any? ? { ports: } : {}
end

#renderObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/metatron/templates/service.rb', line 38

def render
  {
    apiVersion:,
    kind:,
    metadata: {
      name:,
      labels: { "#{label_namespace}/name": name }.merge(additional_labels)
    }.merge(formatted_annotations).merge(formatted_namespace),
    spec: {
      type:,
      selector: selector.merge(additional_selector_labels),
      publishNotReadyAddresses:
    }.merge(formatted_ports)
  }
end