Skip to content

Commit

Permalink
Merge pull request #839 from weaveworks/803-only-with-tf-changes
Browse files Browse the repository at this point in the history
feat: do not create branch planner resources if there are no terraform changes
  • Loading branch information
yitsushi authored Aug 10, 2023
2 parents a3d0160 + 5c963fc commit f32f82a
Show file tree
Hide file tree
Showing 21 changed files with 389 additions and 23 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust

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

Expand Down
12 changes: 12 additions & 0 deletions api/v1alpha2/terraform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ type TerraformSpec struct {
// and allow interactive shell in case of emergency.
// +optional
BreakTheGlass bool `json:"breakTheGlass,omitempty"`

// BranchPlanner configuration.
// +optional
BranchPlanner *BranchPlanner `json:"branchPlanner,omitempty"`
}

type BranchPlanner struct {
// EnablePathScope specifies if the Branch Planner should or shouldn't check
// if a Pull Request has changes under `.spec.path`. If enabled extra
// resources will be created only if there are any changes in terraform files.
// +optional
EnablePathScope bool `json:"enablePathScope"`
}

type CloudSpec struct {
Expand Down
20 changes: 20 additions & 0 deletions api/v1alpha2/zz_generated.deepcopy.go

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

4 changes: 2 additions & 2 deletions charts/tf-controller/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: tf-controller
description: The Helm chart for Weave GitOps Terraform Controller
type: application
version: 0.16.0-rc.2
appVersion: "v0.16.0-rc.2"
version: 0.16.0-rc.3
appVersion: "v0.16.0-rc.3"
4 changes: 2 additions & 2 deletions charts/tf-controller/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Weave GitOps Terraform Controller

![Version: 0.16.0-rc.2](https://img.shields.io/badge/Version-0.16.0--rc.2-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: v0.16.0-rc.2](https://img.shields.io/badge/AppVersion-v0.16.0--rc.2-informational?style=flat-square)
![Version: 0.16.0-rc.3](https://img.shields.io/badge/Version-0.16.0--rc.3-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: v0.16.0-rc.3](https://img.shields.io/badge/AppVersion-v0.16.0--rc.3-informational?style=flat-square)

The Helm chart for Weave GitOps Terraform Controller

Expand Down Expand Up @@ -72,7 +72,7 @@ __Note__: If you need to use the `imagePullSecrets` it would be best to set `ser
| rbac.create | bool | `true` | If `true`, create and use RBAC resources |
| replicaCount | int | `1` | Number of TF-Controller pods to deploy |
| resources | object | `{"limits":{"cpu":"1000m","memory":"1Gi"},"requests":{"cpu":"200m","memory":"64Mi"}}` | Resource limits and requests |
| runner | object | `{"creationTimeout":"5m0s","grpc":{"maxMessageSize":4},"image":{"repository":"ghcr.io/weaveworks/tf-runner","tag":"v0.16.0-rc.2"},"serviceAccount":{"allowedNamespaces":[],"annotations":{},"create":true,"name":""}}` | Runner-specific configurations |
| runner | object | `{"creationTimeout":"5m0s","grpc":{"maxMessageSize":4},"image":{"repository":"ghcr.io/weaveworks/tf-runner","tag":"v0.16.0-rc.3"},"serviceAccount":{"allowedNamespaces":[],"annotations":{},"create":true,"name":""}}` | Runner-specific configurations |
| runner.creationTimeout | string | `"5m0s"` | Timeout for runner-creation (Controller) |
| runner.grpc.maxMessageSize | int | `4` | Maximum GRPC message size (Controller) |
| runner.image.repository | string | `"ghcr.io/weaveworks/tf-runner"` | Runner image repository |
Expand Down
10 changes: 10 additions & 0 deletions charts/tf-controller/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5064,6 +5064,16 @@ spec:
- name
type: object
type: array
branchPlanner:
description: BarnchPlanner configuration.
properties:
enablePathScope:
description: EnablePathScope specifies if the Branch Planner should
or shouldn't check if a Pull Request has changes under `.spec.path`.
If enabled extra resources will be created only if there are
any changes in terraform files.
type: boolean
type: object
breakTheGlass:
description: BreakTheGlass specifies if the reconciliation should
stop and allow interactive shell in case of emergency.
Expand Down
4 changes: 2 additions & 2 deletions charts/tf-controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ image:
pullPolicy: IfNotPresent
# -- Overrides the image tag whose default is the chart appVersion.
# @default -- `.Chart.AppVersion`
tag: "v0.16.0-rc.2"
tag: "v0.16.0-rc.3"
# -- Controller image pull secret
imagePullSecrets: []
# -- Resource limits and requests
Expand Down Expand Up @@ -114,7 +114,7 @@ runner:
repository: ghcr.io/weaveworks/tf-runner
# -- Runner image tag
# @default -- `.Chart.AppVersion`
tag: "v0.16.0-rc.2"
tag: "v0.16.0-rc.3"
grpc:
# -- Maximum GRPC message size (Controller)
maxMessageSize: 4
Expand Down
2 changes: 1 addition & 1 deletion config/branch-planner/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ generatorOptions:
images:
- name: weaveworks/branch-planner
newName: ghcr.io/weaveworks/branch-planner
newTag: v0.16.0-rc.2
newTag: v0.16.0-rc.3
10 changes: 10 additions & 0 deletions config/crd/bases/infra.contrib.fluxcd.io_terraforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5062,6 +5062,16 @@ spec:
- name
type: object
type: array
branchPlanner:
description: BarnchPlanner configuration.
properties:
enablePathScope:
description: EnablePathScope specifies if the Branch Planner should
or shouldn't check if a Pull Request has changes under `.spec.path`.
If enabled extra resources will be created only if there are
any changes in terraform files.
type: boolean
type: object
breakTheGlass:
description: BreakTheGlass specifies if the reconciliation should
stop and allow interactive shell in case of emergency.
Expand Down
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ generatorOptions:
images:
- name: weaveworks/tf-controller
newName: ghcr.io/weaveworks/tf-controller
newTag: v0.16.0-rc.2
newTag: v0.16.0-rc.3
60 changes: 60 additions & 0 deletions docs/References/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,40 @@ transient error will still result in a reconciliation failure.</p>
</table>
</div>
</div>
<h3 id="infra.contrib.fluxcd.io/v1alpha2.BranchPlanner">BranchPlanner
</h3>
<p>
(<em>Appears on:</em>
<a href="#infra.contrib.fluxcd.io/v1alpha2.TerraformSpec">TerraformSpec</a>)
</p>
<div class="md-typeset__scrollwrap">
<div class="md-typeset__table">
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>enablePathScope</code><br>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>EnablePathScope specifies if the Branch Planner should or shouldn&rsquo;t check
if a Pull Request has changes under <code>.spec.path</code>. If enabled extra
resources will be created only if there are any changes in terraform files.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<h3 id="infra.contrib.fluxcd.io/v1alpha2.CloudSpec">CloudSpec
</h3>
<p>
Expand Down Expand Up @@ -1712,6 +1746,19 @@ bool
and allow interactive shell in case of emergency.</p>
</td>
</tr>
<tr>
<td>
<code>branchPlanner</code><br>
<em>
<a href="#infra.contrib.fluxcd.io/v1alpha2.BranchPlanner">
BranchPlanner
</a>
</em>
</td>
<td>
<p>BarnchPlanner configuration.</p>
</td>
</tr>
</table>
</td>
</tr>
Expand Down Expand Up @@ -2234,6 +2281,19 @@ bool
and allow interactive shell in case of emergency.</p>
</td>
</tr>
<tr>
<td>
<code>branchPlanner</code><br>
<em>
<a href="#infra.contrib.fluxcd.io/v1alpha2.BranchPlanner">
BranchPlanner
</a>
</em>
</td>
<td>
<p>BarnchPlanner configuration.</p>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down
8 changes: 4 additions & 4 deletions docs/branch_planner/rc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spec:
sourceRef:
kind: HelmRepository
name: tf-controller
version: '>=0.16.0-rc.2'
version: '>=0.16.0-rc.3'
interval: 1h0s
releaseName: tf-controller
targetNamespace: flux-system
Expand All @@ -46,13 +46,13 @@ spec:
caCertValidityDuration: 24h
certRotationCheckFrequency: 30m
image:
tag: v0.16.0-rc.2
tag: v0.16.0-rc.3
runner:
image:
tag: v0.16.0-rc.2
tag: v0.16.0-rc.3
grpc:
maxMessageSize: 30
branchPlanner:
enabled: true
image:
tag: v0.16.0-rc.2
tag: v0.16.0-rc.3
8 changes: 4 additions & 4 deletions docs/branch_planner/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spec:
sourceRef:
kind: HelmRepository
name: tf-controller
version: '>=0.16.0-rc.2'
version: '>=0.16.0-rc.3'
interval: 1h0s
releaseName: tf-controller
targetNamespace: flux-system
Expand All @@ -46,13 +46,13 @@ spec:
caCertValidityDuration: 24h
certRotationCheckFrequency: 30m
image:
tag: v0.16.0-rc.2
tag: v0.16.0-rc.3
runner:
image:
tag: v0.16.0-rc.2
tag: v0.16.0-rc.3
grpc:
maxMessageSize: 30
branchPlanner:
enabled: true
image:
tag: v0.16.0-rc.2
tag: v0.16.0-rc.3
6 changes: 3 additions & 3 deletions docs/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spec:
sourceRef:
kind: HelmRepository
name: tf-controller
version: '>=0.16.0-rc.2'
version: '>=0.16.0-rc.3'
interval: 1h0s
releaseName: tf-controller
targetNamespace: flux-system
Expand All @@ -46,9 +46,9 @@ spec:
caCertValidityDuration: 24h
certRotationCheckFrequency: 30m
image:
tag: v0.16.0-rc.2
tag: v0.16.0-rc.3
runner:
image:
tag: v0.16.0-rc.2
tag: v0.16.0-rc.3
grpc:
maxMessageSize: 30
14 changes: 14 additions & 0 deletions internal/git/provider/change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package provider

type Change struct {
Path string
PreviousPath string
Patch string
Sha string
Additions int
Deletions int
Changes int
Added bool
Renamed bool
Deleted bool
}
26 changes: 26 additions & 0 deletions internal/git/provider/github_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ type GitHubProvider struct {
client *scm.Client
}

func (p *GitHubProvider) ListPullRequestChanges(ctx context.Context, pr PullRequest) ([]Change, error) {
changes := []Change{}

changeList, _, err := p.client.PullRequests.ListChanges(ctx, pr.Repository.String(), pr.Number, &scm.ListOptions{})
if err != nil {
return changes, fmt.Errorf("unable to list pull request changes: %w", err)
}

for _, change := range changeList {
changes = append(changes, Change{
Path: change.Path,
PreviousPath: change.PreviousPath,
Patch: change.Patch,
Sha: change.Sha,
Additions: change.Additions,
Deletions: change.Deletions,
Changes: change.Changes,
Added: change.Added,
Renamed: change.Renamed,
Deleted: change.Deleted,
})
}

return changes, nil
}

func (p *GitHubProvider) ListPullRequests(ctx context.Context, repo Repository) ([]PullRequest, error) {
prList, _, err := p.client.PullRequests.List(ctx, repo.String(), &scm.PullRequestListOptions{})
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/git/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package provider

import (
"fmt"
"time"

"github.com/go-logr/logr"
giturl "github.com/kubescape/go-git-url"
giturlapis "github.com/kubescape/go-git-url/apis"
"golang.org/x/net/context"
"time"
)

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

SetLogger(logr.Logger) error
SetToken(tokenType, token string) error
Expand Down
Loading

0 comments on commit f32f82a

Please sign in to comment.