Skip to content

Commit 376fee2

Browse files
authored
feat(git): add tag support for TerraformLayers and enhance provider fallback logic (#652)
* feat: Add tag support for git providers - Add CloneWithFallback function to support both branch and tag references - Implement ReferenceName and ReferenceNameForTag helper functions - Update GitHub, GitLab, and Standard providers to use new clone strategy - Add comprehensive test coverage for new functionality - Improve error handling and logging for clone operations This enhancement allows TerraformRepository resources to reference Git tags in addition to branches, enabling more flexible versioning strategies. * feat: Add ServiceAccount and ClusterRole for burrito-runner in RBAC configuration chore: Update .gitignore to include test-repo directory fix: Clean up whitespace in bundle.go for better readability * feat: Add allowPrOnTags field to TerraformLayerSpec and update related manifests * feat: Replace AllowPrOnTags with AdditionalTargetRefs in TerraformLayerSpec and update related manifests * feat: Include layer name in the generated comment template for better clarity * refactor: Simplify additionalTargetRefs check using slices.Contains * feat: Add RBAC configuration for burrito-runner with ClusterRole and necessary permissions
1 parent 8df4a70 commit 376fee2

File tree

13 files changed

+67
-30
lines changed

13 files changed

+67
-30
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ test.out/*
4040
.venv
4141
env/
4242
venv/
43+
44+
test-repo/
45+
TIltfile

api/v1alpha1/terraformlayer_types.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ type TerraformLayerSpec struct {
3030
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
3131
// Important: Run "make" to regenerate code after modifying this file
3232

33-
Path string `json:"path,omitempty"`
34-
Branch string `json:"branch,omitempty"`
35-
TerraformConfig TerraformConfig `json:"terraform,omitempty"`
36-
OpenTofuConfig OpenTofuConfig `json:"opentofu,omitempty"`
37-
TerragruntConfig TerragruntConfig `json:"terragrunt,omitempty"`
38-
Repository TerraformLayerRepository `json:"repository,omitempty"`
39-
RemediationStrategy RemediationStrategy `json:"remediationStrategy,omitempty"`
40-
OverrideRunnerSpec OverrideRunnerSpec `json:"overrideRunnerSpec,omitempty"`
41-
RunHistoryPolicy RunHistoryPolicy `json:"runHistoryPolicy,omitempty"`
33+
Path string `json:"path,omitempty"`
34+
Branch string `json:"branch,omitempty"`
35+
AdditionalTargetRefs []string `json:"additionalTargetRefs,omitempty"`
36+
TerraformConfig TerraformConfig `json:"terraform,omitempty"`
37+
OpenTofuConfig OpenTofuConfig `json:"opentofu,omitempty"`
38+
TerragruntConfig TerragruntConfig `json:"terragrunt,omitempty"`
39+
Repository TerraformLayerRepository `json:"repository,omitempty"`
40+
RemediationStrategy RemediationStrategy `json:"remediationStrategy,omitempty"`
41+
OverrideRunnerSpec OverrideRunnerSpec `json:"overrideRunnerSpec,omitempty"`
42+
RunHistoryPolicy RunHistoryPolicy `json:"runHistoryPolicy,omitempty"`
4243
}
4344

4445
type TerraformLayerRepository struct {

api/v1alpha1/zz_generated.deepcopy.go

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

deploy/charts/burrito/templates/rbac-runner.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
apiVersion: rbac.authorization.k8s.io/v1
23
kind: ClusterRole
34
metadata:
@@ -32,4 +33,4 @@ rules:
3233
resources:
3334
- terraformrepositories
3435
verbs:
35-
- get
36+
- get

internal/controllers/terraformpullrequest/comment/default.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
)
1818

1919
type ReportedLayer struct {
20+
Name string
2021
ShortDiff string
2122
Path string
2223
PrettyPlan string
@@ -49,6 +50,7 @@ func (c *DefaultComment) Generate(commit string) (string, error) {
4950
return "", err
5051
}
5152
reportedLayer := ReportedLayer{
53+
Name: layer.Name,
5254
Path: layer.Spec.Path,
5355
ShortDiff: string(shortDiff),
5456
PrettyPlan: string(plan),

internal/controllers/terraformpullrequest/comment/templates/comment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{{ range .Layers }}
66

7-
### Layer {{ .Path }}
7+
### Layer {{ .Name }} ({{ .Path }})
88

99
`{{ .ShortDiff }}`
1010

internal/controllers/terraformpullrequest/layer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package terraformpullrequest
33
import (
44
"context"
55
"fmt"
6+
"slices"
67

78
corev1 "k8s.io/api/core/v1"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -48,7 +49,12 @@ func isLayerAffected(layer configv1alpha1.TerraformLayer, pr configv1alpha1.Terr
4849
if layer.Spec.Repository.Namespace != pr.Spec.Repository.Namespace {
4950
return false
5051
}
51-
if layer.Spec.Branch != pr.Spec.Base {
52+
// Check if branch matches OR if PR base is in additionalTargetRefs
53+
branchMatches := layer.Spec.Branch == pr.Spec.Base
54+
additionalTargetMatches := slices.Contains(layer.Spec.AdditionalTargetRefs, pr.Spec.Base)
55+
56+
// If neither branch matches nor is in additional targets, skip this layer
57+
if !branchMatches && !additionalTargetMatches {
5258
return false
5359
}
5460
if controller.LayerFilesHaveChanged(layer, changes) {

internal/utils/gitprovider/common/bundle.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ const (
1919
BundleDir = "/tmp/burrito/gitbundles"
2020
)
2121

22+
// ReferenceName converts a ref string to a plumbing.ReferenceName
23+
// If ref starts with "refs/", use it directly; otherwise assume it's a branch
24+
func ReferenceName(ref string) plumbing.ReferenceName {
25+
if strings.HasPrefix(ref, "refs/") {
26+
return plumbing.ReferenceName(ref)
27+
}
28+
// Default to branch for backward compatibility
29+
return plumbing.NewBranchReferenceName(ref)
30+
}
31+
2232
func GetGitBundle(repository *configv1alpha1.TerraformRepository, ref string, revision string, auth transport.AuthMethod) ([]byte, error) {
2333
repoKey := fmt.Sprintf("%s-%s-%s", repository.Namespace, repository.Name, strings.ReplaceAll(ref, "/", "--"))
2434
repoDir := filepath.Join(WorkingDir, repoKey)
@@ -32,10 +42,11 @@ func GetGitBundle(repository *configv1alpha1.TerraformRepository, ref string, re
3242

3343
// Clone if it doesn't exist
3444
log.Infof("Cloning repository %s to %s", repository.Spec.Repository.Url, repoDir)
45+
3546
cloneOpts := &git.CloneOptions{
3647
URL: repository.Spec.Repository.Url,
3748
Auth: auth,
38-
ReferenceName: plumbing.NewBranchReferenceName(ref),
49+
ReferenceName: ReferenceName(ref),
3950
}
4051

4152
repo, err = git.PlainClone(repoDir, false, cloneOpts)

internal/utils/gitprovider/github/github.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313

1414
"github.com/bradleyfalzon/ghinstallation/v2"
1515
"github.com/go-git/go-git/v5"
16-
"github.com/go-git/go-git/v5/plumbing"
1716
"github.com/go-git/go-git/v5/plumbing/transport"
1817
"github.com/go-git/go-git/v5/plumbing/transport/http"
1918
wh "github.com/go-playground/webhooks/github"
2019
"github.com/google/go-github/v74/github"
2120
configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
2221
"github.com/padok-team/burrito/internal/annotations"
2322
"github.com/padok-team/burrito/internal/controllers/terraformpullrequest/comment"
23+
"github.com/padok-team/burrito/internal/utils/gitprovider/common"
2424
"github.com/padok-team/burrito/internal/utils/gitprovider/types"
2525
utils "github.com/padok-team/burrito/internal/utils/url"
2626
"github.com/padok-team/burrito/internal/webhook/event"
@@ -185,12 +185,12 @@ func (g *Github) Clone(repository *configv1alpha1.TerraformRepository, branch st
185185
}
186186

187187
cloneOptions := &git.CloneOptions{
188-
ReferenceName: plumbing.NewBranchReferenceName(branch),
188+
ReferenceName: common.ReferenceName(branch),
189189
URL: repository.Spec.Repository.Url,
190190
Auth: auth,
191191
}
192192

193-
log.Infof("Cloning github repository %s on %s branch with github %s authentication", repository.Spec.Repository.Url, branch, g.GitHubClientType)
193+
log.Infof("Cloning github repository %s on ref %s with github %s authentication", repository.Spec.Repository.Url, branch, g.GitHubClientType)
194194
repo, err := git.PlainClone(repositoryPath, false, cloneOptions)
195195
if err != nil {
196196
return nil, err

internal/utils/gitprovider/gitlab/gitlab.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
"strings"
1111

1212
"github.com/go-git/go-git/v5"
13-
"github.com/go-git/go-git/v5/plumbing"
1413
"github.com/go-git/go-git/v5/plumbing/transport"
1514
"github.com/go-git/go-git/v5/plumbing/transport/http"
1615
wh "github.com/go-playground/webhooks/gitlab"
1716
configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
1817
"github.com/padok-team/burrito/internal/annotations"
1918
"github.com/padok-team/burrito/internal/controllers/terraformpullrequest/comment"
19+
"github.com/padok-team/burrito/internal/utils/gitprovider/common"
2020
"github.com/padok-team/burrito/internal/utils/gitprovider/types"
2121
utils "github.com/padok-team/burrito/internal/utils/url"
2222
"github.com/padok-team/burrito/internal/webhook/event"
@@ -139,17 +139,17 @@ func (g *Gitlab) Clone(repository *configv1alpha1.TerraformRepository, branch st
139139
return nil, err
140140
}
141141

142+
if auth == nil {
143+
return nil, errors.New("no valid authentication method provided")
144+
}
145+
142146
cloneOptions := &git.CloneOptions{
143-
ReferenceName: plumbing.NewBranchReferenceName(branch),
147+
ReferenceName: common.ReferenceName(branch),
144148
URL: repository.Spec.Repository.Url,
145149
Auth: auth,
146150
}
147151

148-
if auth == nil {
149-
return nil, errors.New("no valid authentication method provided")
150-
}
151-
152-
log.Infof("Cloning gitlab repository %s on %s branch", repository.Spec.Repository.Url, branch)
152+
log.Infof("Cloning gitlab repository %s on ref %s", repository.Spec.Repository.Url, branch)
153153
repo, err := git.PlainClone(repositoryPath, false, cloneOptions)
154154
if err != nil {
155155
return nil, err

0 commit comments

Comments
 (0)