mirror of https://github.com/docker/compose.git
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)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"),
|
||||
}}
|
||||
|
|
|
@ -30,25 +30,27 @@ import (
|
|||
"helm.sh/helm/v3/pkg/release"
|
||||
)
|
||||
|
||||
type HelmActions struct {
|
||||
// Actions helm actions
|
||||
type Actions struct {
|
||||
Config *action.Configuration
|
||||
Settings *env.EnvSettings
|
||||
kube_conn_init bool
|
||||
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{
|
||||
return &Actions{
|
||||
Config: new(action.Configuration),
|
||||
Settings: settings,
|
||||
kube_conn_init: false,
|
||||
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 {
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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`
|
||||
|
|
Loading…
Reference in New Issue