2021-01-19 11:49:50 +01:00
|
|
|
// +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 kube
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-29 14:22:22 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-01-19 11:49:50 +01:00
|
|
|
|
|
|
|
"github.com/compose-spec/compose-go/types"
|
2021-01-26 22:42:49 +01:00
|
|
|
|
2021-01-19 11:49:50 +01:00
|
|
|
"github.com/docker/compose-cli/api/compose"
|
2021-01-29 14:22:22 +01:00
|
|
|
apicontext "github.com/docker/compose-cli/api/context"
|
|
|
|
"github.com/docker/compose-cli/api/context/store"
|
2021-01-19 11:49:50 +01:00
|
|
|
"github.com/docker/compose-cli/api/errdefs"
|
2021-01-29 14:22:22 +01:00
|
|
|
"github.com/docker/compose-cli/api/progress"
|
2021-02-04 15:36:57 +01:00
|
|
|
"github.com/docker/compose-cli/kube/client"
|
2021-01-29 14:22:22 +01:00
|
|
|
"github.com/docker/compose-cli/kube/helm"
|
|
|
|
"github.com/docker/compose-cli/kube/resources"
|
2021-01-19 11:49:50 +01:00
|
|
|
)
|
|
|
|
|
2021-01-29 14:22:22 +01:00
|
|
|
type composeService struct {
|
2021-02-04 15:36:57 +01:00
|
|
|
sdk *helm.Actions
|
|
|
|
client *client.KubeClient
|
2021-01-29 14:22:22 +01:00
|
|
|
}
|
|
|
|
|
2021-01-19 11:49:50 +01:00
|
|
|
// NewComposeService create a kubernetes implementation of the compose.Service API
|
2021-01-28 14:14:13 +01:00
|
|
|
func NewComposeService(ctx context.Context) (compose.Service, error) {
|
2021-01-29 14:22:22 +01:00
|
|
|
contextStore := store.ContextStore(ctx)
|
|
|
|
currentContext := apicontext.CurrentContext(ctx)
|
|
|
|
var kubeContext store.KubeContext
|
|
|
|
|
|
|
|
if err := contextStore.GetEndpoint(currentContext, &kubeContext); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config, err := resources.LoadConfig(kubeContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
actions, err := helm.NewActions(config)
|
2021-02-04 16:57:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-04 15:36:57 +01:00
|
|
|
apiClient, err := client.NewKubeClient(config)
|
2021-01-19 11:49:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-04 15:36:57 +01:00
|
|
|
|
2021-01-19 11:49:50 +01:00
|
|
|
return &composeService{
|
2021-02-04 15:36:57 +01:00
|
|
|
sdk: actions,
|
|
|
|
client: apiClient,
|
2021-01-19 11:49:50 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Up executes the equivalent to a `compose up`
|
|
|
|
func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
|
2021-01-29 14:22:22 +01:00
|
|
|
w := progress.ContextWriter(ctx)
|
|
|
|
|
|
|
|
eventName := "Convert to Helm charts"
|
|
|
|
w.Event(progress.CreatingEvent(eventName))
|
|
|
|
|
|
|
|
chart, err := helm.GetChartInMemory(project)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Event(progress.NewEvent(eventName, progress.Done, ""))
|
|
|
|
|
|
|
|
eventName = "Install Helm charts"
|
|
|
|
w.Event(progress.CreatingEvent(eventName))
|
|
|
|
|
|
|
|
err = s.sdk.InstallChart(project.Name, chart, func(format string, v ...interface{}) {
|
|
|
|
message := fmt.Sprintf(format, v...)
|
|
|
|
w.Event(progress.NewEvent(eventName, progress.Done, message))
|
|
|
|
})
|
|
|
|
|
|
|
|
w.Event(progress.NewEvent(eventName, progress.Done, ""))
|
|
|
|
return err
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Down executes the equivalent to a `compose down`
|
|
|
|
func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
|
2021-01-29 14:22:22 +01:00
|
|
|
w := progress.ContextWriter(ctx)
|
|
|
|
|
|
|
|
eventName := fmt.Sprintf("Remove %s", projectName)
|
|
|
|
w.Event(progress.CreatingEvent(eventName))
|
|
|
|
|
|
|
|
logger := func(format string, v ...interface{}) {
|
|
|
|
message := fmt.Sprintf(format, v...)
|
|
|
|
if strings.Contains(message, "Starting delete") {
|
|
|
|
action := strings.Replace(message, "Starting delete for", "Delete", 1)
|
|
|
|
|
|
|
|
w.Event(progress.CreatingEvent(action))
|
|
|
|
w.Event(progress.NewEvent(action, progress.Done, ""))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Event(progress.NewEvent(eventName, progress.Working, message))
|
|
|
|
}
|
|
|
|
err := s.sdk.Uninstall(projectName, logger)
|
|
|
|
w.Event(progress.NewEvent(eventName, progress.Done, ""))
|
|
|
|
|
|
|
|
return err
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// List executes the equivalent to a `docker stack ls`
|
2021-01-26 09:43:12 +01:00
|
|
|
func (s *composeService) List(ctx context.Context) ([]compose.Stack, error) {
|
2021-01-29 14:22:22 +01:00
|
|
|
return s.sdk.ListReleases()
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build executes the equivalent to a `compose build`
|
|
|
|
func (s *composeService) Build(ctx context.Context, project *types.Project) error {
|
|
|
|
return errdefs.ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push executes the equivalent ot a `compose push`
|
|
|
|
func (s *composeService) Push(ctx context.Context, project *types.Project) error {
|
|
|
|
return errdefs.ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull executes the equivalent of a `compose pull`
|
|
|
|
func (s *composeService) Pull(ctx context.Context, project *types.Project) error {
|
|
|
|
return errdefs.ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create executes the equivalent to a `compose create`
|
|
|
|
func (s *composeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
|
|
|
|
return errdefs.ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start executes the equivalent to a `compose start`
|
|
|
|
func (s *composeService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
|
|
|
|
return errdefs.ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2021-01-26 18:20:00 +01:00
|
|
|
// Stop executes the equivalent to a `compose stop`
|
2021-01-26 22:42:49 +01:00
|
|
|
func (s *composeService) Stop(ctx context.Context, project *types.Project) error {
|
2021-01-26 18:20:00 +01:00
|
|
|
return errdefs.ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2021-01-19 11:49:50 +01:00
|
|
|
// Logs executes the equivalent to a `compose logs`
|
|
|
|
func (s *composeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
|
|
|
|
return errdefs.ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ps executes the equivalent to a `compose ps`
|
2021-01-29 14:41:44 +01:00
|
|
|
func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
|
2021-02-04 16:57:06 +01:00
|
|
|
return s.client.GetContainers(ctx, projectName, options.All)
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert translate compose model into backend's native format
|
|
|
|
func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
|
|
|
|
return nil, errdefs.ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2021-01-31 18:42:13 +01:00
|
|
|
func (s *composeService) Kill(ctx context.Context, project *types.Project, options compose.KillOptions) error {
|
|
|
|
return errdefs.ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2021-01-19 11:49:50 +01:00
|
|
|
// RunOneOffContainer creates a service oneoff container and starts its dependencies
|
|
|
|
func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
|
|
|
|
return errdefs.ErrNotImplemented
|
|
|
|
}
|