Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jul 1, 2024
1 parent c3ca120 commit 0860c9f
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 161 deletions.
73 changes: 48 additions & 25 deletions internal/charts/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package charts

import (
"fmt"
"path/filepath"
"strings"

"github.com/mrjosh/helm-ls/internal/log"
Expand Down Expand Up @@ -37,6 +38,25 @@ func NewChart(rootURI uri.URI, valuesFilesConfig util.ValuesFilesConfig) *Chart
}
}

func NewChartFromHelmChart(helmChart *chart.Chart, rootURI uri.URI) *Chart {
valuesFile := NewValuesFileFromValues(uri.File(filepath.Join(rootURI.Filename(), "values.yaml")), helmChart.Values)
return &Chart{
ValuesFiles: &ValuesFiles{
MainValuesFile: valuesFile,
OverlayValuesFile: &ValuesFile{},
AdditionalValuesFiles: []*ValuesFile{},
},
ChartMetadata: &ChartMetadata{}, // TODO: there is no usecase for this currently
RootURI: rootURI,
ParentChart: ParentChart{},
HelmChart: helmChart,
}
}

func (c *Chart) GetDependecyUri(dependencyName string) uri.URI {

Check failure on line 56 in internal/charts/chart.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

var-naming: method GetDependecyUri should be GetDependecyURI (revive)
return uri.File(filepath.Join(c.RootURI.Filename(), "charts", DependencyCacheFolder, dependencyName))
}

func loadHelmChart(rootURI uri.URI) *chart.Chart {
var helmChart *chart.Chart
loader, err := loader.Loader(rootURI.Filename())
Expand Down Expand Up @@ -66,31 +86,8 @@ func (c *Chart) ResolveValueFiles(query []string, chartStore *ChartStore) []*Que
return result
}

for _, dependency := range c.HelmChart.Dependencies() {
logger.Debug(fmt.Sprintf("Resolving dependency %s with query %s", dependency.Name(), query))
if dependency.Name() == query[0] {

subQuery := []string{}
if len(query) > 1 {
subQuery = query[1:]
}

valueNode, error := util.ValuesToYamlNode(dependency.Values)
if error != nil {
logger.Error(fmt.Sprintf("Error loading values file %s: %s", dependency.Name(), error.Error()))
continue
}

result = append(result,
// TODO: should we do this now? or should we create a chart in the store for each dependency
&QueriedValuesFiles{Selector: subQuery, ValuesFiles: &ValuesFiles{
MainValuesFile: &ValuesFile{
Values: dependency.Values,
ValueNode: valueNode,
URI: uri.File(dependency.ChartPath()), // TODO: Fix this, chartPath is not a file path but something like chartNameA.common
},
}})
}
if c.HelmChart != nil {
result = c.resolveValuesFilesOfDependencies(query, chartStore, result)
}

parentChart := c.ParentChart.GetParentChart(chartStore)
Expand All @@ -99,6 +96,7 @@ func (c *Chart) ResolveValueFiles(query []string, chartStore *ChartStore) []*Que
}

if query[0] == "global" {
// TODO: add dependency support
return append(result,
parentChart.ResolveValueFiles(query, chartStore)...)
}
Expand All @@ -109,6 +107,31 @@ func (c *Chart) ResolveValueFiles(query []string, chartStore *ChartStore) []*Que
parentChart.ResolveValueFiles(extendedQuery, chartStore)...)
}

func (c *Chart) resolveValuesFilesOfDependencies(query []string, chartStore *ChartStore, result []*QueriedValuesFiles) []*QueriedValuesFiles {
for _, dependency := range c.HelmChart.Dependencies() {
logger.Debug(fmt.Sprintf("Resolving dependency %s with query %s", dependency.Name(), query))
if dependency.Name() == query[0] {

subQuery := []string{}
if len(query) > 1 {
subQuery = query[1:]
}

dependencyChart := chartStore.Charts[c.GetDependecyUri(dependency.Name())]
if dependencyChart == nil {
logger.Error(fmt.Sprintf("Could not find dependency %s", dependency.Name()))
continue
}

// TODO: could call this recursively

result = append(result,
&QueriedValuesFiles{Selector: subQuery, ValuesFiles: dependencyChart.ValuesFiles})
}
}
return result
}

func (c *Chart) GetValueLocation(templateContext []string) (lsp.Location, error) {
modifyedVar := make([]string, len(templateContext))
// make the first letter lowercase since in the template the first letter is
Expand Down
1 change: 1 addition & 0 deletions internal/charts/chart_for_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (s *ChartStore) GetChartForDoc(uri lsp.DocumentURI) (*Chart, error) {

chart, err := s.getChartFromFilesystemForTemplates(uri.Filename())
s.Charts[chart.RootURI] = chart
s.loadChartDependencies(chart)
if err != nil {
return chart, ErrChartNotFound{
URI: uri,
Expand Down
10 changes: 10 additions & 0 deletions internal/charts/chart_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ func (s *ChartStore) ReloadValuesFile(file uri.URI) {
}
}
}

func (s *ChartStore) loadChartDependencies(chart *Chart) {
for _, dependency := range chart.HelmChart.Dependencies() {
dependencyUri := chart.GetDependecyUri(dependency.Name())

Check failure on line 76 in internal/charts/chart_store.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

var-naming: var dependencyUri should be dependencyURI (revive)
chart := NewChartFromHelmChart(dependency, dependencyUri)

s.Charts[dependencyUri] = chart
s.loadChartDependencies(chart)
}
}
15 changes: 15 additions & 0 deletions internal/charts/values_file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package charts

import (
"fmt"

"github.com/mrjosh/helm-ls/internal/util"
"go.lsp.dev/uri"
"helm.sh/helm/v3/pkg/chartutil"
Expand All @@ -24,6 +26,19 @@ func NewValuesFile(filePath string) *ValuesFile {
}
}

func NewValuesFileFromValues(uri uri.URI, values chartutil.Values) *ValuesFile {
valueNode, error := util.ValuesToYamlNode(values)

Check failure on line 30 in internal/charts/values_file.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

redefines-builtin-id: redefinition of the built-in type error (revive)
if error != nil {
logger.Error(fmt.Sprintf("Could not load values for file %s", uri.Filename()), error)
return &ValuesFile{}
}
return &ValuesFile{
ValueNode: valueNode,
Values: values,
URI: uri,
}
}

func (v *ValuesFile) Reload() {
vals, valueNodes := readInValuesFile(v.URI.Filename())

Expand Down
3 changes: 3 additions & 0 deletions internal/handler/text_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func (h *langHandler) DidRenameFiles(ctx context.Context, params *lsp.RenameFile
}

func (h *langHandler) LoadDocsOnNewChart(chart *charts.Chart) {
if chart.HelmChart == nil {
return
}
for _, file := range chart.HelmChart.Templates {
h.documents.Store(filepath.Join(chart.RootURI.Filename(), file.Name), file.Data, h.helmlsConfig)
}
Expand Down
32 changes: 0 additions & 32 deletions testdata/dependenciesExample/templates/hpa.yaml

This file was deleted.

61 changes: 0 additions & 61 deletions testdata/dependenciesExample/templates/ingress.yaml

This file was deleted.

15 changes: 0 additions & 15 deletions testdata/dependenciesExample/templates/service.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions testdata/dependenciesExample/templates/serviceaccount.yaml

This file was deleted.

15 changes: 0 additions & 15 deletions testdata/dependenciesExample/templates/tests/test-connection.yaml

This file was deleted.

0 comments on commit 0860c9f

Please sign in to comment.