Class: Kube::Helm::Chart
- Inherits:
-
Object
- Object
- Kube::Helm::Chart
- Defined in:
- lib/kube/helm/chart.rb
Overview
Represents a Helm Chart.yaml as a Ruby object.
The Chart holds metadata from Chart.yaml and can render resources via #apply_values. It can be backed by either a local directory (with templates on disk) or a remote chart reference.
# Virtual chart (just metadata, no templates)
chart = Kube::Helm::Chart.new {
name = "my-app"
version = "1.0.0"
appVersion = "3.4.5"
}
# Load from a local chart directory
chart = Kube::Helm::Chart.open("./charts/my-app")
manifest = chart.apply_values({ "replicaCount" => 3 })
# From a remote repo
manifest = Kube::Helm::Repo
.new("bitnami", url: "https://charts.bitnami.com/bitnami")
.fetch("nginx", version: "18.1.0")
.apply_values({ "replicaCount" => 3 })
Instance Attribute Summary collapse
-
#cluster ⇒ Object
readonly
Returns the value of attribute cluster.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#ref ⇒ Object
readonly
Returns the value of attribute ref.
Class Method Summary collapse
-
.open(path, cluster: nil) ⇒ Chart
Open a chart from a local directory.
Instance Method Summary collapse
- #app_version ⇒ Object
-
#apply_values(values, release: nil, namespace: nil) ⇒ Kube::Schema::Manifest
Render the chart templates with values applied.
-
#crds ⇒ Array<Kube::Cluster::CustomResourceDefinition>
Return the chart’s CRDs as Kube::Cluster::CustomResourceDefinition objects.
- #dependencies ⇒ Object
- #description ⇒ Object
-
#initialize(data = {}, path: nil, ref: nil, cluster: nil, &block) ⇒ Chart
constructor
A new instance of Chart.
- #name ⇒ Object
-
#show_values ⇒ Hash
Show the chart’s default values.
- #to_s ⇒ Object
- #type ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(data = {}, path: nil, ref: nil, cluster: nil, &block) ⇒ Chart
Returns a new instance of Chart.
52 53 54 55 56 57 58 |
# File 'lib/kube/helm/chart.rb', line 52 def initialize(data = {}, path: nil, ref: nil, cluster: nil, &block) @data = deep_symbolize_keys(data) @path = path @ref = ref @cluster = cluster @data.instance_exec(&block) if block_given? end |
Instance Attribute Details
#cluster ⇒ Object (readonly)
Returns the value of attribute cluster.
32 33 34 |
# File 'lib/kube/helm/chart.rb', line 32 def cluster @cluster end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
32 33 34 |
# File 'lib/kube/helm/chart.rb', line 32 def path @path end |
#ref ⇒ Object (readonly)
Returns the value of attribute ref.
32 33 34 |
# File 'lib/kube/helm/chart.rb', line 32 def ref @ref end |
Class Method Details
.open(path, cluster: nil) ⇒ Chart
Open a chart from a local directory. Reads Chart.yaml and stores the path so that helm commands run against the local chart.
40 41 42 43 44 45 46 |
# File 'lib/kube/helm/chart.rb', line 40 def self.open(path, cluster: nil) chart_file = File.join(path, "Chart.yaml") raise Kube::Error, "No Chart.yaml found at #{path}" unless File.exist?(chart_file) yaml = YAML.safe_load_file(chart_file) new(yaml, path: path, cluster: cluster) end |
Instance Method Details
#app_version ⇒ Object
62 |
# File 'lib/kube/helm/chart.rb', line 62 def app_version = @data[:appVersion] |
#apply_values(values, release: nil, namespace: nil) ⇒ Kube::Schema::Manifest
Render the chart templates with values applied.
Shells out to ‘helm template` and returns a Manifest of typed Resource objects.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/kube/helm/chart.rb', line 76 def apply_values(values, release: nil, namespace: nil) raise Kube::Error, "No chart source" unless source release_name = release || name source_ref = source ver = version_flag cmd = helm.call { template.(release_name).(source_ref) } cmd = cmd.version(ver) if ver cmd = cmd.namespace(namespace) if namespace if values.is_a?(Hash) && values.any? tmpfile = write_values_tempfile(values) cmd = cmd.f(tmpfile.path) end Kube::Schema::Manifest.parse(helm.run(cmd.to_s)) end |
#crds ⇒ Array<Kube::Cluster::CustomResourceDefinition>
Return the chart’s CRDs as Kube::Cluster::CustomResourceDefinition objects.
First tries ‘helm show crds`. If that returns nothing (some charts ship CRDs in templates rather than the crds/ directory), falls back to rendering via `helm template –set installCRDs=true` and filtering for CustomResourceDefinition resources.
chart.crds.each do |crd|
s = crd.to_json_schema
Kube::Schema.register(s[:kind], schema: s[:schema], api_version: s[:api_version])
end
122 123 124 125 126 127 128 |
# File 'lib/kube/helm/chart.rb', line 122 def crds raise Kube::Error, "No chart source" unless source results = crds_from_show results = crds_from_template if results.empty? results end |
#dependencies ⇒ Object
65 |
# File 'lib/kube/helm/chart.rb', line 65 def dependencies = @data[:dependencies] || [] |
#description ⇒ Object
63 |
# File 'lib/kube/helm/chart.rb', line 63 def description = @data[:description] |
#name ⇒ Object
60 |
# File 'lib/kube/helm/chart.rb', line 60 def name = @data[:name] |
#show_values ⇒ Hash
Show the chart’s default values.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/kube/helm/chart.rb', line 98 def show_values raise Kube::Error, "No chart source" unless source source_ref = source ver = version_flag cmd = helm.call { show.values.(source_ref) } cmd = cmd.version(ver) if ver YAML.safe_load(helm.run(cmd.to_s), permitted_classes: [Symbol]) || {} end |
#to_s ⇒ Object
130 131 132 |
# File 'lib/kube/helm/chart.rb', line 130 def to_s version ? "#{name}:#{version}" : name.to_s end |
#type ⇒ Object
64 |
# File 'lib/kube/helm/chart.rb', line 64 def type = @data[:type] |
#version ⇒ Object
61 |
# File 'lib/kube/helm/chart.rb', line 61 def version = @data[:version] |