-
Notifications
You must be signed in to change notification settings - Fork 41
/
local.go
174 lines (150 loc) · 4.13 KB
/
local.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
171
172
173
174
package local
import (
"errors"
"fmt"
"io"
"strings"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/buildpacks/imgutil"
)
// Image wraps an imgutil.CNBImageCore and implements the methods needed to complete the imgutil.Image interface.
type Image struct {
*imgutil.CNBImageCore
repoName string
store *Store
lastIdentifier string
daemonOS string
}
func (i *Image) Kind() string {
return "local"
}
func (i *Image) Name() string {
return i.repoName
}
func (i *Image) Rename(name string) {
i.repoName = name
}
func (i *Image) Found() bool {
return i.lastIdentifier != ""
}
func (i *Image) Identifier() (imgutil.Identifier, error) {
return IDIdentifier{
ImageID: strings.TrimPrefix(i.lastIdentifier, "sha256:"),
}, nil
}
func (i *Image) Valid() bool {
// local images are actually always invalid, because they are missing a manifest, but let's ignore that for now
return true
}
// GetLayer returns an io.ReadCloser with uncompressed layer data.
// The layer will always have data, even if that means downloading ALL the image layers from the daemon.
func (i *Image) GetLayer(diffID string) (io.ReadCloser, error) {
layerHash, err := v1.NewHash(diffID)
if err != nil {
return nil, err
}
configFile, err := i.ConfigFile()
if err != nil {
return nil, err
}
if !contains(configFile.RootFS.DiffIDs, layerHash) {
return nil, imgutil.ErrLayerNotFound{DiffID: layerHash.String()}
}
layer, err := i.LayerByDiffID(layerHash)
if err == nil {
// this avoids downloading ALL the image layers from the daemon
// if the layer is available locally
// (e.g., it was added using AddLayer).
if size, err := layer.Size(); err != nil && size != -1 {
return layer.Uncompressed()
}
}
if err = i.ensureLayers(); err != nil {
return nil, err
}
layer, err = i.LayerByDiffID(layerHash)
if err != nil {
return nil, err
}
return layer.Uncompressed()
}
func contains(diffIDs []v1.Hash, hash v1.Hash) bool {
for _, diffID := range diffIDs {
if diffID.String() == hash.String() {
return true
}
}
return false
}
func (i *Image) ensureLayers() error {
if err := i.store.downloadLayersFor(i.lastIdentifier); err != nil {
return fmt.Errorf("failed to fetch base layers: %w", err)
}
return nil
}
func (i *Image) SetOS(osVal string) error {
if osVal != i.daemonOS {
return errors.New("invalid os: must match the daemon")
}
return i.CNBImageCore.SetOS(osVal)
}
var emptyHistory = v1.History{Created: v1.Time{Time: imgutil.NormalizedDateTime}}
func (i *Image) AddLayer(path string) error {
layer, err := i.store.AddLayer(path)
if err != nil {
return err
}
return i.AddLayerWithHistory(layer, emptyHistory)
}
func (i *Image) AddLayerWithDiffID(path, _ string) error {
layer, err := i.store.AddLayer(path)
if err != nil {
return err
}
return i.AddLayerWithHistory(layer, emptyHistory)
}
func (i *Image) AddLayerWithDiffIDAndHistory(path, _ string, history v1.History) error {
layer, err := i.store.AddLayer(path)
if err != nil {
return err
}
return i.AddLayerWithHistory(layer, history)
}
func (i *Image) AddOrReuseLayerWithHistory(path string, diffID string, history v1.History) error {
prevLayerExists, err := i.PreviousImageHasLayer(diffID)
if err != nil {
return err
}
if !prevLayerExists {
return i.AddLayerWithDiffIDAndHistory(path, diffID, history)
}
return i.ReuseLayerWithHistory(diffID, history)
}
func (i *Image) Rebase(baseTopLayerDiffID string, withNewBase imgutil.Image) error {
if err := i.ensureLayers(); err != nil {
return err
}
return i.CNBImageCore.Rebase(baseTopLayerDiffID, withNewBase)
}
func (i *Image) Save(additionalNames ...string) error {
err := i.SetCreatedAtAndHistory()
if err != nil {
return err
}
i.lastIdentifier, err = i.store.Save(i, i.Name(), additionalNames...)
return err
}
func (i *Image) SaveAs(name string, additionalNames ...string) error {
err := i.SetCreatedAtAndHistory()
if err != nil {
return err
}
i.lastIdentifier, err = i.store.Save(i, name, additionalNames...)
return err
}
func (i *Image) SaveFile() (string, error) {
return i.store.SaveFile(i, i.Name())
}
func (i *Image) Delete() error {
return i.store.Delete(i.lastIdentifier)
}