Skip to content

Commit fc21bf2

Browse files
author
John Howard
committed
LCOW: Adds platform to the layer store
Signed-off-by: John Howard <[email protected]>
1 parent 55f8828 commit fc21bf2

10 files changed

Lines changed: 98 additions & 0 deletions

File tree

distribution/xfer/download_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type mockLayer struct {
2626
diffID layer.DiffID
2727
chainID layer.ChainID
2828
parent layer.Layer
29+
platform layer.Platform
2930
}
3031

3132
func (ml *mockLayer) TarStream() (io.ReadCloser, error) {
@@ -56,6 +57,10 @@ func (ml *mockLayer) DiffSize() (size int64, err error) {
5657
return 0, nil
5758
}
5859

60+
func (ml *mockLayer) Platform() layer.Platform {
61+
return ml.platform
62+
}
63+
5964
func (ml *mockLayer) Metadata() (map[string]string, error) {
6065
return make(map[string]string), nil
6166
}

layer/empty.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func (el *emptyLayer) Metadata() (map[string]string, error) {
5555
return make(map[string]string), nil
5656
}
5757

58+
func (el *emptyLayer) Platform() Platform {
59+
return ""
60+
}
61+
5862
// IsEmpty returns true if the layer is an EmptyLayer
5963
func IsEmpty(diffID DiffID) bool {
6064
return diffID == DigestSHA256EmptyTar

layer/filestore_unix.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// +build !windows
2+
3+
package layer
4+
5+
// SetPlatform writes the "platform" file to the layer filestore
6+
func (fm *fileMetadataTransaction) SetPlatform(platform Platform) error {
7+
return nil
8+
}
9+
10+
// GetPlatform reads the "platform" file from the layer filestore
11+
func (fms *fileMetadataStore) GetPlatform(layer ChainID) (Platform, error) {
12+
return "", nil
13+
}

layer/filestore_windows.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package layer
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"strings"
8+
)
9+
10+
// SetPlatform writes the "platform" file to the layer filestore
11+
func (fm *fileMetadataTransaction) SetPlatform(platform Platform) error {
12+
if platform == "" {
13+
return nil
14+
}
15+
return fm.ws.WriteFile("platform", []byte(platform), 0644)
16+
}
17+
18+
// GetPlatform reads the "platform" file from the layer filestore
19+
func (fms *fileMetadataStore) GetPlatform(layer ChainID) (Platform, error) {
20+
contentBytes, err := ioutil.ReadFile(fms.getLayerFilename(layer, "platform"))
21+
if err != nil {
22+
// For backwards compatibility, the platform file may not exist. Default to "windows" if missing.
23+
if os.IsNotExist(err) {
24+
return "windows", nil
25+
}
26+
return "", err
27+
}
28+
content := strings.TrimSpace(string(contentBytes))
29+
30+
if content != "windows" && content != "linux" {
31+
return "", fmt.Errorf("invalid platform value: %s", content)
32+
}
33+
34+
return Platform(content), nil
35+
}

layer/layer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ func (id ChainID) String() string {
6464
return string(id)
6565
}
6666

67+
// Platform is the platform of a layer
68+
type Platform string
69+
70+
// String returns a string rendition of layers target platform
71+
func (id Platform) String() string {
72+
return string(id)
73+
}
74+
6775
// DiffID is the hash of an individual layer tar.
6876
type DiffID digest.Digest
6977

@@ -99,6 +107,9 @@ type Layer interface {
99107
// Parent returns the next layer in the layer chain.
100108
Parent() Layer
101109

110+
// Platform returns the platform of the layer
111+
Platform() Platform
112+
102113
// Size returns the size of the entire layer chain. The size
103114
// is calculated from the total size of all files in the layers.
104115
Size() (int64, error)
@@ -208,6 +219,7 @@ type MetadataTransaction interface {
208219
SetDiffID(DiffID) error
209220
SetCacheID(string) error
210221
SetDescriptor(distribution.Descriptor) error
222+
SetPlatform(Platform) error
211223
TarSplitWriter(compressInput bool) (io.WriteCloser, error)
212224

213225
Commit(ChainID) error
@@ -228,6 +240,7 @@ type MetadataStore interface {
228240
GetDiffID(ChainID) (DiffID, error)
229241
GetCacheID(ChainID) (string, error)
230242
GetDescriptor(ChainID) (distribution.Descriptor, error)
243+
GetPlatform(ChainID) (Platform, error)
231244
TarSplitReader(ChainID) (io.ReadCloser, error)
232245

233246
SetMountID(string, string) error

layer/layer_store.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ func (ls *layerStore) loadLayer(layer ChainID) (*roLayer, error) {
144144
return nil, fmt.Errorf("failed to get descriptor for %s: %s", layer, err)
145145
}
146146

147+
platform, err := ls.store.GetPlatform(layer)
148+
if err != nil {
149+
return nil, fmt.Errorf("failed to get platform for %s: %s", layer, err)
150+
}
151+
147152
cl = &roLayer{
148153
chainID: layer,
149154
diffID: diff,
@@ -152,6 +157,7 @@ func (ls *layerStore) loadLayer(layer ChainID) (*roLayer, error) {
152157
layerStore: ls,
153158
references: map[Layer]struct{}{},
154159
descriptor: descriptor,
160+
platform: platform,
155161
}
156162

157163
if parent != "" {

layer/ro_layer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type roLayer struct {
1616
size int64
1717
layerStore *layerStore
1818
descriptor distribution.Descriptor
19+
platform Platform
1920

2021
referenceCount int
2122
references map[Layer]struct{}
@@ -142,6 +143,9 @@ func storeLayer(tx MetadataTransaction, layer *roLayer) error {
142143
return err
143144
}
144145
}
146+
if err := tx.SetPlatform(layer.platform); err != nil {
147+
return err
148+
}
145149

146150
return nil
147151
}

layer/ro_layer_unix.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build !windows
2+
3+
package layer
4+
5+
func (rl *roLayer) Platform() Platform {
6+
return ""
7+
}

layer/ro_layer_windows.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ var _ distribution.Describable = &roLayer{}
77
func (rl *roLayer) Descriptor() distribution.Descriptor {
88
return rl.descriptor
99
}
10+
11+
func (rl *roLayer) Platform() Platform {
12+
if rl.platform == "" {
13+
return "windows"
14+
}
15+
return rl.platform
16+
}

migrate/v1/migratev1_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ func (l *mockLayer) DiffSize() (int64, error) {
433433
return 0, nil
434434
}
435435

436+
func (l *mockLayer) Platform() layer.Platform {
437+
return ""
438+
}
439+
436440
func (l *mockLayer) Metadata() (map[string]string, error) {
437441
return nil, nil
438442
}

0 commit comments

Comments
 (0)