2022-12-01 01:27:49 +00:00
|
|
|
package binary
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-04-14 02:50:37 +00:00
|
|
|
"os"
|
2022-12-01 01:27:49 +00:00
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/portainer/portainer/pkg/libhelm/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
// helmBinaryPackageManager is a wrapper for the helm binary which implements HelmPackageManager
|
|
|
|
type helmBinaryPackageManager struct {
|
|
|
|
binaryPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHelmBinaryPackageManager initializes a new HelmPackageManager service.
|
|
|
|
func NewHelmBinaryPackageManager(binaryPath string) *helmBinaryPackageManager {
|
|
|
|
return &helmBinaryPackageManager{binaryPath: binaryPath}
|
|
|
|
}
|
|
|
|
|
|
|
|
// runWithKubeConfig will execute run against the provided Kubernetes cluster with kubeconfig as cli arguments.
|
2023-04-14 02:50:37 +00:00
|
|
|
func (hbpm *helmBinaryPackageManager) runWithKubeConfig(command string, args []string, kca *options.KubernetesClusterAccess, env []string) ([]byte, error) {
|
2022-12-01 01:27:49 +00:00
|
|
|
cmdArgs := make([]string, 0)
|
|
|
|
if kca != nil {
|
|
|
|
cmdArgs = append(cmdArgs, "--kube-apiserver", kca.ClusterServerURL)
|
|
|
|
cmdArgs = append(cmdArgs, "--kube-token", kca.AuthToken)
|
|
|
|
cmdArgs = append(cmdArgs, "--kube-ca-file", kca.CertificateAuthorityFile)
|
|
|
|
}
|
|
|
|
cmdArgs = append(cmdArgs, args...)
|
2023-04-14 02:50:37 +00:00
|
|
|
return hbpm.run(command, cmdArgs, env)
|
2022-12-01 01:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run will execute helm command against the provided Kubernetes cluster.
|
|
|
|
// The endpointId and authToken are dynamic params (based on the user) that allow helm to execute commands
|
|
|
|
// in the context of the current user against specified k8s cluster.
|
2023-04-14 02:50:37 +00:00
|
|
|
func (hbpm *helmBinaryPackageManager) run(command string, args []string, env []string) ([]byte, error) {
|
2022-12-01 01:27:49 +00:00
|
|
|
cmdArgs := make([]string, 0)
|
|
|
|
cmdArgs = append(cmdArgs, command)
|
|
|
|
cmdArgs = append(cmdArgs, args...)
|
|
|
|
|
|
|
|
helmPath := path.Join(hbpm.binaryPath, "helm")
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
helmPath = path.Join(hbpm.binaryPath, "helm.exe")
|
|
|
|
}
|
|
|
|
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd := exec.Command(helmPath, cmdArgs...)
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
|
2023-04-14 02:50:37 +00:00
|
|
|
cmd.Env = os.Environ()
|
|
|
|
cmd.Env = append(cmd.Env, env...)
|
|
|
|
|
2022-12-01 01:27:49 +00:00
|
|
|
output, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, stderr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|