-
Notifications
You must be signed in to change notification settings - Fork 204
/
component_configs.go
178 lines (147 loc) · 4.92 KB
/
component_configs.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
package datamodel
import (
"encoding/json"
"fmt"
"os"
"strings"
)
// Components is the data model for the component config.
// The component config is a JSON file, for example, vhdbuilder/packer/components.json.
// for example, vhdbuilder/packer/components.json.
type Components struct {
ContainerImages []*ContainerImage `json:"ContainerImages"`
DownloadFiles []DownloadFile `json:"DownloadFiles"`
}
type ContainerImage struct {
DownloadURL string `json:"downloadURL"`
Amd64OnlyVersions []string `json:"amd64OnlyVersions"`
MultiArchVersions []string `json:"multiArchVersions"`
}
type DownloadFile struct {
FileName string `json:"fileName"`
DownloadLocation string `json:"downloadLocation"`
DownloadURL string `json:"downloadURL"`
Versions []string `json:"versions"`
TargetContainerRuntime string `json:"targetContainerRuntime,omitempty"`
}
// KubeProxyImages is the data model for the kube-proxy image config.
// The kube-proxy image config is a JSON file, for example, vhdbuilder/packer/kube-proxy-images.json.
type KubeProxyImages struct {
DockerKubeProxyImages *DockerKubeProxyImages `json:"dockerKubeProxyImages"`
ContainerdKubeProxyImages *DockerKubeProxyImages `json:"containerdKubeProxyImages"`
}
type DockerKubeProxyImages struct {
ContainerImages []*ContainerImage `json:"ContainerImages"`
}
func loadJSONFromFile(path string, v interface{}) error {
configFile, err := os.Open(path)
if err != nil {
return err
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
return jsonParser.Decode(&v)
}
func toImageList(downloadURL string, imageTagList []string) ([]string, error) {
ret := []string{}
if !strings.Contains(downloadURL, "*") {
return ret, fmt.Errorf("downloadURL does not contain *")
}
for _, tag := range imageTagList {
ret = append(ret, strings.Replace(downloadURL, "*", tag, 1))
}
return ret, nil
}
// begins Components
// NewComponentsFromFile loads component config from the given file and returns a Components object.
func NewComponentsFromFile(path string) (*Components, error) {
ret := &Components{}
err := loadJSONFromFile(path, ret)
if err != nil {
return nil, err
}
return ret, nil
}
/*
ToImageList returns a list of image names from the given Components object.
The image names are generated from the given Components object and the given downloadURL.
The image names are generated by replacing the * in the downloadURL with the image tag.
*/
func (c *Components) ToImageList() []string {
ret := []string{}
if c.ContainerImages != nil {
for _, image := range c.ContainerImages {
if image.Amd64OnlyVersions != nil {
amd64OnlyImageList, _ := toImageList(image.DownloadURL, image.Amd64OnlyVersions)
ret = append(ret, amd64OnlyImageList...)
}
if image.MultiArchVersions != nil {
multiArchImageList, _ := toImageList(image.DownloadURL, image.MultiArchVersions)
ret = append(ret, multiArchImageList...)
}
}
}
return ret
}
// ends Components
// begins KubeProxyImages
/*
NewKubeProxyImagesFromFile loads kube-proxy image config from the given file and returns a KubeProxyImages object.
The given file should be a KubeProxyImages object, and should be in JSON format.
The given file should be in JSON format.
*/
func NewKubeProxyImagesFromFile(path string) (*KubeProxyImages, error) {
ret := &KubeProxyImages{}
err := loadJSONFromFile(path, ret)
if err != nil {
return nil, err
}
return ret, nil
}
// processProxyImages is a helper function for ToImageList().
func processProxyImages(image *ContainerImage, ret *[]string) error {
var err error
var amd64OnlyImageList []string
var multiArchImageList []string
if image.Amd64OnlyVersions != nil {
amd64OnlyImageList, err = toImageList(image.DownloadURL, image.Amd64OnlyVersions)
if err != nil {
return err
}
*ret = append(*ret, amd64OnlyImageList...)
}
if image.MultiArchVersions != nil {
multiArchImageList, err = toImageList(image.DownloadURL, image.MultiArchVersions)
if err != nil {
return err
}
*ret = append(*ret, multiArchImageList...)
}
return err
}
/*
ToImageList returns a list of image names from the given KubeProxyImages object.
The image names are generated from the given KubeProxyImages object and the given downloadURL.
The image names are generated by replacing the * in the downloadURL with the image tag.
*/
func (k *KubeProxyImages) ToImageList() ([]string, error) {
var err error
var ret []string
if k.DockerKubeProxyImages != nil && k.DockerKubeProxyImages.ContainerImages != nil {
for _, image := range k.DockerKubeProxyImages.ContainerImages {
err = processProxyImages(image, &ret)
if err != nil {
return ret, err
}
}
}
if k.ContainerdKubeProxyImages != nil && k.ContainerdKubeProxyImages.ContainerImages != nil {
for _, image := range k.ContainerdKubeProxyImages.ContainerImages {
err = processProxyImages(image, &ret)
if err != nil {
return ret, err
}
}
}
return ret, err
}