-
Notifications
You must be signed in to change notification settings - Fork 25
/
fedora.go
239 lines (204 loc) · 5.89 KB
/
fedora.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
236
237
238
239
package fedora
import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/containerdisks/pkg/api"
"kubevirt.io/containerdisks/pkg/architecture"
"kubevirt.io/containerdisks/pkg/common"
"kubevirt.io/containerdisks/pkg/docs"
"kubevirt.io/containerdisks/pkg/http"
"kubevirt.io/containerdisks/pkg/tests"
)
type Releases []Release
type Release struct {
Subvariant string `json:"subvariant"`
Variant string `json:"variant"`
Version string `json:"version"`
Link string `json:"link"`
Sha256 string `json:"sha256,omitempty"`
Arch string `json:"arch"`
Size string `json:"size,omitempty"`
}
type fedora struct {
Version string
Arch string
Variant string
getter http.Getter
EnvVariables map[string]string
}
type fedoraGatherer struct {
Version string
Archs []string
Variant string
Subvariant string
getter http.Getter
}
const minimumVersion = 38
//nolint:lll
const description = `<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Fedora_logo.svg/240px-Fedora_logo.svg.png" alt="drawing" width="15"/> Fedora [Cloud](https://alt.fedoraproject.org/cloud/) images for KubeVirt.
<br />
<br />
Visit [getfedora.org](https://getfedora.org/) to learn more about the Fedora project.`
var additionalUniqueTagRegExp = regexp.MustCompile(`\d+-\d+\.\d+`)
func (f *fedora) Metadata() *api.Metadata {
return &api.Metadata{
Name: "fedora",
Version: f.Version,
Description: description,
ExampleUserData: docs.UserData{
Username: "fedora",
},
EnvVariables: f.EnvVariables,
}
}
func (f *fedora) Inspect() (*api.ArtifactDetails, error) {
releases, err := getReleases(f.getter)
if err != nil {
return nil, fmt.Errorf("error getting releases: %v", err)
}
for i, release := range releases {
if !f.releaseMatches(&releases[i]) {
continue
}
details := &api.ArtifactDetails{
SHA256Sum: release.Sha256,
DownloadURL: release.Link,
ImageArchitecture: architecture.GetImageArchitecture(f.Arch),
}
components := strings.Split(release.Link, "/")
fileName := components[len(components)-1]
if matches := additionalUniqueTagRegExp.FindStringSubmatch(fileName); len(matches) > 0 {
details.AdditionalUniqueTags = append(details.AdditionalUniqueTags, matches[0])
}
return details, nil
}
return nil, fmt.Errorf("no release information in releases.json for fedora:%q found", f.Version)
}
func (f *fedora) VM(name, imgRef, userData string) *v1.VirtualMachine {
return docs.NewVM(
name,
imgRef,
docs.WithRng(),
docs.WithCloudInitNoCloud(userData),
docs.WithSecureBoot(),
)
}
func (f *fedora) UserData(data *docs.UserData) string {
return docs.CloudInit(data)
}
func (f *fedora) Tests() []api.ArtifactTest {
return []api.ArtifactTest{
tests.GuestOsInfo,
tests.SSH,
}
}
func (f *fedoraGatherer) Gather() ([][]api.Artifact, error) {
releases, err := getReleases(f.getter)
if err != nil {
return nil, fmt.Errorf("error getting releases: %v", err)
}
versions := map[string][]Release{}
for i := range releases {
release := releases[i]
if f.releaseMatches(&release) {
versions[release.Version] = append(versions[release.Version], release)
}
}
// Ensure versions are always sorted with the latest first
versionKeys := make([]string, 0, len(versions))
for key := range versions {
versionKeys = append(versionKeys, key)
}
sort.Sort(sort.Reverse(sort.StringSlice(versionKeys)))
var artifacts [][]api.Artifact
for _, key := range versionKeys {
releases := versions[key]
var releaseArtifcats []api.Artifact
for _, release := range releases {
artifact := New(release.Version, release.Arch)
releaseArtifcats = append(releaseArtifcats, artifact)
}
artifacts = append(artifacts, releaseArtifcats)
}
return artifacts, nil
}
func getReleases(getter http.Getter) (Releases, error) {
raw, err := getter.GetAll("https://getfedora.org/releases.json")
if err != nil {
return nil, fmt.Errorf("error downloading the fedora releases.json file: %v", err)
}
releases := Releases{}
if err := json.Unmarshal(raw, &releases); err != nil {
return nil, fmt.Errorf("error parsing the releases.json file: %v", err)
}
return releases, nil
}
func (f *fedora) releaseMatches(release *Release) bool {
return release.Version == f.Version &&
release.Arch == f.Arch &&
release.Variant == f.Variant &&
strings.HasSuffix(release.Link, "qcow2")
}
func (f *fedoraGatherer) releaseMatches(release *Release) bool {
version, err := strconv.Atoi(release.Version)
if err != nil {
return false
}
for _, arch := range f.Archs {
if release.Arch == arch {
return version >= minimumVersion &&
release.Variant == f.Variant &&
release.Subvariant == f.Subvariant &&
strings.HasSuffix(release.Link, "qcow2")
}
}
return false
}
const (
defaultInstancetype = "u1.medium"
defaultPreferenceX86_64 = "fedora"
defaultPreferenceAarch64 = "fedora.arm64"
defaultPreferenceS390x = "fedora.s390x"
)
func (f *fedora) setEnvVariables() {
switch f.Arch {
case "x86_64":
f.EnvVariables = map[string]string{
common.DefaultInstancetypeEnv: defaultInstancetype,
common.DefaultPreferenceEnv: defaultPreferenceX86_64,
}
case "aarch64":
f.EnvVariables = map[string]string{
common.DefaultInstancetypeEnv: defaultInstancetype,
common.DefaultPreferenceEnv: defaultPreferenceAarch64,
}
case "s390x":
f.EnvVariables = map[string]string{
common.DefaultInstancetypeEnv: defaultInstancetype,
common.DefaultPreferenceEnv: defaultPreferenceS390x,
}
}
}
func New(release, arch string) *fedora {
f := &fedora{
Version: release,
Arch: arch,
Variant: "Cloud",
getter: &http.HTTPGetter{},
}
f.setEnvVariables()
return f
}
func NewGatherer() *fedoraGatherer {
return &fedoraGatherer{
Archs: []string{"x86_64", "aarch64", "s390x"},
Variant: "Cloud",
Subvariant: "Cloud_Base",
getter: &http.HTTPGetter{},
}
}