Skip to content

Commit acda998

Browse files
darkowlzzhiddeco
authored andcommitted
gitrepo: Use commit msg in NewArtifact message
Use commit message in the NewArtifact event message to make it more user friendly. Signed-off-by: Sunny <[email protected]>
1 parent 45df2d7 commit acda998

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

controllers/gitrepository_controller.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ type GitRepositoryReconcilerOptions struct {
107107

108108
// gitRepoReconcilerFunc is the function type for all the Git repository
109109
// reconciler functions.
110-
type gitRepoReconcilerFunc func(ctx context.Context, obj *sourcev1.GitRepository, artifact *sourcev1.Artifact, includes *artifactSet, dir string) (sreconcile.Result, error)
110+
type gitRepoReconcilerFunc func(ctx context.Context, obj *sourcev1.GitRepository, commit *git.Commit, includes *artifactSet, dir string) (sreconcile.Result, error)
111111

112112
func (r *GitRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
113113
return r.SetupWithManagerAndOptions(mgr, GitRepositoryReconcilerOptions{})
@@ -207,7 +207,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, obj *sourcev1.G
207207
conditions.MarkReconciling(obj, "NewGeneration", "reconciling new object generation (%d)", obj.Generation)
208208
}
209209

210-
var artifact sourcev1.Artifact
210+
var commit git.Commit
211211
var includes artifactSet
212212

213213
// Create temp dir for Git clone
@@ -224,7 +224,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, obj *sourcev1.G
224224
var res sreconcile.Result
225225
var resErr error
226226
for _, rec := range reconcilers {
227-
recResult, err := rec(ctx, obj, &artifact, &includes, tmpDir)
227+
recResult, err := rec(ctx, obj, &commit, &includes, tmpDir)
228228
// Exit immediately on ResultRequeue.
229229
if recResult == sreconcile.ResultRequeue {
230230
return sreconcile.ResultRequeue, nil
@@ -248,7 +248,8 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, obj *sourcev1.G
248248
// If the artifact in the Status object of the resource disappeared from storage, it is removed from the object.
249249
// If the object does not have an artifact in its Status object, a v1beta1.ArtifactUnavailableCondition is set.
250250
// If the hostname of any of the URLs on the object do not match the current storage server hostname, they are updated.
251-
func (r *GitRepositoryReconciler) reconcileStorage(ctx context.Context, obj *sourcev1.GitRepository, artifact *sourcev1.Artifact, includes *artifactSet, dir string) (sreconcile.Result, error) {
251+
func (r *GitRepositoryReconciler) reconcileStorage(ctx context.Context,
252+
obj *sourcev1.GitRepository, _ *git.Commit, includes *artifactSet, dir string) (sreconcile.Result, error) {
252253
// Garbage collect previous advertised artifact(s) from storage
253254
_ = r.garbageCollect(ctx, obj)
254255

@@ -284,7 +285,7 @@ func (r *GitRepositoryReconciler) reconcileStorage(ctx context.Context, obj *sou
284285
// If both the checkout and signature verification are successful, the given artifact pointer is set to a new artifact
285286
// with the available metadata.
286287
func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
287-
obj *sourcev1.GitRepository, artifact *sourcev1.Artifact, _ *artifactSet, dir string) (sreconcile.Result, error) {
288+
obj *sourcev1.GitRepository, commit *git.Commit, _ *artifactSet, dir string) (sreconcile.Result, error) {
288289
// Configure authentication strategy to access the source
289290
var authOpts *git.AuthOptions
290291
var err error
@@ -344,7 +345,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
344345
// Checkout HEAD of reference in object
345346
gitCtx, cancel := context.WithTimeout(ctx, obj.Spec.Timeout.Duration)
346347
defer cancel()
347-
commit, err := checkoutStrategy.Checkout(gitCtx, dir, obj.Spec.URL, authOpts)
348+
c, err := checkoutStrategy.Checkout(gitCtx, dir, obj.Spec.URL, authOpts)
348349
if err != nil {
349350
e := &serror.Event{
350351
Err: fmt.Errorf("failed to checkout and determine revision: %w", err),
@@ -354,6 +355,8 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
354355
// Coin flip on transient or persistent error, return error and hope for the best
355356
return sreconcile.ResultEmpty, e
356357
}
358+
// Assign the commit to the shared commit reference.
359+
*commit = *c
357360
ctrl.LoggerFrom(ctx).V(logger.DebugLevel).Info("git repository checked out", "url", obj.Spec.URL, "revision", commit.String())
358361
conditions.Delete(obj, sourcev1.FetchFailedCondition)
359362

@@ -362,9 +365,6 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
362365
return result, err
363366
}
364367

365-
// Create potential new artifact with current available metadata
366-
*artifact = r.Storage.NewArtifactFor(obj.Kind, obj.GetObjectMeta(), commit.String(), fmt.Sprintf("%s.tar.gz", commit.Hash.String()))
367-
368368
// Mark observations about the revision on the object
369369
if !obj.GetArtifact().HasRevision(commit.String()) {
370370
message := fmt.Sprintf("new upstream revision '%s'", commit.String())
@@ -383,7 +383,11 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
383383
// Source ignore patterns are loaded, and the given directory is archived.
384384
// On a successful archive, the artifact and includes in the status of the given object are set, and the symlink in the
385385
// storage is updated to its path.
386-
func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, obj *sourcev1.GitRepository, artifact *sourcev1.Artifact, includes *artifactSet, dir string) (sreconcile.Result, error) {
386+
func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context,
387+
obj *sourcev1.GitRepository, commit *git.Commit, includes *artifactSet, dir string) (sreconcile.Result, error) {
388+
// Create potential new artifact with current available metadata
389+
artifact := r.Storage.NewArtifactFor(obj.Kind, obj.GetObjectMeta(), commit.String(), fmt.Sprintf("%s.tar.gz", commit.Hash.String()))
390+
387391
// Always restore the Ready condition in case it got removed due to a transient error
388392
defer func() {
389393
if obj.GetArtifact().HasRevision(artifact.Revision) && !includes.Diff(obj.Status.IncludedArtifacts) {
@@ -419,14 +423,14 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, obj *so
419423
}
420424

421425
// Ensure artifact directory exists and acquire lock
422-
if err := r.Storage.MkdirAll(*artifact); err != nil {
426+
if err := r.Storage.MkdirAll(artifact); err != nil {
423427
e := &serror.Event{
424428
Err: fmt.Errorf("failed to create artifact directory: %w", err),
425429
Reason: sourcev1.StorageOperationFailedReason,
426430
}
427431
return sreconcile.ResultEmpty, e
428432
}
429-
unlock, err := r.Storage.Lock(*artifact)
433+
unlock, err := r.Storage.Lock(artifact)
430434
if err != nil {
431435
return sreconcile.ResultEmpty, &serror.Event{
432436
Err: fmt.Errorf("failed to acquire lock for artifact: %w", err),
@@ -448,7 +452,7 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, obj *so
448452
}
449453

450454
// Archive directory to storage
451-
if err := r.Storage.Archive(artifact, dir, SourceIgnoreFilter(ps, nil)); err != nil {
455+
if err := r.Storage.Archive(&artifact, dir, SourceIgnoreFilter(ps, nil)); err != nil {
452456
return sreconcile.ResultEmpty, &serror.Event{
453457
Err: fmt.Errorf("unable to archive artifact to storage: %w", err),
454458
Reason: sourcev1.StorageOperationFailedReason,
@@ -457,14 +461,14 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, obj *so
457461
r.AnnotatedEventf(obj, map[string]string{
458462
"revision": artifact.Revision,
459463
"checksum": artifact.Checksum,
460-
}, corev1.EventTypeNormal, "NewArtifact", "stored artifact for revision '%s'", artifact.Revision)
464+
}, corev1.EventTypeNormal, "NewArtifact", "stored artifact for commit '%s'", commit.ShortMessage())
461465

462466
// Record it on the object
463467
obj.Status.Artifact = artifact.DeepCopy()
464468
obj.Status.IncludedArtifacts = *includes
465469

466470
// Update symlink on a "best effort" basis
467-
url, err := r.Storage.Symlink(*artifact, "latest.tar.gz")
471+
url, err := r.Storage.Symlink(artifact, "latest.tar.gz")
468472
if err != nil {
469473
r.eventLogf(ctx, obj, corev1.EventTypeWarning, sourcev1.StorageOperationFailedReason,
470474
"failed to update status URL symlink: %s", err)
@@ -481,7 +485,8 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, obj *so
481485
// If an include is unavailable, it marks the object with v1beta1.IncludeUnavailableCondition and returns early.
482486
// If the copy operations are successful, it deletes the v1beta1.IncludeUnavailableCondition from the object.
483487
// If the artifactSet differs from the current set, it marks the object with v1beta1.ArtifactOutdatedCondition.
484-
func (r *GitRepositoryReconciler) reconcileInclude(ctx context.Context, obj *sourcev1.GitRepository, _ *sourcev1.Artifact, includes *artifactSet, dir string) (sreconcile.Result, error) {
488+
func (r *GitRepositoryReconciler) reconcileInclude(ctx context.Context,
489+
obj *sourcev1.GitRepository, _ *git.Commit, includes *artifactSet, dir string) (sreconcile.Result, error) {
485490
artifacts := make(artifactSet, len(obj.Spec.Include))
486491
for i, incl := range obj.Spec.Include {
487492
// Do this first as it is much cheaper than copy operations

controllers/gitrepository_controller_test.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,14 @@ func TestGitRepositoryReconciler_reconcileSource_authStrategy(t *testing.T) {
511511
assertConditions[k].Message = strings.ReplaceAll(assertConditions[k].Message, "<url>", obj.Spec.URL)
512512
}
513513

514-
var artifact sourcev1.Artifact
514+
var commit git.Commit
515515
var includes artifactSet
516516

517-
got, err := r.reconcileSource(context.TODO(), obj, &artifact, &includes, tmpDir)
517+
got, err := r.reconcileSource(context.TODO(), obj, &commit, &includes, tmpDir)
518518
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(tt.assertConditions))
519519
g.Expect(err != nil).To(Equal(tt.wantErr))
520520
g.Expect(got).To(Equal(tt.want))
521-
g.Expect(artifact).ToNot(BeNil())
521+
g.Expect(commit).ToNot(BeNil())
522522
})
523523
}
524524
})
@@ -666,17 +666,17 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
666666
obj := obj.DeepCopy()
667667
obj.Spec.GitImplementation = i
668668

669-
var artifact sourcev1.Artifact
669+
var commit git.Commit
670670
var includes artifactSet
671-
got, err := r.reconcileSource(ctx, obj, &artifact, &includes, tmpDir)
671+
got, err := r.reconcileSource(ctx, obj, &commit, &includes, tmpDir)
672672
if err != nil {
673673
println(err.Error())
674674
}
675675
g.Expect(err != nil).To(Equal(tt.wantErr))
676676
g.Expect(got).To(Equal(tt.want))
677677
if tt.wantRevision != "" {
678678
revision := strings.ReplaceAll(tt.wantRevision, "<commit>", headRef.Hash().String())
679-
g.Expect(artifact.Revision).To(Equal(revision))
679+
g.Expect(commit.String()).To(Equal(revision))
680680
g.Expect(conditions.IsTrue(obj, sourcev1.ArtifactOutdatedCondition)).To(BeTrue())
681681
}
682682
})
@@ -691,7 +691,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
691691
dir string
692692
includes artifactSet
693693
beforeFunc func(obj *sourcev1.GitRepository)
694-
afterFunc func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact)
694+
afterFunc func(t *WithT, obj *sourcev1.GitRepository)
695695
want sreconcile.Result
696696
wantErr bool
697697
assertConditions []metav1.Condition
@@ -702,7 +702,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
702702
beforeFunc: func(obj *sourcev1.GitRepository) {
703703
obj.Spec.Interval = metav1.Duration{Duration: interval}
704704
},
705-
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
705+
afterFunc: func(t *WithT, obj *sourcev1.GitRepository) {
706706
t.Expect(obj.GetArtifact()).ToNot(BeNil())
707707
t.Expect(obj.Status.URL).ToNot(BeEmpty())
708708
},
@@ -719,7 +719,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
719719
beforeFunc: func(obj *sourcev1.GitRepository) {
720720
obj.Spec.Interval = metav1.Duration{Duration: interval}
721721
},
722-
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
722+
afterFunc: func(t *WithT, obj *sourcev1.GitRepository) {
723723
t.Expect(obj.GetArtifact()).ToNot(BeNil())
724724
t.Expect(obj.GetArtifact().Checksum).To(Equal("60a3bf69f337cb5ec9ebd00abefbb6e7f2a2cf27158ecf438d52b2035b184172"))
725725
t.Expect(obj.Status.IncludedArtifacts).ToNot(BeEmpty())
@@ -740,7 +740,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
740740
obj.Status.Artifact = &sourcev1.Artifact{Revision: "main/revision"}
741741
obj.Status.IncludedArtifacts = []*sourcev1.Artifact{{Revision: "main/revision"}}
742742
},
743-
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
743+
afterFunc: func(t *WithT, obj *sourcev1.GitRepository) {
744744
t.Expect(obj.Status.URL).To(BeEmpty())
745745
},
746746
want: sreconcile.ResultSuccess,
@@ -755,7 +755,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
755755
obj.Spec.Interval = metav1.Duration{Duration: interval}
756756
obj.Spec.Ignore = pointer.StringPtr("!**.txt\n")
757757
},
758-
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
758+
afterFunc: func(t *WithT, obj *sourcev1.GitRepository) {
759759
t.Expect(obj.GetArtifact()).ToNot(BeNil())
760760
t.Expect(obj.GetArtifact().Checksum).To(Equal("11f7f007dce5619bd79e6c57688261058d09f5271e802463ac39f2b9ead7cabd"))
761761
},
@@ -772,7 +772,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
772772
obj.Spec.Interval = metav1.Duration{Duration: interval}
773773
conditions.MarkTrue(obj, sourcev1.ArtifactOutdatedCondition, "Foo", "")
774774
},
775-
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
775+
afterFunc: func(t *WithT, obj *sourcev1.GitRepository) {
776776
t.Expect(obj.GetArtifact()).ToNot(BeNil())
777777
t.Expect(obj.GetArtifact().Checksum).To(Equal("60a3bf69f337cb5ec9ebd00abefbb6e7f2a2cf27158ecf438d52b2035b184172"))
778778
t.Expect(obj.Status.URL).ToNot(BeEmpty())
@@ -789,7 +789,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
789789
beforeFunc: func(obj *sourcev1.GitRepository) {
790790
obj.Spec.Interval = metav1.Duration{Duration: interval}
791791
},
792-
afterFunc: func(t *WithT, obj *sourcev1.GitRepository, artifact sourcev1.Artifact) {
792+
afterFunc: func(t *WithT, obj *sourcev1.GitRepository) {
793793
t.Expect(obj.GetArtifact()).ToNot(BeNil())
794794

795795
localPath := testStorage.LocalPath(*obj.GetArtifact())
@@ -843,15 +843,18 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
843843
tt.beforeFunc(obj)
844844
}
845845

846-
artifact := testStorage.NewArtifactFor(obj.Kind, obj, "main/revision", "checksum.tar.gz")
846+
commit := git.Commit{
847+
Hash: []byte("revision"),
848+
Reference: "refs/heads/main",
849+
}
847850

848-
got, err := r.reconcileArtifact(ctx, obj, &artifact, &tt.includes, tt.dir)
851+
got, err := r.reconcileArtifact(ctx, obj, &commit, &tt.includes, tt.dir)
849852
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions(tt.assertConditions))
850853
g.Expect(err != nil).To(Equal(tt.wantErr))
851854
g.Expect(got).To(Equal(tt.want))
852855

853856
if tt.afterFunc != nil {
854-
tt.afterFunc(g, obj, artifact)
857+
tt.afterFunc(g, obj)
855858
}
856859
})
857860
}
@@ -1036,10 +1039,10 @@ func TestGitRepositoryReconciler_reconcileInclude(t *testing.T) {
10361039
g.Expect(err).NotTo(HaveOccurred())
10371040
defer os.RemoveAll(tmpDir)
10381041

1039-
var artifact sourcev1.Artifact
1042+
var commit git.Commit
10401043
var includes artifactSet
10411044

1042-
got, err := r.reconcileInclude(ctx, obj, &artifact, &includes, tmpDir)
1045+
got, err := r.reconcileInclude(ctx, obj, &commit, &includes, tmpDir)
10431046
g.Expect(obj.GetConditions()).To(conditions.MatchConditions(tt.assertConditions))
10441047
g.Expect(err != nil).To(Equal(tt.wantErr))
10451048
if err == nil {

0 commit comments

Comments
 (0)