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
|
package compose
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
internal "github.com/docker/helm-prototype/pkg/compose/internal"
|
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/helm"
|
||||||
"github.com/docker/helm-prototype/pkg/compose/internal/kube"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var Settings = internal.GetDefault()
|
var Settings = internal.GetDefault()
|
||||||
|
@ -41,26 +41,27 @@ func Load(name string, configpaths []string) (*ComposeProject, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cp *ComposeProject) GenerateChart(dirname string) error {
|
func (cp *ComposeProject) GenerateChart(dirname string) error {
|
||||||
objects, err := kube.MapToKubernetesObjects(cp.config, cp.Name)
|
if dirname == "" {
|
||||||
if err != nil {
|
dirname = cp.config.Filename
|
||||||
return err
|
if strings.Contains(dirname, ".") {
|
||||||
|
splits := strings.SplitN(dirname, ".", 2)
|
||||||
|
dirname = splits[0]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err = helm.Write(cp.Name, objects, dirname)
|
name := filepath.Base(dirname)
|
||||||
if err != nil {
|
dirname = filepath.Dir(dirname)
|
||||||
return err
|
return internal.SaveChart(cp.config, name, dirname)
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cp *ComposeProject) Install(name, path string) error {
|
func (cp *ComposeProject) Install(name, path string) error {
|
||||||
if path == "" {
|
if path != "" {
|
||||||
cwd, err := os.Getwd()
|
return cp.helm.InstallChartFromDir(name, path)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
path = cwd
|
|
||||||
}
|
}
|
||||||
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 {
|
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/loader"
|
||||||
"github.com/compose-spec/compose-go/types"
|
"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"
|
"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"
|
// Kind is "kubernetes" or "docker"
|
||||||
|
@ -68,3 +72,20 @@ func GetConfig(name string, configPaths []string) (*types.Config, string, error)
|
||||||
}
|
}
|
||||||
return config, workingDir, nil
|
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"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
|
chart "helm.sh/helm/v3/pkg/chart"
|
||||||
|
loader "helm.sh/helm/v3/pkg/chart/loader"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Write(project string, objects map[string]runtime.Object, target string) error {
|
func ConvertToChart(name string, objects map[string]runtime.Object) (*chart.Chart, error) {
|
||||||
out := Outputer{target}
|
|
||||||
|
|
||||||
if err := out.Write("README.md", []byte("This chart was created by converting a Compose file")); err != nil {
|
files := []*loader.BufferedFile{
|
||||||
return err
|
&loader.BufferedFile{
|
||||||
}
|
Name: "README.md",
|
||||||
|
Data: []byte("This chart was created by converting a Compose file"),
|
||||||
|
}}
|
||||||
|
|
||||||
chart := `name: {{.Name}}
|
chart := `name: {{.Name}}
|
||||||
description: A generated Helm Chart for {{.Name}} from Skippbox Kompose
|
description: A generated Helm Chart for {{.Name}} from Skippbox Kompose
|
||||||
|
@ -31,42 +33,35 @@ home:
|
||||||
|
|
||||||
t, err := template.New("ChartTmpl").Parse(chart)
|
t, err := template.New("ChartTmpl").Parse(chart)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
type ChartDetails struct {
|
type ChartDetails struct {
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
var chartData bytes.Buffer
|
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 {
|
files = append(files, &loader.BufferedFile{
|
||||||
return err
|
Name: "Chart.yaml",
|
||||||
}
|
Data: chartData.Bytes(),
|
||||||
|
})
|
||||||
|
|
||||||
for name, o := range objects {
|
for name, o := range objects {
|
||||||
j, err := json.Marshal(o)
|
j, err := json.Marshal(o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
b, err := jsonToYaml(j, 2)
|
buf, err := jsonToYaml(j, 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
|
||||||
if err := out.Write(filepath.Join("templates", name), b); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
files = append(files, &loader.BufferedFile{
|
||||||
|
Name: filepath.Join("templates", name),
|
||||||
|
Data: buf,
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return loader.LoadFiles(files)
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert JSON to YAML.
|
// Convert JSON to YAML.
|
||||||
|
@ -90,7 +85,4 @@ func jsonToYaml(j []byte, spaces int) ([]byte, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
|
|
||||||
// Marshal this object into YAML.
|
|
||||||
// return yaml.Marshal(jsonObj)
|
|
||||||
}
|
}
|
|
@ -3,9 +3,9 @@ package helm
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
|
|
||||||
action "helm.sh/helm/v3/pkg/action"
|
action "helm.sh/helm/v3/pkg/action"
|
||||||
|
chart "helm.sh/helm/v3/pkg/chart"
|
||||||
loader "helm.sh/helm/v3/pkg/chart/loader"
|
loader "helm.sh/helm/v3/pkg/chart/loader"
|
||||||
env "helm.sh/helm/v3/pkg/cli"
|
env "helm.sh/helm/v3/pkg/cli"
|
||||||
"helm.sh/helm/v3/pkg/release"
|
"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 {
|
if hc.kube_conn_init {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -47,20 +47,17 @@ func (hc *HelmActions) InitKubeClient() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HelmActions) Install(name, chartpath string) error {
|
func (hc *HelmActions) InstallChartFromDir(name string, chartpath string) error {
|
||||||
hc.InitKubeClient()
|
|
||||||
|
|
||||||
if chartpath == "" {
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
chartpath = cwd
|
|
||||||
}
|
|
||||||
chart, err := loader.Load(chartpath)
|
chart, err := loader.Load(chartpath)
|
||||||
if err != nil {
|
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 := action.NewInstall(hc.Config)
|
||||||
actInstall.ReleaseName = name
|
actInstall.ReleaseName = name
|
||||||
actInstall.Namespace = hc.Settings.Namespace()
|
actInstall.Namespace = hc.Settings.Namespace()
|
||||||
|
@ -75,7 +72,7 @@ func (hc *HelmActions) Install(name, chartpath string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HelmActions) Uninstall(name string) error {
|
func (hc *HelmActions) Uninstall(name string) error {
|
||||||
hc.InitKubeClient()
|
hc.initKubeClient()
|
||||||
release, err := hc.Get(name)
|
release, err := hc.Get(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -93,7 +90,7 @@ func (hc *HelmActions) Uninstall(name string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HelmActions) Get(name string) (*release.Release, error) {
|
func (hc *HelmActions) Get(name string) (*release.Release, error) {
|
||||||
hc.InitKubeClient()
|
hc.initKubeClient()
|
||||||
|
|
||||||
actGet := action.NewGet(hc.Config)
|
actGet := action.NewGet(hc.Config)
|
||||||
return actGet.Run(name)
|
return actGet.Run(name)
|
||||||
|
|
Loading…
Reference in New Issue