Skip to content

Commit

Permalink
feat: migrate to tag-based images
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Nov 11, 2023
1 parent cdd7a56 commit eb7c9b6
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 42 deletions.
1 change: 1 addition & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ vars:
PLATFORM: sulfone
SERVICE: boron
USER: kirinnee/
REGISTRY_HOST: http://host.docker.internal:9001

includes:
docker: tasks/Taskfile.docker.yaml
Expand Down
8 changes: 4 additions & 4 deletions docker_executor/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (d *DockerClient) ListImages() ([]DockerImageReference, error) {
var imageNames []DockerImageReference

for _, image := range images {
for _, digest := range image.RepoDigests {
s, err := DockerImageToStruct(digest)
for _, tags := range image.RepoTags {
s, err := DockerImageToStruct(tags)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -116,8 +116,8 @@ func (d *DockerClient) GetCoordinatorImage() (DockerImageReference, error) {

}

for _, digest := range latest.RepoDigests {
s, e := DockerImageToStruct(digest)
for _, tag := range latest.RepoTags {
s, e := DockerImageToStruct(tag)
if e != nil {
return DockerImageReference{}, e
}
Expand Down
17 changes: 6 additions & 11 deletions docker_executor/domain_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,22 @@ func InsertDash(uuid string) string {

type DockerImageReference struct {
Reference string
Sha string
Tag string
}

func DockerImageToString(image DockerImageReference) string {
return fmt.Sprintf("%s@sha256:%s", image.Reference, image.Sha)
return fmt.Sprintf("%s:%s", image.Reference, image.Tag)
}

func DockerImageToStruct(imageString string) (DockerImageReference, error) {
parts := strings.Split(imageString, "@")
parts := strings.Split(imageString, ":")
if len(parts) != 2 {
fmt.Println("Invalid image string format")
return DockerImageReference{}, errors.New("invalid image string format")
}
reference := parts[0]
shaWithPrefix := parts[1]

sha := strings.TrimPrefix(shaWithPrefix, "sha256:")

return DockerImageReference{
Reference: reference,
Sha: sha,
Reference: parts[0],
Tag: parts[1],
}, nil
}

Expand Down Expand Up @@ -73,7 +68,7 @@ func DockerContainerNameToStruct(name string) (DockerContainerReference, error)
SessionId: sessionId,
}, nil
}
return DockerContainerReference{}, errors.New("invalid container name")
return DockerContainerReference{}, errors.New("invalid container name: " + name)
}

type DockerVolumeReference struct {
Expand Down
12 changes: 6 additions & 6 deletions docker_executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func (e Executor) missingPluginsImages(images []DockerImageReference) []DockerIm
for _, plugin := range e.Template.Plugins {
c := DockerImageReference{
Reference: plugin.DockerReference,
Sha: plugin.DockerSHA,
Tag: plugin.DockerTag,
}
found := false
for _, i := range images {
if i.Reference == plugin.DockerReference && i.Sha == plugin.DockerSHA {
if i.Reference == plugin.DockerReference && i.Tag == plugin.DockerTag {
found = true
break
}
Expand All @@ -41,11 +41,11 @@ func (e Executor) missingProcessorImages(images []DockerImageReference) []Docker
for _, processor := range e.Template.Processors {
c := DockerImageReference{
Reference: processor.DockerReference,
Sha: processor.DockerSHA,
Tag: processor.DockerTag,
}
found := false
for _, i := range images {
if i.Reference == processor.DockerReference && i.Sha == processor.DockerSHA {
if i.Reference == processor.DockerReference && i.Tag == processor.DockerTag {
found = true
break
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func (e Executor) startProcessors(session string, temVolRef, workVolRef DockerVo
semaphore <- 0
i := DockerImageReference{
Reference: processor.DockerReference,
Sha: processor.DockerSHA,
Tag: processor.DockerTag,
}
c := DockerContainerReference{
CyanId: processor.ID,
Expand Down Expand Up @@ -159,7 +159,7 @@ func (e Executor) startPlugins(session string, temVolRef, workVolRef DockerVolum
semaphore <- 0
i := DockerImageReference{
Reference: plugin.DockerReference,
Sha: plugin.DockerSHA,
Tag: plugin.DockerTag,
}
c := DockerContainerReference{
CyanId: plugin.ID,
Expand Down
4 changes: 2 additions & 2 deletions docker_executor/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type RegistryPluginVersionPrincipalRes struct {
CreatedAt string `json:"created_at"`
Description string `json:"description"`
DockerReference string `json:"dockerReference"`
DockerSha string `json:"dockerSha"`
DockerTag string `json:"dockerTag"`
}

type RegistryPluginRes struct {
Expand All @@ -131,7 +131,7 @@ type RegistryProcessorVersionPrincipalRes struct {
CreatedAt string `json:"created_at"`
Description string `json:"description"`
DockerReference string `json:"dockerReference"`
DockerSha string `json:"dockerSha"`
DockerTag string `json:"dockerTag"`
}

type RegistryProcessorRes struct {
Expand Down
8 changes: 4 additions & 4 deletions docker_executor/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type PluginRes struct {
CreatedAt string `json:"createdAt"`
Description string `json:"description"`
DockerReference string `json:"dockerReference"`
DockerSHA string `json:"dockerSha"`
DockerTag string `json:"dockerTag"`
}

type ProcessorRes struct {
Expand All @@ -22,7 +22,7 @@ type ProcessorRes struct {
CreatedAt string `json:"createdAt"`
Description string `json:"description"`
DockerReference string `json:"dockerReference"`
DockerSHA string `json:"dockerSha"`
DockerTag string `json:"dockerTag"`
}

type TemplateVersionPrincipalRes struct {
Expand All @@ -31,9 +31,9 @@ type TemplateVersionPrincipalRes struct {
CreatedAt string `json:"createdAt"`
Description string `json:"description"`
BlobDockerReference string `json:"blobDockerReference"`
BlobDockerSHA string `json:"blobDockerSha"`
BlobDockerTag string `json:"blobDockerTag"`
TemplateDockerReference string `json:"templateDockerReference"`
TemplateDockerSHA string `json:"templateDockerSha"`
TemplateDockerTag string `json:"templateDockerTag"`
}

type TemplatePrincipalRes struct {
Expand Down
12 changes: 8 additions & 4 deletions docker_executor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type RegistryClient struct {
func (rc RegistryClient) getProcessorVersion(username string, name string, version string) (RegistryProcessorVersionRes, error) {
url := rc.Endpoint + "/api/v1/Processor/slug/" + username + "/" + name + "/versions/" + version

fmt.Println("🔍 Getting version of processor:", url)
resp, err := http.Get(url)
if err != nil {
fmt.Printf("🚨 Error occurred making a request to %s: %v\n", url, err)
Expand All @@ -31,7 +32,7 @@ func (rc RegistryClient) getProcessorVersion(username string, name string, versi

if resp.StatusCode != http.StatusOK {
fmt.Printf("🚨 Unexpected status code: %d. Body: %s\n", resp.StatusCode, body)
return RegistryProcessorVersionRes{}, err
return RegistryProcessorVersionRes{}, fmt.Errorf("unexpected status code: %d. Body: %s\n", resp.StatusCode, body)
}

var res RegistryProcessorVersionRes
Expand All @@ -46,6 +47,7 @@ func (rc RegistryClient) getProcessorVersion(username string, name string, versi
func (rc RegistryClient) getProcessorVersionLatest(username string, name string) (RegistryProcessorVersionRes, error) {
url := rc.Endpoint + "/api/v1/Processor/slug/" + username + "/" + name + "/versions/latest"

fmt.Println("🔍 Getting latest version of processor:", url)
resp, err := http.Get(url)
if err != nil {
fmt.Printf("🚨 Error occurred making a request to %s: %v\n", url, err)
Expand All @@ -63,7 +65,7 @@ func (rc RegistryClient) getProcessorVersionLatest(username string, name string)

if resp.StatusCode != http.StatusOK {
fmt.Printf("🚨 Unexpected status code: %d. Body: %s\n", resp.StatusCode, body)
return RegistryProcessorVersionRes{}, err
return RegistryProcessorVersionRes{}, fmt.Errorf("unexpected status code: %d. Body: %s\n", resp.StatusCode, body)
}

var res RegistryProcessorVersionRes
Expand All @@ -78,6 +80,7 @@ func (rc RegistryClient) getProcessorVersionLatest(username string, name string)
func (rc RegistryClient) getPluginVersion(username string, name string, version string) (RegistryPluginVersionRes, error) {
url := rc.Endpoint + "/api/v1/Plugin/slug/" + username + "/" + name + "/versions/" + version

fmt.Println("🔍 Getting version of plugin:", url)
resp, err := http.Get(url)
if err != nil {
fmt.Printf("🚨 Error occurred making a request to %s: %v\n", url, err)
Expand All @@ -95,7 +98,7 @@ func (rc RegistryClient) getPluginVersion(username string, name string, version

if resp.StatusCode != http.StatusOK {
fmt.Printf("🚨 Unexpected status code: %d. Body: %s\n", resp.StatusCode, body)
return RegistryPluginVersionRes{}, err
return RegistryPluginVersionRes{}, fmt.Errorf("unexpected status code: %d. Body: %s\n", resp.StatusCode, body)
}

var res RegistryPluginVersionRes
Expand All @@ -110,6 +113,7 @@ func (rc RegistryClient) getPluginVersion(username string, name string, version
func (rc RegistryClient) getPluginVersionLatest(username string, name string) (RegistryPluginVersionRes, error) {
url := rc.Endpoint + "/api/v1/Plugin/slug/" + username + "/" + name + "/versions/latest"

fmt.Println("🔍 Getting latest version of plugin:", url)
resp, err := http.Get(url)
if err != nil {
fmt.Printf("🚨 Error occurred making a request to %s: %v\n", url, err)
Expand All @@ -127,7 +131,7 @@ func (rc RegistryClient) getPluginVersionLatest(username string, name string) (R

if resp.StatusCode != http.StatusOK {
fmt.Printf("🚨 Unexpected status code: %d. Body: %s\n", resp.StatusCode, body)
return RegistryPluginVersionRes{}, err
return RegistryPluginVersionRes{}, fmt.Errorf("unexpected status code: %d. Body: %s\n", resp.StatusCode, body)
}

var res RegistryPluginVersionRes
Expand Down
12 changes: 6 additions & 6 deletions docker_executor/template_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func (de TemplateExecutor) missingTemplateVolumeImage(images []DockerImageRefere
template := de.Template
i := DockerImageReference{
Reference: template.BlobDockerReference,
Sha: template.BlobDockerSHA,
Tag: template.BlobDockerTag,
}
for _, image := range images {
if image.Reference == i.Reference && image.Sha == i.Sha {
if image.Reference == i.Reference && image.Tag == i.Tag {
return false, i
}
}
Expand All @@ -60,10 +60,10 @@ func (de TemplateExecutor) missingTemplateImages(images []DockerImageReference)
template := de.Template
i := DockerImageReference{
Reference: template.TemplateDockerReference,
Sha: template.TemplateDockerSHA,
Tag: template.TemplateDockerTag,
}
for _, image := range images {
if image.Reference == i.Reference && image.Sha == i.Sha {
if image.Reference == i.Reference && image.Tag == i.Tag {
return false, i
}
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (de TemplateExecutor) listContainersVolumesImages() (
func (de TemplateExecutor) startContainer(conRef DockerContainerReference) error {
imageRef := DockerImageReference{
Reference: de.Template.TemplateDockerReference,
Sha: de.Template.TemplateDockerSHA,
Tag: de.Template.TemplateDockerTag,
}
err := de.Docker.CreateContainer(conRef, imageRef)
if err != nil {
Expand All @@ -168,7 +168,7 @@ func (de TemplateExecutor) startVolume(volRef DockerVolumeReference) error {
}
unzipImage := DockerImageReference{
Reference: de.Template.BlobDockerReference,
Sha: de.Template.BlobDockerSHA,
Tag: de.Template.BlobDockerTag,
}
unzipContainer := DockerContainerReference{
CyanId: de.Template.ID,
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ func main() {
Commands: []*cli.Command{
{
Name: "start",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "registry",
Aliases: []string{"r"},
Value: "https://api.zinc.sulfone.raichu.cluster.atomi.cloud",
},
},
Action: func(context *cli.Context) error {
server()
registry := context.String("registry")
server(registry)
return nil
},
},
Expand Down
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func stringifyErrors(e []error) []string {
return errs
}

func server() {
func server(registryEndpoint string) {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, docker_executor.StandardResponse{Status: "OK"})
Expand Down Expand Up @@ -85,7 +85,7 @@ func server() {
merger := docker_executor.Merger{
ParallelismLimit: cpu,
RegistryClient: docker_executor.RegistryClient{
Endpoint: "https://api.zinc.sulfone.raichu.cluster.atomi.cloud",
Endpoint: registryEndpoint,
},
Template: req.Template,
SessionId: sessionId,
Expand Down Expand Up @@ -642,7 +642,7 @@ func server() {
m := docker_executor.Merger{
ParallelismLimit: cpu,
RegistryClient: docker_executor.RegistryClient{
Endpoint: "https://api.zinc.sulfone.raichu.cluster.atomi.cloud",
Endpoint: registryEndpoint,
},
Template: req.Template,
SessionId: sessionId,
Expand Down
2 changes: 1 addition & 1 deletion tasks/Taskfile.docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tasks:
desc: "Run docker images"
cmds:
- docker run --rm -v /var/run/docker.sock:/var/run/docker.sock {{.USER}}{{.PLATFORM}}-{{.SERVICE}}:{{.CLI_ARGS |default "latest"}} setup
- docker run --rm -it -p 9000:9000 --network cyanprint -v /var/run/docker.sock:/var/run/docker.sock {{.USER}}{{.PLATFORM}}-{{.SERVICE}}:{{.CLI_ARGS |default "latest"}}
- docker run --rm -it -p 9000:9000 --network cyanprint -v /var/run/docker.sock:/var/run/docker.sock {{.USER}}{{.PLATFORM}}-{{.SERVICE}}:{{.CLI_ARGS |default "latest"}} start -r {{.REGISTRY_HOST}}
push:
desc: "Push docker images"
cmds:
Expand Down

0 comments on commit eb7c9b6

Please # to comment.