diff --git a/cli/cmd/context/create_kube.go b/cli/cmd/context/create_kube.go index 5e2260f65..a963002fe 100644 --- a/cli/cmd/context/create_kube.go +++ b/cli/cmd/context/create_kube.go @@ -65,16 +65,13 @@ func runCreateKube(ctx context.Context, contextName string, opts kube.ContextPar return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName) } - contextData, description, err := createContextData(ctx, opts) - if err != nil { - return err - } + contextData, description := createContextData(opts) return createDockerContext(ctx, contextName, store.KubeContextType, description, contextData) } -func createContextData(ctx context.Context, opts kube.ContextParams) (interface{}, string, error) { +func createContextData(opts kube.ContextParams) (interface{}, string) { return store.KubeContext{ Endpoint: opts.Endpoint, FromEnvironment: opts.FromEnvironment, - }, opts.Description, nil + }, opts.Description } diff --git a/kube/charts/charts.go b/kube/charts/charts.go index b09608ca6..f3ead6bb6 100644 --- a/kube/charts/charts.go +++ b/kube/charts/charts.go @@ -33,11 +33,13 @@ import ( helmenv "helm.sh/helm/v3/pkg/cli" ) +//SDK chart SDK type SDK struct { - h *helm.HelmActions + h *helm.Actions environment map[string]string } +// NewSDK new chart SDK func NewSDK(ctx store.KubeContext) (SDK, error) { return SDK{ environment: environment(), @@ -60,15 +62,16 @@ func (s SDK) Uninstall(projectName string) error { } // List returns a list of compose stacks -func (s SDK) List(projectName string) ([]compose.Stack, error) { +func (s SDK) List() ([]compose.Stack, error) { return s.h.ListReleases() } -// GetDefault initializes Helm EnvSettings +// GetDefaultEnv initializes Helm EnvSettings func (s SDK) GetDefaultEnv() *helmenv.EnvSettings { return helmenv.New() } +// GetChartInMemory get memory representation of helm chart func (s SDK) GetChartInMemory(project *types.Project) (*chart.Chart, error) { // replace _ with - in volume names for k, v := range project.Volumes { @@ -86,6 +89,7 @@ func (s SDK) GetChartInMemory(project *types.Project) (*chart.Chart, error) { return helm.ConvertToChart(project.Name, objects) } +// SaveChart converts compose project to helm and saves the chart func (s SDK) SaveChart(project *types.Project, dest string) error { chart, err := s.GetChartInMemory(project) if err != nil { @@ -94,6 +98,7 @@ func (s SDK) SaveChart(project *types.Project, dest string) error { return util.SaveDir(chart, dest) } +// GenerateChart generates helm chart from Compose project func (s SDK) GenerateChart(project *types.Project, dirname string) error { if strings.Contains(dirname, ".") { splits := strings.SplitN(dirname, ".", 2) diff --git a/kube/charts/helm/chart.go b/kube/charts/helm/chart.go index d82ed6e9c..e198195cb 100644 --- a/kube/charts/helm/chart.go +++ b/kube/charts/helm/chart.go @@ -31,10 +31,11 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) +//ConvertToChart convert Kube objects to helm chart func ConvertToChart(name string, objects map[string]runtime.Object) (*chart.Chart, error) { files := []*loader.BufferedFile{ - &loader.BufferedFile{ + { Name: "README.md", Data: []byte("This chart was created by converting a Compose file"), }} diff --git a/kube/charts/helm/helm.go b/kube/charts/helm/helm.go index e7e7f42fd..dc03eb6da 100644 --- a/kube/charts/helm/helm.go +++ b/kube/charts/helm/helm.go @@ -30,25 +30,27 @@ import ( "helm.sh/helm/v3/pkg/release" ) -type HelmActions struct { - Config *action.Configuration - Settings *env.EnvSettings - kube_conn_init bool +// Actions helm actions +type Actions struct { + Config *action.Configuration + Settings *env.EnvSettings + kubeConnInit bool } -func NewHelmActions(settings *env.EnvSettings) *HelmActions { +// NewHelmActions new helm action +func NewHelmActions(settings *env.EnvSettings) *Actions { if settings == nil { settings = env.New() } - return &HelmActions{ - Config: new(action.Configuration), - Settings: settings, - kube_conn_init: false, + return &Actions{ + Config: new(action.Configuration), + Settings: settings, + kubeConnInit: false, } } -func (hc *HelmActions) initKubeClient() error { - if hc.kube_conn_init { +func (hc *Actions) initKubeClient() error { + if hc.kubeConnInit { return nil } if err := hc.Config.Init( @@ -62,11 +64,12 @@ func (hc *HelmActions) initKubeClient() error { if err := hc.Config.KubeClient.IsReachable(); err != nil { return err } - hc.kube_conn_init = true + hc.kubeConnInit = true return nil } -func (hc *HelmActions) InstallChartFromDir(name string, chartpath string) error { +//InstallChartFromDir install from dir +func (hc *Actions) InstallChartFromDir(name string, chartpath string) error { chart, err := loader.Load(chartpath) if err != nil { return err @@ -74,8 +77,12 @@ func (hc *HelmActions) InstallChartFromDir(name string, chartpath string) error return hc.InstallChart(name, chart) } -func (hc *HelmActions) InstallChart(name string, chart *chart.Chart) error { - hc.initKubeClient() +// InstallChart instal chart +func (hc *Actions) InstallChart(name string, chart *chart.Chart) error { + err := hc.initKubeClient() + if err != nil { + return err + } actInstall := action.NewInstall(hc.Config) actInstall.ReleaseName = name @@ -90,14 +97,19 @@ func (hc *HelmActions) InstallChart(name string, chart *chart.Chart) error { return nil } -func (hc *HelmActions) Uninstall(name string) error { - hc.initKubeClient() +// Uninstall uninstall chart +func (hc *Actions) Uninstall(name string) error { + err := hc.initKubeClient() + if err != nil { + return err + } + release, err := hc.Get(name) if err != nil { return err } if release == nil { - return errors.New("No release found with the name provided.") + return errors.New("no release found with the name provided") } actUninstall := action.NewUninstall(hc.Config) response, err := actUninstall.Run(name) @@ -108,16 +120,22 @@ func (hc *HelmActions) Uninstall(name string) error { return nil } -func (hc *HelmActions) Get(name string) (*release.Release, error) { - hc.initKubeClient() - +// Get get released object for a named chart +func (hc *Actions) Get(name string) (*release.Release, error) { + err := hc.initKubeClient() + if err != nil { + return nil, err + } actGet := action.NewGet(hc.Config) return actGet.Run(name) } -func (hc *HelmActions) ListReleases() ([]compose.Stack, error) { - hc.initKubeClient() - +// ListReleases lists chart releases +func (hc *Actions) ListReleases() ([]compose.Stack, error) { + err := hc.initKubeClient() + if err != nil { + return nil, err + } actList := action.NewList(hc.Config) releases, err := actList.Run() if err != nil { diff --git a/kube/charts/kubernetes/kube.go b/kube/charts/kubernetes/kube.go index ded7419b8..6b08d79a9 100644 --- a/kube/charts/kubernetes/kube.go +++ b/kube/charts/kubernetes/kube.go @@ -33,6 +33,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" ) +//MapToKubernetesObjects maps compose project to Kubernetes objects func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object, error) { objects := map[string]runtime.Object{} @@ -72,7 +73,7 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser for _, p := range service.Ports { ports = append(ports, core.ServicePort{ - Name: fmt.Sprintf("%d-%s", p.Target, strings.ToLower(string(p.Protocol))), + Name: fmt.Sprintf("%d-%s", p.Target, strings.ToLower(p.Protocol)), Port: int32(p.Target), TargetPort: intstr.FromInt(int(p.Target)), Protocol: toProtocol(p.Protocol), diff --git a/kube/charts/kubernetes/pod.go b/kube/charts/kubernetes/pod.go index 632d122e2..e2acf0533 100644 --- a/kube/charts/kubernetes/pod.go +++ b/kube/charts/kubernetes/pod.go @@ -138,21 +138,6 @@ func toPodTemplate(project *types.Project, serviceConfig types.ServiceConfig, la return tpl, nil } -func toImagePullPolicy(image string, specifiedPolicy string) (apiv1.PullPolicy, error) { - if specifiedPolicy == "" { - if strings.HasSuffix(image, ":latest") { - return apiv1.PullAlways, nil - } - return apiv1.PullIfNotPresent, nil - } - switch apiv1.PullPolicy(specifiedPolicy) { - case apiv1.PullAlways, apiv1.PullIfNotPresent, apiv1.PullNever: - return apiv1.PullPolicy(specifiedPolicy), nil - default: - return "", errors.Errorf("invalid pull policy %q, must be %q, %q or %q", specifiedPolicy, apiv1.PullAlways, apiv1.PullIfNotPresent, apiv1.PullNever) - } -} - func toHostAliases(extraHosts []string) ([]apiv1.HostAlias, error) { if extraHosts == nil { return nil, nil @@ -356,12 +341,3 @@ func toCapabilities(list []string) (capabilities []apiv1.Capability) { } return } - -//nolint: unparam -func forceRestartPolicy(podTemplate apiv1.PodTemplateSpec, forcedRestartPolicy apiv1.RestartPolicy) apiv1.PodTemplateSpec { - if podTemplate.Spec.RestartPolicy != "" { - podTemplate.Spec.RestartPolicy = forcedRestartPolicy - } - - return podTemplate -} diff --git a/kube/charts/kubernetes/volumes.go b/kube/charts/kubernetes/volumes.go index 6182cf055..0e331ac59 100644 --- a/kube/charts/kubernetes/volumes.go +++ b/kube/charts/kubernetes/volumes.go @@ -37,16 +37,6 @@ type volumeSpec struct { source *apiv1.VolumeSource } -func hasPersistentVolumes(s types.ServiceConfig) bool { - for _, volume := range s.Volumes { - if volume.Type == "volume" { - return true - } - } - - return false -} - func toVolumeSpecs(project *types.Project, s types.ServiceConfig) ([]volumeSpec, error) { var specs []volumeSpec for i, m := range s.Volumes { diff --git a/kube/compose.go b/kube/compose.go index b9e55c322..5160a48d0 100644 --- a/kube/compose.go +++ b/kube/compose.go @@ -30,13 +30,13 @@ import ( // NewComposeService create a kubernetes implementation of the compose.Service API func NewComposeService(ctx store.KubeContext) (compose.Service, error) { - chartsApi, err := charts.NewSDK(ctx) + chartsAPI, err := charts.NewSDK(ctx) if err != nil { return nil, err } return &composeService{ ctx: ctx, - sdk: chartsApi, + sdk: chartsAPI, }, nil } @@ -56,8 +56,8 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c } // List executes the equivalent to a `docker stack ls` -func (s *composeService) List(ctx context.Context, projectName string) ([]compose.Stack, error) { - return s.sdk.List(projectName) +func (s *composeService) List(ctx context.Context) ([]compose.Stack, error) { + return s.sdk.List() } // Build executes the equivalent to a `compose build`