› kube_cluster › Source › Kube › Helm › Chart

class Chart

Represents a Helm Chart.yaml as a Ruby object.

Example: Virtual chart (just metadata, no templates)

chart = Kube::Helm::Chart.new {
  name = "my-app"
  version = "1.0.0"
  appVersion = "3.4.5"
}

Example: Load from a local chart directory

chart = Kube::Helm::Chart.open("./charts/my-app")
manifest = chart.apply_values({ "replicaCount" => 3 })

Example: 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 })

Signature

Definitions

def self.open(path, cluster: nil)

Open a chart from a local directory.

Signature

parameter path String

path to the chart directory.

parameter cluster Kube::Cluster::Instance, nil

an optional cluster connection.

returns Chart

the chart, with path set for local helm commands.

Implementation

def self.open(path, cluster: nil)
  chart_file = File.join(path, "Chart.yaml")
  unless File.exist?(chart_file)
    raise Kube::Error, "No Chart.yaml found at #{path}"
  end

  yaml = YAML.safe_load_file(chart_file)
  new(yaml, path: path, cluster: cluster)
end

def initialize(data = {}, path: nil, ref: nil, cluster: nil, &block)

Signature

parameter data Hash

Chart.yaml content, string or symbol keyed.

parameter path String, nil

a filesystem path to a local chart directory.

parameter ref String, nil

a chart reference for helm commands on remote charts.

parameter cluster Kube::Cluster::Instance, nil

an optional cluster connection.

Implementation

def initialize(data = {}, path: nil, ref: nil, cluster: nil, &block)
  @data    = deep_symbolize_keys(data)
  @path    = path
  @ref     = ref
  @cluster = cluster
  if block_given?
    @data.instance_exec(&block)
  end
end

def apply_values(values, release: nil, namespace: nil)

Renders the chart templates with values applied.

Signature

parameter values Hash

values to apply to the chart templates.

parameter release String, nil

a release name, defaulting to the chart name.

parameter namespace String, nil

a namespace for the rendered resources.

returns Kube::Schema::Manifest

the rendered resources, via helm template.

Implementation

def apply_values(values, release: nil, namespace: nil)
  unless source
    raise Kube::Error, "No chart source"
  end

  release_name = release || name
  source_ref = source
  ver = version_flag

  cmd = helm.call { template.(release_name).(source_ref).include_crds(true) }
  if ver
    cmd = cmd.version(ver)
  end
  if namespace
    cmd = cmd.namespace(namespace)
  end

  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

def show_values

Show the chart's default values.

Signature

returns Hash

the default values, via helm show values.

Implementation

def show_values
  unless source
    raise Kube::Error, "No chart source"
  end

  source_ref = source
  ver = version_flag
  cmd = helm.call { show.values.(source_ref) }
  if ver
    cmd = cmd.version(ver)
  end

  YAML.safe_load(helm.run(cmd.to_s), permitted_classes: [Symbol]) || {}
end

def crds

The chart's CRDs, as typed CustomResourceDefinition objects.

Example.

chart.crds.each do |crd|
  s = crd.to_json_schema
  Kube::Schema.register(s[:kind], schema: s[:schema], api_version: s[:api_version])
end

Signature

returns Array(Kube::Cluster::CustomResourceDefinition)

the chart's CRDs.

Implementation

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

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

def split_documents(yaml_output)

Splits concatenated CRD YAML at each top-level apiVersion:.

Signature

parameter yaml_output String

the raw helm show crds output, which joins several CRDs without a "---", silently losing all but the last.

returns String

the same YAML, with "---" before each document.

Implementation

def split_documents(yaml_output)
  yaml_output.gsub(/\n(?=apiVersion:)/, "\n---\n")
end

def source

The chart source for helm commands.

Signature

returns String, nil

the local path, the remote ref, or nil.

Implementation

def source
  @path || @ref
end

def version_flag

Version flag for remote charts. Local charts don't need --version.

Signature

returns String, nil

the chart version, or nil for local charts.

Implementation

def version_flag
  @ref ? version : nil
end