Skip to content

Commit dac3c33

Browse files
authored
Merge pull request #369 from pjbgf/libgit2-cacheless-conns
2 parents d89eb19 + ebc40b2 commit dac3c33

File tree

9 files changed

+190
-98
lines changed

9 files changed

+190
-98
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ WORKDIR /workspace
7272
COPY main.go main.go
7373
COPY controllers/ controllers/
7474
COPY pkg/ pkg/
75+
COPY internal/ internal/
7576

7677
COPY --from=musl-tool-chain /workspace/build /workspace/build
7778

api/go.mod

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

55
require (
66
github.com/fluxcd/pkg/apis/meta v0.14.1
7-
github.com/fluxcd/source-controller/api v0.24.4
7+
github.com/fluxcd/source-controller/api v0.25.0
88
k8s.io/apimachinery v0.24.0
99
sigs.k8s.io/controller-runtime v0.11.2
1010
)

api/go.sum

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ github.com/fluxcd/pkg/apis/acl v0.0.3 h1:Lw0ZHdpnO4G7Zy9KjrzwwBmDZQuy4qEjaU/RvA6
2121
github.com/fluxcd/pkg/apis/acl v0.0.3/go.mod h1:XPts6lRJ9C9fIF9xVWofmQwftvhY25n1ps7W9xw0XLU=
2222
github.com/fluxcd/pkg/apis/meta v0.14.1 h1:lPDs9yV67DnwalHPb13bbnDkAatALfUiAMRHjUm4UBw=
2323
github.com/fluxcd/pkg/apis/meta v0.14.1/go.mod h1:1uJkTJGSZWrZxL5PFpx1IxGLrFmT1Cd0C2fFWrbv77I=
24-
github.com/fluxcd/source-controller/api v0.24.4 h1:m54sS1rJlgJf5j9qDRgKLhbPJAnJ9dY+VrstPKj0aQo=
25-
github.com/fluxcd/source-controller/api v0.24.4/go.mod h1:b0MmMPGE8gcpgSyGXe5m7see77tBW26eZrvGkkPstUs=
24+
github.com/fluxcd/source-controller/api v0.25.0 h1:+uL+hQb/6h2MHuE9/Iq054TrDWF70puAuWBcoBrZK5M=
25+
github.com/fluxcd/source-controller/api v0.25.0/go.mod h1:tuMrqHHpRt7mxdLeRXGIMtTKAMufLwLTm5uXkEOJWFw=
2626
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
2727
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
2828
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
@@ -231,7 +231,7 @@ gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
231231
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
232232
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
233233
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
234-
k8s.io/api v0.23.5 h1:zno3LUiMubxD/V1Zw3ijyKO3wxrhbUF1Ck+VjBvfaoA=
234+
k8s.io/api v0.24.0 h1:J0hann2hfxWr1hinZIDefw7Q96wmCBx6SSB8IY0MdDg=
235235
k8s.io/apimachinery v0.24.0 h1:ydFCyC/DjCvFCHK5OPMKBlxayQytB8pxy8YQInd5UyQ=
236236
k8s.io/apimachinery v0.24.0/go.mod h1:82Bi4sCzVBdpYjyI4jY6aHX+YCUchUIrZrXKedjd2UM=
237237
k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=

controllers/imageupdateautomation_controller.go

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -250,31 +250,20 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
250250
return failWithError(err)
251251
}
252252

253-
repositoryURL := origin.Spec.URL
253+
// managed GIT transport only affects the libgit2 implementation
254254
if managed.Enabled() {
255-
// At present only HTTP connections have the ability to define remote options.
256-
// Although this can be easily extended by ensuring that the fake URL below uses the
257-
// target ssh scheme, and the libgit2/managed/ssh.go pulls that information accordingly.
258-
//
259-
// This is due to the fact the key libgit2 remote callbacks do not take place for HTTP
260-
// whilst most still work for SSH.
261-
if strings.HasPrefix(repositoryURL, "http") {
262-
if access.auth != nil && len(access.auth.CAFile) > 0 {
263-
// Due to the lack of the callback feature, a fake target URL is created to allow
264-
// for the smart sub transport be able to pick the options specific for this
265-
// GitRepository object.
266-
// The URL should use unique information that do not collide in a multi tenant
267-
// deployment.
268-
repositoryURL = fmt.Sprintf("http://%s/%s/%d", auto.Name, auto.UID, auto.Generation)
269-
managed.AddTransportOptions(repositoryURL,
270-
managed.TransportOptions{
271-
TargetURL: repositoryURL,
272-
CABundle: access.auth.CAFile,
273-
})
274-
275-
// We remove the options from memory, to avoid accumulating unused options over time.
276-
defer managed.RemoveTransportOptions(repositoryURL)
277-
}
255+
// We set the TransportOptionsURL of this set of authentication options here by constructing
256+
// a unique URL that won't clash in a multi tenant environment. This unique URL is used by
257+
// libgit2 managed transports. This enables us to bypass the inbuilt credentials callback in
258+
// libgit2, which is inflexible and unstable.
259+
// NB: The Transport Options URL must be unique, therefore it must use the object under
260+
// reconciliation details, instead of the repository it depends on.
261+
if strings.HasPrefix(origin.Spec.URL, "http") {
262+
access.auth.TransportOptionsURL = fmt.Sprintf("http://%s/%s/%d", auto.Name, auto.UID, auto.Generation)
263+
} else if strings.HasPrefix(origin.Spec.URL, "ssh") {
264+
access.auth.TransportOptionsURL = fmt.Sprintf("ssh://%s/%s/%d", auto.Name, auto.UID, auto.Generation)
265+
} else {
266+
return failWithError(fmt.Errorf("git repository URL '%s' has invalid transport type, supported types are: http, https, ssh", origin.Spec.URL))
278267
}
279268
}
280269

@@ -287,6 +276,20 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
287276
}
288277
defer repo.Free()
289278

