Skip to content

Commit ebc40b2

Browse files
Paulo Gomesstefanprodan
authored andcommitted
Fix regression on push branches
Fixes regression in which IAC will fail to update push branch when the push branch already exists and checkout branch is ahead. The reconciliation errors with: 'cannot push because a reference that you are trying to update on the remote contains commits that are not present locally.' Regression introduced on #330. Signed-off-by: Paulo Gomes <[email protected]>
1 parent 4f04466 commit ebc40b2

File tree

5 files changed

+80
-72
lines changed

5 files changed

+80
-72
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.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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,12 @@ var errRemoteBranchMissing = errors.New("remote branch missing")
735735
// switchToBranch switches to a branch after fetching latest from upstream.
736736
// If the branch does not exist, it is created using the head as the starting point.
737737
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+
738744
callbacks := access.remoteCallbacks(ctx)
739745
if managed.Enabled() {
740746
// Override callbacks with dummy ones as they are not needed within Managed Transport.
@@ -743,6 +749,14 @@ func switchToBranch(repo *libgit2.Repository, ctx context.Context, branch string
743749
}
744750

745751
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+
746760
remoteBranch, err := repo.LookupBranch(branchRef, libgit2.BranchRemote)
747761
if err != nil && !libgit2.IsErrorCode(err, libgit2.ErrorCodeNotFound) {
748762
return err

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)