Skip to content

Commit 726062d

Browse files
committed
Add variable subs for image component
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent 0740aa7 commit 726062d

14 files changed

+286
-25
lines changed

pkg/validation/variables/test-fixtures/all/devfile-bad-output.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ components:
5757
- name: component4
5858
openshift:
5959
uri: "{{foo}}"
60+
- name: component5
61+
image:
62+
imageName: "myimage:{{ tag }}"
63+
dockerfile:
64+
uri: "{{foo}}/Dockerfile"
65+
buildContext: /{{foo}}/{{foo}}
66+
args:
67+
- "-f"
68+
- "/dev/null"
6069
commands:
6170
- id: command1
6271
exec:

pkg/validation/variables/test-fixtures/all/devfile-bad.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ components:
5757
- name: component4
5858
openshift:
5959
uri: "{{foo}}"
60+
- name: component5
61+
image:
62+
imageName: "myimage:{{ tag }}"
63+
dockerfile:
64+
uri: "{{foo}}/Dockerfile"
65+
buildContext: /{{foo}}/{{foo}}
66+
args:
67+
- "-f"
68+
- "{{ devnull }}"
6069
commands:
6170
- id: command1
6271
exec:

pkg/validation/variables/test-fixtures/all/devfile-good-output.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ components:
4242
- name: endpoint1
4343
exposure: "public"
4444
targetPort: 9999
45+
- name: component3
46+
image:
47+
imageName: "myimage:xyz"
48+
dockerfile:
49+
uri: "FOO/Dockerfile"
50+
buildContext: /FOO/FOO
51+
args:
52+
- "-f"
53+
- "/dev/null"
4554
commands:
4655
- id: command1
4756
exec:

pkg/validation/variables/test-fixtures/all/devfile-good.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ components:
4242
- name: endpoint1
4343
exposure: "public"
4444
targetPort: 9999
45+
- name: component3
46+
image:
47+
imageName: "myimage:{{ tag }}"
48+
dockerfile:
49+
uri: "{{foo}}/Dockerfile"
50+
buildContext: /{{foo}}/{{foo}}
51+
args:
52+
- "-f"
53+
- "{{ devnull }}"
4554
commands:
4655
- id: command1
4756
exec:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
imageName: "myimage:xyz"
2+
dockerfile:
3+
git:
4+
gitLocation: "uri/Dockerfile"
5+
checkoutFrom:
6+
revision: "FOO"
7+
remote: "BAR"
8+
remotes:
9+
"foo": "BAR"
10+
"FOOBAR": "BARFOO"
11+
buildContext: /FOO/BAR
12+
rootRequired: true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
imageName: "myimage:{{ tag }}"
2+
dockerfile:
3+
git:
4+
gitLocation: "{{ uri }}/Dockerfile"
5+
checkoutFrom:
6+
revision: "{{foo}}"
7+
remote: "{{bar}}"
8+
remotes:
9+
"foo": "{{bar}}"
10+
"{{foo}}{{bar}}": "{{ bar }}{{ foo }}"
11+
buildContext: /{{foo}}/{{bar}}
12+
rootRequired: true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
imageName: "myimage:xyz"
2+
dockerfile:
3+
registry:
4+
id: "FOO/BAR"
5+
devfileRegistryUrl: "http://uri"
6+
buildContext: /FOO/BAR
7+
rootRequired: true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
imageName: "myimage:{{ tag }}"
2+
dockerfile:
3+
registry:
4+
id: "{{foo}}/{{bar}}"
5+
devfileRegistryUrl: "http://{{ uri }}"
6+
buildContext: /{{foo}}/{{bar}}
7+
rootRequired: true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
imageName: "myimage:xyz"
2+
dockerfile:
3+
uri: "uri/Dockerfile"
4+
buildContext: /FOO/BAR
5+
args:
6+
- "-f"
7+
- "/dev/null"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
imageName: "myimage:{{ tag }}"
2+
dockerfile:
3+
uri: "{{uri}}/Dockerfile"
4+
buildContext: /{{foo}}/{{bar}}
5+
args:
6+
- "-f"
7+
- "{{ devnull }}"

