Skip to content

Commit 1e0fad1

Browse files
authored
Merge pull request #565 from fluxcd/proxy-options
add support for specified proxy in source GitRepository
2 parents c120f91 + 95a7b39 commit 1e0fad1

File tree

6 files changed

+224
-66
lines changed

6 files changed

+224
-66
lines changed

api/go.mod

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

55
require (
66
github.com/fluxcd/pkg/apis/meta v1.1.2
7-
github.com/fluxcd/source-controller/api v1.0.1
7+
github.com/fluxcd/source-controller/api v1.1.0
88
k8s.io/apimachinery v0.27.4
99
sigs.k8s.io/controller-runtime v0.15.1
1010
)

api/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/fluxcd/pkg/apis/meta v1.1.2 h1:Unjo7hxadtB2dvGpeFqZZUdsjpRA08YYSBb7dF2WIAM=
66
github.com/fluxcd/pkg/apis/meta v1.1.2/go.mod h1:BHQyRHCskGMEDf6kDGbgQ+cyiNpUHbLsCOsaMYM2maI=
7-
github.com/fluxcd/source-controller/api v1.0.1 h1:nycylbNBnQd+EO4UHpqXqAQJ1cGAPxgBbrfERCQ1pp8=
8-
github.com/fluxcd/source-controller/api v1.0.1/go.mod h1:rAY5FRFGZUKpIFNyYANHIgPgJPvbALBHWsq/zHw/cXQ=
7+
github.com/fluxcd/source-controller/api v1.1.0 h1:JPtt9WTTqVNdJfPpea8q7fUWF/00kDihxbhISzcb0WE=
8+
github.com/fluxcd/source-controller/api v1.1.0/go.mod h1:ZLkaUd1KQIjtLPCvO63Ni5zpnSTVBAkeRgFBzMItbDQ=
99
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
1010
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
1111
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=

