Skip to content

Commit d36e409

Browse files
authored
Add DevfileOptions to GetPodTemplateSpec (#167)
* Add DevfileOptions to GetPodTemplateSpec Signed-off-by: Philippe Martin <phmartin@redhat.com> * Fix Options visibility + add tests + Copyright Dates Signed-off-by: Philippe Martin <phmartin@redhat.com> * More unit tests Signed-off-by: Philippe Martin <phmartin@redhat.com> --------- Signed-off-by: Philippe Martin <phmartin@redhat.com>
1 parent f87c692 commit d36e409

File tree

2 files changed

+118
-3
lines changed

2 files changed

+118
-3
lines changed

pkg/devfile/generator/generators.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2022 Red Hat, Inc.
2+
// Copyright 2022-2023 Red Hat, Inc.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -238,6 +238,7 @@ func GetDeployment(devfileObj parser.DevfileObj, deployParams DeploymentParams)
238238
// PodTemplateParams is a struct that contains the required data to create a podtemplatespec object
239239
type PodTemplateParams struct {
240240
ObjectMeta metav1.ObjectMeta
241+
Options common.DevfileOptions
241242
// PodSecurityAdmissionPolicy is the policy to be respected by the created pod
242243
// The pod will be patched, if necessary, to respect the policies
243244
PodSecurityAdmissionPolicy psaapi.Policy
@@ -249,8 +250,9 @@ type PodTemplateParams struct {
249250
// - gets the init container for every preStart devfile event
250251
// - patches the pod template and containers to satisfy PodSecurityAdmissionPolicy
251252
// - patches the pod template and containers to apply pod and container overrides
253+
// The containers included in the podTemplateSpec can be filtered using podTemplateParams.Options
252254
func GetPodTemplateSpec(devfileObj parser.DevfileObj, podTemplateParams PodTemplateParams) (*corev1.PodTemplateSpec, error) {
253-
containers, err := GetContainers(devfileObj, common.DevfileOptions{})
255+
containers, err := GetContainers(devfileObj, podTemplateParams.Options)
254256
if err != nil {
255257
return nil, err
256258
}

pkg/devfile/generator/generators_test.go

+114-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2022 Red Hat, Inc.
2+
// Copyright 2022-2023 Red Hat, Inc.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package generator
1717

1818
import (
19+
"errors"
1920
"fmt"
2021
"reflect"
2122
"strings"
@@ -24,10 +25,13 @@ import (
2425
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2526
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2627
"github.com/devfile/api/v2/pkg/attributes"
28+
"github.com/devfile/library/v2/pkg/devfile"
2729
"github.com/devfile/library/v2/pkg/devfile/parser"
30+
context "github.com/devfile/library/v2/pkg/devfile/parser/context"
2831
"github.com/devfile/library/v2/pkg/devfile/parser/data"
2932
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
3033
"github.com/devfile/library/v2/pkg/testingutil"
34+
"github.com/devfile/library/v2/pkg/testingutil/filesystem"
3135
"github.com/devfile/library/v2/pkg/util"
3236
"github.com/golang/mock/gomock"
3337
"github.com/google/go-cmp/cmp"
@@ -1345,6 +1349,53 @@ func TestGetPodTemplateSpec(t *testing.T) {
13451349
},
13461350
wantErr: true,
13471351
},
1352+
{
1353+
name: "GetContainers returns err",
1354+
args: args{
1355+
devfileObj: func(ctrl *gomock.Controller) parser.DevfileObj {
1356+
mockDevfileData := data.NewMockDevfileData(ctrl)
1357+
mockDevfileData.EXPECT().GetComponents(gomock.Any()).Return(nil, errors.New("an error")).AnyTimes()
1358+
return parser.DevfileObj{
1359+
Data: mockDevfileData,
1360+
}
1361+
},
1362+
},
1363+
wantErr: true,
1364+
},
1365+
{
1366+
name: "GetDevfileContainerComponents returns err",
1367+
args: args{
1368+
devfileObj: func(ctrl *gomock.Controller) parser.DevfileObj {
1369+
containers := []v1alpha2.Component{
1370+
{
1371+
Name: "main",
1372+
ComponentUnion: v1.ComponentUnion{
1373+
Container: &v1.ContainerComponent{
1374+
Container: v1.Container{
1375+
Image: "an-image",
1376+
},
1377+
},
1378+
},
1379+
Attributes: attributes.Attributes{
1380+
ContainerOverridesAttribute: apiext.JSON{Raw: []byte("{\"spec\": \"serviceAccountName\": \"new-service-account\"}")},
1381+
},
1382+
},
1383+
}
1384+
events := v1alpha2.Events{}
1385+
mockDevfileData := data.NewMockDevfileData(ctrl)
1386+
mockDevfileData.EXPECT().GetComponents(gomock.Any()).Return(containers, nil).AnyTimes()
1387+
mockDevfileData.EXPECT().GetProjects(gomock.Any()).Return(nil, nil).AnyTimes()
1388+
mockDevfileData.EXPECT().GetEvents().Return(events).AnyTimes()
1389+
mockDevfileData.EXPECT().GetDevfileContainerComponents(gomock.Any()).Return(nil, errors.New("an error")).AnyTimes()
1390+
mockDevfileData.EXPECT().GetAttributes().Return(attributes.Attributes{}, nil)
1391+
mockDevfileData.EXPECT().GetSchemaVersion().Return("2.1.0").AnyTimes()
1392+
return parser.DevfileObj{
1393+
Data: mockDevfileData,
1394+
}
1395+
},
1396+
},
1397+
wantErr: true,
1398+
},
13481399
{
13491400
name: "Devfile with local container-override on the first container only",
13501401
args: args{
@@ -1833,6 +1884,68 @@ func TestGetPodTemplateSpec(t *testing.T) {
18331884
},
18341885
},
18351886
},
1887+
{
1888+
name: "Filter components by name",
1889+
args: args{
1890+
devfileObj: func(ctrl *gomock.Controller) parser.DevfileObj {
1891+
containers := []v1alpha2.Component{
1892+
{
1893+
Name: "main",
1894+
ComponentUnion: v1.ComponentUnion{
1895+
Container: &v1.ContainerComponent{
1896+
Container: v1.Container{
1897+
Image: "an-image",
1898+
},
1899+
},
1900+
},
1901+
},
1902+
{
1903+
Name: "tools",
1904+
ComponentUnion: v1.ComponentUnion{
1905+
Container: &v1.ContainerComponent{
1906+
Container: v1.Container{
1907+
Image: "a-tool-image",
1908+
},
1909+
},
1910+
},
1911+
},
1912+
}
1913+
parserArgs := parser.ParserArgs{
1914+
Data: []byte(`schemaVersion: 2.2.0`),
1915+
}
1916+
var err error
1917+
devfile, _, err := devfile.ParseDevfileAndValidate(parserArgs)
1918+
if err != nil {
1919+
t.Errorf("error creating devfile: %v", err)
1920+
}
1921+
devfile.Ctx = context.FakeContext(filesystem.NewFakeFs(), "/devfile.yaml")
1922+
devfile.Data.AddComponents(containers)
1923+
return devfile
1924+
},
1925+
podTemplateParams: PodTemplateParams{
1926+
Options: common.DevfileOptions{
1927+
FilterByName: "tools",
1928+
},
1929+
},
1930+
},
1931+
want: &corev1.PodTemplateSpec{
1932+
Spec: corev1.PodSpec{
1933+
Containers: []corev1.Container{
1934+
{
1935+
Name: "tools",
1936+
Image: "a-tool-image",
1937+
Env: []corev1.EnvVar{
1938+
{Name: "PROJECTS_ROOT", Value: "/projects"},
1939+
{Name: "PROJECT_SOURCE", Value: "/projects"},
1940+
},
1941+
ImagePullPolicy: corev1.PullAlways,
1942+
Ports: []corev1.ContainerPort{},
1943+
},
1944+
},
1945+
InitContainers: []corev1.Container{},
1946+
},
1947+
},
1948+
},
18361949
}
18371950
for _, tt := range tests {
18381951
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)