Skip to content

Commit a3b0bcd

Browse files
Support globs
1 parent b9cdbd0 commit a3b0bcd

File tree

9 files changed

+196
-105
lines changed

9 files changed

+196
-105
lines changed

data/data.go

Lines changed: 115 additions & 77 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/dgrijalva/jwt-go v3.2.0+incompatible
1313
github.com/fatih/color v1.7.0
1414
github.com/ghodss/yaml v1.0.0
15+
github.com/gobwas/glob v0.2.3
1516
github.com/gogo/protobuf v1.3.1
1617
github.com/google/go-github/v30 v30.0.0
1718
github.com/gorilla/mux v1.7.4

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh
145145
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
146146
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
147147
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
148+
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
148149
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
149150
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
150151
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=

pkg/events/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,14 @@ func (whh *WebhookHandler) handleApplyResultCommentEvent(
319319
func (whh *WebhookHandler) getClusterClients(
320320
ctx context.Context,
321321
client pullreq.PullRequestClient,
322-
clusterIDs []string,
322+
selectedClusterGlobStrs []string,
323323
flags map[string]string,
324324
) ([]cluster.ClusterClient, error) {
325325
clusterClients := []cluster.ClusterClient{}
326326

327327
coveredClusters, err := client.GetCoveredClusters(
328328
whh.settings.Env,
329-
clusterIDs,
329+
selectedClusterGlobStrs,
330330
flags["subpath"],
331331
)
332332
if err != nil {

pkg/pullreq/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type PullRequestClient interface {
1515
// GetCoveredClusters gets the configs for all clusters "covered" by a pull request.
1616
GetCoveredClusters(
1717
env string,
18-
selectedClusterIDs []string,
18+
selectedClusterGlobStrs []string,
1919
subpathOverride string,
2020
) ([]*config.ClusterConfig, error)
2121

pkg/pullreq/diffs.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sort"
88
"strings"
99

10+
"github.com/gobwas/glob"
1011
"github.com/google/go-github/v30/github"
1112
"github.com/segmentio/kubeapply/pkg/config"
1213
log "github.com/sirupsen/logrus"
@@ -25,21 +26,26 @@ import (
2526
// subpaths.
2627
//
2728
// There are a few overrides that adjust this behavior:
28-
// 1. selectedClusterIDs: If set, then clusters in this slice are never dropped, even if they
29+
// 1. selectedClusterGlobStrs: If set, then clusters in this slice are never dropped, even if they
2930
// don't have have any diffs in them.
3031
// 2. subpathOverride: If set, then this is used for the cluster subpaths instead of the procedure
3132
// in step 6 above.
3233
func GetCoveredClusters(
3334
repoRoot string,
3435
diffs []*github.CommitFile,
3536
env string,
36-
selectedClusterIDs []string,
37+
selectedClusterGlobStrs []string,
3738
subpathOverride string,
3839
multiSubpaths bool,
3940
) ([]*config.ClusterConfig, error) {
40-
selectedClusterIDsMap := map[string]struct{}{}
41-
for _, selectedClusterID := range selectedClusterIDs {
42-
selectedClusterIDsMap[selectedClusterID] = struct{}{}
41+
selectedClusterGlobs := []glob.Glob{}
42+
43+
for _, globStr := range selectedClusterGlobStrs {
44+
globObj, err := glob.Compile(globStr)
45+
if err != nil {
46+
return nil, err
47+
}
48+
selectedClusterGlobs = append(selectedClusterGlobs, globObj)
4349
}
4450

4551
changedClusterPaths := map[string][]string{}
@@ -80,8 +86,17 @@ func GetCoveredClusters(
8086

8187
log.Infof("Found cluster config: %s", path)
8288

83-
if len(selectedClusterIDsMap) > 0 {
84-
if _, ok := selectedClusterIDsMap[configObj.DescriptiveName()]; !ok {
89+
if len(selectedClusterGlobs) > 0 {
90+
var matches bool
91+
92+
for _, globObj := range selectedClusterGlobs {
93+
if globObj.Match(configObj.DescriptiveName()) {
94+
matches = true
95+
break
96+
}
97+
}
98+
99+
if !matches {
85100
log.Infof(
86101
"Ignoring cluster %s because selectedClusters is set and cluster is not in set",
87102
configObj.DescriptiveName(),
@@ -118,7 +133,7 @@ func GetCoveredClusters(
118133
return err
119134
}
120135

121-
if len(selectedClusterIDsMap) > 0 {
136+
if len(selectedClusterGlobs) > 0 {
122137
changedClusterPaths[relPath] = []string{}
123138
}
124139

pkg/pullreq/diffs_test.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111

1212
func TestGetCoveredClusters(t *testing.T) {
1313
type clusterTestCase struct {
14-
diffs []*github.CommitFile
15-
selectedClusterIDs []string
16-
subpathOverride string
17-
multiSubpaths bool
18-
expectedClustersIDs []string
19-
expectedSubpaths []string
14+
diffs []*github.CommitFile
15+
selectedClusterGlobStrs []string
16+
subpathOverride string
17+
multiSubpaths bool
18+
expectedClustersIDs []string
19+
expectedSubpaths []string
2020
}
2121

2222
testCases := []clusterTestCase{
@@ -104,7 +104,7 @@ func TestGetCoveredClusters(t *testing.T) {
104104
Filename: aws.String("clusters/clustertype/expanded/cluster2/file2.yaml"),
105105
},
106106
},
107-
selectedClusterIDs: []string{
107+
selectedClusterGlobStrs: []string{
108108
"stage:us-west-2:cluster1",
109109
"production:us-west-2:cluster2",
110110
"stage:us-west-2:cluster3",
@@ -130,7 +130,29 @@ func TestGetCoveredClusters(t *testing.T) {
130130
Filename: aws.String("clusters/clustertype/expanded/cluster2/file2.yaml"),
131131
},
132132
},
133-
selectedClusterIDs: []string{
133+
selectedClusterGlobStrs: []string{
134+
"stage:*1",
135+
},
136+
expectedClustersIDs: []string{
137+
"stage:us-west-2:cluster1",
138+
},
139+
expectedSubpaths: []string{
140+
".",
141+
},
142+
},
143+
{
144+
diffs: []*github.CommitFile{
145+
{
146+
Filename: aws.String("clusters/clustertype/expanded/cluster1/file1.yaml"),
147+
},
148+
{
149+
Filename: aws.String("clusters/clustertype/expanded/cluster1/subdir1/file3.yaml"),
150+
},
151+
{
152+
Filename: aws.String("clusters/clustertype/expanded/cluster2/file2.yaml"),
153+
},
154+
},
155+
selectedClusterGlobStrs: []string{
134156
"stage:us-west-2:cluster3",
135157
},
136158
expectedClustersIDs: []string{
@@ -148,7 +170,7 @@ func TestGetCoveredClusters(t *testing.T) {
148170
"testdata/repo",
149171
testCase.diffs,
150172
"stage",
151-
testCase.selectedClusterIDs,
173+
testCase.selectedClusterGlobStrs,
152174
testCase.subpathOverride,
153175
testCase.multiSubpaths,
154176
)

pkg/pullreq/fake_client.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/gobwas/glob"
78
"github.com/segmentio/kubeapply/pkg/config"
89
)
910

@@ -31,19 +32,32 @@ func (prc *FakePullRequestClient) Init(ctx context.Context) error {
3132
// this pull request.
3233
func (prc *FakePullRequestClient) GetCoveredClusters(
3334
env string,
34-
selectedClusterIDs []string,
35+
selectedClusterGlobStrs []string,
3536
subpathOverride string,
3637
) ([]*config.ClusterConfig, error) {
37-
selectedClusterIDsMap := map[string]struct{}{}
38-
for _, selectedClusterID := range selectedClusterIDs {
39-
selectedClusterIDsMap[selectedClusterID] = struct{}{}
38+
globObjs := []glob.Glob{}
39+
40+
for _, globStr := range selectedClusterGlobStrs {
41+
globObj, err := glob.Compile(globStr)
42+
if err != nil {
43+
return nil, err
44+
}
45+
globObjs = append(globObjs, globObj)
4046
}
4147

4248
coveredClusters := []*config.ClusterConfig{}
4349

4450
for _, clusterConfig := range prc.ClusterConfigs {
45-
if len(selectedClusterIDsMap) > 0 {
46-
if _, ok := selectedClusterIDsMap[clusterConfig.DescriptiveName()]; !ok {
51+
if len(globObjs) > 0 {
52+
var matches bool
53+
for _, globObj := range globObjs {
54+
if globObj.Match(clusterConfig.DescriptiveName()) {
55+
matches = true
56+
break
57+
}
58+
}
59+
60+
if !matches {
4761
continue
4862
}
4963
}

pkg/pullreq/github_client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,14 @@ func (prc *GHPullRequestClient) Init(ctx context.Context) error {
232232
// this pull request.
233233
func (prc *GHPullRequestClient) GetCoveredClusters(
234234
env string,
235-
selectedClusterIDs []string,
235+
selectedClusterGlobStrs []string,
236236
subpathOverride string,
237237
) ([]*config.ClusterConfig, error) {
238238
return GetCoveredClusters(
239239
prc.clonePath,
240240
prc.files,
241241
env,
242-
selectedClusterIDs,
242+
selectedClusterGlobStrs,
243243
subpathOverride,
244244
true,
245245
)

0 commit comments

Comments
 (0)