kube_clusterSourceKubeHelmChart

class Chart

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 )

Definitions

def self.open(path, cluster: nil)

Open a chart from a local directory. Reads Chart.yaml and stores the path so that helm commands run against the local chart.

Implementation

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

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

Implementation

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

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

Render the chart templates with values applied.

Shells out to helm template and returns a Manifest of typed Resource objects.

Implementation

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

def show_values

Show the chart's default values.

Implementation

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

def crds

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

Implementation

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

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

def source

The chart source for helm commands — either a local path or a remote ref.

Implementation

def source
  @path || @ref
end

def version_flag

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

Implementation

def version_flag
  @ref ? version : nil
end