Skip to content

Commit

Permalink
chore: add datamodel for component config files (#3560)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDYA authored Aug 24, 2023
1 parent 1675412 commit 39f2bff
Show file tree
Hide file tree
Showing 3 changed files with 487 additions and 0 deletions.
167 changes: 167 additions & 0 deletions pkg/vhdbuilder/datamodel/component_configs.go
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

Check failure on line 12 in pkg/vhdbuilder/datamodel/component_configs.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
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

Check failure on line 34 in pkg/vhdbuilder/datamodel/component_configs.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
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 {

Check warning on line 44 in pkg/vhdbuilder/datamodel/component_configs.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: func loadJsonFromFile should be loadJSONFromFile (revive)
configFile, err := os.Open(path)
defer configFile.Close()

Check failure on line 46 in pkg/vhdbuilder/datamodel/component_configs.go

View workflow job for this annotation

GitHub Actions / lint

SA5001: should check returned error before deferring configFile.Close() (staticcheck)

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

Check failure on line 73 in pkg/vhdbuilder/datamodel/component_configs.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
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

Check failure on line 90 in pkg/vhdbuilder/datamodel/component_configs.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
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

Check failure on line 117 in pkg/vhdbuilder/datamodel/component_configs.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
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

Check failure on line 134 in pkg/vhdbuilder/datamodel/component_configs.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
func (k *KubeProxyImages) ToImageList() []string {

Check failure on line 135 in pkg/vhdbuilder/datamodel/component_configs.go

View workflow job for this annotation

GitHub Actions / lint

cognitive complexity 24 of func `(*KubeProxyImages).ToImageList` is high (> 20) (gocognit)
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
}
Loading

0 comments on commit 39f2bff

Please sign in to comment.