Class: Kube::Helm::Chart

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, path: nil, ref: nil, cluster: nil, &block) ⇒ Chart

Returns a new instance of Chart.

Parameters:

  • data (Hash) (defaults to: {})

    Chart.yaml content (string or symbol keys)

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

    filesystem path to chart directory (local charts)

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

    chart reference for helm commands (remote charts)

  • cluster (Kube::Cluster::Instance, nil) (defaults to: nil)

    optional cluster connection



57
58
59
60
61
62
63
# File 'lib/kube/helm/chart.rb', line 57

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

#clusterObject (readonly)

Returns the value of attribute cluster.



37
38
39
# File 'lib/kube/helm/chart.rb', line 37

def cluster
  @cluster
end

#pathObject (readonly)

Returns the value of attribute path.



37
38
39
# File 'lib/kube/helm/chart.rb', line 37

def path
  @path
end

#refObject (readonly)

Returns the value of attribute ref.



37
38
39
# File 'lib/kube/helm/chart.rb', line 37

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.

Parameters:

  • path (String)

    path to the chart directory

  • cluster (Kube::Cluster::Instance, nil) (defaults to: nil)

    optional cluster connection

Returns:

Raises:



45
46
47
48
49
50
51
# File 'lib/kube/helm/chart.rb', line 45

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_versionObject



67
# File 'lib/kube/helm/chart.rb', line 67

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.

Parameters:

  • values (Hash)

    values to apply to the chart templates

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

    release name (defaults to chart name)

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

    namespace for rendered resources

Returns:

  • (Kube::Schema::Manifest)

Raises:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kube/helm/chart.rb', line 81

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).include_crds(true) }
  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

#crdsArray<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

Returns:

  • (Array<Kube::Cluster::CustomResourceDefinition>)

Raises:



127
128
129
130
131
132
133
# File 'lib/kube/helm/chart.rb', line 127

def crds
  raise Kube::Error, "No chart source" unless source

  results = crds_from_show
  results = crds_from_template if results.empty?
  results
end

#dependenciesObject



70
# File 'lib/kube/helm/chart.rb', line 70

def dependencies = @data[:dependencies] || []

#descriptionObject



68
# File 'lib/kube/helm/chart.rb', line 68

def description  = @data[:description]

#nameObject



65
# File 'lib/kube/helm/chart.rb', line 65

def name         = @data[:name]

#show_valuesHash

Show the chart’s default values.

Returns:

  • (Hash)

    the default values as a Ruby hash

Raises:



103
104
105
106
107
108
109
110
111
112
# File 'lib/kube/helm/chart.rb', line 103

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_sObject



135
136
137
# File 'lib/kube/helm/chart.rb', line 135

def to_s
  version ? "#{name}:#{version}" : name.to_s
end

#typeObject



69
# File 'lib/kube/helm/chart.rb', line 69

def type         = @data[:type]

#versionObject



66
# File 'lib/kube/helm/chart.rb', line 66

def version      = @data[:version]