Skip to content

Commit

Permalink
fix(appset): Apply nested matrix and merge generator's generator sele…
Browse files Browse the repository at this point in the history
…ctors (argoproj#11984)

Signed-off-by: Elias Rieb <e.rieb@posteo.de>
Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
Co-authored-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
  • Loading branch information
2 people authored and tesla59 committed Dec 16, 2023
1 parent 804a17f commit 9e80c1c
Show file tree
Hide file tree
Showing 17 changed files with 1,300 additions and 649 deletions.
13 changes: 13 additions & 0 deletions applicationset/generators/generator_spec_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,16 @@ func InterpolateGenerator(requestedGenerator *argoprojiov1alpha1.ApplicationSetG

return *interpolatedGenerator, nil
}

// Fixes https://github.com/argoproj/argo-cd/issues/11982 while ensuring backwards compatibility.
// This is only a short-term solution and should be removed in a future major version.
func dropDisabledNestedSelectors(generators []argoprojiov1alpha1.ApplicationSetNestedGenerator) bool {
var foundSelector bool
for i := range generators {
if generators[i].Selector != nil {
foundSelector = true
generators[i].Selector = nil
}
}
return foundSelector
}
14 changes: 14 additions & 0 deletions applicationset/generators/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/argoproj/argo-cd/v2/applicationset/utils"
argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"

log "github.com/sirupsen/logrus"
)

