Skip to content

Commit 560014f

Browse files
author
aiordache
committed
Add progress writer to compose up/down
- remove intermediate compose interface Signed-off-by: aiordache <[email protected]>
1 parent 628417f commit 560014f

File tree

13 files changed

+149
-175
lines changed

13 files changed

+149
-175
lines changed

kube/charts/charts.go

Lines changed: 0 additions & 120 deletions
This file was deleted.

kube/compose.go

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,98 @@ package kube
2020

2121
import (
2222
"context"
23+
"fmt"
24+
"strings"
2325

2426
"github.com/compose-spec/compose-go/types"
2527

2628
"github.com/docker/compose-cli/api/compose"
29+
apicontext "github.com/docker/compose-cli/api/context"
30+
"github.com/docker/compose-cli/api/context/store"
2731
"github.com/docker/compose-cli/api/errdefs"
28-
"github.com/docker/compose-cli/kube/charts"
32+
"github.com/docker/compose-cli/api/progress"
33+
"github.com/docker/compose-cli/kube/helm"
34+
"github.com/docker/compose-cli/kube/resources"
2935
)
3036

37+
type composeService struct {
38+
sdk *helm.Actions
39+
}
40+
3141
// NewComposeService create a kubernetes implementation of the compose.Service API
3242
func NewComposeService(ctx context.Context) (compose.Service, error) {
33-
chartsAPI, err := charts.NewSDK(ctx)
43+
contextStore := store.ContextStore(ctx)
44+
currentContext := apicontext.CurrentContext(ctx)
45+
var kubeContext store.KubeContext
46+
47+
if err := contextStore.GetEndpoint(currentContext, &kubeContext); err != nil {
48+
return nil, err
49+
}
50+
config, err := resources.LoadConfig(kubeContext)
51+
if err != nil {
52+
return nil, err
53+
}
54+
actions, err := helm.NewActions(config)
3455
if err != nil {
3556
return nil, err
3657
}
3758
return &composeService{
38-
sdk: chartsAPI,
59+
sdk: actions,
3960
}, nil
4061
}
4162

42-
type composeService struct {
43-
sdk charts.SDK
44-
}
45-
4663
// Up executes the equivalent to a `compose up`
4764
func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
48-
return s.sdk.Install(project)
65+
w := progress.ContextWriter(ctx)
66+
67+
eventName := "Convert to Helm charts"
68+
w.Event(progress.CreatingEvent(eventName))
69+
70+
chart, err := helm.GetChartInMemory(project)
71+
if err != nil {
72+
return err
73+
}
74+
w.Event(progress.NewEvent(eventName, progress.Done, ""))
75+
76+
eventName = "Install Helm charts"
77+
w.Event(progress.CreatingEvent(eventName))
78+
79+
err = s.sdk.InstallChart(project.Name, chart, func(format string, v ...interface{}) {
80+
message := fmt.Sprintf(format, v...)
81+
w.Event(progress.NewEvent(eventName, progress.Done, message))
82+
})
83+
84+
w.Event(progress.NewEvent(eventName, progress.Done, ""))
85+
return err
4986
}
5087

5188
// Down executes the equivalent to a `compose down`
5289
func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
53-
return s.sdk.Uninstall(projectName)
90+
w := progress.ContextWriter(ctx)
91+
92+
eventName := fmt.Sprintf("Remove %s", projectName)
93+
w.Event(progress.CreatingEvent(eventName))
94+
95+
logger := func(format string, v ...interface{}) {
96+
message := fmt.Sprintf(format, v...)
97+
if strings.Contains(message, "Starting delete") {
98+
action := strings.Replace(message, "Starting delete for", "Delete", 1)
99+
100+
w.Event(progress.CreatingEvent(action))
101+
w.Event(progress.NewEvent(action, progress.Done, ""))
102+
return
103+
}
104+
w.Event(progress.NewEvent(eventName, progress.Working, message))
105+
}
106+
err := s.sdk.Uninstall(projectName, logger)
107+
w.Event(progress.NewEvent(eventName, progress.Done, ""))
108+
109+
return err
54110
}
55111

56112
// List executes the equivalent to a `docker stack ls`
57113
func (s *composeService) List(ctx context.Context) ([]compose.Stack, error) {
58-
return s.sdk.List()
114+
return s.sdk.ListReleases()
59115
}
60116

61117
// Build executes the equivalent to a `compose build`

kube/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/docker/compose-cli/api/errdefs"
2727
"github.com/docker/compose-cli/utils/prompt"
2828

29-
"github.com/docker/compose-cli/kube/charts/kubernetes"
29+
"github.com/docker/compose-cli/kube/resources"
3030
)
3131

3232
// ContextParams options for creating a Kubernetes context
@@ -47,7 +47,7 @@ func (cp ContextParams) CreateContextData() (interface{}, string, error) {
4747
}
4848
user := prompt.User{}
4949
selectContext := func() error {
50-
contexts, err := kubernetes.ListAvailableKubeConfigContexts(cp.KubeConfigPath)
50+
contexts, err := resources.ListAvailableKubeConfigContexts(cp.KubeConfigPath)
5151
if err != nil {
5252
return err
5353
}

kube/charts/helm/chart.go renamed to kube/helm/chart.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ import (
2323
"encoding/json"
2424
"html/template"
2525
"path/filepath"
26+
"strings"
2627

28+
"github.com/compose-spec/compose-go/types"
29+
"github.com/docker/compose-cli/kube/resources"
2730
"gopkg.in/yaml.v3"
2831

2932
chart "helm.sh/helm/v3/pkg/chart"
3033
loader "helm.sh/helm/v3/pkg/chart/loader"
34+
util "helm.sh/helm/v3/pkg/chartutil"
3135
"k8s.io/apimachinery/pkg/runtime"
3236
)
3337

@@ -107,3 +111,41 @@ func jsonToYaml(j []byte, spaces int) ([]byte, error) {
107111
}
108112
return b.Bytes(), nil
109113
}
114+
115+
// GetChartInMemory get memory representation of helm chart
116+
func GetChartInMemory(project *types.Project) (*chart.Chart, error) {
117+
// replace _ with - in volume names
118+
for k, v := range project.Volumes {
119+
volumeName := strings.ReplaceAll(k, "_", "-")
120+
if volumeName != k {
121+
project.Volumes[volumeName] = v
122+
delete(project.Volumes, k)
123+
}
124+
}
125+
objects, err := resources.MapToKubernetesObjects(project)
126+
if err != nil {
127+
return nil, err
128+
}
129+
//in memory files
130+
return ConvertToChart(project.Name, objects)
131+
}
132+
133+
// SaveChart converts compose project to helm and saves the chart
134+
func SaveChart(project *types.Project, dest string) error {
135+
chart, err := GetChartInMemory(project)
136+
if err != nil {
137+
return err
138+
}
139+
return util.SaveDir(chart, dest)
140+
}
141+
142+
// GenerateChart generates helm chart from Compose project
143+
func GenerateChart(project *types.Project, dirname string) error {
144+
if strings.Contains(dirname, ".") {
145+
splits := strings.SplitN(dirname, ".", 2)
146+
dirname = splits[0]
147+
}
148+
149+
dirname = filepath.Dir(dirname)
150+
return SaveChart(project, dirname)
151+
}

0 commit comments

Comments
 (0)