Skip to content

Commit 858ed1c

Browse files
committed
feat: do not create branch planner resources if there are no terraform changes
Branch Planner creates new resources if there is a new Pull Request. It does this even if the Pull Request has no changes at all to the path defined in `.spec.path` on the original Terraform object. Listing pull requests doesn't contain information about the changes, an extra API call is required to get this information. As it uses an extra API call, I added a new field to Terraform that can be the place to configure other options like a reference to a secret. Now, it contains only one field to enable path scoping for Pull Requests. Closes flux-iac#803 References: * flux-iac#803 Signed-off-by: Balazs Nadasdi <[email protected]>
1 parent 7a638d5 commit 858ed1c

File tree

12 files changed

+356
-4
lines changed

12 files changed

+356
-4
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
5252

5353
.PHONY: generate
5454
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
55+
go generate ./...
5556
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
5657
cd api; $(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..."
5758

api/v1alpha2/terraform_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,18 @@ type TerraformSpec struct {
256256
// and allow interactive shell in case of emergency.
257257
// +optional
258258
BreakTheGlass bool `json:"breakTheGlass,omitempty"`
259+
260+
// BarnchPlanner configuration.
261+
// +optional
262+
BranchPlanner *BranchPlanner `json:"branchPlanner,omitempty"`
263+
}
264+
265+
type BranchPlanner struct {
266+
// EnablePathScope specifies if the Branch Planner should or shouldn't check
267+
// if a Pull Request has changes under `.spec.path`. If enabled extra
268+
// resources will be created only if there are any changes in terraform files.
269+
// +optional
270+
EnablePathScope bool `json:"enablePathScope"`
259271
}
260272

261273
type CloudSpec struct {

api/v1alpha2/zz_generated.deepcopy.go

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

config/crd/bases/infra.contrib.fluxcd.io_terraforms.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5062,6 +5062,16 @@ spec:
50625062
- name
50635063
type: object
50645064
type: array
5065+
branchPlanner:
5066+
description: BarnchPlanner configuration.
5067+
properties:
5068+
enablePathScope:
5069+
description: EnablePathScope specifies if the Branch Planner should
5070+
or shouldn't check if a Pull Request has changes under `.spec.path`.
5071+
If enabled extra resources will be created only if there are
5072+
any changes in terraform files.
5073+
type: boolean
5074+
type: object
50655075
breakTheGlass:
50665076
description: BreakTheGlass specifies if the reconciliation should
50675077
stop and allow interactive shell in case of emergency.

docs/References/terraform.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,40 @@ transient error will still result in a reconciliation failure.</p>
165165
</table>
166166
</div>
167167
</div>
168+
<h3 id="infra.contrib.fluxcd.io/v1alpha2.BranchPlanner">BranchPlanner
169+
</h3>
170+
<p>
171+
(<em>Appears on:</em>
172+
<a href="#infra.contrib.fluxcd.io/v1alpha2.TerraformSpec">TerraformSpec</a>)
173+
</p>
174+
<div class="md-typeset__scrollwrap">
175+
<div class="md-typeset__table">
176+
<table>
177+
<thead>
178+
<tr>
179+
<th>Field</th>
180+
<th>Description</th>
181+
</tr>
182+
</thead>
183+
<tbody>
184+
<tr>
185+
<td>
186+
<code>enablePathScope</code><br>
187+
<em>
188+
bool
189+
</em>
190+
</td>
191+
<td>
192+
<em>(Optional)</em>
193+
<p>EnablePathScope specifies if the Branch Planner should or shouldn&rsquo;t check
194+
if a Pull Request has changes under <code>.spec.path</code>. If enabled extra
195+
resources will be created only if there are any changes in terraform files.</p>
196+
</td>
197+
</tr>
198+
</tbody>
199+
</table>
200+
</div>
201+
</div>
168202
<h3 id="infra.contrib.fluxcd.io/v1alpha2.CloudSpec">CloudSpec
169203
</h3>
170204
<p>
@@ -1712,6 +1746,19 @@ bool
17121746
and allow interactive shell in case of emergency.</p>
17131747
</td>
17141748
</tr>
1749+
<tr>
1750+
<td>
1751+
<code>branchPlanner</code><br>
1752+
<em>
1753+
<a href="#infra.contrib.fluxcd.io/v1alpha2.BranchPlanner">
1754+
BranchPlanner
1755+
</a>
1756+
</em>
1757+
</td>
1758+
<td>
1759+
<p>BarnchPlanner configuration.</p>
1760+
</td>
1761+
</tr>
17151762
</table>
17161763
</td>
17171764
</tr>
@@ -2234,6 +2281,19 @@ bool
22342281
and allow interactive shell in case of emergency.</p>
22352282
</td>
22362283
</tr>
2284+
<tr>
2285+
<td>
2286+
<code>branchPlanner</code><br>
2287+
<em>
2288+
<a href="#infra.contrib.fluxcd.io/v1alpha2.BranchPlanner">
2289+
BranchPlanner
2290+
</a>
2291+
</em>
2292+
</td>
2293+
<td>
2294+
<p>BarnchPlanner configuration.</p>
2295+
</td>
2296+
</tr>
22372297
</tbody>
22382298
</table>
22392299
</div>

internal/git/provider/change.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package provider
2+
3+
type Change struct {
4+
Path string
5+
PreviousPath string
6+
Patch string
7+
Sha string
8+
Additions int
9+
Deletions int
10+
Changes int
11+
Added bool
12+
Renamed bool
13+
Deleted bool
14+
}

internal/git/provider/github_provider.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ type GitHubProvider struct {
2121
client *scm.Client
2222
}
2323

24+
func (p *GitHubProvider) ListPullRequestChanges(ctx context.Context, pr PullRequest) ([]Change, error) {
25+
changes := []Change{}
26+
27+
changeList, _, err := p.client.PullRequests.ListChanges(ctx, pr.Repository.String(), pr.Number, &scm.ListOptions{})
28+
if err != nil {
29+
return changes, fmt.Errorf("unable to list pull request changes: %w", err)
30+
}
31+
32+
for _, change := range changeList {
33+
changes = append(changes, Change{
34+
Path: change.Path,
35+
PreviousPath: change.PreviousPath,
36+
Patch: change.Patch,
37+
Sha: change.Sha,
38+
Additions: change.Additions,
39+
Deletions: change.Deletions,
40+
Changes: change.Changes,
41+
Added: change.Added,
42+
Renamed: change.Renamed,
43+
Deleted: change.Deleted,
44+
})
45+
}
46+
47+
return changes, nil
48+
}
49+
2450
func (p *GitHubProvider) ListPullRequests(ctx context.Context, repo Repository) ([]PullRequest, error) {
2551
prList, _, err := p.client.PullRequests.List(ctx, repo.String(), &scm.PullRequestListOptions{})
2652
if err != nil {

internal/git/provider/provider.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package provider
22

33
import (
44
"fmt"
5+
"time"
6+
57
"github.com/go-logr/logr"
68
giturl "github.com/kubescape/go-git-url"
79
giturlapis "github.com/kubescape/go-git-url/apis"
810
"golang.org/x/net/context"
9-
"time"
1011
)
1112

1213
type ProviderType string
@@ -25,6 +26,7 @@ type Provider interface {
2526
AddCommentToPullRequest(ctx context.Context, repo PullRequest, body []byte) (*Comment, error)
2627
GetLastComments(ctx context.Context, pr PullRequest, since time.Time) ([]*Comment, error)
2728
UpdateCommentOfPullRequest(ctx context.Context, pr PullRequest, commentID int, body []byte) error
29+
ListPullRequestChanges(ctx context.Context, pr PullRequest) ([]Change, error)
2830

2931
SetLogger(logr.Logger) error
3032
SetToken(tokenType, token string) error

internal/git/provider/providerfakes/fake_provider.go

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

internal/server/polling/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func expectToSucceed(g gomega.Gomega, arg interface{}) {
7070
g.ExpectWithOffset(1, arg).To(gomega.Succeed())
7171
}
7272

73-
func expectToEqual(g gomega.Gomega, arg interface{}, expect interface{}) {
74-
g.ExpectWithOffset(1, arg).To(gomega.Equal(expect))
73+
func expectToEqual(g gomega.Gomega, arg interface{}, expect interface{}, desc ...interface{}) {
74+
g.ExpectWithOffset(1, expect).To(gomega.Equal(arg), desc...)
7575
}
7676

7777
// Minimal test to check the scaffolding works.

0 commit comments

Comments
 (0)