Skip to content

Commit 1b4eacc

Browse files
author
Philip Laine
committed
Refactor argument name to enable git2go
Signed-off-by: Philip Laine <[email protected]>
1 parent cb12736 commit 1b4eacc

File tree

8 files changed

+72
-45
lines changed

8 files changed

+72
-45
lines changed

api/v1beta1/gitrepository_types.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import (
2626
const (
2727
// GitRepositoryKind is the string representation of a GitRepository.
2828
GitRepositoryKind = "GitRepository"
29+
// Represents the go-git git implementation kind
30+
GoGitImplementation = "go-git"
31+
// Represents the gi2go git implementation kind
32+
Git2GoImplementation = "git2go"
2933
)
3034

3135
// GitRepositorySpec defines the desired state of a Git repository.
@@ -71,9 +75,12 @@ type GitRepositorySpec struct {
7175
// +optional
7276
Suspend bool `json:"suspend,omitempty"`
7377

74-
// Enables support for git servers that require v2.
78+
// Determines which git client library to use.
79+
// Defaults to go-git, valid values are ('go-git', 'git2go').
80+
// +kubebuilder:validation:Enum=go-git;git2go
81+
// +kubebuilder:default:=go-git
7582
// +optional
76-
GitProtocolV2Compatibility bool `json:"gitProtocolV2Compatibility"`
83+
GitImplementation string `json:"gitImplementation,omitempty"`
7784
}
7885

7986
// GitRepositoryRef defines the Git ref used for pull and checkout operations.

config/crd/bases/source.toolkit.fluxcd.io_gitrepositories.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ spec:
4949
spec:
5050
description: GitRepositorySpec defines the desired state of a Git repository.
5151
properties:
52-
gitProtocolV2Compatibility:
53-
description: Enables support for git servers that require v2.
54-
type: boolean
52+
gitImplementation:
53+
default: go-git
54+
description: Determines which git client library to use. Defaults
55+
to go-git, valid values are ('go-git', 'git2go').
56+
enum:
57+
- go-git
58+
- git2go
59+
type: string
5560
ignore:
5661
description: Ignore overrides the set of excluded patterns in the
5762
.sourceignore format (which is the same as .gitignore). If not provided,

controllers/gitrepository_controller.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
183183
// determine auth method
184184
auth := &common.Auth{}
185185
if repository.Spec.SecretRef != nil {
186-
authStrategy, err := git.AuthSecretStrategyForURL(repository.Spec.URL, repository.Spec.GitProtocolV2Compatibility)
186+
authStrategy, err := git.AuthSecretStrategyForURL(repository.Spec.URL, repository.Spec.GitImplementation)
187187
if err != nil {
188188
return sourcev1.GitRepositoryNotReady(repository, sourcev1.AuthenticationFailedReason, err.Error()), err
189189
}
@@ -207,7 +207,10 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
207207
}
208208
}
209209

