Skip to content

Commit

Permalink
Modelling image build strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
laszlocph committed Mar 13, 2024
1 parent 066f672 commit 09932d2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
45 changes: 4 additions & 41 deletions pkg/dashboard/gitops/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,51 +385,14 @@ func ExtractImageStrategy(envConfig *dx.Manifest) string {
return "dynamic"
}

image := envConfig.Values["image"]
hasVariable := false
pointsToBuiltInRegistry := false
hasDockerfile := false

if image != nil {
if image, ok := envConfig.Values["image"]; ok {
imageMap := image.(map[string]interface{})

var repository, tag, dockerfile string
if val, ok := imageMap["repository"]; ok {
repository = val.(string)
}
if val, ok := imageMap["tag"]; ok {
tag = val.(string)
}
if val, ok := imageMap["dockerfile"]; ok {
dockerfile = val.(string)
}

if strings.Contains(repository, "{{") ||
strings.Contains(tag, "{{") {
hasVariable = true
}
if strings.Contains(repository, "127.0.0.1:32447") {
pointsToBuiltInRegistry = true
}
if dockerfile != "" {
hasDockerfile = true
}
}

strategy := "static"
if hasVariable {
if pointsToBuiltInRegistry {
if hasDockerfile {
strategy = "dockerfile"
} else {
strategy = "buildpacks"
}
} else {
strategy = "dynamic"
if strategy, ok := imageMap["strategy"]; ok {
return strategy.(string)
}
}

return strategy
return "dynamic"
}

func ExtractImageRepoTagAndDockerfile(envConfig *dx.Manifest, vars map[string]string) (string, string, string) {
Expand Down
34 changes: 33 additions & 1 deletion pkg/dashboard/gitops/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ func initHistory() *git.Repository {
return repo
}

/*
static-site
static
dynamic (artifact)
image:
repository:
tag:
dockerfile:
startegy: dynamic(default)/static/dockerfile/buildpacks
*/

func Test_ExtractImageStrategy(t *testing.T) {
strategy := ExtractImageStrategy(&dx.Manifest{})
assert.Equal(t, "dynamic", strategy)
Expand All @@ -174,7 +188,7 @@ func Test_ExtractImageStrategy(t *testing.T) {
Name: "onechart",
},
})
assert.Equal(t, "static", strategy)
assert.Equal(t, "dynamic", strategy)

strategy = ExtractImageStrategy(&dx.Manifest{
Chart: dx.Chart{
Expand All @@ -185,6 +199,7 @@ func Test_ExtractImageStrategy(t *testing.T) {
"image": map[string]interface{}{
"repository": "nginx",
"tag": "1.19",
"strategy": "static", //breaking change
},
},
})
Expand Down Expand Up @@ -221,8 +236,25 @@ func Test_ExtractImageStrategy(t *testing.T) {
"image": map[string]interface{}{
"repository": "127.0.0.1:32447",
"tag": "{{ .SHA }}",
"strategy": "buildpacks", //breaking change
},
},
})
assert.Equal(t, "buildpacks", strategy)

strategy = ExtractImageStrategy(&dx.Manifest{
Chart: dx.Chart{
Repository: "repository: https://chart.onechart.dev",
Name: "onechart",
},
Values: map[string]interface{}{
"image": map[string]interface{}{
"repository": "127.0.0.1:32447",
"tag": "{{ .SHA }}",
"dockerfile": "Dockerfile",
"strategy": "dockerfile",
},
},
})
assert.Equal(t, "dockerfile", strategy)
}

0 comments on commit 09932d2

Please sign in to comment.