@@ -22,6 +22,8 @@ import (
2222 "context"
2323 "encoding/json"
2424 "fmt"
25+ "slices"
26+ "time"
2527
2628 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2729 oras "oras.land/oras-go/v2"
@@ -34,6 +36,10 @@ import (
3436 compatv1alpha1 "sigs.k8s.io/node-feature-discovery/api/image-compatibility/v1alpha1"
3537)
3638
39+ const (
40+ ArtifactCreationTimestampKey = "org.opencontainers.image.created"
41+ )
42+
3743// ArtifactClient interface contain set of functions to manipulate compatibility artfact.
3844type ArtifactClient interface {
3945 // FetchCompatibilitySpec downloads the compatibility specifcation associated with the image.
@@ -90,6 +96,14 @@ func (c *Client) FetchCompatibilitySpec(ctx context.Context) (*compatv1alpha1.Sp
9096 } else if len (descs ) < 1 {
9197 return nil , fmt .Errorf ("compatibility artifact not found" )
9298 }
99+
100+ // Sort the artifacts in desc order.
101+ // If the artifact does not have creation timestamp it will be moved to the top of the slice.
102+ slices .SortFunc (descs , func (i , j ocispec.Descriptor ) int {
103+ it , _ := time .Parse (time .RFC3339 , i .Annotations [ArtifactCreationTimestampKey ])
104+ jt , _ := time .Parse (time .RFC3339 , j .Annotations [ArtifactCreationTimestampKey ])
105+ return it .Compare (jt )
106+ })
93107 artifactDesc := descs [len (descs )- 1 ]
94108
95109 _ , content , err := oras .FetchBytes (ctx , repo .Manifests (), artifactDesc .Digest .String (), oras .DefaultFetchBytesOptions )
0 commit comments