210-
checkoutStrategy := git.CheckoutStrategyForRef(repository.Spec.Reference, repository.Spec.GitProtocolV2Compatibility)
210+
checkoutStrategy, err := git.CheckoutStrategyForRef(repository.Spec.Reference, repository.Spec.GitImplementation)
211+
if err != nil {
212+
return sourcev1.GitRepositoryNotReady(repository, sourcev1.GitOperationFailedReason, err.Error()), err
213+
}
211214
commit, revision, err := checkoutStrategy.Checkout(ctx, tmpGit, repository.Spec.URL, auth)
212215
if err != nil {
213216
return sourcev1.GitRepositoryNotReady(repository, sourcev1.GitOperationFailedReason, err.Error()), err

controllers/gitrepository_controller_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ var _ = Describe("GitRepositoryReconciler", func() {
8787
expectMessage string
8888
expectRevision string
8989

90-
v2 bool
90+
gitImplementation string
9191
}
9292

9393
DescribeTable("Git references tests", func(t refTestCase) {
@@ -284,10 +284,10 @@ var _ = Describe("GitRepositoryReconciler", func() {
284284
Namespace: key.Namespace,
285285
},
286286
Spec: sourcev1.GitRepositorySpec{
287-
URL: u.String(),
288-
Interval: metav1.Duration{Duration: indexInterval},
289-
Reference: t.reference,
290-
GitProtocolV2Compatibility: t.v2,
287+
URL: u.String(),
288+
Interval: metav1.Duration{Duration: indexInterval},
289+
Reference: t.reference,
290+
GitImplementation: t.gitImplementation,
291291
},
292292
}
293293
Expect(k8sClient.Create(context.Background(), created)).Should(Succeed())
@@ -317,11 +317,11 @@ var _ = Describe("GitRepositoryReconciler", func() {
317317
expectMessage: "x509: certificate signed by unknown authority",
318318
}),
319319
Entry("self signed v2", refTestCase{
320-
reference: &sourcev1.GitRepositoryRef{Branch: "main"},
321-
waitForReason: sourcev1.GitOperationFailedReason,
322-
expectStatus: metav1.ConditionFalse,
323-
expectMessage: "error: user rejected certificate",
324-
v2: true,
320+
reference: &sourcev1.GitRepositoryRef{Branch: "main"},
321+
waitForReason: sourcev1.GitOperationFailedReason,
322+
expectStatus: metav1.ConditionFalse,
323+
expectMessage: "error: user rejected certificate",
324+
gitImplementation: sourcev1.Git2GoImplementation,
325325
}),
326326
)
327327
})

docs/api/source.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,15 @@ bool
389389
</tr>
390390
<tr>
391391
<td>
392-
<code>gitProtocolV2Compatibility</code><br>
392+
<code>gitImplementation</code><br>
393393
<em>
394-
bool
394+
string
395395
</em>
396396
</td>
397397
<td>
398398
<em>(Optional)</em>
399-
<p>Enables support for git servers that require v2.</p>
399+
<p>Determines which git client library to use.
400+
Defaults to go-git, valid values are (&lsquo;go-git&rsquo;, &lsquo;git2go&rsquo;).</p>
400401
</td>
401402
</tr>
402403
</table>
@@ -1234,14 +1235,15 @@ bool
12341235
</tr>
12351236
<tr>
12361237
<td>
1237-
<code>gitProtocolV2Compatibility</code><br>
1238+
<code>gitImplementation</code><br>
12381239
<em>
1239-
bool
1240+
string
12401241
</em>
12411242
</td>
12421243
<td>
12431244
<em>(Optional)</em>
1244-
<p>Enables support for git servers that require v2.</p>
1245+
<p>Determines which git client library to use.
1246+
Defaults to go-git, valid values are (&lsquo;go-git&rsquo;, &lsquo;git2go&rsquo;).</p>
12451247
</td>
12461248
</tr>
12471249
</tbody>

docs/spec/v1beta1/gitrepositories.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ type GitRepositorySpec struct {
5151
// +optional
5252
Suspend bool `json:"suspend,omitempty"`
5353

54-
// Enables support for git servers that require v2.
54+
// Determines which git client library to use.
55+
// Defaults to go-git, valid values are ('go-git', 'git2go').
56+
// +kubebuilder:validation:Enum=go-git;git2go
57+
// +kubebuilder:default:=go-git
5558
// +optional
56-
GitProtocolV2Compatibility bool `json:"gitProtocolV2Compatibility"`
59+
GitImplementation string `json:"gitImplementation,omitempty"`
5760
}
5861
```
5962

@@ -174,12 +177,13 @@ spec:
174177
175178
When specified, `spec.ignore` overrides the default exclusion list.
176179

177-
## Git V2
180+
## Git Implementation
178181

179-
You should skip this unless you know that you need v2 compatibility. Enabling
180-
this feature comes with its own set of drawbacks.
182+
You can skip this section unless you know that you need support for either
183+
specific git wire protocol functionality. Changing the git implementation
184+
comes with its own set of drawbacks.
181185

182-
Some git providers like Azure DevOps require special features in the users git client
186+
Some git providers like Azure DevOps require that the git client supports specific capabilities
183187
to be able to communicate. The initial library used in source-controller did not support
184188
this functionality while other libraries that did were missinging other critical functionality,
185189
specifically the ability to do shallow cloning. Shallow cloning is important as it allows
@@ -188,12 +192,12 @@ For some very large repositories this means downloading GB of data that could fi
188192
and also impact the traffic costs.
189193

190194
To be able to support Azure DevOps a compromise solution was built, giving the user the
191-
option to enable V2 compatibility with the drawbacks it brings.
195+
option to select the git library while accepting the drawbacks.
192196

193-
| V2 Compatibility | Shallow Clones | Azure DevOps Support |
197+
| Git Implementation | Shallow Clones | Azure DevOps Support |
194198
|---|---|---|
195-
| false | true | false |
196-
| true | false | true |
199+
| 'go-git' | true | false |
200+
| 'git2go' | false | true |
197201

198202
Pull the master branch from a repository in Azure DevOps.
199203

@@ -206,7 +210,7 @@ metadata:
206210
spec:
207211
interval: 1m
208212
url: https://dev.azure.com/org/proj/_git/repo
209-
gitProtocolV2Compatibility: true
213+
gitImplementation: git2go
210214
```
211215

212216
## Spec examples

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
224224
github.com/fluxcd/pkg/apis/meta v0.3.0/go.mod h1:wOzQQx8CdtUQCGaLzqGu4QgnNxYkI6/wvdvlovxWhF0=
225225
github.com/fluxcd/pkg/apis/meta v0.4.0 h1:JChqB9GGgorW9HWKxirTVV0rzrcLyzBaVjinmqZ0iHA=
226226
github.com/fluxcd/pkg/apis/meta v0.4.0/go.mod h1:wOzQQx8CdtUQCGaLzqGu4QgnNxYkI6/wvdvlovxWhF0=
227-
github.com/fluxcd/pkg/gittestserver v0.0.3-0.20201202222244-96033b836a6a h1:fmfbt5VrEPUb4X0UI14a0K2FWr0iv/NRUadk8X35byc=
228-
github.com/fluxcd/pkg/gittestserver v0.0.3-0.20201202222244-96033b836a6a/go.mod h1:HWZaoib03fQeSsauCAN2iAFdr6bnjKQ+CFxMFD2mwDY=
229227
github.com/fluxcd/pkg/gittestserver v0.1.0 h1:BvIG+bBhgbmqhtpSS2qUpOXRIL1P1Ow2jauloH8X86U=
230228
github.com/fluxcd/pkg/gittestserver v0.1.0/go.mod h1:HWZaoib03fQeSsauCAN2iAFdr6bnjKQ+CFxMFD2mwDY=
231229
github.com/fluxcd/pkg/helmtestserver v0.0.1 h1:8RcLZdg7Zr9ZqyijsIIASjjMXQtF4UWP4Uds4iK2VJM=

pkg/git/git.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package git
1818

1919
import (
20+
"fmt"
21+
2022
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
2123
"github.com/fluxcd/source-controller/pkg/git/common"
2224
gitv1 "github.com/fluxcd/source-controller/pkg/git/v1"
@@ -27,18 +29,24 @@ const (
2729
defaultBranch = "master"
2830
)
2931

30-
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, useGitV2 bool) common.CheckoutStrategy {
31-
if useGitV2 {
32-
return gitv2.CheckoutStrategyForRef(ref)
32+
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, gitImplementation string) (common.CheckoutStrategy, error) {
33+
switch gitImplementation {
34+
case sourcev1.GoGitImplementation:
35+
return gitv1.CheckoutStrategyForRef(ref), nil
36+
case sourcev1.Git2GoImplementation:
37+
return gitv2.CheckoutStrategyForRef(ref), nil
38+
default:
39+
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
3340
}
34-
35-
return gitv1.CheckoutStrategyForRef(ref)
3641
}
3742

38-
func AuthSecretStrategyForURL(url string, useGitV2 bool) (common.AuthSecretStrategy, error) {
39-
if useGitV2 {
43+
func AuthSecretStrategyForURL(url string, gitImplementation string) (common.AuthSecretStrategy, error) {
44+
switch gitImplementation {
45+
case sourcev1.GoGitImplementation:
46+
return gitv1.AuthSecretStrategyForURL(url)
47+
case sourcev1.Git2GoImplementation:
4048
return gitv2.AuthSecretStrategyForURL(url)
49+
default:
50+
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
4151
}
42-
43-
return gitv1.AuthSecretStrategyForURL(url)
4452
}

0 commit comments

Comments
 (0)