diff --git a/compose/compose.go b/compose/compose.go index ee51fd97d..6f820c97a 100644 --- a/compose/compose.go +++ b/compose/compose.go @@ -1,12 +1,12 @@ package compose import ( - "os" + "path/filepath" + "strings" "github.com/compose-spec/compose-go/types" internal "github.com/docker/helm-prototype/pkg/compose/internal" "github.com/docker/helm-prototype/pkg/compose/internal/helm" - "github.com/docker/helm-prototype/pkg/compose/internal/kube" ) var Settings = internal.GetDefault() @@ -41,26 +41,27 @@ func Load(name string, configpaths []string) (*ComposeProject, error) { } func (cp *ComposeProject) GenerateChart(dirname string) error { - objects, err := kube.MapToKubernetesObjects(cp.config, cp.Name) - if err != nil { - return err + if dirname == "" { + dirname = cp.config.Filename + if strings.Contains(dirname, ".") { + splits := strings.SplitN(dirname, ".", 2) + dirname = splits[0] + } } - err = helm.Write(cp.Name, objects, dirname) - if err != nil { - return err - } - return nil + name := filepath.Base(dirname) + dirname = filepath.Dir(dirname) + return internal.SaveChart(cp.config, name, dirname) } func (cp *ComposeProject) Install(name, path string) error { - if path == "" { - cwd, err := os.Getwd() - if err != nil { - return err - } - path = cwd + if path != "" { + return cp.helm.InstallChartFromDir(name, path) } - return cp.helm.Install(name, path) + chart, err := internal.GetChartInMemory(cp.config, name) + if err != nil { + return err + } + return cp.helm.InstallChart(name, chart) } func (cp *ComposeProject) Uninstall(name string) error { diff --git a/compose/internal/env.go b/compose/internal/env.go index 4b571c80d..d16f6eac3 100644 --- a/compose/internal/env.go +++ b/compose/internal/env.go @@ -6,7 +6,11 @@ import ( "github.com/compose-spec/compose-go/loader" "github.com/compose-spec/compose-go/types" + "github.com/docker/helm-prototype/pkg/compose/internal/helm" + "github.com/docker/helm-prototype/pkg/compose/internal/kube" "github.com/docker/helm-prototype/pkg/compose/internal/utils" + chart "helm.sh/helm/v3/pkg/chart" + util "helm.sh/helm/v3/pkg/chartutil" ) // Kind is "kubernetes" or "docker" @@ -68,3 +72,20 @@ func GetConfig(name string, configPaths []string) (*types.Config, string, error) } return config, workingDir, nil } + +func GetChartInMemory(config *types.Config, name string) (*chart.Chart, error) { + objects, err := kube.MapToKubernetesObjects(config, name) + if err != nil { + return nil, err + } + //in memory files + return helm.ConvertToChart(name, objects) +} + +func SaveChart(config *types.Config, name, dest string) error { + chart, err := GetChartInMemory(config, name) + if err != nil { + return err + } + return util.SaveDir(chart, dest) +} diff --git a/compose/internal/helm/output.go b/compose/internal/helm/chart.go similarity index 60% rename from compose/internal/helm/output.go rename to compose/internal/helm/chart.go index 6c37e2685..f4e86d82e 100644 --- a/compose/internal/helm/output.go +++ b/compose/internal/helm/chart.go @@ -4,20 +4,22 @@ import ( "bytes" "encoding/json" "html/template" - "io/ioutil" - "os" "path/filepath" "gopkg.in/yaml.v3" + + chart "helm.sh/helm/v3/pkg/chart" + loader "helm.sh/helm/v3/pkg/chart/loader" "k8s.io/apimachinery/pkg/runtime" ) -func Write(project string, objects map[string]runtime.Object, target string) error { - out := Outputer{target} +func ConvertToChart(name string, objects map[string]runtime.Object) (*chart.Chart, error) { - if err := out.Write("README.md", []byte("This chart was created by converting a Compose file")); err != nil { - return err - } + files := []*loader.BufferedFile{ + &loader.BufferedFile{ + Name: "README.md", + Data: []byte("This chart was created by converting a Compose file"), + }} chart := `name: {{.Name}} description: A generated Helm Chart for {{.Name}} from Skippbox Kompose @@ -31,42 +33,35 @@ home: t, err := template.New("ChartTmpl").Parse(chart) if err != nil { - return err + return nil, err } type ChartDetails struct { Name string } var chartData bytes.Buffer - _ = t.Execute(&chartData, ChartDetails{Name: project}) + _ = t.Execute(&chartData, ChartDetails{Name: name}) - if err := out.Write("Chart.yaml", chartData.Bytes()); err != nil { - return err - } + files = append(files, &loader.BufferedFile{ + Name: "Chart.yaml", + Data: chartData.Bytes(), + }) for name, o := range objects { j, err := json.Marshal(o) if err != nil { - return err + return nil, err } - b, err := jsonToYaml(j, 2) + buf, err := jsonToYaml(j, 2) if err != nil { - return err - } - if err := out.Write(filepath.Join("templates", name), b); err != nil { - return err + return nil, err } + files = append(files, &loader.BufferedFile{ + Name: filepath.Join("templates", name), + Data: buf, + }) + } - return nil -} - -type Outputer struct { - Dir string -} - -func (o Outputer) Write(path string, content []byte) error { - out := filepath.Join(o.Dir, path) - os.MkdirAll(filepath.Dir(out), 0744) - return ioutil.WriteFile(out, content, 0644) + return loader.LoadFiles(files) } // Convert JSON to YAML. @@ -90,7 +85,4 @@ func jsonToYaml(j []byte, spaces int) ([]byte, error) { return nil, err } return b.Bytes(), nil - - // Marshal this object into YAML. - // return yaml.Marshal(jsonObj) } diff --git a/compose/internal/helm/helm.go b/compose/internal/helm/helm.go index 4d453f48c..288e34884 100644 --- a/compose/internal/helm/helm.go +++ b/compose/internal/helm/helm.go @@ -3,9 +3,9 @@ package helm import ( "errors" "log" - "os" action "helm.sh/helm/v3/pkg/action" + chart "helm.sh/helm/v3/pkg/chart" loader "helm.sh/helm/v3/pkg/chart/loader" env "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/release" @@ -28,7 +28,7 @@ func NewHelmActions(settings *env.EnvSettings) *HelmActions { } } -func (hc *HelmActions) InitKubeClient() error { +func (hc *HelmActions) initKubeClient() error { if hc.kube_conn_init { return nil } @@ -47,20 +47,17 @@ func (hc *HelmActions) InitKubeClient() error { return nil } -func (hc *HelmActions) Install(name, chartpath string) error { - hc.InitKubeClient() - - if chartpath == "" { - cwd, err := os.Getwd() - if err != nil { - return nil - } - chartpath = cwd - } +func (hc *HelmActions) InstallChartFromDir(name string, chartpath string) error { chart, err := loader.Load(chartpath) if err != nil { - return nil + return err } + return hc.InstallChart(name, chart) +} + +func (hc *HelmActions) InstallChart(name string, chart *chart.Chart) error { + hc.initKubeClient() + actInstall := action.NewInstall(hc.Config) actInstall.ReleaseName = name actInstall.Namespace = hc.Settings.Namespace() @@ -75,7 +72,7 @@ func (hc *HelmActions) Install(name, chartpath string) error { } func (hc *HelmActions) Uninstall(name string) error { - hc.InitKubeClient() + hc.initKubeClient() release, err := hc.Get(name) if err != nil { return err @@ -93,7 +90,7 @@ func (hc *HelmActions) Uninstall(name string) error { } func (hc *HelmActions) Get(name string) (*release.Release, error) { - hc.InitKubeClient() + hc.initKubeClient() actGet := action.NewGet(hc.Config) return actGet.Run(name)