Class: Kube::Schema::Manifest

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*resources, filename: nil) ⇒ Manifest

Create a new Manifest, optionally seeded with resources.

Parameters:

  • resources (Array<Resource, Manifest>)

    initial resources to include

  • filename (String, nil) (defaults to: nil)

    optional filename for file-backed manifests



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

#filenameObject (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

Read a YAML file containing one or more Kubernetes resource documents and return a Manifest populated with Resource objects.

manifest = Kube::Schema::Manifest.open("deploy.yaml")
manifest.count  #=> 3
manifest.filename #=> "deploy.yaml"

Parameters:

  • path (String)

    path to a YAML file

Returns:



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.

Parameters:

Returns:

  • (self)


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

Yields:

  • (Resource)

    each resource in insertion order



64
65
66
# File 'lib/kube/schema/manifest.rb', line 64

def each(&block)
  @resources.each(&block)
end

#sizeObject 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_aArray<Hash>

Returns an array of resource hashes.

Returns:

  • (Array<Hash>)


85
86
87
# File 'lib/kube/schema/manifest.rb', line 85

def to_a
  @resources.dup
end

#to_yamlString

Returns the manifest as multi-document YAML, separated by “—”. This is the standard format for Kubernetes manifest files.

Returns:

  • (String)


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.

Parameters:

  • path (String, nil) (defaults to: nil)

    destination path; defaults to the filename the manifest was opened from.

Returns:

  • (String)

    the path written to

Raises:

  • (ArgumentError)

    if no path is given and no filename is set



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