var _ Generator = (*MatrixGenerator)(nil)
Expand Down Expand Up @@ -84,10 +86,22 @@ func (m *MatrixGenerator) getParams(appSetBaseGenerator argoprojiov1alpha1.Appli
if err != nil {
return nil, err
}
if matrixGen != nil && !appSet.Spec.ApplyNestedSelectors {
foundSelector := dropDisabledNestedSelectors(matrixGen.Generators)
if foundSelector {
log.Warnf("AppSet '%v' defines selector on nested matrix generator's generator without enabling them via 'spec.applyNestedSelectors', ignoring nested selectors", appSet.Name)
}
}
mergeGen, err := getMergeGenerator(appSetBaseGenerator)
if err != nil {
return nil, err
}
if mergeGen != nil && !appSet.Spec.ApplyNestedSelectors {
foundSelector := dropDisabledNestedSelectors(mergeGen.Generators)
if foundSelector {
log.Warnf("AppSet '%v' defines selector on nested merge generator's generator without enabling them via 'spec.applyNestedSelectors', ignoring nested selectors", appSet.Name)
}
}

t, err := Transform(
argoprojiov1alpha1.ApplicationSetGenerator{
Expand Down
14 changes: 14 additions & 0 deletions applicationset/generators/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/argoproj/argo-cd/v2/applicationset/utils"
argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"

log "github.com/sirupsen/logrus"
)

var _ Generator = (*MergeGenerator)(nil)
Expand Down Expand Up @@ -141,10 +143,22 @@ func (m *MergeGenerator) getParams(appSetBaseGenerator argoprojiov1alpha1.Applic
if err != nil {
return nil, err
}
if matrixGen != nil && !appSet.Spec.ApplyNestedSelectors {
foundSelector := dropDisabledNestedSelectors(matrixGen.Generators)
if foundSelector {
log.Warnf("AppSet '%v' defines selector on nested matrix generator's generator without enabling them via 'spec.applyNestedSelectors', ignoring nested selector", appSet.Name)
}
}
mergeGen, err := getMergeGenerator(appSetBaseGenerator)
if err != nil {
return nil, err
}
if mergeGen != nil && !appSet.Spec.ApplyNestedSelectors {
foundSelector := dropDisabledNestedSelectors(mergeGen.Generators)
if foundSelector {
log.Warnf("AppSet '%v' defines selector on nested merge generator's generator without enabling them via 'spec.applyNestedSelectors', ignoring nested selector", appSet.Name)
}
}

t, err := Transform(
argoprojiov1alpha1.ApplicationSetGenerator{
Expand Down
4 changes: 4 additions & 0 deletions assets/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5943,6 +5943,10 @@
"description": "ApplicationSetSpec represents a class of application set state.",
"type": "object",
"properties": {
"applyNestedSelectors": {
"type": "boolean",
"title": "ApplyNestedSelectors enables selectors defined within the generators of two level-nested matrix or merge generators"
},
"generators": {
"type": "array",
"items": {
Expand Down
11 changes: 11 additions & 0 deletions docs/operator-manual/applicationset/Generators-Matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,14 @@ For example, the below example would be invalid (cluster-generator must come aft
revision: HEAD
files:
- path: "examples/git-generator-files-discovery/cluster-config/engineering/{{name}}**/config.json" # {{name}} is produced by cluster generator

1. When using a Matrix generator nested inside another Matrix or Merge generator, [Post Selectors](../../user-guide/application-set.md#post-selector-all-generators) for this nested generator's generators will only be applied when enabled via `spec.applyNestedSelectors`.

- matrix:
generators:
- matrix:
generators:
- list
elements:
- # (...)
selector: { } # Only applied when applyNestedSelectors is true
11 changes: 11 additions & 0 deletions docs/operator-manual/applicationset/Generators-Merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,14 @@ Assuming a cluster named `germany01` with the label `metadata.labels.location=Ge
- merge:
mergeKeys:
- values.merge

1. When using a Merge generator nested inside another Matrix or Merge generator, [Post Selectors](../../user-guide/application-set.md#post-selector-all-generators) for this nested generator's generators will only be applied when enabled via `spec.applyNestedSelectors`.

- merge:
generators:
- merge:
generators:
- list
elements:
- # (...)
selector: { } # Only applied when applyNestedSelectors is true
2 changes: 2 additions & 0 deletions manifests/core-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4393,6 +4393,8 @@ spec:
type: object
spec:
properties:
applyNestedSelectors:
type: boolean
generators:
items:
properties:
Expand Down
2 changes: 2 additions & 0 deletions manifests/crds/applicationset-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ spec:
type: object
spec:
properties:
applyNestedSelectors:
type: boolean
generators:
items:
properties:
Expand Down
2 changes: 2 additions & 0 deletions manifests/ha/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4393,6 +4393,8 @@ spec:
type: object
spec:
properties:
applyNestedSelectors:
type: boolean
generators:
items:
properties:
Expand Down
2 changes: 2 additions & 0 deletions manifests/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4393,6 +4393,8 @@ spec:
type: object
spec:
properties:
applyNestedSelectors:
type: boolean
generators:
items:
properties:
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/application/v1alpha1/applicationset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type ApplicationSetSpec struct {
Strategy *ApplicationSetStrategy `json:"strategy,omitempty" protobuf:"bytes,5,opt,name=strategy"`
PreservedFields *ApplicationPreservedFields `json:"preservedFields,omitempty" protobuf:"bytes,6,opt,name=preservedFields"`
GoTemplateOptions []string `json:"goTemplateOptions,omitempty" protobuf:"bytes,7,opt,name=goTemplateOptions"`
// ApplyNestedSelectors enables selectors defined within the generators of two level-nested matrix or merge generators
ApplyNestedSelectors bool `json:"applyNestedSelectors,omitempty" protobuf:"bytes,8,name=applyNestedSelectors"`
}

type ApplicationPreservedFields struct {
Expand Down Expand Up @@ -192,6 +194,9 @@ type ApplicationSetTerminalGenerator struct {
ClusterDecisionResource *DuckTypeGenerator `json:"clusterDecisionResource,omitempty" protobuf:"bytes,5,name=clusterDecisionResource"`
PullRequest *PullRequestGenerator `json:"pullRequest,omitempty" protobuf:"bytes,6,name=pullRequest"`
Plugin *PluginGenerator `json:"plugin,omitempty" protobuf:"bytes,7,name=pullRequest"`

// Selector allows to post-filter all generator.
Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,8,name=selector"`
}

type ApplicationSetTerminalGenerators []ApplicationSetTerminalGenerator
Expand All @@ -210,6 +215,7 @@ func (g ApplicationSetTerminalGenerators) toApplicationSetNestedGenerators() []A
ClusterDecisionResource: terminalGenerator.ClusterDecisionResource,
PullRequest: terminalGenerator.PullRequest,
Plugin: terminalGenerator.Plugin,
Selector: terminalGenerator.Selector,
}
}
return nestedGenerators
Expand Down
Loading

0 comments on commit 9e80c1c

Please sign in to comment.