mirror of
https://github.com/docker/compose.git
synced 2025-07-03 20:04:25 +02:00
Fixing Kube compile / lint errors
Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
parent
dc790d542f
commit
67a50b4ce6
@ -65,16 +65,13 @@ func runCreateKube(ctx context.Context, contextName string, opts kube.ContextPar
|
|||||||
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
|
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
|
||||||
}
|
}
|
||||||
|
|
||||||
contextData, description, err := createContextData(ctx, opts)
|
contextData, description := createContextData(opts)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return createDockerContext(ctx, contextName, store.KubeContextType, description, contextData)
|
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{
|
return store.KubeContext{
|
||||||
Endpoint: opts.Endpoint,
|
Endpoint: opts.Endpoint,
|
||||||
FromEnvironment: opts.FromEnvironment,
|
FromEnvironment: opts.FromEnvironment,
|
||||||
}, opts.Description, nil
|
}, opts.Description
|
||||||
}
|
}
|
||||||
|
@ -33,11 +33,13 @@ import (
|
|||||||
helmenv "helm.sh/helm/v3/pkg/cli"
|
helmenv "helm.sh/helm/v3/pkg/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//SDK chart SDK
|
||||||
type SDK struct {
|
type SDK struct {
|
||||||
h *helm.HelmActions
|
h *helm.Actions
|
||||||
environment map[string]string
|
environment map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSDK new chart SDK
|
||||||
func NewSDK(ctx store.KubeContext) (SDK, error) {
|
func NewSDK(ctx store.KubeContext) (SDK, error) {
|
||||||
return SDK{
|
return SDK{
|
||||||
environment: environment(),
|
environment: environment(),
|
||||||
@ -60,15 +62,16 @@ func (s SDK) Uninstall(projectName string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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() ([]compose.Stack, error) {
|
||||||
return s.h.ListReleases()
|
return s.h.ListReleases()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDefault initializes Helm EnvSettings
|
// GetDefaultEnv initializes Helm EnvSettings
|
||||||
func (s SDK) GetDefaultEnv() *helmenv.EnvSettings {
|
func (s SDK) GetDefaultEnv() *helmenv.EnvSettings {
|
||||||
return helmenv.New()
|
return helmenv.New()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetChartInMemory get memory representation of helm chart
|
||||||
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 {
|
||||||
@ -86,6 +89,7 @@ func (s SDK) GetChartInMemory(project *types.Project) (*chart.Chart, error) {
|
|||||||
return helm.ConvertToChart(project.Name, objects)
|
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 {
|
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 {
|
||||||
@ -94,6 +98,7 @@ func (s SDK) SaveChart(project *types.Project, dest string) error {
|
|||||||
return util.SaveDir(chart, dest)
|
return util.SaveDir(chart, dest)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateChart generates helm chart from Compose project
|
||||||
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)
|
||||||
|
@ -31,10 +31,11 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//ConvertToChart convert Kube objects to helm chart
|
||||||
func ConvertToChart(name string, objects map[string]runtime.Object) (*chart.Chart, error) {
|
func ConvertToChart(name string, objects map[string]runtime.Object) (*chart.Chart, error) {
|
||||||
|
|
||||||
files := []*loader.BufferedFile{
|
files := []*loader.BufferedFile{
|
||||||
&loader.BufferedFile{
|
{
|
||||||
Name: "README.md",
|
Name: "README.md",
|
||||||
Data: []byte("This chart was created by converting a Compose file"),
|
Data: []byte("This chart was created by converting a Compose file"),
|
||||||
}}
|
}}
|
||||||
|
@ -30,25 +30,27 @@ import (
|
|||||||
"helm.sh/helm/v3/pkg/release"
|
"helm.sh/helm/v3/pkg/release"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HelmActions struct {
|
// Actions helm actions
|
||||||
Config *action.Configuration
|
type Actions struct {
|
||||||
Settings *env.EnvSettings
|
Config *action.Configuration
|
||||||
kube_conn_init bool
|
Settings *env.EnvSettings
|
||||||
|
kubeConnInit bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHelmActions(settings *env.EnvSettings) *HelmActions {
|
// NewHelmActions new helm action
|
||||||
|
func NewHelmActions(settings *env.EnvSettings) *Actions {
|
||||||
if settings == nil {
|
if settings == nil {
|
||||||
settings = env.New()
|
settings = env.New()
|
||||||
}
|
}
|
||||||
return &HelmActions{
|
return &Actions{
|
||||||
Config: new(action.Configuration),
|
Config: new(action.Configuration),
|
||||||
Settings: settings,
|
Settings: settings,
|
||||||
kube_conn_init: false,
|
kubeConnInit: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HelmActions) initKubeClient() error {
|
func (hc *Actions) initKubeClient() error {
|
||||||
if hc.kube_conn_init {
|
if hc.kubeConnInit {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := hc.Config.Init(
|
if err := hc.Config.Init(
|
||||||
@ -62,11 +64,12 @@ func (hc *HelmActions) initKubeClient() error {
|
|||||||
if err := hc.Config.KubeClient.IsReachable(); err != nil {
|
if err := hc.Config.KubeClient.IsReachable(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
hc.kube_conn_init = true
|
hc.kubeConnInit = true
|
||||||
return nil
|
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)
|
chart, err := loader.Load(chartpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -74,8 +77,12 @@ func (hc *HelmActions) InstallChartFromDir(name string, chartpath string) error
|
|||||||
return hc.InstallChart(name, chart)
|
return hc.InstallChart(name, chart)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HelmActions) InstallChart(name string, chart *chart.Chart) error {
|
// InstallChart instal chart
|
||||||
hc.initKubeClient()
|
func (hc *Actions) InstallChart(name string, chart *chart.Chart) error {
|
||||||
|
err := hc.initKubeClient()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
actInstall := action.NewInstall(hc.Config)
|
actInstall := action.NewInstall(hc.Config)
|
||||||
actInstall.ReleaseName = name
|
actInstall.ReleaseName = name
|
||||||
@ -90,14 +97,19 @@ func (hc *HelmActions) InstallChart(name string, chart *chart.Chart) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HelmActions) Uninstall(name string) error {
|
// Uninstall uninstall chart
|
||||||
hc.initKubeClient()
|
func (hc *Actions) Uninstall(name string) error {
|
||||||
|
err := hc.initKubeClient()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
release, err := hc.Get(name)
|
release, err := hc.Get(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if release == nil {
|
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)
|
actUninstall := action.NewUninstall(hc.Config)
|
||||||
response, err := actUninstall.Run(name)
|
response, err := actUninstall.Run(name)
|
||||||
@ -108,16 +120,22 @@ func (hc *HelmActions) Uninstall(name string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HelmActions) Get(name string) (*release.Release, error) {
|
// Get get released object for a named chart
|
||||||
hc.initKubeClient()
|
func (hc *Actions) Get(name string) (*release.Release, error) {
|
||||||
|
err := hc.initKubeClient()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
actGet := action.NewGet(hc.Config)
|
actGet := action.NewGet(hc.Config)
|
||||||
return actGet.Run(name)
|
return actGet.Run(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HelmActions) ListReleases() ([]compose.Stack, error) {
|
// ListReleases lists chart releases
|
||||||
hc.initKubeClient()
|
func (hc *Actions) ListReleases() ([]compose.Stack, error) {
|
||||||
|
err := hc.initKubeClient()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
actList := action.NewList(hc.Config)
|
actList := action.NewList(hc.Config)
|
||||||
releases, err := actList.Run()
|
releases, err := actList.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -33,6 +33,7 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/util/intstr"
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//MapToKubernetesObjects maps compose project to Kubernetes objects
|
||||||
func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object, error) {
|
func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object, error) {
|
||||||
objects := map[string]runtime.Object{}
|
objects := map[string]runtime.Object{}
|
||||||
|
|
||||||
@ -72,7 +73,7 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
|
|||||||
for _, p := range service.Ports {
|
for _, p := range service.Ports {
|
||||||
ports = append(ports,
|
ports = append(ports,
|
||||||
core.ServicePort{
|
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),
|
Port: int32(p.Target),
|
||||||
TargetPort: intstr.FromInt(int(p.Target)),
|
TargetPort: intstr.FromInt(int(p.Target)),
|
||||||
Protocol: toProtocol(p.Protocol),
|
Protocol: toProtocol(p.Protocol),
|
||||||
|
@ -138,21 +138,6 @@ func toPodTemplate(project *types.Project, serviceConfig types.ServiceConfig, la
|
|||||||
return tpl, nil
|
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) {
|
func toHostAliases(extraHosts []string) ([]apiv1.HostAlias, error) {
|
||||||
if extraHosts == nil {
|
if extraHosts == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -356,12 +341,3 @@ func toCapabilities(list []string) (capabilities []apiv1.Capability) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint: unparam
|
|
||||||
func forceRestartPolicy(podTemplate apiv1.PodTemplateSpec, forcedRestartPolicy apiv1.RestartPolicy) apiv1.PodTemplateSpec {
|
|
||||||
if podTemplate.Spec.RestartPolicy != "" {
|
|
||||||
podTemplate.Spec.RestartPolicy = forcedRestartPolicy
|
|
||||||
}
|
|
||||||
|
|
||||||
return podTemplate
|
|
||||||
}
|
|
||||||
|
@ -37,16 +37,6 @@ type volumeSpec struct {
|
|||||||
source *apiv1.VolumeSource
|
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) {
|
func toVolumeSpecs(project *types.Project, s types.ServiceConfig) ([]volumeSpec, error) {
|
||||||
var specs []volumeSpec
|
var specs []volumeSpec
|
||||||
for i, m := range s.Volumes {
|
for i, m := range s.Volumes {
|
||||||
|
@ -30,13 +30,13 @@ 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) {
|
||||||
chartsApi, 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: chartsApi,
|
sdk: chartsAPI,
|
||||||
}, nil
|
}, 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`
|
// List executes the equivalent to a `docker stack ls`
|
||||||
func (s *composeService) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
|
func (s *composeService) List(ctx context.Context) ([]compose.Stack, error) {
|
||||||
return s.sdk.List(projectName)
|
return s.sdk.List()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build executes the equivalent to a `compose build`
|
// Build executes the equivalent to a `compose build`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user