feat: adding support digest pinning for flux plugin install
Signed-off-by: iam-karan-suresh <karansuresh.info@gmail.com>
This commit is contained in:
@@ -165,3 +165,33 @@ func ResolvePlatform(pv *plugintypes.Version, goos, goarch string) (*plugintypes
|
||||
|
||||
return nil, fmt.Errorf("no binary for %s/%s", goos, goarch)
|
||||
}
|
||||
|
||||
// DigestMatch holds the version and platform resolved from a digest lookup.
|
||||
type DigestMatch struct {
|
||||
Version *plugintypes.Version
|
||||
Platform *plugintypes.Platform
|
||||
}
|
||||
|
||||
// ResolveByDigest scans all versions and platforms for a checksum matching
|
||||
// digest. The digest must be in "algorithm:hex" format (e.g.
|
||||
// "sha256:06e0a38..."). Only platforms matching goos/goarch are considered.
|
||||
// Returns the first match (versions are ordered newest-first in the manifest).
|
||||
func ResolveByDigest(manifest *plugintypes.Manifest, digest, goos, goarch string) (*DigestMatch, error) {
|
||||
if len(manifest.Versions) == 0 {
|
||||
return nil, fmt.Errorf("plugin %q has no versions", manifest.Name)
|
||||
}
|
||||
|
||||
for i := range manifest.Versions {
|
||||
for j := range manifest.Versions[i].Platforms {
|
||||
p := &manifest.Versions[i].Platforms[j]
|
||||
if p.OS == goos && p.Arch == goarch && p.Checksum == digest {
|
||||
return &DigestMatch{
|
||||
Version: &manifest.Versions[i],
|
||||
Platform: p,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("digest %q not found for plugin %q on %s/%s", digest, manifest.Name, goos, goarch)
|
||||
}
|
||||
|
||||
@@ -239,3 +239,70 @@ func TestResolvePlatform(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestResolveByDigest(t *testing.T) {
|
||||
manifest := &plugintypes.Manifest{
|
||||
Name: "operator",
|
||||
Versions: []plugintypes.Version{
|
||||
{
|
||||
Version: "0.46.0",
|
||||
Platforms: []plugintypes.Platform{
|
||||
{OS: "linux", Arch: "amd64", URL: "https://example.com/v46_linux.tar.gz", Checksum: "sha256:aaaa"},
|
||||
{OS: "darwin", Arch: "arm64", URL: "https://example.com/v46_darwin.tar.gz", Checksum: "sha256:bbbb"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Version: "0.45.0",
|
||||
Platforms: []plugintypes.Platform{
|
||||
{OS: "linux", Arch: "amd64", URL: "https://example.com/v45_linux.tar.gz", Checksum: "sha256:cccc"},
|
||||
{OS: "darwin", Arch: "arm64", URL: "https://example.com/v45_darwin.tar.gz", Checksum: "sha256:dddd"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("found in latest version", func(t *testing.T) {
|
||||
dm, err := ResolveByDigest(manifest, "sha256:aaaa", "linux", "amd64")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if dm.Version.Version != "0.46.0" {
|
||||
t.Errorf("expected version '0.46.0', got %q", dm.Version.Version)
|
||||
}
|
||||
if dm.Platform.Checksum != "sha256:aaaa" {
|
||||
t.Errorf("expected checksum 'sha256:aaaa', got %q", dm.Platform.Checksum)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("found in older version", func(t *testing.T) {
|
||||
dm, err := ResolveByDigest(manifest, "sha256:cccc", "linux", "amd64")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if dm.Version.Version != "0.45.0" {
|
||||
t.Errorf("expected version '0.45.0', got %q", dm.Version.Version)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("wrong platform", func(t *testing.T) {
|
||||
// sha256:bbbb exists for darwin/arm64, not linux/amd64.
|
||||
_, err := ResolveByDigest(manifest, "sha256:bbbb", "linux", "amd64")
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("not found", func(t *testing.T) {
|
||||
_, err := ResolveByDigest(manifest, "sha256:nonexistent", "linux", "amd64")
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no versions", func(t *testing.T) {
|
||||
_, err := ResolveByDigest(&plugintypes.Manifest{Name: "empty"}, "sha256:abc", "linux", "amd64")
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user