Writes and verify image

This commit is contained in:
2026-04-04 02:45:46 +08:00
parent 9cb593ffc0
commit 4f490ab37e
20 changed files with 596 additions and 65 deletions

View File

@@ -0,0 +1,30 @@
package catalog
import (
"encoding/hex"
"fmt"
"strings"
)
func (c *CatalogImage) SHA256() (string, error) {
if c.Checksum == "" {
return "", fmt.Errorf("checksum is empty")
}
const prefix = "sha256:"
if !strings.HasPrefix(c.Checksum, prefix) {
return "", fmt.Errorf("unsupported checksum format (expected sha256:...)")
}
hash := strings.TrimPrefix(c.Checksum, prefix)
if len(hash) != 64 {
return "", fmt.Errorf("invalid sha256 length: got %d, want 64", len(hash))
}
if _, err := hex.DecodeString(hash); err != nil {
return "", fmt.Errorf("invalid sha256 hex: %w", err)
}
return hash, nil
}

View File

@@ -21,19 +21,17 @@ const (
// ResolveCatalog resolves catalog using priority:
// Inline > ConfigMap > URL > cached
func ResolveCatalog(
ctx context.Context,
func ResolveCatalog(ctx context.Context,
kubeClient kubernetes.Interface,
namespace string,
src *monov1alpha1.VersionCatalogSource,
namespace string, src *monov1alpha1.VersionCatalogSource,
) (*VersionCatalog, error) {
// 1. Inline
// Inline
if src != nil && src.Inline != "" {
return parseCatalog([]byte(src.Inline))
}
// 2. ConfigMap
// ConfigMap
if src != nil && src.ConfigMap != "" {
cm, err := kubeClient.CoreV1().ConfigMaps(namespace).Get(ctx, src.ConfigMap, metav1.GetOptions{})
if err != nil {
@@ -48,7 +46,7 @@ func ResolveCatalog(
return parseCatalog([]byte(data))
}
// 3. URL
// URL
if src != nil && src.URL != "" {
cat, err := fetchCatalog(src.URL)
if err == nil {
@@ -64,7 +62,7 @@ func ResolveCatalog(
return nil, fmt.Errorf("fetch catalog failed and no cache: %w", err)
}
// 4. cached fallback
// fallback cache
if cached, err := loadCached(); err == nil {
return cached, nil
}

View File

@@ -10,4 +10,5 @@ type CatalogImage struct {
Version string `json:"version" yaml:"version"`
URL string `json:"url" yaml:"url"`
Checksum string `json:"checksum,omitempty" yaml:"checksum,omitempty"`
Size int64 `json:"size,omitempty" yaml:"size,omitempty"`
}