279+
if managed.Enabled() {
280+
// Checkout removes TransportOptions before returning, therefore this
281+
// must happen after cloneInto.
282+
// TODO(pjbgf): Git consolidation should improve the API workflow.
283+
managed.AddTransportOptions(access.auth.TransportOptionsURL, managed.TransportOptions{
284+
TargetURL: origin.Spec.URL,
285+
AuthOpts: access.auth,
286+
ProxyOptions: &libgit2.ProxyOptions{Type: libgit2.ProxyTypeAuto},
287+
Context: cloneCtx,
288+
})
289+
290+
defer managed.RemoveTransportOptions(access.auth.TransportOptionsURL)
291+
}
292+
290293
// When there's a push spec, the pushed-to branch is where commits
291294
// shall be made
292295

@@ -732,7 +735,28 @@ var errRemoteBranchMissing = errors.New("remote branch missing")
732735
// switchToBranch switches to a branch after fetching latest from upstream.
733736
// If the branch does not exist, it is created using the head as the starting point.
734737
func switchToBranch(repo *libgit2.Repository, ctx context.Context, branch string, access repoAccess) error {
738+
origin, err := repo.Remotes.Lookup(originRemote)
739+
if err != nil {
740+
return fmt.Errorf("cannot lookup remote: %w", err)
741+
}
742+
defer origin.Free()
743+
744+
callbacks := access.remoteCallbacks(ctx)
745+
if managed.Enabled() {
746+
// Override callbacks with dummy ones as they are not needed within Managed Transport.
747+
// However, not setting them may lead to git2go panicing.
748+
callbacks = managed.RemoteCallbacks()
749+
}
750+
735751
branchRef := fmt.Sprintf("origin/%s", branch)
752+
// Force the fetching of the remote branch.
753+
err = origin.Fetch([]string{branch}, &libgit2.FetchOptions{
754+
RemoteCallbacks: callbacks,
755+
}, "")
756+
if err != nil {
757+
return fmt.Errorf("cannot fetch remote branch: %w", err)
758+
}
759+
736760
remoteBranch, err := repo.LookupBranch(branchRef, libgit2.BranchRemote)
737761
if err != nil && !libgit2.IsErrorCode(err, libgit2.ErrorCodeNotFound) {
738762
return err
@@ -806,6 +830,11 @@ func push(ctx context.Context, path, branch string, access repoAccess) error {
806830
defer origin.Free()
807831

808832
callbacks := access.remoteCallbacks(ctx)
833+
if managed.Enabled() {
834+
// Override callbacks with dummy ones as they are not needed within Managed Transport.
835+
// However, not setting them may lead to git2go panicing.
836+
callbacks = managed.RemoteCallbacks()
837+
}
809838

810839
// calling repo.Push will succeed even if a reference update is
811840
// rejected; to detect this case, this callback is supplied.

controllers/update_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ func createSSHIdentitySecret(kClient client.Client, name, namespace, repoURL str
15931593
if err != nil {
15941594
return err
15951595
}
1596-
knownhosts, err := ssh.ScanHostKey(url.Host, 5*time.Second)
1596+
knownhosts, err := ssh.ScanHostKey(url.Host, 5*time.Second, []string{}, false)
15971597
if err != nil {
15981598
return err
15991599
}

go.mod

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ require (
1212
github.com/fluxcd/image-reflector-controller/api v0.18.0
1313
github.com/fluxcd/pkg/apis/acl v0.0.3
1414
github.com/fluxcd/pkg/apis/meta v0.14.1
15-
github.com/fluxcd/pkg/gittestserver v0.5.2
15+
github.com/fluxcd/pkg/gittestserver v0.5.3
1616
github.com/fluxcd/pkg/runtime v0.16.1
17-
github.com/fluxcd/pkg/ssh v0.3.2
18-
github.com/fluxcd/source-controller v0.24.4
19-
github.com/fluxcd/source-controller/api v0.24.4
17+
github.com/fluxcd/pkg/ssh v0.4.1
18+
github.com/fluxcd/source-controller v0.25.0
19+
github.com/fluxcd/source-controller/api v0.25.0
2020
github.com/go-logr/logr v1.2.3
2121
github.com/google/go-containerregistry v0.9.0
2222
github.com/libgit2/git2go/v33 v33.0.9
@@ -31,10 +31,6 @@ require (
3131
sigs.k8s.io/kustomize/kyaml v0.13.7
3232
)
3333

34-
require github.com/sosedoff/gitkit v0.3.0 // indirect
35-
36-
replace github.com/sosedoff/gitkit => github.com/fluxcd/gitkit v0.5.1
37-
3834
// Fix CVE-2022-28948
3935
replace gopkg.in/yaml.v3 => gopkg.in/yaml.v3 v3.0.0
4036

@@ -51,8 +47,9 @@ require (
5147
github.com/cespare/xxhash/v2 v2.1.2 // indirect
5248
github.com/davecgh/go-spew v1.1.1 // indirect
5349
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
54-
github.com/emirpasic/gods v1.12.0 // indirect
50+
github.com/emirpasic/gods v1.18.1 // indirect
5551
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
52+
github.com/fluxcd/gitkit v0.5.1 // indirect
5653
github.com/fluxcd/pkg/gitutil v0.1.0 // indirect
5754
github.com/fluxcd/pkg/version v0.1.0 // indirect
5855
github.com/fsnotify/fsnotify v1.5.1 // indirect
@@ -73,14 +70,17 @@ require (
7370
github.com/google/gofuzz v1.2.0 // indirect
7471
github.com/google/uuid v1.3.0 // indirect
7572
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
73+
github.com/hashicorp/go-hclog v0.12.0 // indirect
7674
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
7775
github.com/huandu/xstrings v1.3.2 // indirect
7876
github.com/imdario/mergo v0.3.12 // indirect
77+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
7978
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
8079
github.com/josharian/intern v1.0.0 // indirect
8180
github.com/json-iterator/go v1.1.12 // indirect
82-
github.com/kevinburke/ssh_config v1.1.0 // indirect
81+
github.com/kevinburke/ssh_config v1.2.0 // indirect
8382
github.com/mailru/easyjson v0.7.6 // indirect
83+
github.com/matryer/is v1.4.0 // indirect
8484
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
8585
github.com/mitchellh/copystructure v1.2.0 // indirect
8686
github.com/mitchellh/go-homedir v1.1.0 // indirect
@@ -91,22 +91,23 @@ require (
9191
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
9292
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
9393
github.com/pkg/errors v0.9.1 // indirect
94-
github.com/prometheus/client_golang v1.12.1 // indirect
94+
github.com/prometheus/client_golang v1.12.2 // indirect
9595
github.com/prometheus/client_model v0.2.0 // indirect
9696
github.com/prometheus/common v0.32.1 // indirect
9797
github.com/prometheus/procfs v0.7.3 // indirect
9898
github.com/sergi/go-diff v1.2.0 // indirect
9999
github.com/shopspring/decimal v1.2.0 // indirect
100100
github.com/spf13/cast v1.4.1 // indirect
101+
github.com/spf13/cobra v1.4.0 // indirect
101102
github.com/xanzy/ssh-agent v0.3.1 // indirect
102103
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
103104
go.uber.org/atomic v1.7.0 // indirect
104105
go.uber.org/multierr v1.6.0 // indirect
105106
go.uber.org/zap v1.21.0 // indirect
106107
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
107-
golang.org/x/net v0.0.0-20220516155154-20f960328961 // indirect
108+
golang.org/x/net v0.0.0-20220524220425-1d687d428aca // indirect
108109
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
109-
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect
110+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
110111
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
111112
golang.org/x/text v0.3.7 // indirect
112113
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
@@ -117,11 +118,11 @@ require (
117118
gopkg.in/warnings.v0 v0.1.2 // indirect
118119
gopkg.in/yaml.v2 v2.4.0 // indirect
119120
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
120-
k8s.io/apiextensions-apiserver v0.23.5 // indirect
121+
k8s.io/apiextensions-apiserver v0.24.0 // indirect
121122
k8s.io/component-base v0.24.0 // indirect
122123
k8s.io/klog/v2 v2.60.1 // indirect
123124
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
124-
sigs.k8s.io/cli-utils v0.30.0 // indirect
125+
sigs.k8s.io/cli-utils v0.31.1 // indirect
125126
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
126127
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
127128
sigs.k8s.io/yaml v1.3.0 // indirect

0 commit comments

Comments
 (0)