Skip to content

Commit

Permalink
feat(webhook): add additional paths trigger (#129)
Browse files Browse the repository at this point in the history
* feat(webhook): add additional paths trigger

* feat(webhook): update AdditionnalTriggerPaths

* feat(additionnalpaths): debug: add verbosity

* feat(additionnalpaths): add debug

* feat(common.go): remove debug los

* feat(rbac): update

* feat(additionnalpaths): enable on PR

* fix(webhook): pr event revision name

* feat(documentation): additionnal trigger paths

* test(webhook): add tests for event handling on layers and PRs

* fix(webhook): support relative parent paths

* refactor(pullrequest): make LayerFilesHaveChanged public

* chore(files): remove unused files

* test(webhook): add tests around pull request closed events

---------

Co-authored-by: Julien Jourdain <Julien Jourdain>
Co-authored-by: Luca CORRIERI <lucac@padok.fr>
Co-authored-by: Alan <alanl@padok.fr>
  • Loading branch information
3 people authored Jul 12, 2023
1 parent a613394 commit e7a3a59
Show file tree
Hide file tree
Showing 29 changed files with 771 additions and 104 deletions.
55 changes: 55 additions & 0 deletions docs/user-guide/additionnal-trigger-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Additionnal Trigger Paths

By default, when you creating a layer, you must specify a repository and a path. This path is used to trigger the layer changes which means that when a change occurs in this path, the layer will be plan / apply accordingly.

Sometimes, you need to trigger changes on a layer where the changes are not in the same path (e.g. update made on an internal terraform module hosted on the same repository).

That's where the additional trigger paths feature comes!

Let's take the following `TerraformLayer`:

```yaml
apiVersion: config.terraform.padok.cloud/v1alpha1
kind: TerraformLayer
metadata:
name: random-pets-terragrunt
spec:
terraform:
version: "1.3.1"
terragrunt:
enabled: true
version: "0.45.4"
remediationStrategy: autoApply
path: "terragrunt/random-pets/test"
branch: "main"
repository:
name: burrito
namespace: burrito
```
The repository's path of my `TerraformLayer` is set to `terragrunt/random-pets/test`. But I want to trigger the layer plan / apply when a change occurs on my module which is in the `modules/random-pets` directory of my repository.

To do so, I just have to add the `config.terraform.padok.cloud/additionnal-trigger-paths` annotation to my `TerraformLayer` as follow:

```yaml
apiVersion: config.terraform.padok.cloud/v1alpha1
kind: TerraformLayer
metadata:
name: random-pets-terragrunt
annotations:
config.terraform.padok.cloud/additionnal-trigger-paths: "modules/random-pets"
spec:
terraform:
version: "1.3.1"
terragrunt:
enabled: true
version: "0.45.4"
remediationStrategy: autoApply
path: "terragrunt/random-pets/test"
branch: "main"
repository:
name: burrito
namespace: burrito
```

Now, when a change occurs in the `modules/random-pets` directory, the layer will be plan / apply.
2 changes: 2 additions & 0 deletions internal/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (

LastDiscoveredCommit string = "pullrequest.terraform.padok.cloud/last-discovered-commit"
LastCommentedCommit string = "pullrequest.terraform.padok.cloud/last-commented-commit"

AdditionnalTriggerPaths string = "config.terraform.padok.cloud/additionnal-trigger-paths"
)

func Add(ctx context.Context, c client.Client, obj client.Object, annotations map[string]string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/annotations/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
"github.com/padok-team/burrito/internal/annotations"
utils "github.com/padok-team/burrito/internal/controllers/testing"
utils "github.com/padok-team/burrito/internal/testing"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
Expand Down
5 changes: 2 additions & 3 deletions internal/burrito/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ type Config struct {
}

type WebhookConfig struct {
Github WebhookGithubConfig `yaml:"github"`
Gitlab WebhookGitlabConfig `yaml:"gitlab"`
Namespace string `yaml:"namespace"`
Github WebhookGithubConfig `yaml:"github"`
Gitlab WebhookGitlabConfig `yaml:"gitlab"`
}

type WebhookGithubConfig struct {
Expand Down
36 changes: 36 additions & 0 deletions internal/controllers/terraformlayer/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package terraformlayer
import (
"errors"
"fmt"
"path/filepath"
"strings"
"time"

configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
Expand Down Expand Up @@ -210,3 +212,37 @@ func GetLastActionTime(r *Reconciler, layer *configv1alpha1.TerraformLayer) (tim
}
return lastActionTime, nil
}

func LayerFilesHaveChanged(layer configv1alpha1.TerraformLayer, changedFiles []string) bool {
if len(changedFiles) == 0 {
return true
}

// At last one changed file must be under refresh path
for _, f := range changedFiles {
f = ensureAbsPath(f)
if strings.Contains(f, layer.Spec.Path) {
return true
}
// Check if the file is under an additionnal trigger path
if val, ok := layer.Annotations[annotations.AdditionnalTriggerPaths]; ok {
for _, p := range strings.Split(val, ",") {
p = ensureAbsPath(p)
// Handle relative parent paths (like "../")
p = filepath.Clean(filepath.Join(layer.Spec.Path, p))
if strings.Contains(f, p) {
return true
}
}
}
}

return false
}

func ensureAbsPath(input string) string {
if !filepath.IsAbs(input) {
return string(filepath.Separator) + input
}
return input
}
2 changes: 1 addition & 1 deletion internal/controllers/terraformlayer/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
controller "github.com/padok-team/burrito/internal/controllers/terraformlayer"
utils "github.com/padok-team/burrito/internal/controllers/testing"
storage "github.com/padok-team/burrito/internal/storage/mock"
utils "github.com/padok-team/burrito/internal/testing"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down
28 changes: 2 additions & 26 deletions internal/controllers/terraformpullrequest/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package terraformpullrequest
import (
"context"
"fmt"
"path/filepath"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
"github.com/padok-team/burrito/internal/annotations"
controller "github.com/padok-team/burrito/internal/controllers/terraformlayer"
)

func (r *Reconciler) getAffectedLayers(repository *configv1alpha1.TerraformRepository, pr *configv1alpha1.TerraformPullRequest) ([]configv1alpha1.TerraformLayer, error) {
Expand Down Expand Up @@ -52,7 +51,7 @@ func isLayerAffected(layer configv1alpha1.TerraformLayer, pr configv1alpha1.Terr
if layer.Spec.Branch != pr.Spec.Base {
return false
}
if layerFilesHaveChanged(layer, changes) {
if controller.LayerFilesHaveChanged(layer, changes) {
return true
}
return false
Expand Down Expand Up @@ -94,26 +93,3 @@ func generateTempLayers(pr *configv1alpha1.TerraformPullRequest, layers []config
}
return list
}

func layerFilesHaveChanged(layer configv1alpha1.TerraformLayer, changedFiles []string) bool {
if len(changedFiles) == 0 {
return true
}

// At last one changed file must be under refresh path
for _, f := range changedFiles {
f = ensureAbsPath(f)
if strings.Contains(f, layer.Spec.Path) {
return true
}
}

return false
}

func ensureAbsPath(input string) string {
if !filepath.IsAbs(input) {
return string(filepath.Separator) + input
}
return input
}
Binary file not shown.
2 changes: 1 addition & 1 deletion internal/lock/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
. "github.com/onsi/gomega"

configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
utils "github.com/padok-team/burrito/internal/controllers/testing"
"github.com/padok-team/burrito/internal/lock"
utils "github.com/padok-team/burrito/internal/testing"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
Expand Down
File renamed without changes.
24 changes: 0 additions & 24 deletions internal/webhook/event/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package event

import (
"fmt"
"path/filepath"
"strings"

configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
Expand Down Expand Up @@ -51,22 +50,6 @@ func isLayerLinkedToAnyRepositories(repositories []configv1alpha1.TerraformRepos
return false
}

func layerFilesHaveChanged(layer configv1alpha1.TerraformLayer, changedFiles []string) bool {
if len(changedFiles) == 0 {
return true
}

// At last one changed file must be under refresh path
for _, f := range changedFiles {
f = ensureAbsPath(f)
if strings.Contains(f, layer.Spec.Path) {
return true
}
}

return false
}

func isPRLinkedToAnyRepositories(pr configv1alpha1.TerraformPullRequest, repos []configv1alpha1.TerraformRepository) bool {
for _, r := range repos {
if r.Name == pr.Spec.Repository.Name && r.Namespace == pr.Spec.Repository.Namespace {
Expand All @@ -75,10 +58,3 @@ func isPRLinkedToAnyRepositories(pr configv1alpha1.TerraformPullRequest, repos [
}
return false
}

func ensureAbsPath(input string) string {
if !filepath.IsAbs(input) {
return string(filepath.Separator) + input
}
return input
}
Loading

0 comments on commit e7a3a59

Please sign in to comment.