Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkg/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,31 @@ func enableOrDisableAddonInternal(cc *config.ClusterConfig, addon *assets.Addon,
}
Copy link
Member

Choose a reason for hiding this comment

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

we can add a documentation to the website https://minikube.sigs.k8s.io/docs/contrib/addons/

we can add a block (how to add helm based addons)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed a new site doc with some explanation. As we work on adding a actual addon we will update the doc for the helm based 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
89 changes: 89 additions & 0 deletions pkg/addons/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
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 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"))
}

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))
_, err = runner.RunCmd(exec.Command("sudo", "mv", "/usr/local/bin/helm", "/usr/bin/helm"))
if err != nil {
return errors.Wrap(err, "installing helm")
}
}
return err
}
76 changes: 76 additions & 0 deletions pkg/addons/helm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
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