Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webhook): add additional paths trigger #129

Merged
merged 14 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
9 changes: 9 additions & 0 deletions internal/controllers/terraformpullrequest/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ func layerFilesHaveChanged(layer configv1alpha1.TerraformLayer, changedFiles []s
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)
corrieriluca marked this conversation as resolved.
Show resolved Hide resolved
if strings.Contains(f, p) {
return true
}
}
}
}

return false
Expand Down
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.
12 changes: 12 additions & 0 deletions internal/webhook/event/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
"github.com/padok-team/burrito/internal/annotations"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -62,6 +63,17 @@ func layerFilesHaveChanged(layer configv1alpha1.TerraformLayer, changedFiles []s
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
Expand Down
Loading