mirror of https://github.com/docker/compose.git
save chart structure
Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
parent
facb86fab2
commit
836732ed37
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue