Merge pull request #1249 from aiordache/kube_convert_cmd

Kube backend: Add `compose convert`
This commit is contained in:
Guillaume Tardif 2021-02-09 11:31:26 +01:00 committed by GitHub
commit 1562af9e41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 17 deletions

View File

@ -81,6 +81,8 @@ type DownOptions struct {
type ConvertOptions struct { type ConvertOptions struct {
// Format define the output format used to dump converted application model (json|yaml) // Format define the output format used to dump converted application model (json|yaml)
Format string Format string
// Output defines the path to save the application model
Output string
} }
// KillOptions group options of the Kill API // KillOptions group options of the Kill API

View File

@ -30,8 +30,11 @@ import (
type convertOptions struct { type convertOptions struct {
*projectOptions *projectOptions
Format string Format string
Output string
} }
var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
func convertCommand(p *projectOptions) *cobra.Command { func convertCommand(p *projectOptions) *cobra.Command {
opts := convertOptions{ opts := convertOptions{
projectOptions: p, projectOptions: p,
@ -47,6 +50,10 @@ func convertCommand(p *projectOptions) *cobra.Command {
flags := convertCmd.Flags() flags := convertCmd.Flags()
flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]") flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")
// add flags for hidden backends
for _, f := range addFlagsFuncs {
f(convertCmd, &opts)
}
return convertCmd return convertCmd
} }
@ -64,11 +71,14 @@ func runConvert(ctx context.Context, opts convertOptions, services []string) err
json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{ json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{
Format: opts.Format, Format: opts.Format,
Output: opts.Output,
}) })
if err != nil { if err != nil {
return err return err
} }
if opts.Output != "" {
fmt.Print("model saved to ")
}
fmt.Println(string(json)) fmt.Println(string(json))
return nil return nil
} }

View File

@ -0,0 +1,31 @@
// +build kube
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package compose
import (
"github.com/spf13/cobra"
)
func init() {
addFlagsFuncs = append(addFlagsFuncs, func(cmd *cobra.Command, opts *convertOptions) {
flags := cmd.Flags()
flags.StringVar(&opts.Output, "output", "", "Save to directory")
})
}

View File

@ -168,7 +168,25 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options com
// Convert translate compose model into backend's native format // Convert translate compose model into backend's native format
func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) { func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
return nil, errdefs.ErrNotImplemented
chart, err := helm.GetChartInMemory(project)
if err != nil {
return nil, err
}
if options.Output != "" {
fullpath, err := helm.SaveChart(chart, options.Output)
return []byte(fullpath), err
}
buff := []byte{}
for _, f := range chart.Raw {
header := "\n" + f.Name + "\n" + strings.Repeat("-", len(f.Name)) + "\n"
buff = append(buff, []byte(header)...)
buff = append(buff, f.Data...)
buff = append(buff, []byte("\n")...)
}
return buff, nil
} }
func (s *composeService) Kill(ctx context.Context, project *types.Project, options compose.KillOptions) error { func (s *composeService) Kill(ctx context.Context, project *types.Project, options compose.KillOptions) error {

View File

@ -22,16 +22,17 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"html/template" "html/template"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/docker/compose-cli/kube/resources" "github.com/docker/compose-cli/kube/resources"
"github.com/pkg/errors"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
chart "helm.sh/helm/v3/pkg/chart" chart "helm.sh/helm/v3/pkg/chart"
loader "helm.sh/helm/v3/pkg/chart/loader" loader "helm.sh/helm/v3/pkg/chart/loader"
util "helm.sh/helm/v3/pkg/chartutil"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
) )
@ -130,22 +131,38 @@ func GetChartInMemory(project *types.Project) (*chart.Chart, error) {
return ConvertToChart(project.Name, objects) return ConvertToChart(project.Name, objects)
} }
// SaveChart converts compose project to helm and saves the chart // SaveChart saves the chart to directory
func SaveChart(project *types.Project, dest string) error { func SaveChart(c *chart.Chart, dest string) (string, error) {
chart, err := GetChartInMemory(project) dir, err := filepath.Abs(dest)
if err != nil { if err != nil {
return err return "", err
} }
return util.SaveDir(chart, dest) for _, file := range c.Raw {
filename := filepath.Join(dir, file.Name)
filedir := filepath.Dir(filename)
stat, err := os.Stat(filedir)
if err != nil {
if os.IsNotExist(err) {
if err2 := os.MkdirAll(filedir, 0755); err2 != nil {
return "", err2
}
} else {
return "", err
}
} else if !stat.IsDir() {
return "", errors.Errorf("%s: not a directory", dest)
} }
// GenerateChart generates helm chart from Compose project f, err := os.Create(filename)
func GenerateChart(project *types.Project, dirname string) error { if err != nil {
if strings.Contains(dirname, ".") { return "", err
splits := strings.SplitN(dirname, ".", 2)
dirname = splits[0]
} }
_, err = f.Write(file.Data)
dirname = filepath.Dir(dirname) if err != nil {
return SaveChart(project, dirname) return "", err
}
}
return dir, nil
} }