Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit d53d437

Browse files
authored
Merge pull request #1177 from gtardif/kube_linter
Fixing Kube compile / lint errors
2 parents dc790d5 + 67a50b4 commit d53d437

File tree

8 files changed

+61
-73
lines changed

8 files changed

+61
-73
lines changed

cli/cmd/context/create_kube.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,13 @@ func runCreateKube(ctx context.Context, contextName string, opts kube.ContextPar
6565
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
6666
}
6767

68-
contextData, description, err := createContextData(ctx, opts)
69-
if err != nil {
70-
return err
71-
}
68+
contextData, description := createContextData(opts)
7269
return createDockerContext(ctx, contextName, store.KubeContextType, description, contextData)
7370
}
7471

75-
func createContextData(ctx context.Context, opts kube.ContextParams) (interface{}, string, error) {
72+
func createContextData(opts kube.ContextParams) (interface{}, string) {
7673
return store.KubeContext{
7774
Endpoint: opts.Endpoint,
7875
FromEnvironment: opts.FromEnvironment,
79-
}, opts.Description, nil
76+
}, opts.Description
8077
}

kube/charts/charts.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ import (
3333
helmenv "helm.sh/helm/v3/pkg/cli"
3434
)
3535

36+
//SDK chart SDK
3637
type SDK struct {
37-
h *helm.HelmActions
38+
h *helm.Actions
3839
environment map[string]string
3940
}
4041

