diff --git a/compose/compose.go b/compose/compose.go index 2f4f5fef1..ee51fd97d 100644 --- a/compose/compose.go +++ b/compose/compose.go @@ -3,46 +3,56 @@ package compose import ( "os" + "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" ) -type ProjectOptions struct { - ConfigPaths []string - Name string -} - var Settings = internal.GetDefault() -type ComposeAPI struct { - project *internal.Project +type ComposeProject struct { + config *types.Config + helm *helm.HelmActions + ProjectDir string + Name string `yaml:"-" json:"-"` } -// projectFromOptions load a compose project based on command line options -func ProjectFromOptions(options *ProjectOptions) (*ComposeAPI, error) { - if options == nil { - options = &ProjectOptions{ - ConfigPaths: []string{}, - Name: "docker-compose", - } - } +type ComposeResult struct { + Info string + Status string + Descriptin string +} - if options.Name == "" { - options.Name = "docker-compose" +func Load(name string, configpaths []string) (*ComposeProject, error) { + if name == "" { + name = "docker-compose" } - - project, err := internal.GetProject(options.Name, options.ConfigPaths) + model, workingDir, err := internal.GetConfig(name, configpaths) if err != nil { return nil, err } - - return &ComposeAPI{project: project}, nil + return &ComposeProject{ + config: model, + helm: helm.NewHelmActions(nil), + ProjectDir: workingDir, + Name: name, + }, nil } -func (c *ComposeAPI) GenerateChart(dirname string) error { - return c.project.ExportToCharts(dirname) +func (cp *ComposeProject) GenerateChart(dirname string) error { + objects, err := kube.MapToKubernetesObjects(cp.config, cp.Name) + if err != nil { + return err + } + err = helm.Write(cp.Name, objects, dirname) + if err != nil { + return err + } + return nil } -func (c *ComposeAPI) Install(name, path string) error { +func (cp *ComposeProject) Install(name, path string) error { if path == "" { cwd, err := os.Getwd() if err != nil { @@ -50,9 +60,9 @@ func (c *ComposeAPI) Install(name, path string) error { } path = cwd } - return c.project.Install(name, path) + return cp.helm.Install(name, path) } -func (c *ComposeAPI) Uninstall(name string) error { - return c.project.Uninstall(name) +func (cp *ComposeProject) Uninstall(name string) error { + return cp.helm.Uninstall(name) } diff --git a/compose/internal/env.go b/compose/internal/env.go new file mode 100644 index 000000000..4b571c80d --- /dev/null +++ b/compose/internal/env.go @@ -0,0 +1,70 @@ +package env + +import ( + "os" + "strings" + + "github.com/compose-spec/compose-go/loader" + "github.com/compose-spec/compose-go/types" + "github.com/docker/helm-prototype/pkg/compose/internal/utils" +) + +// Kind is "kubernetes" or "docker" +type Kind string + +const ( + // Kubernetes specifies to use a kubernetes cluster. + Kubernetes Kind = "kubernetes" + // Docker specifies to use Docker engine. + DockerEngine Kind = "docker" +) + +type Engine struct { + Namespace string + + Kind Kind + + Config string + // Context is the name of the kubeconfig/docker context. + Context string + // Token used for authentication (kubernetes) + Token string + // Kubernetes API Server Endpoint for authentication + APIServer string +} + +func GetDefault() *Engine { + return &Engine{Kind: Kubernetes} +} + +func Environment() map[string]string { + vars := make(map[string]string) + env := os.Environ() + for _, v := range env { + k := strings.SplitN(v, "=", 2) + vars[k[0]] = k[1] + } + return vars +} + +func GetConfig(name string, configPaths []string) (*types.Config, string, error) { + if name == "" { + name = "docker-compose" + } + workingDir, configs, err := utils.GetConfigs( + name, + configPaths, + ) + if err != nil { + return nil, "", err + } + config, err := loader.Load(types.ConfigDetails{ + WorkingDir: workingDir, + ConfigFiles: configs, + Environment: Environment(), + }) + if err != nil { + return nil, "", err + } + return config, workingDir, nil +} diff --git a/compose/internal/helm/helm.go b/compose/internal/helm/helm.go index 277a2c85f..4d453f48c 100644 --- a/compose/internal/helm/helm.go +++ b/compose/internal/helm/helm.go @@ -69,10 +69,9 @@ func (hc *HelmActions) Install(name, chartpath string) error { if err != nil { return err } - - println("Release status: ", release.Info.Status) - println("Release description: ", release.Info.Description) - return hc.Config.Releases.Update(release) + log.Println("Release status: ", release.Info.Status) + log.Println(release.Info.Description) + return nil } func (hc *HelmActions) Uninstall(name string) error { @@ -85,8 +84,12 @@ func (hc *HelmActions) Uninstall(name string) error { return errors.New("No release found with the name provided.") } actUninstall := action.NewUninstall(hc.Config) - _, err = actUninstall.Run(name) - return err + response, err := actUninstall.Run(name) + if err != nil { + return err + } + log.Println(response.Release.Info.Description) + return nil } func (hc *HelmActions) Get(name string) (*release.Release, error) { diff --git a/compose/internal/project.go b/compose/internal/project.go deleted file mode 100644 index 314a7b341..000000000 --- a/compose/internal/project.go +++ /dev/null @@ -1,102 +0,0 @@ -package project - -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/utils" - - "github.com/docker/helm-prototype/pkg/compose/internal/kube" -) - -// Kind is "kubernetes" or "docker" -type Kind string - -const ( - // Kubernetes specifies to use a kubernetes cluster. - Kubernetes Kind = "kubernetes" - // Docker specifies to use Docker engine. - DockerEngine Kind = "docker" -) - -type Engine struct { - Namespace string - - Kind Kind - - Config string - // Context is the name of the kubeconfig/docker context. - Context string - // Token used for authentication (kubernetes) - Token string - // Kubernetes API Server Endpoint for authentication - APIServer string -} - -func GetDefault() *Engine { - return &Engine{Kind: Kubernetes} -} - -type Project struct { - Config *types.Config - Helm *helm.HelmActions - ProjectDir string - Name string `yaml:"-" json:"-"` -} - -func NewProject(config types.ConfigDetails, name string) (*Project, error) { - model, err := loader.Load(config) - if err != nil { - return nil, err - } - - p := Project{ - Config: model, - Helm: helm.NewHelmActions(nil), - ProjectDir: config.WorkingDir, - Name: name, - } - return &p, nil -} - -func GetProject(name string, configPaths []string) (*Project, error) { - if name == "" { - name = "docker-compose" - } - - workingDir, configs, err := utils.GetConfigs( - name, - configPaths, - ) - if err != nil { - return nil, err - } - - return NewProject(types.ConfigDetails{ - WorkingDir: workingDir, - ConfigFiles: configs, - Environment: utils.Environment(), - }, name) - -} - -func (p *Project) ExportToCharts(path string) error { - objects, err := kube.MapToKubernetesObjects(p.Config, p.Name) - if err != nil { - return err - } - err = helm.Write(p.Name, objects, path) - if err != nil { - return err - } - return nil -} - -func (p *Project) Install(name, path string) error { - return p.Helm.Install(name, path) -} - -func (p *Project) Uninstall(name string) error { - - return p.Helm.Uninstall(name) -} diff --git a/compose/internal/utils/env.go b/compose/internal/utils/env.go deleted file mode 100644 index e7673fef0..000000000 --- a/compose/internal/utils/env.go +++ /dev/null @@ -1,16 +0,0 @@ -package utils - -import ( - "os" - "strings" -) - -func Environment() map[string]string { - vars := make(map[string]string) - env := os.Environ() - for _, v := range env { - k := strings.SplitN(v, "=", 2) - vars[k[0]] = k[1] - } - return vars -}