-
Notifications
You must be signed in to change notification settings - Fork 102
/
types.go
235 lines (213 loc) · 5.04 KB
/
types.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2022 Chainguard, Inc.
//
// 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 types
import (
"sort"
v1 "github.com/google/go-containerregistry/pkg/v1"
)
type User struct {
UserName string
UID uint32
GID uint32
}
type Group struct {
GroupName string
GID uint32
Members []string
}
type PathMutation struct {
Path string
Type string
UID uint32
GID uint32
Permissions uint32
Source string
Recursive bool
}
type OSRelease struct {
Name string
ID string
VersionID string `yaml:"version-id"`
PrettyName string `yaml:"pretty-name"`
HomeURL string `yaml:"home-url"`
BugReportURL string `yaml:"bug-report-url"`
}
type ImageConfiguration struct {
Contents struct {
Repositories []string
Keyring []string
Packages []string
}
Entrypoint struct {
Type string
Command string
ShellFragment string `yaml:"shell-fragment"`
// TBD: presently a map of service names and the command to run
Services map[interface{}]interface{}
}
Cmd string
WorkDir string `yaml:"work-dir"`
Accounts struct {
RunAs string `yaml:"run-as"`
Users []User
Groups []Group
}
Archs []Architecture
Environment map[string]string
Paths []PathMutation
OSRelease OSRelease `yaml:"os-release"`
VCSUrl string `yaml:"vcs-url"`
Annotations map[string]string `yaml:"annotations"`
Include string
}
// Architecture represents a CPU architecture for the container image.
type Architecture struct{ s string }
func (a Architecture) String() string { return a.s }
func (a *Architecture) UnmarshalYAML(unmarshal func(interface{}) error) error {
var buf string
if err := unmarshal(&buf); err != nil {
return err
}
a.s = ParseArchitecture(buf).s
return nil
}
var (
_386 = Architecture{"386"}
amd64 = Architecture{"amd64"}
arm64 = Architecture{"arm64"}
armv6 = Architecture{"arm/v6"}
armv7 = Architecture{"arm/v7"}
ppc64le = Architecture{"ppc64le"}
riscv64 = Architecture{"riscv64"}
s390x = Architecture{"s390x"}
)
// AllArchs contains the standard set of supported architectures, which are
// used by `apko publish` when no architectures are specified.
var AllArchs = []Architecture{
_386,
amd64,
arm64,
armv6,
armv7,
ppc64le,
riscv64,
s390x,
}
// ToAPK returns the apk-style equivalent string for the Architecture.
func (a Architecture) ToAPK() string {
switch a {
case _386:
return "x86"
case amd64:
return "x86_64"
case arm64:
return "aarch64"
case armv6:
return "armhf"
case armv7:
return "armv7"
default:
return a.s
}
}
func (a Architecture) ToOCIPlatform() *v1.Platform {
plat := v1.Platform{OS: "linux"}
switch a {
case armv6:
plat.Architecture = "arm"
plat.Variant = "v6"
case armv7:
plat.Architecture = "arm"
plat.Variant = "v7"
default:
plat.Architecture = a.s
}
return &plat
}
func (a Architecture) ToQEmu() string {
switch a {
case _386:
return "i386"
case amd64:
return "x86_64"
case arm64:
return "aarch64"
case armv6:
return "arm"
case armv7:
return "arm"
default:
return a.s
}
}
func (a Architecture) Compatible(b Architecture) bool {
switch b {
case _386:
return a == b
case amd64:
return a == _386 || a == b
case arm64:
return a == armv6 || a == armv7 || a == b
case armv6:
return a == b
case armv7:
return a == armv6 || a == b
default:
return false
}
}
// ParseArchitecture parses a single architecture in string form, and returns
// the equivalent Architecture value.
//
// Any apk-style arch string (e.g., "x86_64") is converted to the OCI-style
// equivalent ("amd64").
func ParseArchitecture(s string) Architecture {
switch s {
case "x86":
return _386
case "x86_64":
return amd64
case "aarch64":
return arm64
case "armhf":
return armv6
case "armv7":
return armv7
}
return Architecture{s}
}
// ParseArchitectures parses architecture values in string form, and returns
// the equivalent slice of Architectures.
//
// apk-style arch strings (e.g., "x86_64") are converted to the OCI-style
// equivalent ("amd64"). Values are deduped, and the resulting slice is sorted
// for reproducibility.
func ParseArchitectures(in []string) []Architecture {
if len(in) == 1 && in[0] == "all" {
return AllArchs
}
uniq := map[Architecture]struct{}{}
for _, s := range in {
a := ParseArchitecture(s)
uniq[a] = struct{}{}
}
archs := make([]Architecture, 0, len(uniq))
for k := range uniq {
archs = append(archs, k)
}
sort.Slice(archs, func(i, j int) bool {
return archs[i].s < archs[j].s
})
return archs
}