github.com/replicatedhq/ship@v0.55.0/pkg/lifecycle/render/helm/commands.go (about) 1 package helm 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/go-kit/kit/log" 8 "github.com/go-kit/kit/log/level" 9 "github.com/pkg/errors" 10 "github.com/replicatedhq/ship/pkg/constants" 11 "github.com/replicatedhq/ship/pkg/helm" 12 "github.com/spf13/afero" 13 "k8s.io/helm/pkg/chartutil" 14 ) 15 16 // Commands are Helm commands that are available to the Ship binary. 17 type Commands interface { 18 Init() error 19 MaybeDependencyUpdate(chartRoot string, requirements chartutil.Requirements) error 20 Template(chartName string, args []string) error 21 Fetch(chartRef, repoURL, version, dest, home string) error 22 RepoAdd(name, url, home string) error 23 } 24 25 type helmCommands struct { 26 logger log.Logger 27 fs afero.Afero 28 } 29 30 func (h *helmCommands) Fetch(chartRef, repoURL, version, dest, home string) error { 31 outstring, err := helm.Fetch(chartRef, repoURL, version, dest, home) 32 if err != nil { 33 return errors.Wrap(err, fmt.Sprintf("helm fetch failed, output %q", outstring)) 34 } 35 return nil 36 } 37 38 func (h *helmCommands) Init() error { 39 err := os.MkdirAll(constants.InternalTempHelmHome, 0755) 40 if err != nil { 41 return errors.Wrapf(err, "create %s", constants.InternalTempHelmHome) 42 } 43 output, err := helm.Init(constants.InternalTempHelmHome) 44 return errors.Wrapf(err, "helm init: %s", output) 45 } 46 47 func (h *helmCommands) MaybeDependencyUpdate(chartRoot string, requirements chartutil.Requirements) error { 48 debug := level.Debug(log.With(h.logger, "method", "maybeDependencyUpdate")) 49 allEmpty := true 50 for _, dependency := range requirements.Dependencies { 51 if dependency.Repository != "" { 52 allEmpty = false 53 } 54 } 55 56 if !allEmpty { 57 debug.Log("event", "dependency update") 58 if err := h.dependencyUpdate(chartRoot); err != nil { 59 return errors.Wrap(err, "dependency update") 60 } 61 } else { 62 debug.Log("event", "skip dependency update") 63 } 64 65 return nil 66 } 67 68 func (h *helmCommands) Template(chartName string, args []string) error { 69 templateCommand, err := helm.NewTemplateCmd(append([]string{chartName}, args...)) 70 if err != nil { 71 return err 72 } 73 74 return templateCommand.Execute() 75 } 76 77 // NewCommands returns a helmCommands struct that implements Commands. 78 func NewCommands( 79 fs afero.Afero, 80 logger log.Logger, 81 ) Commands { 82 return &helmCommands{ 83 logger: logger, 84 fs: fs, 85 } 86 } 87 88 func (h *helmCommands) dependencyUpdate(chartRoot string) error { 89 dependencyArgs := []string{ 90 "update", 91 chartRoot, 92 } 93 dependencyCommand, err := helm.NewDependencyCmd(dependencyArgs) 94 if err != nil { 95 return err 96 } 97 return dependencyCommand.Execute() 98 } 99 100 func (h *helmCommands) RepoAdd(name, url, home string) error { 101 outstring, err := helm.RepoAdd(name, url, home) 102 if err != nil { 103 return errors.Wrap(err, fmt.Sprintf("helm repo add failed, output %q", outstring)) 104 } 105 return nil 106 }