pkg/validation/variables/variables_component.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ func ValidateAndReplaceForComponents(variables map[string]string, components []v
3333
componentsWarningMap[components[i].Name] = verr.Keys
3434
}
3535
}
36+
case components[i].Image != nil:
37+
if err = validateAndReplaceForImageComponent(variables, components[i].Image); err != nil {
38+
if verr, ok := err.(*InvalidKeysError); ok {
39+
componentsWarningMap[components[i].Name] = verr.Keys
40+
}
41+
}
3642
case components[i].Volume != nil:
3743
if err = validateAndReplaceForVolumeComponent(variables, components[i].Volume); err != nil {
3844
if verr, ok := err.(*InvalidKeysError); ok {
@@ -189,6 +195,81 @@ func validateAndReplaceForOpenShiftComponent(variables map[string]string, opensh
189195
return newInvalidKeysError(invalidKeys)
190196
}
191197

198+
// validateAndReplaceForImageComponent validates the image component data for global variable references and replaces them with the variable value
199+
func validateAndReplaceForImageComponent(variables map[string]string, image *v1alpha2.ImageComponent) error {
200+
var err error
201+
202+
invalidKeys := make(map[string]bool)
203+
204+
if image != nil {
205+
// Validate image's image name
206+
if image.ImageName, err = validateAndReplaceDataWithVariable(image.ImageName, variables); err != nil {
207+
checkForInvalidError(invalidKeys, err)
208+
}
209+
210+
switch {
211+
case image.Dockerfile != nil:
212+
if err = validateAndReplaceForDockerfileImageComponent(variables, image.Dockerfile); err != nil {
213+
checkForInvalidError(invalidKeys, err)
214+
}
215+
}
216+
217+
}
218+
219+
return newInvalidKeysError(invalidKeys)
220+
}
221+
222+
// validateAndReplaceForDockerfileImageComponent validates the dockerfile image component data for global variable references and replaces them with the variable value
223+
func validateAndReplaceForDockerfileImageComponent(variables map[string]string, dockerfileImage *v1alpha2.DockerfileImage) error {
224+
var err error
225+
226+
invalidKeys := make(map[string]bool)
227+
228+
if dockerfileImage != nil {
229+
230+
switch {
231+
case dockerfileImage.Uri != "":
232+
// Validate dockerfile image URI
233+
if dockerfileImage.Uri, err = validateAndReplaceDataWithVariable(dockerfileImage.Uri, variables); err != nil {
234+
checkForInvalidError(invalidKeys, err)
235+
}
236+
case dockerfileImage.Git != nil:
237+
// Validate dockerfile Git location
238+
if dockerfileImage.Git.GitLocation, err = validateAndReplaceDataWithVariable(dockerfileImage.Git.GitLocation, variables); err != nil {
239+
checkForInvalidError(invalidKeys, err)
240+
}
241+
242+
gitProject := &dockerfileImage.Git.GitLikeProjectSource
243+
if err = validateAndReplaceForGitProjectSource(variables, gitProject); err != nil {
244+
checkForInvalidError(invalidKeys, err)
245+
}
246+
case dockerfileImage.Registry != nil:
247+
// Validate dockerfile devfile registry src
248+
if dockerfileImage.Registry.Id, err = validateAndReplaceDataWithVariable(dockerfileImage.Registry.Id, variables); err != nil {
249+
checkForInvalidError(invalidKeys, err)
250+
}
251+
if dockerfileImage.Registry.DevfileRegistryUrl, err = validateAndReplaceDataWithVariable(dockerfileImage.Registry.DevfileRegistryUrl, variables); err != nil {
252+
checkForInvalidError(invalidKeys, err)
253+
}
254+
}
255+
256+
// Validate dockerfile image's build context
257+
if dockerfileImage.BuildContext, err = validateAndReplaceDataWithVariable(dockerfileImage.BuildContext, variables); err != nil {
258+
checkForInvalidError(invalidKeys, err)
259+
}
260+
261+
// Validate dockerfile image's args
262+
for i := range dockerfileImage.Args {
263+
if dockerfileImage.Args[i], err = validateAndReplaceDataWithVariable(dockerfileImage.Args[i], variables); err != nil {
264+
checkForInvalidError(invalidKeys, err)
265+
}
266+
}
267+
268+
}
269+
270+
return newInvalidKeysError(invalidKeys)
271+
}
272+
192273
// validateAndReplaceForVolumeComponent validates the volume component data for global variable references and replaces them with the variable value
193274
func validateAndReplaceForVolumeComponent(variables map[string]string, volume *v1alpha2.VolumeComponent) error {
194275
var err error

pkg/validation/variables/variables_component_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,81 @@ func TestValidateAndReplaceOpenShiftKubernetesComponent(t *testing.T) {
115115
}
116116
}
117117

118+
func TestValidateAndReplaceImageComponent(t *testing.T) {
119+
120+
tests := []struct {
121+
name string
122+
testFile string
123+
outputFile string
124+
variableFile string
125+
wantErr bool
126+
}{
127+
{
128+
name: "Good Substitution - dockerfile uri src",
129+
testFile: "test-fixtures/components/image-dockerfile-uri.yaml",
130+
outputFile: "test-fixtures/components/image-dockerfile-uri-output.yaml",
131+
variableFile: "test-fixtures/variables/variables-referenced.yaml",
132+
wantErr: false,
133+
},
134+
{
135+
name: "Good Substitution - dockerfile git src",
136+
testFile: "test-fixtures/components/image-dockerfile-git.yaml",
137+
outputFile: "test-fixtures/components/image-dockerfile-git-output.yaml",
138+
variableFile: "test-fixtures/variables/variables-referenced.yaml",
139+
wantErr: false,
140+
},
141+
{
142+
name: "Good Substitution - dockerfile registry src",
143+
testFile: "test-fixtures/components/image-dockerfile-registry.yaml",
144+
outputFile: "test-fixtures/components/image-dockerfile-registry-output.yaml",
145+
variableFile: "test-fixtures/variables/variables-referenced.yaml",
146+
wantErr: false,
147+
},
148+
{
149+
name: "Invalid Reference - dockerfile uri src",
150+
testFile: "test-fixtures/components/image-dockerfile-uri.yaml",
151+
outputFile: "test-fixtures/components/image-dockerfile-uri.yaml",
152+
variableFile: "test-fixtures/variables/variables-notreferenced.yaml",
153+
wantErr: true,
154+
},
155+
{
156+
name: "Invalid Reference - dockerfile git src",
157+
testFile: "test-fixtures/components/image-dockerfile-git.yaml",
158+
outputFile: "test-fixtures/components/image-dockerfile-git.yaml",
159+
variableFile: "test-fixtures/variables/variables-notreferenced.yaml",
160+
wantErr: true,
161+
},
162+
{
163+
name: "Invalid Reference - dockerfile registry src",
164+
testFile: "test-fixtures/components/image-dockerfile-registry.yaml",
165+
outputFile: "test-fixtures/components/image-dockerfile-registry.yaml",
166+
variableFile: "test-fixtures/variables/variables-notreferenced.yaml",
167+
wantErr: true,
168+
},
169+
}
170+
for _, tt := range tests {
171+
t.Run(tt.name, func(t *testing.T) {
172+
testImageComponent := v1alpha2.ImageComponent{}
173+
readFileToStruct(t, tt.testFile, &testImageComponent)
174+
175+
testVariable := make(map[string]string)
176+
readFileToStruct(t, tt.variableFile, &testVariable)
177+
178+
err := validateAndReplaceForImageComponent(testVariable, &testImageComponent)
179+
_, ok := err.(*InvalidKeysError)
180+
if tt.wantErr && !ok {
181+
t.Errorf("Expected InvalidKeysError error from test but got %+v", err)
182+
} else if !tt.wantErr && err != nil {
183+
t.Errorf("Got unexpected error: %s", err)
184+
} else {
185+
expectedImageComponent := v1alpha2.ImageComponent{}
186+
readFileToStruct(t, tt.outputFile, &expectedImageComponent)
187+
assert.Equal(t, expectedImageComponent, testImageComponent, "The two values should be the same.")
188+
}
189+
})
190+
}
191+
}
192+
118193
func TestValidateAndReplaceVolumeComponent(t *testing.T) {
119194

120195
tests := []struct {

pkg/validation/variables/variables_project.go

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,49 @@ func validateandReplaceForProjectSource(variables map[string]string, projectSour
8585
case projectSource.Git != nil:
8686
gitProject := &projectSource.Git.GitLikeProjectSource
8787

88-
if gitProject.CheckoutFrom != nil {
89-
// validate git checkout revision
90-
if gitProject.CheckoutFrom.Revision, err = validateAndReplaceDataWithVariable(gitProject.CheckoutFrom.Revision, variables); err != nil {
91-
checkForInvalidError(invalidKeys, err)
92-
}
93-
94-
// // validate git checkout remote
95-
if gitProject.CheckoutFrom.Remote, err = validateAndReplaceDataWithVariable(gitProject.CheckoutFrom.Remote, variables); err != nil {
96-
checkForInvalidError(invalidKeys, err)
97-
}
88+
if err = validateAndReplaceForGitProjectSource(variables, gitProject); err != nil {
89+
checkForInvalidError(invalidKeys, err)
90+
}
91+
}
92+
}
93+
94+
return newInvalidKeysError(invalidKeys)
95+
}
96+
97+
// validateAndReplaceForGitProjectSource validates a project git src for global variable references and replaces them with the variable value
98+
func validateAndReplaceForGitProjectSource(variables map[string]string, gitProject *v1alpha2.GitLikeProjectSource) error {
99+
100+
var err error
101+
102+
invalidKeys := make(map[string]bool)
103+
104+
if gitProject != nil {
105+
if gitProject.CheckoutFrom != nil {
106+
// validate git checkout revision
107+
if gitProject.CheckoutFrom.Revision, err = validateAndReplaceDataWithVariable(gitProject.CheckoutFrom.Revision, variables); err != nil {
108+
checkForInvalidError(invalidKeys, err)
98109
}
99110

100-
// validate git remotes
101-
for k := range gitProject.Remotes {
102-
// validate remote map value
103-
if gitProject.Remotes[k], err = validateAndReplaceDataWithVariable(gitProject.Remotes[k], variables); err != nil {
104-
checkForInvalidError(invalidKeys, err)
105-
}
106-
107-
// validate remote map key
108-
var updatedKey string
109-
if updatedKey, err = validateAndReplaceDataWithVariable(k, variables); err != nil {
110-
checkForInvalidError(invalidKeys, err)
111-
} else if updatedKey != k {
112-
gitProject.Remotes[updatedKey] = gitProject.Remotes[k]
113-
delete(gitProject.Remotes, k)
114-
}
111+
// // validate git checkout remote
112+
if gitProject.CheckoutFrom.Remote, err = validateAndReplaceDataWithVariable(gitProject.CheckoutFrom.Remote, variables); err != nil {
113+
checkForInvalidError(invalidKeys, err)
114+
}
115+
}
116+
117+
// validate git remotes
118+
for k := range gitProject.Remotes {
119+
// validate remote map value
120+
if gitProject.Remotes[k], err = validateAndReplaceDataWithVariable(gitProject.Remotes[k], variables); err != nil {
121+
checkForInvalidError(invalidKeys, err)
122+
}
123+
124+
// validate remote map key
125+
var updatedKey string
126+
if updatedKey, err = validateAndReplaceDataWithVariable(k, variables); err != nil {
127+
checkForInvalidError(invalidKeys, err)
128+
} else if updatedKey != k {
129+
gitProject.Remotes[updatedKey] = gitProject.Remotes[k]
130+
delete(gitProject.Remotes, k)
115131
}
116132
}
117133
}

pkg/validation/variables/variables_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestValidateGlobalVariableBasic(t *testing.T) {
3939
"component2": {"bar", "foo", "x"},
4040
"component3": {"xyz"},
4141
"component4": {"foo"},
42+
"component5": {"foo", "tag"},
4243
},
4344
Projects: map[string][]string{
4445
"project1": {"dir", "path", "tag", "version", "version1"},

0 commit comments

Comments
 (0)