action_config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package helm
  2. import (
  3. "github.com/porter-dev/porter/internal/logger"
  4. "helm.sh/helm/v3/pkg/action"
  5. "helm.sh/helm/v3/pkg/kube"
  6. "k8s.io/cli-runtime/pkg/genericclioptions"
  7. "k8s.io/client-go/kubernetes"
  8. "k8s.io/client-go/rest"
  9. )
  10. // NewActionConfig creates an action.Configuration, which can then be used to create Helm 3 actions.
  11. // Among other things, the action.Configuration controls which namespace the command is run against.
  12. func NewActionConfig(
  13. l *logger.Logger,
  14. newStorageDriver NewStorageDriver,
  15. config *rest.Config,
  16. clientset *kubernetes.Clientset,
  17. namespace string,
  18. ) (*action.Configuration, error) {
  19. actionConfig := &action.Configuration{}
  20. store := newStorageDriver(l, namespace, clientset)
  21. restClientGetter := NewConfigFlagsFromCluster(namespace, config)
  22. actionConfig.RESTClientGetter = restClientGetter
  23. actionConfig.KubeClient = kube.New(restClientGetter)
  24. actionConfig.Releases = store
  25. actionConfig.Log = l.Printf
  26. return actionConfig, nil
  27. }
  28. // NewConfigFlagsFromCluster returns ConfigFlags with default values set from within cluster.
  29. func NewConfigFlagsFromCluster(namespace string, clusterConfig *rest.Config) genericclioptions.RESTClientGetter {
  30. impersonateGroup := []string{}
  31. // CertFile and KeyFile must be nil for the BearerToken to be used for authentication and authorization instead of the pod's service account.
  32. return &genericclioptions.ConfigFlags{
  33. Insecure: &clusterConfig.TLSClientConfig.Insecure,
  34. Timeout: stringptr("0"),
  35. Namespace: stringptr(namespace),
  36. APIServer: stringptr(clusterConfig.Host),
  37. CAFile: stringptr(clusterConfig.CAFile),
  38. BearerToken: stringptr(clusterConfig.BearerToken),
  39. ImpersonateGroup: &impersonateGroup,
  40. }
  41. }
  42. func stringptr(val string) *string {
  43. return &val
  44. }