Class: Kube::Helm::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/kube/helm/repo.rb

Overview

Models a Helm chart repository (traditional or OCI).

Wraps the lifecycle commands (‘helm repo add`, `helm repo update`, `helm repo remove`) and fetches charts for rendering.

When a cluster is provided, all Helm commands are scoped to that cluster’s kubeconfig.

repo = Kube::Helm::Repo.new("bitnami", url: "https://charts.bitnami.com/bitnami")
chart = repo.fetch("nginx", version: "18.1.0")
manifest = chart.apply_values({ "replicaCount" => 3 })

# One-liner
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

Instance Method Summary collapse

Constructor Details

#initialize(name, url:, cluster: nil) ⇒ Repo

Returns a new instance of Repo.

Parameters:

  • name (String)

    local alias for this repo (e.g. “bitnami”)

  • url (String)

    repository URL (http(s) for traditional, oci:// for OCI)

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

    optional cluster connection



37
38
39
40
41
42
43
44
45
# File 'lib/kube/helm/repo.rb', line 37

def initialize(name, url:, cluster: nil)
  unless name.is_a?(String) && !name.strip.empty?
    raise ArgumentError, "name must be a non-empty String"
  end

  @name     = name
  @endpoint = Endpoint.new(url)
  @cluster  = cluster
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



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

def cluster
  @cluster
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#addself

Register this repo with the local Helm client. No-op for OCI registries.

Returns:

  • (self)


51
52
53
54
55
56
57
58
59
# File 'lib/kube/helm/repo.rb', line 51

def add
  if endpoint.requires_add?
    repo_name = @name
    repo_url = endpoint.url
    cmd = helm.call { repo.add.(repo_name).(repo_url) }
    helm.run(cmd.to_s)
  end
  self
end

#fetch(chart_name, version: nil) ⇒ Chart

Fetch a chart from this repo.

Adds and updates the repo, retrieves the Chart.yaml metadata via ‘helm show chart`, and returns a Chart object with the ref set for subsequent helm commands.

Parameters:

  • chart_name (String)

    the chart name (e.g. “nginx”)

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

    chart version constraint (e.g. “18.1.0”)

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/kube/helm/repo.rb', line 96

def fetch(chart_name, version: nil)
  add
  update

  ref = endpoint.chart_ref(chart_name, repo_name: @name)

  cmd = helm.call { show.chart.(ref) }
  cmd = cmd.version(version) if version
  yaml_output = helm.run(cmd.to_s)

  data = YAML.safe_load(yaml_output, permitted_classes: [Symbol]) || {}
  Chart.new(data, ref: ref, cluster: @cluster)
end

#oci?Boolean

Is this an OCI-backed repo?

Returns:

  • (Boolean)


111
112
113
# File 'lib/kube/helm/repo.rb', line 111

def oci?
  endpoint.oci?
end

#removeself

Remove this repo from the local Helm client. No-op for OCI registries.

Returns:

  • (self)


78
79
80
81
82
83
84
85
# File 'lib/kube/helm/repo.rb', line 78

def remove
  if endpoint.requires_add?
    repo_name = @name
    cmd = helm.call { repo.remove.(repo_name) }
    helm.run(cmd.to_s)
  end
  self
end

#to_sObject



115
116
117
# File 'lib/kube/helm/repo.rb', line 115

def to_s
  "#{@name} (#{endpoint.url})"
end

#updateself

Update the local chart index for this repo. No-op for OCI registries.

Returns:

  • (self)


65
66
67
68
69
70
71
72
# File 'lib/kube/helm/repo.rb', line 65

def update
  if endpoint.requires_add?
    repo_name = @name
    cmd = helm.call { repo.update.(repo_name) }
    helm.run(cmd.to_s)
  end
  self
end