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



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

#clusterObject (readonly)

Returns the value of attribute cluster.



32
33
34
# File 'lib/kube/helm/chart.rb', line 32

def cluster
  @cluster
end

#pathObject (readonly)

Returns the value of attribute path.



32
33
34
# File 'lib/kube/helm/chart.rb', line 32

def path
  @path
end

#refObject (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.

Parameters:

  • path (String)

    path to the chart directory

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

    optional cluster connection

Returns:

Raises:



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_versionObject



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.

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:



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

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



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

#dependenciesObject



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

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

#descriptionObject



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

def description  = @data[:description]

#nameObject



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

def name         = @data[:name]

#show_valuesHash

Show the chart’s default values.

Returns:

  • (Hash)

    the default values as a Ruby hash

Raises:



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_sObject



130
131
132
# File 'lib/kube/helm/chart.rb', line 130

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

#typeObject



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

def type         = @data[:type]

#versionObject



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

def version      = @data[:version]