go.mod

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ require (
2020
github.com/fluxcd/pkg/apis/acl v0.1.0
2121
github.com/fluxcd/pkg/apis/event v0.5.2
2222
github.com/fluxcd/pkg/apis/meta v1.1.2
23-
github.com/fluxcd/pkg/git v0.12.4
24-
github.com/fluxcd/pkg/git/gogit v0.12.1
25-
github.com/fluxcd/pkg/gittestserver v0.8.5
23+
github.com/fluxcd/pkg/git v0.13.0
24+
github.com/fluxcd/pkg/git/gogit v0.13.0
25+
github.com/fluxcd/pkg/gittestserver v0.8.6
2626
github.com/fluxcd/pkg/runtime v0.42.0
27-
github.com/fluxcd/pkg/ssh v0.8.1
28-
github.com/fluxcd/source-controller/api v1.0.1
27+
github.com/fluxcd/pkg/ssh v0.8.2
28+
github.com/fluxcd/source-controller/api v1.1.0
2929
github.com/go-git/go-billy/v5 v5.4.1
3030
github.com/go-git/go-git/v5 v5.8.1
3131
github.com/go-logr/logr v1.2.4
@@ -125,13 +125,13 @@ require (
125125
go.starlark.net v0.0.0-20221028183056-acb66ad56dd2 // indirect
126126
go.uber.org/multierr v1.10.0 // indirect
127127
go.uber.org/zap v1.25.0 // indirect
128-
golang.org/x/crypto v0.11.0 // indirect
128+
golang.org/x/crypto v0.12.0 // indirect
129129
golang.org/x/mod v0.10.0 // indirect
130130
golang.org/x/net v0.13.0 // indirect
131131
golang.org/x/oauth2 v0.8.0 // indirect
132-
golang.org/x/sys v0.10.0 // indirect
133-
golang.org/x/term v0.10.0 // indirect
134-
golang.org/x/text v0.11.0 // indirect
132+
golang.org/x/sys v0.11.0 // indirect
133+
golang.org/x/term v0.11.0 // indirect
134+
golang.org/x/text v0.12.0 // indirect
135135
golang.org/x/time v0.3.0 // indirect
136136
golang.org/x/tools v0.9.3 // indirect
137137
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect

go.sum

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
5959
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6060
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6161
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
62-
github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819 h1:RIB4cRk+lBqKK3Oy0r2gRX4ui7tuhiZq2SuTtTCi0/0=
62+
github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027 h1:1L0aalTpPz7YlMxETKpmQoWMBkeiuorElZIXoNmgiPE=
6363
github.com/emicklei/go-restful/v3 v3.10.0 h1:X4gma4HM7hFm6WMeAsTfqA0GOfdNoCzBIkHGoRLGXuM=
6464
github.com/emicklei/go-restful/v3 v3.10.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
6565
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
@@ -89,20 +89,20 @@ github.com/fluxcd/pkg/apis/event v0.5.2 h1:WtnCOeWglf7wR3dpyiWxb1JtYkw1G5OXcERb1
8989
github.com/fluxcd/pkg/apis/event v0.5.2/go.mod h1:5l6SSxVTkqrXrYjgEqAajOOHkl4x0TPocAuSdu+3AEs=
9090
github.com/fluxcd/pkg/apis/meta v1.1.2 h1:Unjo7hxadtB2dvGpeFqZZUdsjpRA08YYSBb7dF2WIAM=
9191
github.com/fluxcd/pkg/apis/meta v1.1.2/go.mod h1:BHQyRHCskGMEDf6kDGbgQ+cyiNpUHbLsCOsaMYM2maI=
92-
github.com/fluxcd/pkg/git v0.12.4 h1:COuVYUL+gqMOYAm6oD32Vwcmy/8WVsT/nMk8ps0lpJI=
93-
github.com/fluxcd/pkg/git v0.12.4/go.mod h1:rKB1puk7sbC4AYF1oZDBrkvu3cr0aibkd4I5yNbxSQg=
94-
github.com/fluxcd/pkg/git/gogit v0.12.1 h1:06jzHOTntYN5xCSQvyFXtLXdqoP8crLh7VYgtXS9+wo=
95-
github.com/fluxcd/pkg/git/gogit v0.12.1/go.mod h1:Z4Ysp8VifKTvWpjJMKncJsgb2iBqHuIeK80VGjlU41Y=
96-
github.com/fluxcd/pkg/gittestserver v0.8.5 h1:EGqDF4240xPRgW1FFrQAs0Du7fZb8OGXC5qKDIqyXD8=
97-
github.com/fluxcd/pkg/gittestserver v0.8.5/go.mod h1:SyGEh+OBzFpdlTWWqv3XBkiLB42Iu+mijfIQ4hPlEZQ=
92+
github.com/fluxcd/pkg/git v0.13.0 h1:GcJfldYqw6ELf0vbTCV+iFZgSpK6HZBKx3yAvn1Dqfg=
93+
github.com/fluxcd/pkg/git v0.13.0/go.mod h1:rKB1puk7sbC4AYF1oZDBrkvu3cr0aibkd4I5yNbxSQg=
94+
github.com/fluxcd/pkg/git/gogit v0.13.0 h1:XCwfiB5qbz08djUgo0TII09zibH97Hn56v098pkFpns=
95+
github.com/fluxcd/pkg/git/gogit v0.13.0/go.mod h1:V3g+UyIDSAOysg5KCpHhS+HXBUmNmmbNlVruWkpCJgY=
96+
github.com/fluxcd/pkg/gittestserver v0.8.6 h1:YM8prVKB3LC9LBBe+a2p7l1BlfV9erXCgC1em9sbqW4=
97+
github.com/fluxcd/pkg/gittestserver v0.8.6/go.mod h1:3abUQFRNlfBhn+BD+TI2lfXI/JkdntdQ99spSnItFk4=
9898
github.com/fluxcd/pkg/runtime v0.42.0 h1:a5DQ/f90YjoHBmiXZUpnp4bDSLORjInbmqP7K11L4uY=
9999
github.com/fluxcd/pkg/runtime v0.42.0/go.mod h1:p6A3xWVV8cKLLQW0N90GehKgGMMmbNYv+OSJ/0qB0vg=
100-
github.com/fluxcd/pkg/ssh v0.8.1 h1:v35y7Ks/+ABWce8RcnrC7psVIhf3EdCUNFJi5+tYOps=
101-
github.com/fluxcd/pkg/ssh v0.8.1/go.mod h1:M1ouDXuDG+QuhGB4JYEjCNCykNytLJGDhwKn9y4DEOE=
100+
github.com/fluxcd/pkg/ssh v0.8.2 h1:WNfvTmnLnOUyXQDb8luSfmn1X0RIuhJBcKMFtKm6YsQ=
101+
github.com/fluxcd/pkg/ssh v0.8.2/go.mod h1:ewbU9vakYYdGSX92qXhx6Kqi5tVQ3ppmGQakCX1R6Gw=
102102
github.com/fluxcd/pkg/version v0.2.2 h1:ZpVXECeLA5hIQMft11iLp6gN3cKcz6UNuVTQPw/bRdI=
103103
github.com/fluxcd/pkg/version v0.2.2/go.mod h1:NGnh/no8S6PyfCDxRFrPY3T5BUnqP48MxfxNRU0z8C0=
104-
github.com/fluxcd/source-controller/api v1.0.1 h1:nycylbNBnQd+EO4UHpqXqAQJ1cGAPxgBbrfERCQ1pp8=
105-
github.com/fluxcd/source-controller/api v1.0.1/go.mod h1:rAY5FRFGZUKpIFNyYANHIgPgJPvbALBHWsq/zHw/cXQ=
104+
github.com/fluxcd/source-controller/api v1.1.0 h1:JPtt9WTTqVNdJfPpea8q7fUWF/00kDihxbhISzcb0WE=
105+
github.com/fluxcd/source-controller/api v1.1.0/go.mod h1:ZLkaUd1KQIjtLPCvO63Ni5zpnSTVBAkeRgFBzMItbDQ=
106106
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
107107
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
108108
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
@@ -347,8 +347,8 @@ golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0
347347
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
348348
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
349349
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
350-
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
351-
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
350+
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
351+
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
352352
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
353353
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
354354
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
@@ -423,16 +423,16 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
423423
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
424424
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
425425
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
426-
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
427-
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
426+
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
427+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
428428
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
429429
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
430430
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
431431
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
432432
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
433433
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
434-
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
435-
golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o=
434+
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
435+
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
436436
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
437437
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
438438
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@@ -442,8 +442,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
442442
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
443443
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
444444
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
445-
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
446-
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
445+
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
446+
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
447447
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
448448
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
449449
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

internal/controller/imageupdateautomation_controller.go

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/ProtonMail/go-crypto/openpgp"
3232
securejoin "github.com/cyphar/filepath-securejoin"
3333
extgogit "github.com/go-git/go-git/v5"
34+
"github.com/go-git/go-git/v5/plumbing/transport"
3435
"github.com/go-logr/logr"
3536
corev1 "k8s.io/api/core/v1"
3637
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -256,9 +257,20 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
256257
tracelog.Info("using push branch from $ref.branch", "branch", pushBranch)
257258
}
258259

259-
debuglog.Info("attempting to clone git repository", "gitrepository", originName, "ref", checkoutRef, "working", tmp)
260+
authOpts, err := r.getAuthOpts(ctx, &origin)
261+
if err != nil {
262+
return failWithError(err)
263+
}
264+
var proxyOpts *transport.ProxyOptions
265+
if origin.Spec.ProxySecretRef != nil {
266+
proxyOpts, err = r.getProxyOpts(ctx, origin.Spec.ProxySecretRef.Name, origin.GetNamespace())
267+
if err != nil {
268+
return failWithError(err)
269+
}
270+
}
260271

261-
gitClient, err := r.constructGitClient(ctx, &origin, tmp, switchBranch)
272+
clientOpts := r.getGitClientOpts(authOpts.Transport, proxyOpts, switchBranch)
273+
gitClient, err := gogit.NewClient(tmp, authOpts, clientOpts...)
262274
if err != nil {
263275
return failWithError(err)
264276
}
@@ -279,6 +291,7 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
279291
// Use the git operations timeout for the repo.
280292
cloneCtx, cancel := context.WithTimeout(ctx, origin.Spec.Timeout.Duration)
281293
defer cancel()
294+
debuglog.Info("attempting to clone git repository", "gitrepository", originName, "ref", checkoutRef, "working", tmp)
282295
if _, err := gitClient.Clone(cloneCtx, origin.Spec.URL, opts); err != nil {
283296
return failWithError(err)
284297
}
@@ -506,6 +519,31 @@ func intervalOrDefault(auto *imagev1.ImageUpdateAutomation) time.Duration {
506519
return auto.Spec.Interval.Duration
507520
}
508521

522+
func (r *ImageUpdateAutomationReconciler) getGitClientOpts(gitTransport git.TransportType, proxyOpts *transport.ProxyOptions,
523+
diffPushBranch bool) []gogit.ClientOption {
524+
clientOpts := []gogit.ClientOption{gogit.WithDiskStorage()}
525+
if gitTransport == git.HTTP {
526+
clientOpts = append(clientOpts, gogit.WithInsecureCredentialsOverHTTP())
527+
}
528+
529+
if proxyOpts != nil {
530+
clientOpts = append(clientOpts, gogit.WithProxy(*proxyOpts))
531+
}
532+
533+
// If the push branch is different from the checkout ref, we need to
534+
// have all the references downloaded at clone time, to ensure that
535+
// SwitchBranch will have access to the target branch state. fluxcd/flux2#3384
536+
//
537+
// To always overwrite the push branch, the feature gate
538+
// GitAllBranchReferences can be set to false, which will cause
539+
// the SwitchBranch operation to ignore the remote branch state.
540+
allReferences := r.features[features.GitAllBranchReferences]
541+
if diffPushBranch {
542+
clientOpts = append(clientOpts, gogit.WithSingleBranch(!allReferences))
543+
}
544+
return clientOpts
545+
}
546+
509547
// automationsForGitRepo fetches all the automations that refer to a
510548
// particular source.GitRepository object.
511549
func (r *ImageUpdateAutomationReconciler) automationsForGitRepo(ctx context.Context, obj client.Object) []reconcile.Request {
@@ -541,20 +579,17 @@ func (r *ImageUpdateAutomationReconciler) automationsForImagePolicy(ctx context.
541579
return reqs
542580
}
543581

582+
// getAuthOpts fetches the secret containing the auth options (if specified),
583+
// constructs a git.AuthOptions object using those options along with the provided
584+
// repository's URL and returns it.
544585
func (r *ImageUpdateAutomationReconciler) getAuthOpts(ctx context.Context, repository *sourcev1.GitRepository) (*git.AuthOptions, error) {
545586
var data map[string][]byte
587+
var err error
546588
if repository.Spec.SecretRef != nil {
547-
name := types.NamespacedName{
548-
Namespace: repository.GetNamespace(),
549-
Name: repository.Spec.SecretRef.Name,
550-
}
551-
552-
secret := &corev1.Secret{}
553-
err := r.Client.Get(ctx, name, secret)
589+
data, err = r.getSecretData(ctx, repository.Spec.SecretRef.Name, repository.GetNamespace())
554590
if err != nil {
555-
return nil, fmt.Errorf("failed to get secret '%s': %w", name.String(), err)
591+
return nil, fmt.Errorf("failed to get auth secret '%s/%s': %w", repository.GetNamespace(), repository.Spec.SecretRef.Name, err)
556592
}
557-
data = secret.Data
558593
}
559594

560595
u, err := url.Parse(repository.Spec.URL)
@@ -570,36 +605,37 @@ func (r *ImageUpdateAutomationReconciler) getAuthOpts(ctx context.Context, repos
570605
return opts, nil
571606
}
572607

573-
// constructGitClient constructs and returns a new gogit client.
574-
func (r *ImageUpdateAutomationReconciler) constructGitClient(ctx context.Context,
575-
origin *sourcev1.GitRepository, repoDir string, switchBranch bool) (*gogit.Client, error) {
576-
authOpts, err := r.getAuthOpts(ctx, origin)
608+
// getProxyOpts fetches the secret containing the proxy settings, constructs a
609+
// transport.ProxyOptions object using those settings and then returns it.
610+
func (r *ImageUpdateAutomationReconciler) getProxyOpts(ctx context.Context, proxySecretName,
611+
proxySecretNamespace string) (*transport.ProxyOptions, error) {
612+
proxyData, err := r.getSecretData(ctx, proxySecretName, proxySecretNamespace)
577613
if err != nil {
578-
return nil, err
614+
return nil, fmt.Errorf("failed to get proxy secret '%s/%s': %w", proxySecretNamespace, proxySecretName, err)
579615
}
580-
581-
clientOpts := []gogit.ClientOption{gogit.WithDiskStorage()}
582-
if authOpts.Transport == git.HTTP {
583-
clientOpts = append(clientOpts, gogit.WithInsecureCredentialsOverHTTP())
616+
address, ok := proxyData["address"]
617+
if !ok {
618+
return nil, fmt.Errorf("invalid proxy secret '%s/%s': key 'address' is missing", proxySecretNamespace, proxySecretName)
584619
}
585620

586-
// If the push branch is different from the checkout ref, we need to
587-
// have all the references downloaded at clone time, to ensure that
588-
// SwitchBranch will have access to the target branch state. fluxcd/flux2#3384
589-
//
590-
// To always overwrite the push branch, the feature gate
591-
// GitAllBranchReferences can be set to false, which will cause
592-
// the SwitchBranch operation to ignore the remote branch state.
593-
allReferences := r.features[features.GitAllBranchReferences]
594-
if switchBranch {
595-
clientOpts = append(clientOpts, gogit.WithSingleBranch(!allReferences))
621+
proxyOpts := &transport.ProxyOptions{
622+
URL: string(address),
623+
Username: string(proxyData["username"]),
624+
Password: string(proxyData["password"]),
596625
}
626+
return proxyOpts, nil
627+
}
597628

598-
gitClient, err := gogit.NewClient(repoDir, authOpts, clientOpts...)
599-
if err != nil {
629+
func (r *ImageUpdateAutomationReconciler) getSecretData(ctx context.Context, name, namespace string) (map[string][]byte, error) {
630+
key := types.NamespacedName{
631+
Namespace: namespace,
632+
Name: name,
633+
}
634+
var secret corev1.Secret
635+
if err := r.Client.Get(ctx, key, &secret); err != nil {
600636
return nil, err
601637
}
602-
return gitClient, nil
638+
return secret.Data, nil
603639
}
604640

605641
// getSigningEntity retrieves an OpenPGP entity referenced by the

0 commit comments

Comments
 (0)