Sha256: dd325ba34dc37f19c458996e0b513dce9b720c775b65bda96d51d467c2764781

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Metatron
  module Templates
    # The Service Kubernetes resource
    class Service < Template
      include Concerns::Annotated

      attr_accessor :type, :selector, :additional_labels, :ports,
                    :additional_selector_labels, :publish_not_ready_addresses

      def initialize(name, port = nil)
        super(name)
        @kind = "Service"
        @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

      alias publishNotReadyAddresses publish_not_ready_addresses

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
metatron-0.1.4 lib/metatron/templates/service.rb
metatron-0.1.3 lib/metatron/templates/service.rb
metatron-0.1.2 lib/metatron/templates/service.rb
metatron-0.1.1 lib/metatron/templates/service.rb
metatron-0.1.0 lib/metatron/templates/service.rb