forked from vmware-archive/kubeless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
description.go
121 lines (107 loc) · 3.34 KB
/
description.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
/*
Copyright (c) 2016-2017 Bitnami
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package layerbuilder
import (
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"time"
)
// Config represents a container configuration
type Config struct {
Hostname string
Domainname string
User string
AttachStdin bool
AttachStdout bool
AttachStderr bool
Tty bool
OpenStdin bool
StdinOnce bool
Env []string
Cmd []string
ArgsEscaped bool
Image string
Volumes interface{}
WorkingDir string
Entrypoint interface{}
OnBuild interface{}
Labels interface{}
}
// HistoryEntry represents a layer creation info
type HistoryEntry struct {
Created string `json:"created"`
CreatedBy string `json:"created_by,omitifempty"`
Comment string `json:"comment,omitifempty"`
EmptyLayer bool `json:"empty_layer,omitifempty"`
}
// Rootfs represents the root filesystem of an image
type Rootfs struct {
Type string `json:"type"`
DiffIds []string `json:"diff_ids"`
}
// Description represents the specification of a Docker image
type Description struct {
Arch string `json:"architecture"`
Config Config `json:"config"`
Container string `json:"container"`
ContainerConfig Config `json:"container_config"`
Created string `json:"created"`
DockerVersion string `json:"docker_version"`
History []HistoryEntry `json:"history"`
OS string `json:"os"`
Rootfs Rootfs `json:"rootfs"`
}
// New generates a Description object based on the description file
func (d *Description) New(descriptionFile io.Reader) error {
descriptionContent, err := ioutil.ReadAll(descriptionFile)
if err != nil {
return err
}
return json.Unmarshal(descriptionContent, d)
}
// AddLayer adds a new Layer to the image Description
func (d *Description) AddLayer(newLayer *Layer) {
// Delete some properties that doesn't apply anymore
d.Config.Hostname = ""
d.Config.Image = ""
d.Container = ""
d.ContainerConfig.Hostname = ""
d.ContainerConfig.Image = ""
// Update new properties
d.Created = time.Now().UTC().Format(time.RFC3339)
d.History = append(d.History, HistoryEntry{
Created: time.Now().UTC().Format(time.RFC3339),
Comment: "Created by Kubeless",
})
d.Rootfs.DiffIds = append(d.Rootfs.DiffIds, fmt.Sprintf("sha256:%s", newLayer.Sha256))
}
// Content returns the description content
func (d *Description) Content() ([]byte, error) {
return json.Marshal(*d)
}
// ToLayer returns the Description as a Layer
func (d *Description) ToLayer() (*Layer, error) {
content, err := d.Content()
if err != nil {
return nil, err
}
descriptionNewSize := int64(len(content))
descriptionNewSha := fmt.Sprintf("%x", sha256.Sum256(content))
return &Layer{
Size: descriptionNewSize,
Sha256: descriptionNewSha,
}, nil
}