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: Add fileSelector to the GitFileGenerator to support label-based file match(https://github.com/argoproj/argo-cd/issues/17673)(Alpha) #21281

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
39 changes: 37 additions & 2 deletions applicationset/generators/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/jeremywohl/flatten"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -130,8 +132,41 @@ func (g *GitGenerator) generateParamsForGitFiles(appSetGenerator *argoprojiov1al
if err != nil {
return nil, err
}
for filePath, content := range files {
allFiles[filePath] = content
if requestedPath.FileSelector != nil {
for filePath, content := range files {
objects := map[string]interface{}{}
err := yaml.Unmarshal(content, &objects)
if err != nil {
log.Printf("unable to parse the file: %v", err)
continue
}

flat, err := flatten.Flatten(objects, "", flatten.DotStyle)
if err != nil {
log.Printf("error flattening the object: %v", err)
continue
}

labelSet := make(labels.Set)
for k, v := range flat {
if strVal, ok := v.(string); ok {
labelSet[k] = strVal
}
}

selector, err := metav1.LabelSelectorAsSelector(requestedPath.FileSelector)
if err != nil {
return nil, fmt.Errorf("error parsing the label selector: %w", err)
}

if selector.Matches(labelSet) {
allFiles[filePath] = content
}
}
} else {
for filePath, content := range files {
allFiles[filePath] = content
}
}
}

Expand Down
295 changes: 295 additions & 0 deletions applicationset/generators/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,301 @@ cluster:
},
expectedError: nil,
},
{
name: "create params from git files using matchLabels",
files: []argoprojiov1alpha1.GitFileGeneratorItem{{
Path: "**/config.json",
FileSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"cluster.name": "production",
"key1": "val1",
},
},
}},
repoFileContents: map[string][]byte{
"cluster-config/production/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "production",
"address": "https://kubernetes.default.svc"
},
"key1": "val1",
"key2": {
"key2_1": "val2_1",
"key2_2": {
"key2_2_1": "val2_2_1"
}
},
"key3": 123
}`),
"cluster-config/staging/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "staging",
"address": "https://kubernetes.default.svc"
}
}`),
},
repoPathsError: nil,
expected: []map[string]interface{}{
{
"cluster.owner": "[email protected]",
"cluster.name": "production",
"cluster.address": "https://kubernetes.default.svc",
"key1": "val1",
"key2.key2_1": "val2_1",
"key2.key2_2.key2_2_1": "val2_2_1",
"key3": "123",
"path": "cluster-config/production",
"path.basename": "production",
"path[0]": "cluster-config",
"path[1]": "production",
"path.basenameNormalized": "production",
"path.filename": "config.json",
"path.filenameNormalized": "config.json",
},
},
expectedError: nil,
},
{
name: "create params from git files using matchExpressions",
files: []argoprojiov1alpha1.GitFileGeneratorItem{{
Path: "**/config.json",
FileSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{Key: "cluster.name", Operator: metav1.LabelSelectorOpIn, Values: []string{"production", "test"}},
},
},
}},
repoFileContents: map[string][]byte{
"cluster-config/production/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "production",
"address": "https://kubernetes.default.svc"
},
"key1": "val1",
"key2": {
"key2_1": "val2_1",
"key2_2": {
"key2_2_1": "val2_2_1"
}
},
"key3": 123
}`),
"cluster-config/staging/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "staging",
"address": "https://kubernetes.default.svc"
}
}`),
"cluster-config/test/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "test",
"address": "https://kubernetes.default.svc"
},
"key1": "val1",
"key2": {
"key2_1": "val2_1",
"key2_2": {
"key2_2_1": "val2_2_1"
}
},
"key3": 123,
"key4": "val4",
}`),
},
repoPathsError: nil,
expected: []map[string]interface{}{
{
"cluster.owner": "[email protected]",
"cluster.name": "production",
"cluster.address": "https://kubernetes.default.svc",
"key1": "val1",
"key2.key2_1": "val2_1",
"key2.key2_2.key2_2_1": "val2_2_1",
"key3": "123",
"path": "cluster-config/production",
"path.basename": "production",
"path[0]": "cluster-config",
"path[1]": "production",
"path.basenameNormalized": "production",
"path.filename": "config.json",
"path.filenameNormalized": "config.json",
},
{
"cluster.owner": "[email protected]",
"cluster.name": "test",
"cluster.address": "https://kubernetes.default.svc",
"key1": "val1",
"key2.key2_1": "val2_1",
"key2.key2_2.key2_2_1": "val2_2_1",
"key3": "123",
"key4": "val4",
"path": "cluster-config/test",
"path.basename": "test",
"path[0]": "cluster-config",
"path[1]": "test",
"path.basenameNormalized": "test",
"path.filename": "config.json",
"path.filenameNormalized": "config.json",
},
},
expectedError: nil,
},
{
name: "create params from git files matchLabels and matchExpressions",
files: []argoprojiov1alpha1.GitFileGeneratorItem{{
Path: "**/config.json",
FileSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{Key: "cluster.name", Operator: metav1.LabelSelectorOpIn, Values: []string{"production", "test"}},
},
MatchLabels: map[string]string{
"key4": "val4",
},
},
}},
repoFileContents: map[string][]byte{
"cluster-config/production/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "production",
"address": "https://kubernetes.default.svc"
},
"key1": "val1",
"key2": {
"key2_1": "val2_1",
"key2_2": {
"key2_2_1": "val2_2_1"
}
},
"key3": 123
}`),
"cluster-config/staging/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "staging",
"address": "https://kubernetes.default.svc"
}
}`),
"cluster-config/test/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "test",
"address": "https://kubernetes.default.svc"
},
"key1": "val1",
"key2": {
"key2_1": "val2_1",
"key2_2": {
"key2_2_1": "val2_2_1"
}
},
"key3": 123,
"key4": "val4",
}`),
},
repoPathsError: nil,
expected: []map[string]interface{}{
{
"cluster.owner": "[email protected]",
"cluster.name": "test",
"cluster.address": "https://kubernetes.default.svc",
"key1": "val1",
"key2.key2_1": "val2_1",
"key2.key2_2.key2_2_1": "val2_2_1",
"key3": "123",
"key4": "val4",
"path": "cluster-config/test",
"path.basename": "test",
"path[0]": "cluster-config",
"path[1]": "test",
"path.basenameNormalized": "test",
"path.filename": "config.json",
"path.filenameNormalized": "config.json",
},
},
expectedError: nil,
},
{
name: "invalid value for matchLabels",
files: []argoprojiov1alpha1.GitFileGeneratorItem{{
Path: "**/config.json",
FileSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"cluster.owner": "[email protected]",
},
},
}},
repoFileContents: map[string][]byte{
"cluster-config/production/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "production",
"address": "https://kubernetes.default.svc"
},
"key1": "val1",
"key2": {
"key2_1": "val2_1",
"key2_2": {
"key2_2_1": "val2_2_1"
}
},
"key3": 123
}`),
"cluster-config/staging/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "staging",
"address": "https://kubernetes.default.svc"
}
}`),
},
repoPathsError: nil,
expected: []map[string]interface{}{},
expectedError: fmt.Errorf("error generating params from git: error parsing the label selector: values[0][cluster.owner]: Invalid value: \"[email protected]\": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue', or 'my_value', or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')"),
},
{
name: "test no match found",
files: []argoprojiov1alpha1.GitFileGeneratorItem{{
Path: "**/config.json",
FileSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"cluster.owner": "test-owner",
},
},
}},
repoFileContents: map[string][]byte{
"cluster-config/production/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "production",
"address": "https://kubernetes.default.svc"
},
"key1": "val1",
"key2": {
"key2_1": "val2_1",
"key2_2": {
"key2_2_1": "val2_2_1"
}
},
"key3": 123
}`),
"cluster-config/staging/config.json": []byte(`{
"cluster": {
"owner": "[email protected]",
"name": "staging",
"address": "https://kubernetes.default.svc"
}
}`),
},
repoPathsError: nil,
expected: []map[string]interface{}{},
expectedError: nil,
},
}

for _, testCase := range cases {
Expand Down
3 changes: 3 additions & 0 deletions assets/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading