Class: Kube::Cluster::Manifest::Middleware::ServiceForDeployment
- Inherits:
-
Kube::Cluster::Manifest::Middleware
- Object
- Kube::Cluster::Manifest::Middleware
- Kube::Cluster::Manifest::Middleware::ServiceForDeployment
- Defined in:
- lib/kube/cluster/manifest/middleware/service_for_deployment.rb
Overview
Generates a Service for every pod-bearing resource that has containers with named ports.
The generated Service uses spec.selector.matchLabels from the source resource and maps each named container port.
Labels and namespace are copied from the source resource, so subsequent middleware (Labels, Namespace, etc.) will also apply to the generated Service.
stack do
use Middleware::ServiceForDeployment
end
Constant Summary
Constants inherited from Kube::Cluster::Manifest::Middleware
CLUSTER_SCOPED_KINDS, POD_BEARING_KINDS
Instance Method Summary collapse
Methods inherited from Kube::Cluster::Manifest::Middleware
Constructor Details
This class inherits a constructor from Kube::Cluster::Manifest::Middleware
Instance Method Details
#call(resource) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/kube/cluster/manifest/middleware/service_for_deployment.rb', line 22 def call(resource) return resource unless pod_bearing?(resource) h = resource.to_h ports = extract_ports(h) return resource if ports.empty? match_labels = h.dig(:spec, :selector, :matchLabels) return resource unless match_labels && !match_labels.empty? service = Kube::Schema["Service"].new { .name = h.dig(:metadata, :name) .namespace = h.dig(:metadata, :namespace) if h.dig(:metadata, :namespace) .labels = h.dig(:metadata, :labels) || {} spec.selector = match_labels spec.ports = ports.map { |p| { name: p[:name], port: p[:containerPort], targetPort: p[:name], protocol: p.fetch(:protocol, "TCP"), } } } [resource, service] end |