remove redundant API interface

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2021-01-21 14:55:31 +01:00
parent f09a57351f
commit 60aed92367
2 changed files with 13 additions and 34 deletions

View File

@ -19,7 +19,6 @@
package charts package charts
import ( import (
"context"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -34,40 +33,20 @@ import (
helmenv "helm.sh/helm/v3/pkg/cli" helmenv "helm.sh/helm/v3/pkg/cli"
) )
// API defines management methods for helm charts type SDK struct {
type API interface {
GetDefaultEnv() *helmenv.EnvSettings
Connect(ctx context.Context) error
GenerateChart(project *types.Project, dirname string) error
GetChartInMemory(project *types.Project) (*chart.Chart, error)
SaveChart(project *types.Project, dest string) error
Install(project *types.Project) error
Uninstall(projectName string) error
List(projectName string) ([]compose.Stack, error)
}
type sdk struct {
h *helm.HelmActions h *helm.HelmActions
environment map[string]string environment map[string]string
} }
// sdk implement API func NewSDK(ctx store.KubeContext) (SDK, error) {
var _ API = sdk{} return SDK{
func NewSDK(ctx store.KubeContext) (sdk, error) {
return sdk{
environment: environment(), environment: environment(),
h: helm.NewHelmActions(nil), h: helm.NewHelmActions(nil),
}, nil }, nil
} }
func (s sdk) Connect(ctx context.Context) error {
return nil
}
// Install deploys a Compose stack // Install deploys a Compose stack
func (s sdk) Install(project *types.Project) error { func (s SDK) Install(project *types.Project) error {
chart, err := s.GetChartInMemory(project) chart, err := s.GetChartInMemory(project)
if err != nil { if err != nil {
return err return err
@ -76,21 +55,21 @@ func (s sdk) Install(project *types.Project) error {
} }
// Uninstall removes a runnign compose stack // Uninstall removes a runnign compose stack
func (s sdk) Uninstall(projectName string) error { func (s SDK) Uninstall(projectName string) error {
return s.h.Uninstall(projectName) return s.h.Uninstall(projectName)
} }
// List returns a list of compose stacks // List returns a list of compose stacks
func (s sdk) List(projectName string) ([]compose.Stack, error) { func (s SDK) List(projectName string) ([]compose.Stack, error) {
return s.h.ListReleases() return s.h.ListReleases()
} }
// GetDefault initializes Helm EnvSettings // GetDefault initializes Helm EnvSettings
func (s sdk) GetDefaultEnv() *helmenv.EnvSettings { func (s SDK) GetDefaultEnv() *helmenv.EnvSettings {
return helmenv.New() return helmenv.New()
} }
func (s sdk) GetChartInMemory(project *types.Project) (*chart.Chart, error) { func (s SDK) GetChartInMemory(project *types.Project) (*chart.Chart, error) {
// replace _ with - in volume names // replace _ with - in volume names
for k, v := range project.Volumes { for k, v := range project.Volumes {
volumeName := strings.ReplaceAll(k, "_", "-") volumeName := strings.ReplaceAll(k, "_", "-")
@ -107,7 +86,7 @@ func (s sdk) GetChartInMemory(project *types.Project) (*chart.Chart, error) {
return helm.ConvertToChart(project.Name, objects) return helm.ConvertToChart(project.Name, objects)
} }
func (s sdk) SaveChart(project *types.Project, dest string) error { func (s SDK) SaveChart(project *types.Project, dest string) error {
chart, err := s.GetChartInMemory(project) chart, err := s.GetChartInMemory(project)
if err != nil { if err != nil {
return err return err
@ -115,7 +94,7 @@ func (s sdk) SaveChart(project *types.Project, dest string) error {
return util.SaveDir(chart, dest) return util.SaveDir(chart, dest)
} }
func (s sdk) GenerateChart(project *types.Project, dirname string) error { func (s SDK) GenerateChart(project *types.Project, dirname string) error {
if strings.Contains(dirname, ".") { if strings.Contains(dirname, ".") {
splits := strings.SplitN(dirname, ".", 2) splits := strings.SplitN(dirname, ".", 2)
dirname = splits[0] dirname = splits[0]

View File

@ -30,19 +30,19 @@ import (
// NewComposeService create a kubernetes implementation of the compose.Service API // NewComposeService create a kubernetes implementation of the compose.Service API
func NewComposeService(ctx store.KubeContext) (compose.Service, error) { func NewComposeService(ctx store.KubeContext) (compose.Service, error) {
apiclient, err := charts.NewSDK(ctx) chartsApi, err := charts.NewSDK(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &composeService{ return &composeService{
ctx: ctx, ctx: ctx,
sdk: apiclient, sdk: chartsApi,
}, nil }, nil
} }
type composeService struct { type composeService struct {
ctx store.KubeContext ctx store.KubeContext
sdk charts.API sdk charts.SDK
} }
// Up executes the equivalent to a `compose up` // Up executes the equivalent to a `compose up`