@@ -83,28 +83,19 @@ func (r *GitRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
83
83
r .gc (repo )
84
84
85
85
// try git clone
86
- readyCondition , artifacts , err := r .sync (repo )
86
+ syncedRepo , err := r .sync (* repo . DeepCopy () )
87
87
if err != nil {
88
88
log .Info ("Repository sync failed" , "error" , err .Error ())
89
- } else {
90
- // update artifacts if commit hash changed
91
- if repo .Status .Artifact != artifacts {
92
- timeNew := metav1 .Now ()
93
- repo .Status .LastUpdateTime = & timeNew
94
- repo .Status .Artifact = artifacts
95
- }
96
- log .Info ("Repository sync succeeded" , "msg" , readyCondition .Message )
97
89
}
98
90
99
91
// update status
100
- readyCondition .LastTransitionTime = metav1 .Now ()
101
- repo .Status .Conditions = []sourcev1.SourceCondition {readyCondition }
102
-
103
- if err := r .Status ().Update (ctx , & repo ); err != nil {
92
+ if err := r .Status ().Update (ctx , & syncedRepo ); err != nil {
104
93
log .Error (err , "unable to update GitRepository status" )
105
94
return result , err
106
95
}
107
96
97
+ log .Info ("Repository sync succeeded" , "msg" , GitRepositoryReadyMessage (syncedRepo ))
98
+
108
99
// requeue repository
109
100
return result , nil
110
101
}
@@ -116,7 +107,7 @@ func (r *GitRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
116
107
WithEventFilter (predicate.Funcs {
117
108
DeleteFunc : func (e event.DeleteEvent ) bool {
118
109
// delete artifacts
119
- artifact := r .Storage .ArtifactFor (r .Kind , e .Meta , "dummy" )
110
+ artifact := r .Storage .ArtifactFor (r .Kind , e .Meta , "dummy" , "" )
120
111
if err := r .Storage .RemoveAll (artifact ); err != nil {
121
112
r .Log .Error (err , "unable to delete artifacts" ,
122
113
r .Kind , fmt .Sprintf ("%s/%s" , e .Meta .GetNamespace (), e .Meta .GetName ()))
@@ -130,9 +121,10 @@ func (r *GitRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
130
121
Complete (r )
131
122
}
132
123
133
- func (r * GitRepositoryReconciler ) sync (repository sourcev1.GitRepository ) (sourcev1.SourceCondition , string , error ) {
124
+ func (r * GitRepositoryReconciler ) sync (repository sourcev1.GitRepository ) (sourcev1.GitRepository , error ) {
134
125
// set defaults: master branch, no tags fetching, max two commits
135
126
branch := "master"
127
+ revision := ""
136
128
tagMode := git .NoTags
137
129
depth := 2
138
130
@@ -159,21 +151,21 @@ func (r *GitRepositoryReconciler) sync(repository sourcev1.GitRepository) (sourc
159
151
tmpSSH , err := ioutil .TempDir ("" , repository .Name )
160
152
if err != nil {
161
153
err = fmt .Errorf ("tmp dir error: %w" , err )
162
- return NotReadyCondition ( sourcev1 .StorageOperationFailedReason , err .Error ()), "" , err
154
+ return NotReadyGitRepository ( repository , sourcev1 .StorageOperationFailedReason , err .Error ()), err
163
155
}
164
156
defer os .RemoveAll (tmpSSH )
165
157
166
158
auth , err := r .auth (repository , tmpSSH )
167
159
if err != nil {
168
160
err = fmt .Errorf ("auth error: %w" , err )
169
- return NotReadyCondition ( sourcev1 .AuthenticationFailedReason , err .Error ()), "" , err
161
+ return NotReadyGitRepository ( repository , sourcev1 .StorageOperationFailedReason , err .Error ()), err
170
162
}
171
163
172
164
// create tmp dir for the Git clone
173
165
tmpGit , err := ioutil .TempDir ("" , repository .Name )
174
166
if err != nil {
175
167
err = fmt .Errorf ("tmp dir error: %w" , err )
176
- return NotReadyCondition ( sourcev1 .StorageOperationFailedReason , err .Error ()), "" , err
168
+ return NotReadyGitRepository ( repository , sourcev1 .StorageOperationFailedReason , err .Error ()), err
177
169
}
178
170
defer os .RemoveAll (tmpGit )
179
171
@@ -192,7 +184,7 @@ func (r *GitRepositoryReconciler) sync(repository sourcev1.GitRepository) (sourc
192
184
})
193
185
if err != nil {
194
186
err = fmt .Errorf ("git clone error: %w" , err )
195
- return NotReadyCondition ( sourcev1 .GitOperationFailedReason , err .Error ()), "" , err
187
+ return NotReadyGitRepository ( repository , sourcev1 .GitOperationFailedReason , err .Error ()), err
196
188
}
197
189
198
190
// checkout commit or tag
@@ -201,7 +193,7 @@ func (r *GitRepositoryReconciler) sync(repository sourcev1.GitRepository) (sourc
201
193
w , err := repo .Worktree ()
202
194
if err != nil {
203
195
err = fmt .Errorf ("git worktree error: %w" , err )
204
- return NotReadyCondition ( sourcev1 .GitOperationFailedReason , err .Error ()), "" , err
196
+ return NotReadyGitRepository ( repository , sourcev1 .GitOperationFailedReason , err .Error ()), err
205
197
}
206
198
207
199
err = w .Checkout (& git.CheckoutOptions {
@@ -210,19 +202,19 @@ func (r *GitRepositoryReconciler) sync(repository sourcev1.GitRepository) (sourc
210
202
})
211
203
if err != nil {
212
204
err = fmt .Errorf ("git checkout %s for %s error: %w" , commit , branch , err )
213
- return NotReadyCondition ( sourcev1 .GitOperationFailedReason , err .Error ()), "" , err
205
+ return NotReadyGitRepository ( repository , sourcev1 .GitOperationFailedReason , err .Error ()), err
214
206
}
215
207
} else if exp := repository .Spec .Reference .SemVer ; exp != "" {
216
208
rng , err := semver .ParseRange (exp )
217
209
if err != nil {
218
210
err = fmt .Errorf ("semver parse range error: %w" , err )
219
- return NotReadyCondition ( sourcev1 .GitOperationFailedReason , err .Error ()), "" , err
211
+ return NotReadyGitRepository ( repository , sourcev1 .GitOperationFailedReason , err .Error ()), err
220
212
}
221
213
222
214
repoTags , err := repo .Tags ()
223
215
if err != nil {
224
216
err = fmt .Errorf ("git list tags error: %w" , err )
225
- return NotReadyCondition ( sourcev1 .GitOperationFailedReason , err .Error ()), "" , err
217
+ return NotReadyGitRepository ( repository , sourcev1 .GitOperationFailedReason , err .Error ()), err
226
218
}
227
219
228
220
tags := make (map [string ]string )
@@ -246,23 +238,24 @@ func (r *GitRepositoryReconciler) sync(repository sourcev1.GitRepository) (sourc
246
238
v := svers [len (svers )- 1 ]
247
239
t := svTags [v .String ()]
248
240
commit := tags [t ]
241
+ revision = fmt .Sprintf ("%s/%s" , t , commit )
249
242
250
243
w , err := repo .Worktree ()
251
244
if err != nil {
252
245
err = fmt .Errorf ("git worktree error: %w" , err )
253
- return NotReadyCondition ( sourcev1 .GitOperationFailedReason , err .Error ()), "" , err
246
+ return NotReadyGitRepository ( repository , sourcev1 .GitOperationFailedReason , err .Error ()), err
254
247
}
255
248
256
249
err = w .Checkout (& git.CheckoutOptions {
257
250
Hash : plumbing .NewHash (commit ),
258
251
})
259
252
if err != nil {
260
253
err = fmt .Errorf ("git checkout error: %w" , err )
261
- return NotReadyCondition ( sourcev1 .GitOperationFailedReason , err .Error ()), "" , err
254
+ return NotReadyGitRepository ( repository , sourcev1 .GitOperationFailedReason , err .Error ()), err
262
255
}
263
256
} else {
264
257
err = fmt .Errorf ("no match found for semver: %s" , repository .Spec .Reference .SemVer )
265
- return NotReadyCondition ( sourcev1 .GitOperationFailedReason , err .Error ()), "" , err
258
+ return NotReadyGitRepository ( repository , sourcev1 .GitOperationFailedReason , err .Error ()), err
266
259
}
267
260
}
268
261
}
@@ -271,51 +264,53 @@ func (r *GitRepositoryReconciler) sync(repository sourcev1.GitRepository) (sourc
271
264
ref , err := repo .Head ()
272
265
if err != nil {
273
266
err = fmt .Errorf ("git resolve HEAD error: %w" , err )
274
- return NotReadyCondition (sourcev1 .GitOperationFailedReason , err .Error ()), "" , err
267
+ return NotReadyGitRepository (repository , sourcev1 .GitOperationFailedReason , err .Error ()), err
268
+ }
269
+
270
+ if revision == "" {
271
+ revision = fmt .Sprintf ("%s/%s" , branch , ref .Hash ().String ())
275
272
}
276
273
277
274
artifact := r .Storage .ArtifactFor (r .Kind , repository .ObjectMeta .GetObjectMeta (),
278
- fmt .Sprintf ("%s.tar.gz" , ref .Hash ().String ()))
275
+ fmt .Sprintf ("%s.tar.gz" , ref .Hash ().String ()), revision )
279
276
280
277
// create artifact dir
281
278
err = r .Storage .MkdirAll (artifact )
282
279
if err != nil {
283
280
err = fmt .Errorf ("mkdir dir error: %w" , err )
284
- return NotReadyCondition ( sourcev1 .StorageOperationFailedReason , err .Error ()), "" , err
281
+ return NotReadyGitRepository ( repository , sourcev1 .StorageOperationFailedReason , err .Error ()), err
285
282
}
286
283
287
284
// acquire lock
288
285
unlock , err := r .Storage .Lock (artifact )
289
286
if err != nil {
290
287
err = fmt .Errorf ("unable to acquire lock: %w" , err )
291
- return NotReadyCondition ( sourcev1 .StorageOperationFailedReason , err .Error ()), "" , err
288
+ return NotReadyGitRepository ( repository , sourcev1 .StorageOperationFailedReason , err .Error ()), err
292
289
}
293
290
defer unlock ()
294
291
295
292
// archive artifact
296
293
err = r .Storage .Archive (artifact , tmpGit , "" )
297
294
if err != nil {
298
295
err = fmt .Errorf ("storage archive error: %w" , err )
299
- return NotReadyCondition ( sourcev1 .StorageOperationFailedReason , err .Error ()), "" , err
296
+ return NotReadyGitRepository ( repository , sourcev1 .StorageOperationFailedReason , err .Error ()), err
300
297
}
301
298
302
299
// update latest symlink
303
- err = r .Storage .Symlink (artifact , "latest.tar.gz" )
300
+ url , err : = r .Storage .Symlink (artifact , "latest.tar.gz" )
304
301
if err != nil {
305
302
err = fmt .Errorf ("storage lock error: %w" , err )
306
- return NotReadyCondition ( sourcev1 .StorageOperationFailedReason , err .Error ()), "" , err
303
+ return NotReadyGitRepository ( repository , sourcev1 .StorageOperationFailedReason , err .Error ()), err
307
304
}
308
305
309
306
message := fmt .Sprintf ("Artifact is available at: %s" , artifact .Path )
310
- return ReadyCondition ( sourcev1 .GitOperationSucceedReason , message ), artifact . URL , nil
307
+ return ReadyGitRepository ( repository , artifact , url , sourcev1 .GitOperationSucceedReason , message ), nil
311
308
}
312
309
313
310
func (r * GitRepositoryReconciler ) shouldResetStatus (repository sourcev1.GitRepository ) (bool , sourcev1.GitRepositoryStatus ) {
314
311
resetStatus := false
315
- if repository .Status .Artifact != "" {
316
- parts := strings .Split (repository .Status .Artifact , "/" )
317
- artifact := r .Storage .ArtifactFor (r .Kind , repository .ObjectMeta .GetObjectMeta (), parts [len (parts )- 1 ])
318
- if ! r .Storage .ArtifactExist (artifact ) {
312
+ if repository .Status .Artifact != nil {
313
+ if ! r .Storage .ArtifactExist (* repository .Status .Artifact ) {
319
314
resetStatus = true
320
315
}
321
316
}
@@ -338,10 +333,8 @@ func (r *GitRepositoryReconciler) shouldResetStatus(repository sourcev1.GitRepos
338
333
}
339
334
340
335
func (r * GitRepositoryReconciler ) gc (repository sourcev1.GitRepository ) {
341
- if repository .Status .Artifact != "" {
342
- parts := strings .Split (repository .Status .Artifact , "/" )
343
- artifact := r .Storage .ArtifactFor (r .Kind , repository .ObjectMeta .GetObjectMeta (), parts [len (parts )- 1 ])
344
- if err := r .Storage .RemoveAllButCurrent (artifact ); err != nil {
336
+ if repository .Status .Artifact != nil {
337
+ if err := r .Storage .RemoveAllButCurrent (* repository .Status .Artifact ); err != nil {
345
338
r .Log .Info ("Artifacts GC failed" , "error" , err )
346
339
}
347
340
}
@@ -370,9 +363,13 @@ func (r *GitRepositoryReconciler) auth(repository sourcev1.GitRepository, tmp st
370
363
auth := & http.BasicAuth {}
371
364
if username , ok := credentials ["username" ]; ok {
372
365
auth .Username = string (username )
366
+ } else {
367
+ return nil , fmt .Errorf ("%s secret does not contain username" , repository .Spec .SecretRef .Name )
373
368
}
374
369
if password , ok := credentials ["password" ]; ok {
375
370
auth .Password = string (password )
371
+ } else {
372
+ return nil , fmt .Errorf ("%s secret does not contain password" , repository .Spec .SecretRef .Name )
376
373
}
377
374
378
375
if auth .Username == "" || auth .Password == "" {
0 commit comments