Skip to content

Commit 241c886

Browse files
committed
Sort the list of compatibility artifacts in desc order
Signed-off-by: Marcin Franczyk <[email protected]>
1 parent 75ed142 commit 241c886

File tree

1 file changed

+14
-0
lines changed
  • pkg/client-nfd/compat/artifact-client

1 file changed

+14
-0
lines changed

pkg/client-nfd/compat/artifact-client/client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3844
type 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

Comments
 (0)