/
image_locations_config.go
100 lines (79 loc) · 2.55 KB
/
image_locations_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package bundle
import (
"fmt"
"os"
"sort"
"sigs.k8s.io/yaml"
)
const (
LocationFilepath = "image-locations.yml"
ImageLocationsKind = "ImageLocations"
LocationAPIVersion = "imgpkg.carvel.dev/v1alpha1"
)
type ImageLocationsConfig struct {
APIVersion string `json:"apiVersion"` // This generated yaml, but due to lib we need to use `json`
Kind string `json:"kind"` // This generated yaml, but due to lib we need to use `json`
Images []ImageLocation `json:"images"` // This generated yaml, but due to lib we need to use `json`
}
type ImageLocation struct {
Image string `json:"image"` // This generated yaml, but due to lib we need to use `json`
IsBundle bool `json:"isBundle"` // This generated yaml, but due to lib we need to use `json`
}
func NewLocationConfigFromPath(path string) (ImageLocationsConfig, error) {
bs, err := os.ReadFile(path)
if err != nil {
return ImageLocationsConfig{}, fmt.Errorf("Reading path %s: %s", path, err)
}
return NewLocationConfigFromBytes(bs)
}
func NewLocationConfigFromBytes(data []byte) (ImageLocationsConfig, error) {
var lock ImageLocationsConfig
err := yaml.Unmarshal(data, &lock)
if err != nil {
return lock, fmt.Errorf("Unmarshaling image locations config: %s", err)
}
err = lock.Validate()
if err != nil {
return ImageLocationsConfig{}, err
}
return lock, nil
}
func (c ImageLocationsConfig) AsBytes() ([]byte, error) {
err := c.Validate()
if err != nil {
return nil, err
}
// Ensure images are sorted deterministically based on content
// within this structure so that it's not influenced by outside
// factors (such as source image locations)
sort.Slice(c.Images, func(i, j int) bool {
return c.Images[i].Image < c.Images[j].Image
})
bs, err := yaml.Marshal(c)
if err != nil {
return nil, fmt.Errorf("Marshaling image locations config: %s", err)
}
return []byte(fmt.Sprintf("---\n%s", bs)), nil
}
func (c ImageLocationsConfig) WriteToPath(path string) error {
bs, err := c.AsBytes()
if err != nil {
return err
}
err = os.WriteFile(path, bs, 0600)
if err != nil {
return fmt.Errorf("Writing image locations config: %s", err)
}
return nil
}
func (c ImageLocationsConfig) Validate() error {
if c.APIVersion != LocationAPIVersion {
return fmt.Errorf("Validating apiVersion: Unknown version (known: %s)", LocationAPIVersion)
}
if c.Kind != ImageLocationsKind {
return fmt.Errorf("Validating kind: Unknown kind (known: %s)", ImageLocationsKind)
}
return nil
}