|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package addons |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os/exec" |
| 23 | + "path" |
| 24 | + |
| 25 | + "github.com/pkg/errors" |
| 26 | + "k8s.io/minikube/pkg/minikube/assets" |
| 27 | + "k8s.io/minikube/pkg/minikube/command" |
| 28 | + "k8s.io/minikube/pkg/minikube/vmpath" |
| 29 | +) |
| 30 | + |
| 31 | +// runs a helm install within the minikube vm or container based on the contents of chart *assets.HelmChart |
| 32 | +func installHelmChart(ctx context.Context, chart *assets.HelmChart) *exec.Cmd { |
| 33 | + args := []string{ |
| 34 | + fmt.Sprintf("KUBECONFIG=%s", path.Join(vmpath.GuestPersistentDir, "kubeconfig")), |
| 35 | + "helm", "upgrade", "--install", chart.Name, chart.Repo, "--create-namespace", |
| 36 | + } |
| 37 | + if chart.Namespace != "" { |
| 38 | + args = append(args, "--namespace", chart.Namespace) |
| 39 | + } |
| 40 | + |
| 41 | + if chart.Values != nil { |
| 42 | + for _, value := range chart.Values { |
| 43 | + args = append(args, "--set", value) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if chart.ValueFiles != nil { |
| 48 | + for _, value := range chart.ValueFiles { |
| 49 | + args = append(args, "--values", value) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return exec.CommandContext(ctx, "sudo", args...) |
| 54 | +} |
| 55 | + |
| 56 | +// runs a helm uninstall based on the contents of chart *assets.HelmChart |
| 57 | +func uninstalllHelmChart(ctx context.Context, chart *assets.HelmChart) *exec.Cmd { |
| 58 | + args := []string{ |
| 59 | + fmt.Sprintf("KUBECONFIG=%s", path.Join(vmpath.GuestPersistentDir, "kubeconfig")), |
| 60 | + "helm", "uninstall", chart.Name, |
| 61 | + } |
| 62 | + if chart.Namespace != "" { |
| 63 | + args = append(args, "--namespace", chart.Namespace) |
| 64 | + } |
| 65 | + return exec.CommandContext(ctx, "sudo", args...) |
| 66 | +} |
| 67 | + |
| 68 | +// based on enable will execute installHelmChart or uninstallHelmChart |
| 69 | +func helmUninstallOrInstall(ctx context.Context, chart *assets.HelmChart, enable bool) *exec.Cmd { |
| 70 | + if enable { |
| 71 | + return installHelmChart(ctx, chart) |
| 72 | + } |
| 73 | + return uninstalllHelmChart(ctx, chart) |
| 74 | +} |
| 75 | + |
| 76 | +func helmInstallBinary(addon *assets.Addon, runner command.Runner) error { |
| 77 | + _, err := runner.RunCmd(exec.Command("test", "-f", "/usr/bin/helm")) |
| 78 | + if err != nil { |
| 79 | + _, err = runner.RunCmd(exec.Command("test", "-d", "/usr/local/bin")) |
| 80 | + if err != nil { |
| 81 | + _, err = runner.RunCmd(exec.Command("sudo", "mkdir", "-p", "/usr/local/bin")) |
| 82 | + } |
| 83 | + |
| 84 | + 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") |
| 85 | + _, err = runner.RunCmd(exec.Command("sudo", "bash", "-c", installCmd)) |
| 86 | + // we copy the binary from /usr/local/bin to /usr/bin because /usr/local/bin is not in PATH in both iso and kicbase |
| 87 | + _, err = runner.RunCmd(exec.Command("sudo", "mv", "/usr/local/bin/helm", "/usr/bin/helm")) |
| 88 | + if err != nil { |
| 89 | + return errors.Wrap(err, "installing helm") |
| 90 | + } |
| 91 | + } |
| 92 | + return err |
| 93 | +} |
0 commit comments