Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: process each helmfile in separate routines #944

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 39 additions & 26 deletions pkg/cmd/helmfile/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"

"github.com/helmfile/helmfile/pkg/state"
Expand Down Expand Up @@ -66,7 +68,6 @@ type Options struct {
CommandRunner cmdrunner.CommandRunner
HelmClient helmer.Helmer
Requirements *jxcore.Requirements
NamespaceCharts []*releasereport.NamespaceReleases
PreviousNamespaceCharts map[string]map[string]*releasereport.ReleaseInfo
}

Expand Down Expand Up @@ -175,33 +176,36 @@ func (o *Options) Run() error {
o.PreviousNamespaceCharts = chartMap
}

var wg sync.WaitGroup
var namespaceCharts []*releasereport.NamespaceReleases
for _, hf := range o.Helmfiles {
charts, err := o.processHelmfile(hf)
if err != nil {
return errors.Wrapf(err, "failed to process helmfile %s", hf.Filepath)
}
if charts != nil {
for i, nc := range o.NamespaceCharts {
// lets remove the old entry for the namespace
if nc.Namespace == charts.Namespace {
s := o.NamespaceCharts[0:i]
if i+1 < len(o.NamespaceCharts) {
s = append(s, o.NamespaceCharts[i+1:]...)
}
o.NamespaceCharts = s
}
wg.Add(1)
go func(hf helmfiles.Helmfile) {
defer wg.Done()
charts, err := o.processHelmfile(hf)
if err != nil {
log.Logger().Errorf("failed to process helmfile %s: %v", hf.Filepath, err)
return
}
o.NamespaceCharts = append(o.NamespaceCharts, charts)
}
if charts == nil {
return
}
namespaceCharts = append(namespaceCharts, charts)
}(hf)
}

err = yamls.SaveFile(o.NamespaceCharts, path)
wg.Wait()

// We should sort the charts to avoid unnecessary diffs in the generated YAML
sortCharts(namespaceCharts)

err = yamls.SaveFile(namespaceCharts, path)
if err != nil {
return errors.Wrapf(err, "failed to save %s", path)
}
log.Logger().Infof("saved %s", info(path))

md, err := ToMarkdown(o.NamespaceCharts)
md, err := ToMarkdown(namespaceCharts)
if err != nil {
return errors.Wrap(err, "failed to convert charts to markdown")
}
Expand All @@ -212,7 +216,7 @@ func (o *Options) Run() error {
}
log.Logger().Infof("saved %s", info(path))

return o.generateChartCRDs()
return o.generateChartCRDs(namespaceCharts)
}

func (o *Options) processHelmfile(helmfile helmfiles.Helmfile) (*releasereport.NamespaceReleases, error) {
Expand All @@ -238,8 +242,6 @@ func (o *Options) processHelmfile(helmfile helmfiles.Helmfile) (*releasereport.N
answer.Namespace = ns
answer.Path = helmfile.Filepath

log.Logger().Infof("namespace %s", ns)

for i := range helmState.Releases {
rel := &helmState.Releases[i]
exists, err := o.verifyReleaseExists(ns, rel)
Expand All @@ -266,7 +268,6 @@ func (o *Options) processHelmfile(helmfile helmfiles.Helmfile) (*releasereport.N
answer.Releases = append(answer.Releases, ci)
log.Logger().Infof("found %s", ci.String())
}
log.Logger().Infof("")
return answer, nil
}

Expand Down Expand Up @@ -492,11 +493,11 @@ func (o *Options) discoverIngress(ci *releasereport.ReleaseInfo, rel *state.Rele
return nil
}

func (o *Options) generateChartCRDs() error {
func (o *Options) generateChartCRDs(namespaceCharts []*releasereport.NamespaceReleases) error {
// lets check if we have installed the jx-charter chart which if not we don't generate Chart CRDs
// as we need the CRD to know if we should create them....
found := false
for _, nc := range o.NamespaceCharts {
for _, nc := range namespaceCharts {
for _, r := range nc.Releases {
if r.Name == "jx-charter" {
found = true
Expand All @@ -508,7 +509,7 @@ func (o *Options) generateChartCRDs() error {
return nil
}

for _, nc := range o.NamespaceCharts {
for _, nc := range namespaceCharts {
for _, r := range nc.Releases {
ns := nc.Namespace
name := r.Name
Expand Down Expand Up @@ -589,3 +590,15 @@ func (o *Options) verifyReleaseExists(ns string, r *state.ReleaseSpec) (bool, er
}
return exists, nil
}

// sortCharts sorts the charts by namespace and then sorts the releases by name
func sortCharts(namespaceCharts []*releasereport.NamespaceReleases) {
sort.SliceStable(namespaceCharts, func(i, j int) bool {
return namespaceCharts[i].Namespace < namespaceCharts[j].Namespace
})
for idx := range namespaceCharts {
sort.SliceStable(namespaceCharts[idx].Releases, func(i, j int) bool {
return namespaceCharts[idx].Releases[i].Name < namespaceCharts[idx].Releases[j].Name
})
}
}
Loading