-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add datamodel for component config files (#3560)
- Loading branch information
Showing
3 changed files
with
487 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
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 | ||
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) | ||
defer configFile.Close() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
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 | ||
// 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 | ||
} | ||
|
||
// 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 { | ||
ret := []string{} | ||
|
||
if k.DockerKubeProxyImages != nil && k.DockerKubeProxyImages.ContainerImages != nil { | ||
for _, image := range k.DockerKubeProxyImages.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...) | ||
} | ||
} | ||
} | ||
|
||
if k.ContainerdKubeProxyImages != nil && k.ContainerdKubeProxyImages.ContainerImages != nil { | ||
for _, image := range k.ContainerdKubeProxyImages.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 | ||
} |
Oops, something went wrong.