Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DevfileOptions to GetPodTemplateSpec #167

Merged
merged 3 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/devfile/generator/generators.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022 Red Hat, Inc.
// Copyright 2022-2023 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -238,6 +238,7 @@ func GetDeployment(devfileObj parser.DevfileObj, deployParams DeploymentParams)
// PodTemplateParams is a struct that contains the required data to create a podtemplatespec object
type PodTemplateParams struct {
ObjectMeta metav1.ObjectMeta
Options common.DevfileOptions
// PodSecurityAdmissionPolicy is the policy to be respected by the created pod
// The pod will be patched, if necessary, to respect the policies
PodSecurityAdmissionPolicy psaapi.Policy
Expand All @@ -249,8 +250,9 @@ type PodTemplateParams struct {
// - gets the init container for every preStart devfile event
// - patches the pod template and containers to satisfy PodSecurityAdmissionPolicy
// - patches the pod template and containers to apply pod and container overrides
// The containers included in the podTemplateSpec can be filtered using podTemplateParams.Options
func GetPodTemplateSpec(devfileObj parser.DevfileObj, podTemplateParams PodTemplateParams) (*corev1.PodTemplateSpec, error) {
containers, err := GetContainers(devfileObj, common.DevfileOptions{})
containers, err := GetContainers(devfileObj, podTemplateParams.Options)
if err != nil {
return nil, err
}
Expand Down
115 changes: 114 additions & 1 deletion pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022 Red Hat, Inc.
// Copyright 2022-2023 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
package generator

import (
"errors"
"fmt"
"reflect"
"strings"
Expand All @@ -24,10 +25,13 @@ import (
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/attributes"
"github.com/devfile/library/v2/pkg/devfile"
"github.com/devfile/library/v2/pkg/devfile/parser"
context "github.com/devfile/library/v2/pkg/devfile/parser/context"
"github.com/devfile/library/v2/pkg/devfile/parser/data"
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
"github.com/devfile/library/v2/pkg/testingutil"
"github.com/devfile/library/v2/pkg/testingutil/filesystem"
"github.com/devfile/library/v2/pkg/util"
"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -1345,6 +1349,53 @@ func TestGetPodTemplateSpec(t *testing.T) {
},
wantErr: true,
},
{
name: "GetContainers returns err",
args: args{
devfileObj: func(ctrl *gomock.Controller) parser.DevfileObj {
mockDevfileData := data.NewMockDevfileData(ctrl)
mockDevfileData.EXPECT().GetComponents(gomock.Any()).Return(nil, errors.New("an error")).AnyTimes()
return parser.DevfileObj{
Data: mockDevfileData,
}
},
},
wantErr: true,
},
{
name: "GetDevfileContainerComponents returns err",
args: args{
devfileObj: func(ctrl *gomock.Controller) parser.DevfileObj {
containers := []v1alpha2.Component{
{
Name: "main",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{
Container: v1.Container{
Image: "an-image",
},
},
},
Attributes: attributes.Attributes{
ContainerOverridesAttribute: apiext.JSON{Raw: []byte("{\"spec\": \"serviceAccountName\": \"new-service-account\"}")},
},
},
}
events := v1alpha2.Events{}
mockDevfileData := data.NewMockDevfileData(ctrl)
mockDevfileData.EXPECT().GetComponents(gomock.Any()).Return(containers, nil).AnyTimes()
mockDevfileData.EXPECT().GetProjects(gomock.Any()).Return(nil, nil).AnyTimes()
mockDevfileData.EXPECT().GetEvents().Return(events).AnyTimes()
mockDevfileData.EXPECT().GetDevfileContainerComponents(gomock.Any()).Return(nil, errors.New("an error")).AnyTimes()
mockDevfileData.EXPECT().GetAttributes().Return(attributes.Attributes{}, nil)
mockDevfileData.EXPECT().GetSchemaVersion().Return("2.1.0").AnyTimes()
return parser.DevfileObj{
Data: mockDevfileData,
}
},
},
wantErr: true,
},
{
name: "Devfile with local container-override on the first container only",
args: args{
Expand Down Expand Up @@ -1833,6 +1884,68 @@ func TestGetPodTemplateSpec(t *testing.T) {
},
},
},
{
name: "Filter components by name",
args: args{
devfileObj: func(ctrl *gomock.Controller) parser.DevfileObj {
containers := []v1alpha2.Component{
{
Name: "main",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{
Container: v1.Container{
Image: "an-image",
},
},
},
},
{
Name: "tools",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{
Container: v1.Container{
Image: "a-tool-image",
},
},
},
},
}
parserArgs := parser.ParserArgs{
Data: []byte(`schemaVersion: 2.2.0`),
}
var err error
devfile, _, err := devfile.ParseDevfileAndValidate(parserArgs)
if err != nil {
t.Errorf("error creating devfile: %v", err)
}
devfile.Ctx = context.FakeContext(filesystem.NewFakeFs(), "/devfile.yaml")
devfile.Data.AddComponents(containers)
return devfile
},
podTemplateParams: PodTemplateParams{
Options: common.DevfileOptions{
FilterByName: "tools",
},
},
},
want: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "tools",
Image: "a-tool-image",
Env: []corev1.EnvVar{
{Name: "PROJECTS_ROOT", Value: "/projects"},
{Name: "PROJECT_SOURCE", Value: "/projects"},
},
ImagePullPolicy: corev1.PullAlways,
Ports: []corev1.ContainerPort{},
},
},
InitContainers: []corev1.Container{},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down