Skip to content

Commit

Permalink
feat(cluster-generator): support matchExpressions in selectors (argop…
Browse files Browse the repository at this point in the history
…roj#25)

Signed-off-by: Michael Goodness <michael.goodness@mlb.com>
  • Loading branch information
mgoodness committed Sep 3, 2020
1 parent 7f94c37 commit 7186089
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkg/generators/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
log "github.com/sirupsen/logrus"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

argoprojiov1alpha1 "github.com/argoproj-labs/applicationset/api/v1alpha1"
Expand Down Expand Up @@ -46,13 +47,14 @@ func (g *ClusterGenerator) GenerateApplications(

// List all Clusters:
clusterSecretList := &corev1.SecretList{}
secretLabels := map[string]string{
ArgoCDSecretTypeLabel: ArgoCDSecretTypeCluster,
}
for k, v := range appSetGenerator.Clusters.Selector.MatchLabels {
secretLabels[k] = v

selector := metav1.AddLabelToSelector(&appSetGenerator.Clusters.Selector, ArgoCDSecretTypeLabel, ArgoCDSecretTypeCluster)
secretSelector, err := metav1.LabelSelectorAsSelector(selector)
if err != nil {
return nil, err
}
if err := g.Client.List(context.Background(), clusterSecretList, client.MatchingLabels(secretLabels)); err != nil {

if err := g.Client.List(context.Background(), clusterSecretList, client.MatchingLabelsSelector{secretSelector}); err != nil {
return nil, err
}
log.Debug("clusters matching labels", "count", len(clusterSecretList.Items))
Expand Down
46 changes: 46 additions & 0 deletions pkg/generators/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func TestGenerateApplications(t *testing.T) {
Labels: map[string]string{
"argocd.argoproj.io/secret-type": "cluster",
"environment": "staging",
"org": "foo",
},
},
Data: map[string][]byte{
Expand All @@ -89,6 +90,7 @@ func TestGenerateApplications(t *testing.T) {
Labels: map[string]string{
"argocd.argoproj.io/secret-type": "cluster",
"environment": "production",
"org": "bar",
},
},
Data: map[string][]byte{
Expand Down Expand Up @@ -149,6 +151,50 @@ func TestGenerateApplications(t *testing.T) {
false,
nil,
},
{
applicationSetTemplate,
metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "environment",
Operator: "In",
Values: []string{
"production",
"staging",
},
},
},
},
[]argov1alpha1.Application{
*getRenderTemplate("app", "staging-01", "https://staging-01.example.com", "staging"),
*getRenderTemplate("app", "production-01", "https://production-01.example.com", "production"),
},
false,
nil,
},
{
applicationSetTemplate,
metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "environment",
Operator: "In",
Values: []string{
"production",
"staging",
},
},
},
MatchLabels: map[string]string{
"org": "foo",
},
},
[]argov1alpha1.Application{
*getRenderTemplate("app", "staging-01", "https://staging-01.example.com", "staging"),
},
false,
nil,
},
{
applicationSetTemplate,
metav1.LabelSelector{},
Expand Down

0 comments on commit 7186089

Please sign in to comment.