Files
zeekay 31c64ef661 test, applicationset: drop Argo branding, fix bugs it was hiding
Rename test names, comments, fixture data, and mock GitHub/webhook
responses that still said Argo/ArgoCD/argocd/argoproj in our own
voice, matching the hanzocd-* binary/configmap names and the
apps.hanzo.ai CRD/annotation domain already established elsewhere
in the fork. Also drop the dated argov1alpha1/argoprojiov1alpha1
import aliases for our own v1alpha1 API package.

Restore the CRD groups a prior blind rename had mangled on a
vendored third-party Argo Workflows example manifest back to the
real argoproj.io, and fix several places where an earlier
half-finished rename had gone silently wrong instead of just being
cosmetic: stale CD_BINARY_NAME values and a stale
--argocd-repo-server flag no longer recognized by the binary, cert
filenames and an ssh_known_hosts alias that didn't match what's
actually on disk, a namespace and a kustomize image substitution
that were silent no-ops, and a test assertion regex that could
never match its own fixture.

External lookup keys we don't control are left byte-exact:
quay.io/argoprojlabs images, the docker.io/argoproj CI builder
image, live argoproj/argo-workflows release URLs, the generated
ArgoprojV1alpha1() clientset method, and the Makefile-coupled
/tmp/argo-e2e and GOPATH mount conventions.
2026-09-05 01:07:59 -07:00

176 lines
5.4 KiB
Go

package generators
import (
"errors"
"fmt"
"time"
"dario.cat/mergo"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/hanzoai/cd/applicationset/utils"
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
)
var _ Generator = (*MatrixGenerator)(nil)
var (
ErrMoreThanTwoGenerators = errors.New("found more than two generators, Matrix support only two")
ErrLessThanTwoGenerators = errors.New("found less than two generators, Matrix support only two")
ErrMoreThenOneInnerGenerators = errors.New("found more than one generator in matrix.Generators")
)
type MatrixGenerator struct {
// The inner generators supported by the matrix generator (cluster, git, list...)
supportedGenerators map[string]Generator
}
func NewMatrixGenerator(supportedGenerators map[string]Generator) Generator {
m := &MatrixGenerator{
supportedGenerators: supportedGenerators,
}
return m
}
func (m *MatrixGenerator) GenerateParams(appSetGenerator *v1alpha1.ApplicationSetGenerator, appSet *v1alpha1.ApplicationSet, client client.Client) ([]map[string]any, error) {
if appSetGenerator.Matrix == nil {
return nil, ErrEmptyAppSetGenerator
}
if len(appSetGenerator.Matrix.Generators) < 2 {
return nil, ErrLessThanTwoGenerators
}
if len(appSetGenerator.Matrix.Generators) > 2 {
return nil, ErrMoreThanTwoGenerators
}
res := []map[string]any{}
g0, err := m.getParams(appSetGenerator.Matrix.Generators[0], appSet, nil, client)
if err != nil {
return nil, fmt.Errorf("error failed to get params for first generator in matrix generator: %w", err)
}
for _, a := range g0 {
g1, err := m.getParams(appSetGenerator.Matrix.Generators[1], appSet, a, client)
if err != nil {
return nil, fmt.Errorf("failed to get params for second generator in the matrix generator: %w", err)
}
for _, b := range g1 {
if appSet.Spec.GoTemplate {
tmp := map[string]any{}
if err := mergo.Merge(&tmp, b, mergo.WithOverride); err != nil {
return nil, fmt.Errorf("failed to merge params from the second generator in the matrix generator with temp map: %w", err)
}
if err := mergo.Merge(&tmp, a, mergo.WithOverride); err != nil {
return nil, fmt.Errorf("failed to merge params from the second generator in the matrix generator with the first: %w", err)
}
res = append(res, tmp)
} else {
val, err := utils.CombineStringMaps(a, b)
if err != nil {
return nil, fmt.Errorf("failed to combine string maps with merging params for the matrix generator: %w", err)
}
res = append(res, val)
}
}
}
return res, nil
}
func (m *MatrixGenerator) getParams(appSetBaseGenerator v1alpha1.ApplicationSetNestedGenerator, appSet *v1alpha1.ApplicationSet, params map[string]any, client client.Client) ([]map[string]any, error) {
matrixGen, err := getMatrixGenerator(appSetBaseGenerator)
if err != nil {
return nil, err
}
mergeGen, err := getMergeGenerator(appSetBaseGenerator)
if err != nil {
return nil, fmt.Errorf("error retrieving merge generator: %w", err)
}
t, err := Transform(
v1alpha1.ApplicationSetGenerator{
List: appSetBaseGenerator.List,
Clusters: appSetBaseGenerator.Clusters,
Git: appSetBaseGenerator.Git,
SCMProvider: appSetBaseGenerator.SCMProvider,
ClusterDecisionResource: appSetBaseGenerator.ClusterDecisionResource,
PullRequest: appSetBaseGenerator.PullRequest,
Plugin: appSetBaseGenerator.Plugin,
Matrix: matrixGen,
Merge: mergeGen,
Selector: appSetBaseGenerator.Selector,
},
m.supportedGenerators,
v1alpha1.ApplicationSetTemplate{},
appSet,
params,
client)
if err != nil {
return nil, fmt.Errorf("child generator returned an error on parameter generation: %w", err)
}
if len(t) == 0 {
return nil, errors.New("child generator generated no parameters")
}
if len(t) > 1 {
return nil, ErrMoreThenOneInnerGenerators
}
return t[0].Params, nil
}
const maxDuration time.Duration = 1<<63 - 1
func (m *MatrixGenerator) GetRequeueAfter(appSetGenerator *v1alpha1.ApplicationSetGenerator) time.Duration {
res := maxDuration
var found bool
for _, r := range appSetGenerator.Matrix.Generators {
matrixGen, _ := getMatrixGenerator(r)
mergeGen, _ := getMergeGenerator(r)
base := &v1alpha1.ApplicationSetGenerator{
List: r.List,
Clusters: r.Clusters,
Git: r.Git,
PullRequest: r.PullRequest,
Plugin: r.Plugin,
SCMProvider: r.SCMProvider,
ClusterDecisionResource: r.ClusterDecisionResource,
Matrix: matrixGen,
Merge: mergeGen,
}
generators := GetRelevantGenerators(base, m.supportedGenerators)
for _, g := range generators {
temp := g.GetRequeueAfter(base)
if temp < res && temp != NoRequeueAfter {
found = true
res = temp
}
}
}
if found {
return res
}
return NoRequeueAfter
}
func getMatrixGenerator(r v1alpha1.ApplicationSetNestedGenerator) (*v1alpha1.MatrixGenerator, error) {
if r.Matrix == nil {
return nil, nil
}
matrix, err := v1alpha1.ToNestedMatrixGenerator(r.Matrix)
if err != nil {
return nil, err
}
return matrix.ToMatrixGenerator(), nil
}
func (m *MatrixGenerator) GetTemplate(appSetGenerator *v1alpha1.ApplicationSetGenerator) *v1alpha1.ApplicationSetTemplate {
return &appSetGenerator.Matrix.Template
}