Skip to content

Commit 0787354

Browse files
enekofbyitsushi
andauthored
support for gitlab ee (#142)
* workaround to work with gitlab ee * review pr * work with gitlab enterprise * review pr * review pr * Update e2e/promotion_pull_request_test.go Co-authored-by: Balazs Nadasdi <[email protected]> * review comments * review comments Co-authored-by: Balazs Nadasdi <[email protected]>
1 parent be66065 commit 0787354

File tree

8 files changed

+200
-55
lines changed

8 files changed

+200
-55
lines changed

Makefile

+2-4
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,13 @@ $(HELM): $(LOCALBIN)
212212
e2e: e2e-setup e2e-test e2e-clean
213213

214214
.PHONY: e2e-setup
215-
e2e-setup: docker-build kind flux deploy
215+
e2e-setup: e2e-kind deploy
216216
kubectl wait -n pipeline-system deployment/pipeline-controller --for=condition=available
217217
kubectl apply -f e2e/testdata --recursive
218218

219-
kind:
219+
e2e-kind: docker-build
220220
kind create cluster --name=pipeline-controller || kubectx kind-pipeline-controller
221221
kind load docker-image --name=pipeline-controller $(IMG)
222-
223-
flux:
224222
flux install
225223

226224
.PHONY: e2e-test

e2e/promotion_pull_request_test.go

+23-22
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"fmt"
1010
"github.com/fluxcd/go-git-providers/gitprovider"
1111
"github.com/fluxcd/helm-controller/api/v2beta1"
12+
"github.com/fluxcd/pkg/runtime/logger"
13+
"github.com/go-logr/logr"
1214
"github.com/google/go-github/v47/github"
1315
"github.com/hashicorp/go-uuid"
1416
. "github.com/onsi/gomega"
@@ -37,6 +39,7 @@ const (
3739

3840
func TestPullRequestPromotions(t *testing.T) {
3941
g := testingutils.NewGomegaWithT(t)
42+
log := logger.NewLogger(logger.Options{})
4043

4144
tests := []struct {
4245
name string
@@ -56,16 +59,15 @@ func TestPullRequestPromotions(t *testing.T) {
5659
"6.0.0",
5760
"6.0.1",
5861
},
59-
//TODO enable me when https://github.com/weaveworks/pipeline-controller/issues/132 is closed
60-
//{
61-
// "can promote gitlab",
62-
// "podinfo-gitlab",
63-
// "flux-system",
64-
// "promotion-flux-system-podinfo-gitlab-prod",
65-
// "prod",
66-
// "6.0.0",
67-
// "6.0.1",
68-
//},
62+
{
63+
"can promote gitlab",
64+
"podinfo-gitlab",
65+
"flux-system",
66+
"promotion-flux-system-podinfo-gitlab-prod",
67+
"prod",
68+
"6.0.0",
69+
"6.0.1",
70+
},
6971
}
7072

7173
g.SetDefaultEventuallyTimeout(defaultTimeout)
@@ -82,15 +84,15 @@ func TestPullRequestPromotions(t *testing.T) {
8284

8385
//cleanup promotion
8486
t.Cleanup(func() {
85-
log.Println("cleaning test")
87+
log.Info("cleaning test")
8688
//restore helm release version
8789
released := releaseNewVersion(g, k8sClient, helmRelease, promotion.currentVersion)
8890
g.Expect(released).To(BeTrue())
8991
helmRelease, err = ensureHelmRelease(g, pipeline, k8sClient, promotion.currentVersion)
9092
//delete git branch
91-
err := deleteGitBranchByName(context.Background(), g, k8sClient, pipeline, promotion.branchName)
93+
err := deleteGitBranchByName(context.Background(), g, k8sClient, pipeline, promotion.branchName, log)
9294
if err != nil {
93-
log.Fatalf("could not delete branch %s", err)
95+
log.Error(err, "could not delete branch")
9496
}
9597
})
9698

@@ -110,7 +112,7 @@ func TestPullRequestPromotions(t *testing.T) {
110112
deletePodsByNamespaceAndLabel(g, "flux-system", "app=notification-controller")
111113
}
112114

113-
func deleteGitBranchByName(ctx context.Context, g *WithT, c client.Client, pipeline v1alpha1.Pipeline, branchName string) error {
115+
func deleteGitBranchByName(ctx context.Context, g *WithT, c client.Client, pipeline v1alpha1.Pipeline, branchName string, log logr.Logger) error {
114116
var secret corev1.Secret
115117

116118
promotion := pipeline.Spec.Promotion
@@ -131,26 +133,27 @@ func deleteGitBranchByName(ctx context.Context, g *WithT, c client.Client, pipel
131133
g.Eventually(func() bool {
132134
//get secret
133135
if err := c.Get(ctx, client.ObjectKey{Namespace: pipeline.Namespace, Name: secretName}, &secret); err != nil {
134-
log.Printf("failed to fetch Secret: %s", err)
136+
log.Error(err, "failed to fetch Secret")
135137
return false
136138
}
137139
return true
138140
}).Should(BeTrue())
139141

140-
hostname := userRepoRef.Domain
141142
tokenString := string(secret.Data["token"])
142143
provider := pullrequest.GitProviderConfig{
143144
Token: tokenString,
144145
TokenType: "oauth2",
145146
Type: pullRequestPromotion.Type,
146-
Hostname: hostname,
147+
Domain: userRepoRef.Domain,
147148
DestructiveCalls: false,
148149
}
149150

150-
gitProviderClient, err := pullrequest.NewGitProviderClientFactory()(provider)
151+
gitProviderClient, err := pullrequest.NewGitProviderClientFactory(log)(provider)
151152
if err != nil {
152153
return fmt.Errorf("could not create git provider client: %w", err)
153154
}
155+
//TODO it should not be needed - gitlab ggp issue
156+
userRepoRef.Domain = gitProviderClient.SupportedDomain()
154157
userRepo, err := gitProviderClient.UserRepositories().Get(ctx, *userRepoRef)
155158
if err != nil {
156159
return fmt.Errorf("could not get repository: %w", err)
@@ -167,19 +170,17 @@ func deleteGitBranchByName(ctx context.Context, g *WithT, c client.Client, pipel
167170
}
168171
owner := userRepoRef.UserLogin
169172
repo := userRepoRef.RepositoryName
170-
_, r, err := gClient.Repositories.RenameBranch(ctx, owner, repo, branchName, fmt.Sprintf("%s-%s", branchName, generatedUuid))
173+
_, _, err = gClient.Repositories.RenameBranch(ctx, owner, repo, branchName, fmt.Sprintf("%s-%s", branchName, generatedUuid))
171174
if err != nil {
172175
return fmt.Errorf("could not rename branch: %w", err)
173176
}
174-
log.Println("branch delete response", r)
175177
case v1alpha1.Gitlab:
176178
gitlabProject := userRepo.APIObject().(*gitlab2.Project)
177179
gClient := clientRaw.(*gitlab2.Client)
178-
deletedBranch, err := gClient.Branches.DeleteBranch(gitlabProject.ID, branchName, nil)
180+
_, err := gClient.Branches.DeleteBranch(gitlabProject.ID, branchName, nil)
179181
if err != nil {
180182
return fmt.Errorf("could not delete gitlab branch: %w", err)
181183
}
182-
log.Println("branch delete response", deletedBranch)
183184
}
184185
return nil
185186
}

e2e/testdata/pipelines/gitlab/pipeline.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ spec:
2020
type: "gitlab"
2121
url: "https://gitlab.git.dev.weave.works/pipeline-controller/pipeline-controller-e2e"
2222
secretRef:
23-
name: "gitlab-promotion-credentials"
23+
name: "gitlab-ee-promotion-credentials"

server/strategy/pullrequest/gitprovider.go

+40-10
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import (
55
"github.com/fluxcd/go-git-providers/github"
66
"github.com/fluxcd/go-git-providers/gitlab"
77
"github.com/fluxcd/go-git-providers/gitprovider"
8+
"github.com/go-logr/logr"
89
"github.com/weaveworks/pipeline-controller/api/v1alpha1"
10+
"strings"
911
)
1012

1113
var (
12-
ErrGitProviderTypeEmpty = fmt.Errorf("git provider type is empty")
13-
ErrGitProviderTypeInvalid = fmt.Errorf("git provider type not supported")
14-
ErrGitProviderDomainIsEmpty = fmt.Errorf("git provider host is empty")
15-
ErrTokenTypeIsEmpty = fmt.Errorf("git provider token type is empty")
16-
ErrTokenIsEmpty = fmt.Errorf("git provider token is empty")
14+
ErrGitProviderTypeEmpty = fmt.Errorf("git provider type is empty")
15+
ErrGitProviderTypeInvalid = fmt.Errorf("git provider type not supported")
16+
ErrGitProviderDomainIsEmpty = fmt.Errorf("git provider host is empty")
17+
ErrGitProviderDomainIsInvalid = fmt.Errorf("git provider domain is invalid")
18+
ErrTokenTypeIsEmpty = fmt.Errorf("git provider token type is empty")
19+
ErrTokenIsEmpty = fmt.Errorf("git provider token is empty")
1720
)
1821

1922
type GitProviderClientFactory func(provider GitProviderConfig) (gitprovider.Client, error)
@@ -22,12 +25,12 @@ type GitProviderConfig struct {
2225
Token string
2326
TokenType string
2427
Type v1alpha1.GitProviderType
25-
Hostname string
28+
Domain string
2629
DestructiveCalls bool
2730
}
2831

2932
// same as https://github.com/weaveworks/weave-gitops-enterprise/blob/7ef05e773d7650a83cfa86dbd642253353b584c0/cmd/clusters-service/pkg/git/git.go#L286
30-
func NewGitProviderClientFactory() GitProviderClientFactory {
33+
func NewGitProviderClientFactory(log logr.Logger) GitProviderClientFactory {
3134
return func(provider GitProviderConfig) (gitprovider.Client, error) {
3235
var client gitprovider.Client
3336
var err error
@@ -53,12 +56,16 @@ func NewGitProviderClientFactory() GitProviderClientFactory {
5356
clientOptions = append(clientOptions, gitprovider.WithOAuth2Token(provider.Token))
5457

5558
if provider.DestructiveCalls {
56-
//TODO bring visibility to this action
59+
log.Info("creating client with destructive calls enabled")
5760
clientOptions = append(clientOptions, gitprovider.WithDestructiveAPICalls(provider.DestructiveCalls))
5861
}
5962

60-
if provider.Hostname != "" {
61-
clientOptions = append(clientOptions, gitprovider.WithDomain(provider.Hostname))
63+
if provider.Domain != "" {
64+
domain, err := decorateCustomDomainByType(provider.Type, provider.Domain)
65+
if err != nil {
66+
return nil, err
67+
}
68+
clientOptions = append(clientOptions, gitprovider.WithDomain(domain))
6269
}
6370

6471
switch provider.Type {
@@ -80,6 +87,29 @@ func NewGitProviderClientFactory() GitProviderClientFactory {
8087

8188
}
8289

90+
// TODO review me when https://github.com/fluxcd/go-git-providers/issues/175 and
91+
// https://github.com/fluxcd/go-git-providers/issues/176 are fixed
92+
// we need to hack this to add the https scheme to the domain to
93+
// have a gitlab client with a correct baseurl otherwise would create an invalid url
94+
func decorateCustomDomainByType(gitProviderType v1alpha1.GitProviderType, domain string) (string, error) {
95+
if gitProviderType == "" {
96+
return "", ErrGitProviderTypeEmpty
97+
}
98+
99+
if domain == "" {
100+
return "", ErrGitProviderDomainIsEmpty
101+
}
102+
103+
switch gitProviderType {
104+
case v1alpha1.Gitlab:
105+
//if not a url we create one with https by default
106+
if !strings.HasPrefix(domain, "http://") && !strings.HasPrefix(domain, "https://") {
107+
return fmt.Sprintf("https://%s", domain), nil
108+
}
109+
}
110+
return domain, nil
111+
}
112+
83113
func gitProviderIsValid(gitProviderType v1alpha1.GitProviderType) (bool, error) {
84114

85115
if gitProviderType == "" {

server/strategy/pullrequest/gitprovider_test.go

+76-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pullrequest
22

33
import (
44
"github.com/fluxcd/go-git-providers/gitprovider"
5+
"github.com/fluxcd/pkg/runtime/logger"
56
"github.com/onsi/gomega"
67
"github.com/weaveworks/pipeline-controller/api/v1alpha1"
78
"github.com/weaveworks/pipeline-controller/internal/testingutils"
@@ -55,7 +56,7 @@ func Test_gitProviderIsEmpty(t *testing.T) {
5556
}
5657
}
5758

58-
func Test_newGitProviderClientFactory(t *testing.T) {
59+
func TestNewGitProviderClientFactory(t *testing.T) {
5960
tests := []struct {
6061
name string
6162
in GitProviderConfig
@@ -128,7 +129,7 @@ func Test_newGitProviderClientFactory(t *testing.T) {
128129
Type: v1alpha1.Github,
129130
TokenType: "oauth2",
130131
Token: "asdf",
131-
Hostname: "github.myenterprise.com",
132+
Domain: "github.myenterprise.com",
132133
},
133134
"github",
134135
"github.myenterprise.com",
@@ -142,27 +143,27 @@ func Test_newGitProviderClientFactory(t *testing.T) {
142143
Token: "abc",
143144
},
144145
"gitlab",
145-
"https://gitlab.com", //TODO: raise issue with ggp cause the domain returns an URL
146+
"https://gitlab.com", //TODO: change me when fixed https://github.com/fluxcd/go-git-providers/issues/175
146147
"",
147148
},
148149
{
149-
"can create git provider for gitlab on prem",
150+
"can create git provider for gitlab enterprise",
150151
GitProviderConfig{
151152
Type: v1alpha1.Gitlab,
152153
TokenType: "oauth2",
153154
Token: "abc",
154-
Hostname: "gitlab.myenterprise.com",
155+
Domain: "gitlab.myenterprise.com",
155156
},
156157
"gitlab",
157-
"https://gitlab.myenterprise.com", //TODO: raise issue with ggp cause the domain returns an URL
158+
"https://gitlab.myenterprise.com", //TODO: change me when fixed https://github.com/fluxcd/go-git-providers/issues/175
158159
"",
159160
},
160161
}
161162

162163
for _, tt := range tests {
163164
t.Run(tt.name, func(t *testing.T) {
164165
g := testingutils.NewGomegaWithT(t)
165-
client, err := NewGitProviderClientFactory()(tt.in)
166+
client, err := NewGitProviderClientFactory(logger.NewLogger(logger.Options{}))(tt.in)
166167
if tt.errPattern != "" {
167168
g.Expect(err).To(gomega.MatchError(gomega.MatchRegexp(tt.errPattern)))
168169
} else {
@@ -173,3 +174,71 @@ func Test_newGitProviderClientFactory(t *testing.T) {
173174
})
174175
}
175176
}
177+
178+
func Test_decorateCustomDomainByType(t *testing.T) {
179+
180+
tests := []struct {
181+
name string
182+
gitProviderType v1alpha1.GitProviderType
183+
domain string
184+
expectedDomain string
185+
errPattern string
186+
}{
187+
{
188+
"cannot decorate if empty git provider",
189+
"",
190+
"",
191+
"",
192+
ErrGitProviderTypeEmpty.Error(),
193+
},
194+
{
195+
"cannot decorate if empty domain",
196+
v1alpha1.Github,
197+
"",
198+
"",
199+
ErrGitProviderDomainIsEmpty.Error(),
200+
},
201+
{
202+
"decorate non-gitlab enterprise domain has no effect",
203+
v1alpha1.Github,
204+
"github.myenterprise.com",
205+
"github.myenterprise.com",
206+
"",
207+
},
208+
{
209+
"decorate gitlab enterprise domain adds https scheme",
210+
v1alpha1.Gitlab,
211+
"gitlab.git.dev.weave.works",
212+
"https://gitlab.git.dev.weave.works",
213+
"",
214+
},
215+
{
216+
"decorate gitlab enterprise with https scheme does not change",
217+
v1alpha1.Gitlab,
218+
"https://gitlab.git.dev.weave.works",
219+
"https://gitlab.git.dev.weave.works",
220+
"",
221+
},
222+
{
223+
"decorate gitlab enterprise with http scheme does not change",
224+
v1alpha1.Gitlab,
225+
"http://gitlab.git.dev.weave.works",
226+
"http://gitlab.git.dev.weave.works",
227+
"",
228+
},
229+
}
230+
231+
for _, tt := range tests {
232+
t.Run(tt.name, func(t *testing.T) {
233+
g := testingutils.NewGomegaWithT(t)
234+
domain, err := decorateCustomDomainByType(tt.gitProviderType, tt.domain)
235+
if tt.errPattern != "" {
236+
g.Expect(err).To(gomega.MatchError(gomega.MatchRegexp(tt.errPattern)))
237+
} else {
238+
g.Expect(domain).To(gomega.Equal(tt.expectedDomain))
239+
g.Expect(err).To(gomega.BeNil())
240+
}
241+
})
242+
}
243+
244+
}

0 commit comments

Comments
 (0)