Skip to content

Commit

Permalink
feature: Allow image tags to be used in place of digest (PROJQUAY-1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathankingfc committed Apr 16, 2021
1 parent 69bbb1a commit c58d804
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
18 changes: 10 additions & 8 deletions pkg/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ func componentImageFor(component v1.ComponentKind) types.Image {
}
image := os.Getenv(envVarFor[component])
if image != "" {
if len(strings.Split(image, "@")) != 2 {
panic(envVarFor[component] + " must use manifest digest reference")
if len(strings.Split(image, "@")) == 2 {
imageOverride.NewName = strings.Split(image, "@")[0]
imageOverride.Digest = strings.Split(image, "@")[1]
} else if len(strings.Split(image, ":")) == 2 {
imageOverride.NewName = strings.Split(image, ":")[0]
imageOverride.NewTag = strings.Split(image, ":")[1]
} else {
panic("image override must be reference by tag or manifest digest: " + image)
}

imageOverride.NewName = strings.Split(image, "@")[0]
imageOverride.Digest = strings.Split(image, "@")[1]
}

return imageOverride
Expand Down Expand Up @@ -315,9 +318,8 @@ func KustomizationFor(ctx *quaycontext.QuayRegistryContext, quay *v1.QuayRegistr
images := []types.Image{}
for _, component := range append(quay.Spec.Components, v1.Component{Kind: "base", Managed: true}) {
if component.Managed {
imageOverride := componentImageFor(component.Kind)
if imageOverride.Digest != "" {
images = append(images, imageOverride)
if image := componentImageFor(component.Kind); image.NewName != "" || image.Digest != "" {
images = append(images, componentImageFor(component.Kind))
}
}
}
Expand Down
39 changes: 38 additions & 1 deletion pkg/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ var kustomizationForTests = []struct {
},
"",
},
{
"ComponentImageOverridesWithTag",
&v1.QuayRegistry{
Spec: v1.QuayRegistrySpec{
Components: []v1.Component{
{Kind: "postgres", Managed: true},
{Kind: "clair", Managed: true},
{Kind: "redis", Managed: true},
},
},
},
quaycontext.QuayRegistryContext{},
&types.Kustomization{
TypeMeta: types.TypeMeta{
APIVersion: types.KustomizationVersion,
Kind: types.KustomizationKind,
},
Resources: []string{},
Components: []string{
"../components/postgres",
"../components/clair",
"../components/redis",
},
Images: []types.Image{
{Name: "quay.io/projectquay/quay", NewName: "quay", NewTag: "latest"},
{Name: "quay.io/projectquay/clair", NewName: "clair", NewTag: "alpine"},
{Name: "centos/redis-32-centos7", NewName: "redis", NewTag: "buster"},
{Name: "centos/postgresql-10-centos7", NewName: "postgres", NewTag: "latest"},
},
SecretGenerator: []types.SecretArgs{},
},
"",
},
}

func TestKustomizationFor(t *testing.T) {
Expand All @@ -107,7 +140,11 @@ func TestKustomizationFor(t *testing.T) {
for _, test := range kustomizationForTests {
if test.expected != nil {
for _, img := range test.expected.Images {
os.Setenv("RELATED_IMAGE_COMPONENT_"+strings.ToUpper(img.NewName), img.NewName+"@"+img.Digest)
if len(img.Digest) != 0 {
os.Setenv("RELATED_IMAGE_COMPONENT_"+strings.ToUpper(img.NewName), img.NewName+"@"+img.Digest)
} else {
os.Setenv("RELATED_IMAGE_COMPONENT_"+strings.ToUpper(img.NewName), img.NewName+":"+img.NewTag)
}
}
}

Expand Down

0 comments on commit c58d804

Please sign in to comment.