fix: includes-transitive issue

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2023-09-09 10:44:23 +08:00
parent 430a825b12
commit e919131f17
10 changed files with 54 additions and 55 deletions

View File

@ -189,7 +189,8 @@ func (a *App) Diff(c DiffConfigProvider) error {
IncludeCRDs: &includeCRDs, IncludeCRDs: &includeCRDs,
Validate: c.Validate(), Validate: c.Validate(),
Concurrency: c.Concurrency(), Concurrency: c.Concurrency(),
IncludeTransitiveNeeds: c.IncludeNeeds(), IncludeTransitiveNeeds: c.IncludeTransitiveNeeds(),
IncludeNeeds: c.IncludeNeeds(),
}, func() { }, func() {
msg, matched, affected, errs = a.diff(run, c) msg, matched, affected, errs = a.diff(run, c)
}) })
@ -255,8 +256,9 @@ func (a *App) Template(c TemplateConfigProvider) error {
SkipCleanup: c.SkipCleanup(), SkipCleanup: c.SkipCleanup(),
Validate: c.Validate(), Validate: c.Validate(),
Concurrency: c.Concurrency(), Concurrency: c.Concurrency(),
IncludeTransitiveNeeds: c.IncludeNeeds(),
Set: c.Set(), Set: c.Set(),
IncludeTransitiveNeeds: c.IncludeTransitiveNeeds(),
IncludeNeeds: c.IncludeNeeds(),
}, func() { }, func() {
ok, errs = a.template(run, c) ok, errs = a.template(run, c)
}) })
@ -327,7 +329,8 @@ func (a *App) Lint(c LintConfigProvider) error {
SkipDeps: c.SkipDeps(), SkipDeps: c.SkipDeps(),
SkipCleanup: c.SkipCleanup(), SkipCleanup: c.SkipCleanup(),
Concurrency: c.Concurrency(), Concurrency: c.Concurrency(),
IncludeTransitiveNeeds: c.IncludeNeeds(), IncludeTransitiveNeeds: c.IncludeTransitiveNeeds(),
IncludeNeeds: c.IncludeNeeds(),
}, func() { }, func() {
ok, lintErrs, errs = a.lint(run, c) ok, lintErrs, errs = a.lint(run, c)
}) })
@ -420,7 +423,8 @@ func (a *App) Apply(c ApplyConfigProvider) error {
SkipCleanup: c.RetainValuesFiles() || c.SkipCleanup(), SkipCleanup: c.RetainValuesFiles() || c.SkipCleanup(),
Validate: c.Validate(), Validate: c.Validate(),
Concurrency: c.Concurrency(), Concurrency: c.Concurrency(),
IncludeTransitiveNeeds: c.IncludeNeeds(), IncludeTransitiveNeeds: c.IncludeTransitiveNeeds(),
IncludeNeeds: c.IncludeNeeds(),
}, func() { }, func() {
matched, updated, es := a.apply(run, c) matched, updated, es := a.apply(run, c)
@ -973,7 +977,7 @@ func (a *App) ForEachState(do func(*Run) (bool, []error), includeTransitiveNeeds
return err return err
} }
func printBatches(batches [][]state.Release) string { func printBatches(batches [][]state.ReleaseSpec) string {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
w := new(tabwriter.Writer) w := new(tabwriter.Writer)
@ -985,7 +989,7 @@ func printBatches(batches [][]state.Release) string {
for i, batch := range batches { for i, batch := range batches {
ids := []string{} ids := []string{}
for _, r := range batch { for _, r := range batch {
ids = append(ids, state.ReleaseToID(&r.ReleaseSpec)) ids = append(ids, state.ReleaseToID(&r))
} }
fmt.Fprintf(w, "%d\t%s\n", i+1, strings.Join(ids, ", ")) fmt.Fprintf(w, "%d\t%s\n", i+1, strings.Join(ids, ", "))
} }
@ -1005,7 +1009,7 @@ func withDAG(templated *state.HelmState, helm helmexec.Interface, logger *zap.Su
return withBatches(opts.Purpose, templated, batches, helm, logger, converge) return withBatches(opts.Purpose, templated, batches, helm, logger, converge)
} }
func withBatches(purpose string, templated *state.HelmState, batches [][]state.Release, helm helmexec.Interface, logger *zap.SugaredLogger, converge func(*state.HelmState, helmexec.Interface) (bool, []error)) (bool, []error) { func withBatches(purpose string, templated *state.HelmState, batches [][]state.ReleaseSpec, helm helmexec.Interface, logger *zap.SugaredLogger, converge func(*state.HelmState, helmexec.Interface) (bool, []error)) (bool, []error) {
numBatches := len(batches) numBatches := len(batches)
if purpose == "" { if purpose == "" {
@ -1018,10 +1022,7 @@ func withBatches(purpose string, templated *state.HelmState, batches [][]state.R
for i, batch := range batches { for i, batch := range batches {
var targets []state.ReleaseSpec var targets []state.ReleaseSpec
targets = append(targets, batch...)
for _, marked := range batch {
targets = append(targets, marked.ReleaseSpec)
}
var releaseIds []string var releaseIds []string
for _, r := range targets { for _, r := range targets {
@ -1347,9 +1348,7 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) {
var toApplyWithNeeds []state.ReleaseSpec var toApplyWithNeeds []state.ReleaseSpec
for _, rs := range plan { for _, rs := range plan {
for _, r := range rs { toApplyWithNeeds = append(toApplyWithNeeds, rs...)
toApplyWithNeeds = append(toApplyWithNeeds, r.ReleaseSpec)
}
} }
// Do build deps and prepare only on selected releases so that we won't waste time // Do build deps and prepare only on selected releases so that we won't waste time
@ -1743,9 +1742,7 @@ func (a *App) sync(r *Run, c SyncConfigProvider) (bool, []error) {
var toSyncWithNeeds []state.ReleaseSpec var toSyncWithNeeds []state.ReleaseSpec
for _, rs := range batches { for _, rs := range batches {
for _, r := range rs { toSyncWithNeeds = append(toSyncWithNeeds, rs...)
toSyncWithNeeds = append(toSyncWithNeeds, r.ReleaseSpec)
}
} }
// Do build deps and prepare only on selected releases so that we won't waste time // Do build deps and prepare only on selected releases so that we won't waste time
@ -1948,9 +1945,7 @@ func (a *App) withNeeds(r *Run, c DAGConfig, includeDisabled bool, f func(*state
var selectedReleasesWithNeeds []state.ReleaseSpec var selectedReleasesWithNeeds []state.ReleaseSpec
for _, rs := range batches { for _, rs := range batches {
for _, r := range rs { selectedReleasesWithNeeds = append(selectedReleasesWithNeeds, rs...)
selectedReleasesWithNeeds = append(selectedReleasesWithNeeds, r.ReleaseSpec)
}
} }
var toRender []state.ReleaseSpec var toRender []state.ReleaseSpec

View File

@ -2141,7 +2141,7 @@ func (c configImpl) SkipTests() bool {
} }
func (c configImpl) IncludeNeeds() bool { func (c configImpl) IncludeNeeds() bool {
return c.includeNeeds || c.IncludeTransitiveNeeds() return c.includeNeeds
} }
func (c configImpl) IncludeTransitiveNeeds() bool { func (c configImpl) IncludeTransitiveNeeds() bool {
@ -2273,7 +2273,7 @@ func (a applyConfig) SkipNeeds() bool {
} }
func (a applyConfig) IncludeNeeds() bool { func (a applyConfig) IncludeNeeds() bool {
return a.includeNeeds || a.IncludeTransitiveNeeds() return a.includeNeeds
} }
func (a applyConfig) IncludeTransitiveNeeds() bool { func (a applyConfig) IncludeTransitiveNeeds() bool {

View File

@ -81,7 +81,7 @@ func (a diffConfig) SkipNeeds() bool {
} }
func (a diffConfig) IncludeNeeds() bool { func (a diffConfig) IncludeNeeds() bool {
return a.includeNeeds || a.IncludeTransitiveNeeds() return a.includeNeeds
} }
func (a diffConfig) IncludeTransitiveNeeds() bool { func (a diffConfig) IncludeTransitiveNeeds() bool {

View File

@ -113,7 +113,7 @@ func (a *ApplyImpl) DiffOutput() string {
// IncludeNeeds returns the include needs. // IncludeNeeds returns the include needs.
func (a *ApplyImpl) IncludeNeeds() bool { func (a *ApplyImpl) IncludeNeeds() bool {
return a.ApplyOptions.IncludeNeeds || a.IncludeTransitiveNeeds() return a.ApplyOptions.IncludeNeeds
} }
// IncludeTests returns the include tests. // IncludeTests returns the include tests.

View File

@ -72,7 +72,7 @@ func (t *DiffImpl) Concurrency() int {
// IncludeNeeds returns the include needs // IncludeNeeds returns the include needs
func (t *DiffImpl) IncludeNeeds() bool { func (t *DiffImpl) IncludeNeeds() bool {
return t.DiffOptions.IncludeNeeds || t.IncludeTransitiveNeeds() return t.DiffOptions.IncludeNeeds
} }
// IncludeTransitiveNeeds returns the include transitive needs // IncludeTransitiveNeeds returns the include transitive needs

View File

@ -58,7 +58,7 @@ func (l *LintImpl) SkipCleanup() bool {
// IncludeNeeds returns the include needs // IncludeNeeds returns the include needs
func (l *LintImpl) IncludeNeeds() bool { func (l *LintImpl) IncludeNeeds() bool {
return l.LintOptions.IncludeNeeds || l.IncludeTransitiveNeeds() return l.LintOptions.IncludeNeeds
} }
// IncludeTransitiveNeeds returns the include transitive needs // IncludeTransitiveNeeds returns the include transitive needs

View File

@ -58,7 +58,7 @@ func (t *SyncImpl) Concurrency() int {
// IncludeNeeds returns the include needs // IncludeNeeds returns the include needs
func (t *SyncImpl) IncludeNeeds() bool { func (t *SyncImpl) IncludeNeeds() bool {
return t.SyncOptions.IncludeNeeds || t.IncludeTransitiveNeeds() return t.SyncOptions.IncludeNeeds
} }
// IncludeTransitiveNeeds returns the include transitive needs // IncludeTransitiveNeeds returns the include transitive needs

View File

@ -69,7 +69,7 @@ func (t *TemplateImpl) IncludeCRDs() bool {
// IncludeNeeds returns the include needs // IncludeNeeds returns the include needs
func (t *TemplateImpl) IncludeNeeds() bool { func (t *TemplateImpl) IncludeNeeds() bool {
return t.TemplateOptions.IncludeNeeds || t.IncludeTransitiveNeeds() return t.TemplateOptions.IncludeNeeds
} }
// IncludeTransitiveNeeds returns the include transitive needs // IncludeTransitiveNeeds returns the include transitive needs

View File

@ -1045,6 +1045,7 @@ type ChartPrepareOptions struct {
OutputDir string OutputDir string
OutputDirTemplate string OutputDirTemplate string
IncludeTransitiveNeeds bool IncludeTransitiveNeeds bool
IncludeNeeds bool
Concurrency int Concurrency int
KubeVersion string KubeVersion string
Set []string Set []string
@ -2208,9 +2209,7 @@ func markExcludedReleases(releases []ReleaseSpec, selectors []string, commonLabe
} }
filteredReleases = append(filteredReleases, res) filteredReleases = append(filteredReleases, res)
} }
if includeTransitiveNeeds { unmarkNeeds(filteredReleases, releases, includeTransitiveNeeds)
unmarkNeedsAndTransitives(filteredReleases, releases)
}
return filteredReleases, nil return filteredReleases, nil
} }
@ -2237,38 +2236,43 @@ func ConditionEnabled(r ReleaseSpec, values map[string]any) (bool, error) {
return conditionMatch, nil return conditionMatch, nil
} }
func unmarkNeedsAndTransitives(filteredReleases []Release, allReleases []ReleaseSpec) { func unmarkNeeds(filteredReleases []Release, allReleases []ReleaseSpec, includeTransitiveNeeds bool) {
needsWithTranstives := collectAllNeedsWithTransitives(filteredReleases, allReleases) needsWithTranstives := collectAllNeeds(filteredReleases, allReleases, includeTransitiveNeeds)
unmarkReleases(needsWithTranstives, filteredReleases) unmarkReleases(needsWithTranstives, filteredReleases, includeTransitiveNeeds)
} }
func collectAllNeedsWithTransitives(filteredReleases []Release, allReleases []ReleaseSpec) map[string]struct{} { func collectAllNeeds(filteredReleases []Release, allReleases []ReleaseSpec, includeTransitiveNeeds bool) map[string]struct{} {
needsWithTranstives := map[string]struct{}{} needs := map[string]struct{}{}
for _, r := range filteredReleases { for _, r := range filteredReleases {
if !r.Filtered { if !r.Filtered {
collectNeedsWithTransitives(r.ReleaseSpec, allReleases, needsWithTranstives) collectNeeds(r.ReleaseSpec, allReleases, needs, includeTransitiveNeeds)
} }
} }
return needsWithTranstives return needs
} }
func unmarkReleases(toUnmark map[string]struct{}, releases []Release) { func unmarkReleases(toUnmark map[string]struct{}, releases []Release, includeTransitiveNeeds bool) {
for i, r := range releases { for i, r := range releases {
if _, ok := toUnmark[ReleaseToID(&r.ReleaseSpec)]; ok { if _, ok := toUnmark[ReleaseToID(&r.ReleaseSpec)]; ok {
if releases[i].Filtered && !includeTransitiveNeeds {
releases[i].Needs = nil
}
releases[i].Filtered = false releases[i].Filtered = false
} }
} }
} }
func collectNeedsWithTransitives(release ReleaseSpec, allReleases []ReleaseSpec, needsWithTranstives map[string]struct{}) { func collectNeeds(release ReleaseSpec, allReleases []ReleaseSpec, needs map[string]struct{}, includeTransitiveNeeds bool) {
for _, id := range release.Needs { for _, id := range release.Needs {
if _, exists := needsWithTranstives[id]; !exists { if _, exists := needs[id]; !exists {
needsWithTranstives[id] = struct{}{} needs[id] = struct{}{}
releaseParts := strings.Split(id, "/") if includeTransitiveNeeds {
releaseName := releaseParts[len(releaseParts)-1] releaseParts := strings.Split(id, "/")
for _, r := range allReleases { releaseName := releaseParts[len(releaseParts)-1]
if r.Name == releaseName { for _, r := range allReleases {
collectNeedsWithTransitives(r, allReleases, needsWithTranstives) if r.Name == releaseName {
collectNeeds(r, allReleases, needs, includeTransitiveNeeds)
}
} }
} }
} }

View File

@ -98,8 +98,8 @@ type PlanOptions struct {
SelectedReleases []ReleaseSpec SelectedReleases []ReleaseSpec
} }
func (st *HelmState) PlanReleases(opts PlanOptions) ([][]Release, error) { func (st *HelmState) PlanReleases(opts PlanOptions) ([][]ReleaseSpec, error) {
marked, err := st.SelectReleases(opts.IncludeTransitiveNeeds) marked, err := st.GetSelectedReleases(opts.IncludeTransitiveNeeds)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -112,7 +112,7 @@ func (st *HelmState) PlanReleases(opts PlanOptions) ([][]Release, error) {
return groups, nil return groups, nil
} }
func SortedReleaseGroups(releases []Release, opts PlanOptions) ([][]Release, error) { func SortedReleaseGroups(releases []ReleaseSpec, opts PlanOptions) ([][]ReleaseSpec, error) {
reverse := opts.Reverse reverse := opts.Reverse
groups, err := GroupReleasesByDependency(releases, opts) groups, err := GroupReleasesByDependency(releases, opts)
@ -129,13 +129,13 @@ func SortedReleaseGroups(releases []Release, opts PlanOptions) ([][]Release, err
return groups, nil return groups, nil
} }
func GroupReleasesByDependency(releases []Release, opts PlanOptions) ([][]Release, error) { func GroupReleasesByDependency(releases []ReleaseSpec, opts PlanOptions) ([][]ReleaseSpec, error) {
idToReleases := map[string][]Release{} idToReleases := map[string][]ReleaseSpec{}
idToIndex := map[string]int{} idToIndex := map[string]int{}
d := dag.New() d := dag.New()
for i, r := range releases { for i, r := range releases {
id := ReleaseToID(&r.ReleaseSpec) id := ReleaseToID(&r)
idToReleases[id] = append(idToReleases[id], r) idToReleases[id] = append(idToReleases[id], r)
idToIndex[id] = i idToIndex[id] = i
@ -226,13 +226,13 @@ func GroupReleasesByDependency(releases []Release, opts PlanOptions) ([][]Releas
return nil, err return nil, err
} }
var result [][]Release var result [][]ReleaseSpec
for groupIndex := 0; groupIndex < len(plan); groupIndex++ { for groupIndex := 0; groupIndex < len(plan); groupIndex++ {
dagNodesInGroup := plan[groupIndex] dagNodesInGroup := plan[groupIndex]
var idsInGroup []string var idsInGroup []string
var releasesInGroup []Release var releasesInGroup []ReleaseSpec
for _, node := range dagNodesInGroup { for _, node := range dagNodesInGroup {
idsInGroup = append(idsInGroup, node.Id) idsInGroup = append(idsInGroup, node.Id)