Class: Metatron::Templates::GRPCRoute

Inherits:
Metatron::Template show all
Includes:
Concerns::Annotated, Concerns::Namespaced, Concerns::RouteResource
Defined in:
lib/metatron/templates/grpc_route.rb,
lib/metatron/templates/grpc_route/rule.rb,
lib/metatron/templates/grpc_route/filter.rb

Overview

Template for GRPCRoute k8s resource (Gateway API)

Defined Under Namespace

Classes: Filter, Rule

Instance Attribute Summary collapse

Attributes inherited from Metatron::Template

#api_version, #base_labels, #kind, #name

Instance Method Summary collapse

Methods included from Concerns::RouteResource

#add_parent_ref, #formatted_parent_refs, included, #route_resource_initialize

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) ⇒ GRPCRoute

Returns a new instance of GRPCRoute.



14
15
16
17
18
19
20
# File 'lib/metatron/templates/grpc_route.rb', line 14

def initialize(name)
  super
  @api_version = "gateway.networking.k8s.io/v1"
  @additional_labels = {}
  @hostnames = []
  @rules = []
end

Instance Attribute Details

#additional_labelsObject

Returns the value of attribute additional_labels.



11
12
13
# File 'lib/metatron/templates/grpc_route.rb', line 11

def additional_labels
  @additional_labels
end

#hostnamesObject (readonly)

Returns the value of attribute hostnames.



12
13
14
# File 'lib/metatron/templates/grpc_route.rb', line 12

def hostnames
  @hostnames
end

#rulesObject (readonly)

Returns the value of attribute rules.



12
13
14
# File 'lib/metatron/templates/grpc_route.rb', line 12

def rules
  @rules
end

Instance Method Details

#add_hostname(hostname) ⇒ Object



22
23
24
# File 'lib/metatron/templates/grpc_route.rb', line 22

def add_hostname(hostname)
  @hostnames << hostname
end

#add_rule(rule = nil, **attrs) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/metatron/templates/grpc_route.rb', line 26

def add_rule(rule = nil, **attrs)
  @rules << if rule.is_a?(Rule)
              rule
            else
              Rule.new(**attrs)
            end
end

#formatted_hostnamesObject



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

def formatted_hostnames
  hostnames.empty? ? {} : { hostnames: }
end

#renderObject



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

def render
  {
    apiVersion:,
    kind:,
    metadata: {
      name:,
      labels: base_labels.merge(additional_labels)
    }.merge(formatted_annotations).merge(formatted_namespace),
    spec: formatted_parent_refs
      .merge(formatted_hostnames)
      .merge(rules.empty? ? {} : { rules: rules.map(&:render) })
  }
end