@@ -20,42 +20,98 @@ package kube
2020
2121import (
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
3242func 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`
4764func (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`
5289func (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`
57113func (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`
0 commit comments