42+
// NewSDK new chart SDK
4143
func NewSDK(ctx store.KubeContext) (SDK, error) {
4244
return SDK{
4345
environment: environment(),
@@ -60,15 +62,16 @@ func (s SDK) Uninstall(projectName string) error {
6062
}
6163

6264
// List returns a list of compose stacks
63-
func (s SDK) List(projectName string) ([]compose.Stack, error) {
65+
func (s SDK) List() ([]compose.Stack, error) {
6466
return s.h.ListReleases()
6567
}
6668

67-
// GetDefault initializes Helm EnvSettings
69+
// GetDefaultEnv initializes Helm EnvSettings
6870
func (s SDK) GetDefaultEnv() *helmenv.EnvSettings {
6971
return helmenv.New()
7072
}
7173

74+
// GetChartInMemory get memory representation of helm chart
7275
func (s SDK) GetChartInMemory(project *types.Project) (*chart.Chart, error) {
7376
// replace _ with - in volume names
7477
for k, v := range project.Volumes {
@@ -86,6 +89,7 @@ func (s SDK) GetChartInMemory(project *types.Project) (*chart.Chart, error) {
8689
return helm.ConvertToChart(project.Name, objects)
8790
}
8891

92+
// SaveChart converts compose project to helm and saves the chart
8993
func (s SDK) SaveChart(project *types.Project, dest string) error {
9094
chart, err := s.GetChartInMemory(project)
9195
if err != nil {
@@ -94,6 +98,7 @@ func (s SDK) SaveChart(project *types.Project, dest string) error {
9498
return util.SaveDir(chart, dest)
9599
}
96100

101+
// GenerateChart generates helm chart from Compose project
97102
func (s SDK) GenerateChart(project *types.Project, dirname string) error {
98103
if strings.Contains(dirname, ".") {
99104
splits := strings.SplitN(dirname, ".", 2)

kube/charts/helm/chart.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ import (
3131
"k8s.io/apimachinery/pkg/runtime"
3232
)
3333

34+
//ConvertToChart convert Kube objects to helm chart
3435
func ConvertToChart(name string, objects map[string]runtime.Object) (*chart.Chart, error) {
3536

3637
files := []*loader.BufferedFile{
37-
&loader.BufferedFile{
38+
{
3839
Name: "README.md",
3940
Data: []byte("This chart was created by converting a Compose file"),
4041
}}

kube/charts/helm/helm.go

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,27 @@ import (
3030
"helm.sh/helm/v3/pkg/release"
3131
)
3232

33-
type HelmActions struct {
34-
Config *action.Configuration
35-
Settings *env.EnvSettings
36-
kube_conn_init bool
33+
// Actions helm actions
34+
type Actions struct {
35+
Config *action.Configuration
36+
Settings *env.EnvSettings
37+
kubeConnInit bool
3738
}
3839

39-
func NewHelmActions(settings *env.EnvSettings) *HelmActions {
40+
// NewHelmActions new helm action
41+
func NewHelmActions(settings *env.EnvSettings) *Actions {
4042
if settings == nil {
4143
settings = env.New()
4244
}
43-
return &HelmActions{
44-
Config: new(action.Configuration),
45-
Settings: settings,
46-
kube_conn_init: false,
45+
return &Actions{
46+
Config: new(action.Configuration),
47+
Settings: settings,
48+
kubeConnInit: false,
4749
}
4850
}
4951

50-
func (hc *HelmActions) initKubeClient() error {
51-
if hc.kube_conn_init {
52+
func (hc *Actions) initKubeClient() error {
53+
if hc.kubeConnInit {
5254
return nil
5355
}
5456
if err := hc.Config.Init(
@@ -62,20 +64,25 @@ func (hc *HelmActions) initKubeClient() error {
6264
if err := hc.Config.KubeClient.IsReachable(); err != nil {
6365
return err
6466
}
65-
hc.kube_conn_init = true
67+
hc.kubeConnInit = true
6668
return nil
6769
}
6870

69-
func (hc *HelmActions) InstallChartFromDir(name string, chartpath string) error {
71+
//InstallChartFromDir install from dir
72+
func (hc *Actions) InstallChartFromDir(name string, chartpath string) error {
7073
chart, err := loader.Load(chartpath)
7174
if err != nil {
7275
return err
7376
}
7477
return hc.InstallChart(name, chart)
7578
}
7679

77-
func (hc *HelmActions) InstallChart(name string, chart *chart.Chart) error {
78-
hc.initKubeClient()
80+
// InstallChart instal chart
81+
func (hc *Actions) InstallChart(name string, chart *chart.Chart) error {
82+
err := hc.initKubeClient()
83+
if err != nil {
84+
return err
85+
}
7986

8087
actInstall := action.NewInstall(hc.Config)
8188
actInstall.ReleaseName = name
@@ -90,14 +97,19 @@ func (hc *HelmActions) InstallChart(name string, chart *chart.Chart) error {
9097
return nil
9198
}
9299

93-
func (hc *HelmActions) Uninstall(name string) error {
94-
hc.initKubeClient()
100+
// Uninstall uninstall chart
101+
func (hc *Actions) Uninstall(name string) error {
102+
err := hc.initKubeClient()
103+
if err != nil {
104+
return err
105+
}
106+
95107
release, err := hc.Get(name)
96108
if err != nil {
97109
return err
98110
}
99111
if release == nil {
100-
return errors.New("No release found with the name provided.")
112+
return errors.New("no release found with the name provided")
101113
}
102114
actUninstall := action.NewUninstall(hc.Config)
103115
response, err := actUninstall.Run(name)
@@ -108,16 +120,22 @@ func (hc *HelmActions) Uninstall(name string) error {
108120
return nil
109121
}
110122

111-
func (hc *HelmActions) Get(name string) (*release.Release, error) {
112-
hc.initKubeClient()
113-
123+
// Get get released object for a named chart
124+
func (hc *Actions) Get(name string) (*release.Release, error) {
125+
err := hc.initKubeClient()
126+
if err != nil {
127+
return nil, err
128+
}
114129
actGet := action.NewGet(hc.Config)
115130
return actGet.Run(name)
116131
}
117132

118-
func (hc *HelmActions) ListReleases() ([]compose.Stack, error) {
119-
hc.initKubeClient()
120-
133+
// ListReleases lists chart releases
134+
func (hc *Actions) ListReleases() ([]compose.Stack, error) {
135+
err := hc.initKubeClient()
136+
if err != nil {
137+
return nil, err
138+
}
121139
actList := action.NewList(hc.Config)
122140
releases, err := actList.Run()
123141
if err != nil {

kube/charts/kubernetes/kube.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"k8s.io/apimachinery/pkg/util/intstr"
3434
)
3535

36+
//MapToKubernetesObjects maps compose project to Kubernetes objects
3637
func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object, error) {
3738
objects := map[string]runtime.Object{}
3839

@@ -72,7 +73,7 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
7273
for _, p := range service.Ports {
7374
ports = append(ports,
7475
core.ServicePort{
75-
Name: fmt.Sprintf("%d-%s", p.Target, strings.ToLower(string(p.Protocol))),
76+
Name: fmt.Sprintf("%d-%s", p.Target, strings.ToLower(p.Protocol)),
7677
Port: int32(p.Target),
7778
TargetPort: intstr.FromInt(int(p.Target)),
7879
Protocol: toProtocol(p.Protocol),

kube/charts/kubernetes/pod.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,6 @@ func toPodTemplate(project *types.Project, serviceConfig types.ServiceConfig, la
138138
return tpl, nil
139139
}
140140

141-
func toImagePullPolicy(image string, specifiedPolicy string) (apiv1.PullPolicy, error) {
142-
if specifiedPolicy == "" {
143-
if strings.HasSuffix(image, ":latest") {
144-
return apiv1.PullAlways, nil
145-
}
146-
return apiv1.PullIfNotPresent, nil
147-
}
148-
switch apiv1.PullPolicy(specifiedPolicy) {
149-
case apiv1.PullAlways, apiv1.PullIfNotPresent, apiv1.PullNever:
150-
return apiv1.PullPolicy(specifiedPolicy), nil
151-
default:
152-
return "", errors.Errorf("invalid pull policy %q, must be %q, %q or %q", specifiedPolicy, apiv1.PullAlways, apiv1.PullIfNotPresent, apiv1.PullNever)
153-
}
154-
}
155-
156141
func toHostAliases(extraHosts []string) ([]apiv1.HostAlias, error) {
157142
if extraHosts == nil {
158143
return nil, nil
@@ -356,12 +341,3 @@ func toCapabilities(list []string) (capabilities []apiv1.Capability) {
356341
}
357342
return
358343
}
359-
360-
//nolint: unparam
361-
func forceRestartPolicy(podTemplate apiv1.PodTemplateSpec, forcedRestartPolicy apiv1.RestartPolicy) apiv1.PodTemplateSpec {
362-
if podTemplate.Spec.RestartPolicy != "" {
363-
podTemplate.Spec.RestartPolicy = forcedRestartPolicy
364-
}
365-
366-
return podTemplate
367-
}

kube/charts/kubernetes/volumes.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@ type volumeSpec struct {
3737
source *apiv1.VolumeSource
3838
}
3939

40-
func hasPersistentVolumes(s types.ServiceConfig) bool {
41-
for _, volume := range s.Volumes {
42-
if volume.Type == "volume" {
43-
return true
44-
}
45-
}
46-
47-
return false
48-
}
49-
5040
func toVolumeSpecs(project *types.Project, s types.ServiceConfig) ([]volumeSpec, error) {
5141
var specs []volumeSpec
5242
for i, m := range s.Volumes {

kube/compose.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import (
3030

3131
// NewComposeService create a kubernetes implementation of the compose.Service API
3232
func NewComposeService(ctx store.KubeContext) (compose.Service, error) {
33-
chartsApi, err := charts.NewSDK(ctx)
33+
chartsAPI, err := charts.NewSDK(ctx)
3434
if err != nil {
3535
return nil, err
3636
}
3737
return &composeService{
3838
ctx: ctx,
39-
sdk: chartsApi,
39+
sdk: chartsAPI,
4040
}, nil
4141
}
4242

@@ -56,8 +56,8 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
5656
}
5757

5858
// List executes the equivalent to a `docker stack ls`
59-
func (s *composeService) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
60-
return s.sdk.List(projectName)
59+
func (s *composeService) List(ctx context.Context) ([]compose.Stack, error) {
60+
return s.sdk.List()
6161
}
6262

6363
// Build executes the equivalent to a `compose build`

0 commit comments

Comments
 (0)