forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.go
170 lines (144 loc) · 4.88 KB
/
metadata.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package lifecycle
import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
"github.com/buildpacks/lifecycle/launch"
)
const (
BuildMetadataLabel = "io.buildpacks.build.metadata"
LayerMetadataLabel = "io.buildpacks.lifecycle.metadata"
StackIDLabel = "io.buildpacks.stack.id"
)
type BuildMetadata struct {
Processes []launch.Process `toml:"processes" json:"processes"`
Buildpacks []Buildpack `toml:"buildpacks" json:"buildpacks"`
BOM []BOMEntry `toml:"bom" json:"bom"`
Launcher LauncherMetadata `toml:"-" json:"launcher"`
Slices []Slice `toml:"slices" json:"-"`
}
type LauncherMetadata struct {
Version string `json:"version"`
Source SourceMetadata `json:"source"`
}
type SourceMetadata struct {
Git GitMetadata `json:"git"`
}
type GitMetadata struct {
Repository string `json:"repository"`
Commit string `json:"commit"`
}
func (md BuildMetadata) hasProcess(processType string) bool {
for _, p := range md.Processes {
if p.Type == processType {
return true
}
}
return false
}
type CacheMetadata struct {
Buildpacks []BuildpackLayersMetadata `json:"buildpacks"`
}
func (cm *CacheMetadata) MetadataForBuildpack(id string) BuildpackLayersMetadata {
for _, bpMD := range cm.Buildpacks {
if bpMD.ID == id {
return bpMD
}
}
return BuildpackLayersMetadata{}
}
// NOTE: This struct MUST be kept in sync with `LayersMetadataCompat`
type LayersMetadata struct {
App []LayerMetadata `json:"app" toml:"app"`
Config LayerMetadata `json:"config" toml:"config"`
Launcher LayerMetadata `json:"launcher" toml:"launcher"`
Buildpacks []BuildpackLayersMetadata `json:"buildpacks" toml:"buildpacks"`
RunImage RunImageMetadata `json:"runImage" toml:"run-image"`
Stack StackMetadata `json:"stack" toml:"stack"`
}
// NOTE: This struct MUST be kept in sync with `LayersMetadata`.
// It exists for situations where the `App` field type cannot be
// guaranteed, yet the original struct data must be maintained.
type LayersMetadataCompat struct {
App interface{} `json:"app" toml:"app"`
Config LayerMetadata `json:"config" toml:"config"`
Launcher LayerMetadata `json:"launcher" toml:"launcher"`
Buildpacks []BuildpackLayersMetadata `json:"buildpacks" toml:"buildpacks"`
RunImage RunImageMetadata `json:"runImage" toml:"run-image"`
Stack StackMetadata `json:"stack" toml:"stack"`
}
type AnalyzedMetadata struct {
Image *ImageIdentifier `toml:"image"`
Metadata LayersMetadata `toml:"metadata"`
}
// FIXME: fix key names to be accurate in the daemon case
type ImageIdentifier struct {
Reference string `toml:"reference"`
}
type LayerMetadata struct {
SHA string `json:"sha" toml:"sha"`
}
type BuildpackLayersMetadata struct {
ID string `json:"key" toml:"key"`
Version string `json:"version" toml:"version"`
Layers map[string]BuildpackLayerMetadata `json:"layers" toml:"layers"`
Store *BuildpackStore `json:"store,omitempty" toml:"store"`
}
type BuildpackLayerMetadata struct {
LayerMetadata
BuildpackLayerMetadataFile
}
type BuildpackLayerMetadataFile struct {
Data interface{} `json:"data" toml:"metadata"`
Build bool `json:"build" toml:"build"`
Launch bool `json:"launch" toml:"launch"`
Cache bool `json:"cache" toml:"cache"`
}
type BuildpackStore struct {
Data map[string]interface{} `json:"metadata" toml:"metadata"`
}
type RunImageMetadata struct {
TopLayer string `json:"topLayer" toml:"top-layer"`
Reference string `json:"reference" toml:"reference"`
}
type StackMetadata struct {
RunImage StackRunImageMetadata `json:"runImage" toml:"run-image"`
}
type StackRunImageMetadata struct {
Image string `toml:"image" json:"image"`
Mirrors []string `toml:"mirrors" json:"mirrors,omitempty"`
}
func (sm *StackMetadata) BestRunImageMirror(registry string) (string, error) {
if sm.RunImage.Image == "" {
return "", errors.New("missing run-image metadata")
}
runImageMirrors := []string{sm.RunImage.Image}
runImageMirrors = append(runImageMirrors, sm.RunImage.Mirrors...)
runImageRef, err := byRegistry(registry, runImageMirrors)
if err != nil {
return "", errors.Wrap(err, "failed to find run-image")
}
return runImageRef, nil
}
func (m *LayersMetadata) MetadataForBuildpack(id string) BuildpackLayersMetadata {
for _, bpMD := range m.Buildpacks {
if bpMD.ID == id {
return bpMD
}
}
return BuildpackLayersMetadata{}
}
func byRegistry(reg string, imgs []string) (string, error) {
if len(imgs) < 1 {
return "", errors.New("no images provided to search")
}
for _, img := range imgs {
ref, err := name.ParseReference(img, name.WeakValidation)
if err != nil {
continue
}
if reg == ref.Context().RegistryStr() {
return img, nil
}
}
return imgs[0], nil
}