Skip to content
17 changes: 16 additions & 1 deletion pkg/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,29 @@ func enableOrDisableAddonInternal(cc *config.ClusterConfig, addon *assets.Addon,
}
}

if addon.HelmChart != nil {
err := helmInstallBinary(addon, runner)
if err != nil {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
cmd := helmUninstallOrInstall(ctx, addon.HelmChart, enable)
_, err = runner.RunCmd(cmd)
return err
}

// on the first attempt try without force, but on subsequent attempts use force
force := false

// Retry, because sometimes we race against an apiserver restart
apply := func() error {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
_, err := runner.RunCmd(kubectlCommand(ctx, cc, deployFiles, enable, force))
cmd := kubectlCommand(ctx, cc, deployFiles, enable, force)
_, err := runner.RunCmd(cmd)

if err != nil {
klog.Warningf("apply failed, will retry: %v", err)
force = true
Expand Down
93 changes: 93 additions & 0 deletions pkg/addons/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2025 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package addons

import (
"context"
"fmt"
"os/exec"
"path"

"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/vmpath"
)

// runs a helm install within the minikube vm or container based on the contents of chart *assets.HelmChart
func installHelmChart(ctx context.Context, chart *assets.HelmChart) *exec.Cmd {
args := []string{
fmt.Sprintf("KUBECONFIG=%s", path.Join(vmpath.GuestPersistentDir, "kubeconfig")),
"helm", "upgrade", "--install", chart.Name, chart.Repo, "--create-namespace",
}
if chart.Namespace != "" {
args = append(args, "--namespace", chart.Namespace)
}

if chart.Values != nil {
for _, value := range chart.Values {
args = append(args, "--set", value)
}
}

if chart.ValueFiles != nil {
for _, value := range chart.ValueFiles {
args = append(args, "--values", value)
}
}

return exec.CommandContext(ctx, "sudo", args...)
}

// runs a helm uninstall based on the contents of chart *assets.HelmChart
func uninstalllHelmChart(ctx context.Context, chart *assets.HelmChart) *exec.Cmd {
args := []string{
fmt.Sprintf("KUBECONFIG=%s", path.Join(vmpath.GuestPersistentDir, "kubeconfig")),
"helm", "uninstall", chart.Name,
}
if chart.Namespace != "" {
args = append(args, "--namespace", chart.Namespace)
}
return exec.CommandContext(ctx, "sudo", args...)
}

// based on enable will execute installHelmChart or uninstallHelmChart
func helmUninstallOrInstall(ctx context.Context, chart *assets.HelmChart, enable bool) *exec.Cmd {
if enable {
return installHelmChart(ctx, chart)
}
return uninstalllHelmChart(ctx, chart)
}

func helmInstallBinary(addon *assets.Addon, runner command.Runner) error {
_, err := runner.RunCmd(exec.Command("test", "-f", "/usr/bin/helm"))
if err != nil {
_, err = runner.RunCmd(exec.Command("test", "-d", "/usr/local/bin"))
if err != nil {
_, err = runner.RunCmd(exec.Command("sudo", "mkdir", "-p", "/usr/local/bin"))

Check failure on line 81 in pkg/addons/helm.go

View workflow job for this annotation

GitHub Actions / Lint-

ineffectual assignment to err (ineffassign)
}

installCmd := fmt.Sprint("curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 && chmod 700 get_helm.sh && ./get_helm.sh")
_, err = runner.RunCmd(exec.Command("sudo", "bash", "-c", installCmd))

Check failure on line 85 in pkg/addons/helm.go

View workflow job for this annotation

GitHub Actions / Lint-

ineffectual assignment to err (ineffassign)
// we copy the binary from /usr/local/bin to /usr/bin because /usr/local/bin is not in PATH in both iso and kicbase
_, err = runner.RunCmd(exec.Command("sudo", "mv", "/usr/local/bin/helm", "/usr/bin/helm"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment explain the reason we do this is /usr/loca/bin/helm is not in PATH on both iso/kicbase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

if err != nil {
return errors.Wrap(err, "installing helm")
}
}
return err
}
79 changes: 79 additions & 0 deletions pkg/addons/helm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2025 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package addons

import (
"context"
"strings"
"testing"

"k8s.io/minikube/pkg/minikube/assets"
)

func TestHelmCommand(t *testing.T) {
tests := []struct {
description string
chart *assets.HelmChart
enable bool
expected string
mode string
}{
{
description: "enable an addon",
chart: &assets.HelmChart{
Name: "addon-name",
Repo: "addon-repo/addon-chart",
Namespace: "addon-namespace",
Values: []string{"key=value"},
ValueFiles: []string{"/etc/kubernetes/addons/values.yaml"},
},
enable: true,
expected: "sudo KUBECONFIG=/var/lib/minikube/kubeconfig helm upgrade --install addon-name addon-repo/addon-chart --create-namespace --namespace addon-namespace --set key=value --values /etc/kubernetes/addons/values.yaml",
},
{
description: "enable an addon without namespace",
chart: &assets.HelmChart{
Name: "addon-name",
Repo: "addon-repo/addon-chart",
Values: []string{"key=value"},
ValueFiles: []string{"/etc/kubernetes/addons/values.yaml"},
},
enable: true,
expected: "sudo KUBECONFIG=/var/lib/minikube/kubeconfig helm upgrade --install addon-name addon-repo/addon-chart --create-namespace --set key=value --values /etc/kubernetes/addons/values.yaml",
},
{
description: "disable an addon",
chart: &assets.HelmChart{
Name: "addon-name",
Namespace: "addon-namespace",
},
enable: false,
expected: "sudo KUBECONFIG=/var/lib/minikube/kubeconfig helm uninstall addon-name --namespace addon-namespace",
mode: "cpu",
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
command := helmUninstallOrInstall(context.Background(), test.chart, test.enable)
actual := strings.Join(command.Args, " ")
if actual != test.expected {
t.Errorf("helm command mismatch:\nexpected: %s\nactual: %s", test.expected, actual)
}
})
}
}
Loading
Loading