@@ -391,6 +391,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
391
391
}
392
392
393
393
// Get the upstream revision from the artifact digest
394
+ // TODO: getRevision resolves the digest, which may change before image is fetched, so it should probaly update ref
394
395
revision , err := r .getRevision (ref , opts )
395
396
if err != nil {
396
397
e := serror .NewGeneric (
@@ -575,33 +576,30 @@ func (r *OCIRepositoryReconciler) selectLayer(obj *ociv1.OCIRepository, image gc
575
576
// getRevision fetches the upstream digest, returning the revision in the
576
577
// format '<tag>@<digest>'.
577
578
func (r * OCIRepositoryReconciler ) getRevision (ref name.Reference , options []remote.Option ) (string , error ) {
578
- repoTag := "" // TODO: can it be obtained from name.Reference directly?
579
- repoName := ref .Context ().RepositoryStr ()
580
- if s := strings .Split (repoName , ":" ); len (s ) == 2 && ! strings .Contains (repoName , "@" ) {
581
- repoTag = s [1 ]
582
- }
583
-
584
- if repoTag == "" && ! strings .Contains (repoName , "@" ) {
585
- repoTag = "latest"
586
- }
587
-
588
- var digest v1.Hash
589
- desc , err := remote .Head (ref , options ... )
590
- if err == nil {
591
- digest = desc .Digest
592
- } else {
593
- rdesc , err := remote .Get (ref , options ... )
579
+ switch ref := ref .(type ) {
580
+ case name.Digest :
581
+ digest , err := v1 .NewHash (ref .DigestStr ())
594
582
if err != nil {
595
583
return "" , err
596
584
}
597
- digest = rdesc .Descriptor .Digest
598
- }
585
+ return digest .String (), nil
586
+ case name.Tag :
587
+ var digest v1.Hash
599
588
600
- revision := digest .String ()
601
- if repoTag != "" {
602
- revision = fmt .Sprintf ("%s@%s" , repoTag , revision )
589
+ desc , err := remote .Head (ref , options ... )
590
+ if err == nil {
591
+ digest = desc .Digest
592
+ } else {
593
+ rdesc , err := remote .Get (ref , options ... )
594
+ if err != nil {
595
+ return "" , err
596
+ }
597
+ digest = rdesc .Descriptor .Digest
598
+ }
599
+ return fmt .Sprintf ("%s@%s" , ref .TagStr (), digest .String ()), nil
600
+ default :
601
+ return "" , fmt .Errorf ("unsupported reference type: %T" , ref )
603
602
}
604
- return revision , nil
605
603
}
606
604
607
605
// digestFromRevision extracts the digest from the revision string.
@@ -696,53 +694,53 @@ func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *ociv
696
694
}
697
695
698
696
// parseRepository validates and extracts the repository URL.
699
- func (r * OCIRepositoryReconciler ) parseRepository (obj * ociv1. OCIRepository ) (name.Reference , error ) {
700
- if ! strings .HasPrefix (obj . Spec . URL , ociv1 .OCIRepositoryPrefix ) {
701
- return nil , fmt .Errorf ("URL must be in format 'oci://<domain>/<org>/<repo>'" )
697
+ func (r * OCIRepositoryReconciler ) parseRepository (url string ) (name.Repository , error ) {
698
+ if ! strings .HasPrefix (url , ociv1 .OCIRepositoryPrefix ) {
699
+ return name. Repository {} , fmt .Errorf ("URL must be in format 'oci://<domain>/<org>/<repo>'" )
702
700
}
703
701
704
- url : = strings .TrimPrefix (obj . Spec . URL , ociv1 .OCIRepositoryPrefix )
705
- ref , err := name .ParseReference (url )
702
+ url = strings .TrimPrefix (url , ociv1 .OCIRepositoryPrefix )
703
+ repo , err := name .NewRepository (url )
706
704
if err != nil {
707
- return nil , err
705
+ return name. Repository {} , err
708
706
}
709
707
710
- imageName := strings .TrimPrefix (url , ref . Context () .RegistryStr ())
708
+ imageName := strings .TrimPrefix (url , repo .RegistryStr ())
711
709
if s := strings .Split (imageName , ":" ); len (s ) > 1 {
712
- return nil , fmt .Errorf ("URL must not contain a tag; remove ':%s'" , s [1 ])
710
+ return name. Repository {} , fmt .Errorf ("URL must not contain a tag; remove ':%s'" , s [1 ])
713
711
}
714
712
715
- return ref , nil
713
+ return repo , nil
716
714
}
717
715
718
716
// getArtifactRef determines which tag or revision should be used and returns the OCI artifact FQN.
719
717
func (r * OCIRepositoryReconciler ) getArtifactRef (obj * ociv1.OCIRepository , options []remote.Option ) (name.Reference , error ) {
720
- ref , err := r .parseRepository (obj )
718
+ repo , err := r .parseRepository (obj . Spec . URL )
721
719
if err != nil {
722
720
return nil , invalidOCIURLError {err }
723
721
}
724
722
725
723
if obj .Spec .Reference != nil {
726
724
if obj .Spec .Reference .Digest != "" {
727
- return ref . Context (). Digest (fmt . Sprintf ( "%s@%s" , ref , obj .Spec .Reference .Digest ) ), nil
725
+ return repo . Digest (obj .Spec .Reference .Digest ), nil
728
726
}
729
727
730
728
if obj .Spec .Reference .SemVer != "" {
731
- return r .getTagBySemver (ref , obj .Spec .Reference .SemVer , options )
729
+ return r .getTagBySemver (repo , obj .Spec .Reference .SemVer , options )
732
730
}
733
731
734
732
if obj .Spec .Reference .Tag != "" {
735
- return ref . Context () .Tag (obj .Spec .Reference .Tag ), nil
733
+ return repo .Tag (obj .Spec .Reference .Tag ), nil
736
734
}
737
735
}
738
736
739
- return ref , nil
737
+ return repo . Tag ( "latest" ) , nil
740
738
}
741
739
742
740
// getTagBySemver call the remote container registry, fetches all the tags from the repository,
743
741
// and returns the latest tag according to the semver expression.
744
- func (r * OCIRepositoryReconciler ) getTagBySemver (ref name.Reference , exp string , options []remote.Option ) (name.Reference , error ) {
745
- tags , err := remote .List (ref . Context (). Repo () , options ... )
742
+ func (r * OCIRepositoryReconciler ) getTagBySemver (repo name.Repository , exp string , options []remote.Option ) (name.Reference , error ) {
743
+ tags , err := remote .List (repo , options ... )
746
744
if err != nil {
747
745
return nil , err
748
746
}
@@ -769,7 +767,7 @@ func (r *OCIRepositoryReconciler) getTagBySemver(ref name.Reference, exp string,
769
767
}
770
768
771
769
sort .Sort (sort .Reverse (semver .Collection (matchingVersions )))
772
- return ref . Context () .Tag (matchingVersions [0 ].Original ()), nil
770
+ return repo .Tag (matchingVersions [0 ].Original ()), nil
773
771
}
774
772
775
773
// keychain generates the credential keychain based on the resource
0 commit comments