Class: Kube::Schema::Manifest
- Inherits:
-
Object
- Object
- Kube::Schema::Manifest
- Includes:
- Enumerable
- Defined in:
- lib/kube/schema/manifest.rb
Overview
A flat, ordered collection of Kubernetes resources.
Manifest is Enumerable over Resource objects. It cannot be nested —appending one Manifest into another extracts and flattens its resources.
manifest = Kube::Schema::Manifest.new
manifest << deployment_resource
manifest << another_manifest # flattened automatically
puts manifest.to_yaml # multi-document YAML output
File I/O:
manifest = Kube::Schema::Manifest.open("cluster.yaml")
manifest << new_resource
manifest.write
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Class Method Summary collapse
-
.open(path) ⇒ Manifest
Read a YAML file containing one or more Kubernetes resource documents and return a Manifest populated with Resource objects.
Instance Method Summary collapse
-
#<<(item) ⇒ self
Append a resource, manifest, or array of resources.
- #each {|Resource| ... } ⇒ Object
-
#initialize(*resources, filename: nil) ⇒ Manifest
constructor
Create a new Manifest, optionally seeded with resources.
-
#size ⇒ Object
(also: #length)
Number of resources in the manifest.
-
#to_a ⇒ Array<Hash>
Returns an array of resource hashes.
-
#to_yaml ⇒ String
Returns the manifest as multi-document YAML, separated by “—”.
-
#write(path = nil) ⇒ String
Write the manifest to a file as multi-document YAML.
Constructor Details
#initialize(*resources, filename: nil) ⇒ Manifest
Create a new Manifest, optionally seeded with resources.
31 32 33 34 35 36 |
# File 'lib/kube/schema/manifest.rb', line 31 def initialize(*resources, filename: nil) @resources = [] @filename = filename resources.each { |r| self << r } end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
25 26 27 |
# File 'lib/kube/schema/manifest.rb', line 25 def filename @filename end |
Class Method Details
.open(path) ⇒ Manifest
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/kube/schema/manifest.rb', line 102 def self.open(path) contents = File.read(path) docs = if YAML.respond_to?(:safe_load_stream) YAML.safe_load_stream(contents, permitted_classes: [Symbol]) else # Ruby < 3.1 fallback YAML.load_stream(contents) end resources = docs.compact.map { |doc| Resource.new(doc) } new(*resources, filename: path) end |
Instance Method Details
#<<(item) ⇒ self
Append a resource, manifest, or array of resources.
-
Resource objects are appended directly.
-
Manifest objects are flattened — their resources are extracted.
-
Arrays are iterated and each element is appended.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kube/schema/manifest.rb', line 46 def <<(item) case item when Manifest item.each { |r| @resources << r } when Array item.each { |r| self << r } when Resource @resources << item else raise ArgumentError, "Expected a Kube::Schema::Resource or Manifest, got #{item.class}. " \ "Use Kube::Schema.parse(hash) once it is implemented to convert hashes." end self end |
#each {|Resource| ... } ⇒ Object
64 65 66 |
# File 'lib/kube/schema/manifest.rb', line 64 def each(&block) @resources.each(&block) end |
#size ⇒ Object Also known as: length
Number of resources in the manifest.
69 70 71 |
# File 'lib/kube/schema/manifest.rb', line 69 def size @resources.size end |
#to_a ⇒ Array<Hash>
Returns an array of resource hashes.
85 86 87 |
# File 'lib/kube/schema/manifest.rb', line 85 def to_a @resources.dup end |
#to_yaml ⇒ String
Returns the manifest as multi-document YAML, separated by “—”. This is the standard format for Kubernetes manifest files.
78 79 80 |
# File 'lib/kube/schema/manifest.rb', line 78 def to_yaml @resources.map { |r| r.to_yaml }.join("") end |
#write(path = nil) ⇒ String
Write the manifest to a file as multi-document YAML.
121 122 123 124 125 126 127 128 |
# File 'lib/kube/schema/manifest.rb', line 121 def write(path = nil) path ||= @filename raise ArgumentError, "No filename set. Pass a path to #write or use Manifest.open." if path.nil? File.write(path, to_yaml) @filename = path path end |