Skip to content

Commit 046c37b

Browse files
committed
fix merge conflict
Signed-off-by: Michael Hoang <[email protected]>
2 parents 80cadcc + bd4a12f commit 046c37b

File tree

19 files changed

+1982
-215
lines changed

19 files changed

+1982
-215
lines changed

.github/workflows/codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Set up Go 1.x
1515
uses: actions/setup-go@v2
1616
with:
17-
go-version: 1.17
17+
go-version: 1.19
1818
- name: Run tests
1919
run: make test
2020
- name: Codecov

.github/workflows/go.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Setup Go environment
1717
uses: actions/[email protected]
1818
with:
19-
go-version: 1.16
19+
go-version: 1.19
2020
id: go
2121

2222
- name: Check out code into the Go module directory
@@ -45,7 +45,7 @@ jobs:
4545
4646
- name: Check license
4747
run: |
48-
go get github.com/google/addlicense@latest
48+
go install github.com/google/addlicense@latest
4949
git reset HEAD --hard
5050
make check_license
5151
if [[ $? != 0 ]]
@@ -60,7 +60,7 @@ jobs:
6060

6161
- name: Run Gosec Security Scanner
6262
run: |
63-
go install github.com/securego/gosec/v2/cmd/gosec@latest
63+
go install github.com/securego/gosec/v2/cmd/gosec@v2.14.0
6464
make gosec
6565
if [[ $? != 0 ]]
6666
then

go.mod

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@ require (
66
github.com/devfile/api/v2 v2.2.0
77
github.com/devfile/registry-support/registry-library v0.0.0-20221018213054-47b3ffaeadba
88
github.com/fatih/color v1.7.0
9-
github.com/fsnotify/fsnotify v1.4.9
9+
github.com/fsnotify/fsnotify v1.6.0
1010
github.com/gobwas/glob v0.2.3
1111
github.com/golang/mock v1.6.0
12-
github.com/google/go-cmp v0.5.6
12+
github.com/google/go-cmp v0.5.9
1313
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7
1414
github.com/hashicorp/go-multierror v1.1.1
1515
github.com/hashicorp/go-version v1.4.0
1616
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
1717
github.com/openshift/api v0.0.0-20200930075302-db52bc4ef99f
1818
github.com/pkg/errors v0.9.1
1919
github.com/spf13/afero v1.6.0
20-
github.com/stretchr/testify v1.7.0
20+
github.com/stretchr/testify v1.8.0
2121
github.com/xeipuuv/gojsonschema v1.2.0
2222
gopkg.in/yaml.v3 v3.0.1
23-
k8s.io/api v0.21.3
24-
k8s.io/apiextensions-apiserver v0.21.3
25-
k8s.io/apimachinery v0.21.3
26-
k8s.io/client-go v0.21.3
23+
k8s.io/api v0.26.1
24+
k8s.io/apiextensions-apiserver v0.26.1
25+
k8s.io/apimachinery v0.26.1
26+
k8s.io/client-go v0.26.1
2727
k8s.io/klog v1.0.0
28-
k8s.io/utils v0.0.0-20210722164352-7f3ee0f31471
29-
sigs.k8s.io/controller-runtime v0.9.5
30-
sigs.k8s.io/yaml v1.2.0
28+
k8s.io/pod-security-admission v0.26.1
29+
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448
30+
sigs.k8s.io/controller-runtime v0.14.4
31+
sigs.k8s.io/yaml v1.3.0
3132
)

go.sum

Lines changed: 302 additions & 52 deletions
Large diffs are not rendered by default.

pkg/devfile/generator/generators.go

Lines changed: 150 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
package generator
1717

1818
import (
19+
"errors"
1920
"fmt"
2021

22+
"github.com/devfile/api/v2/pkg/attributes"
23+
"github.com/devfile/library/v2/pkg/devfile/parser/data"
24+
2125
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2226
"github.com/devfile/library/v2/pkg/devfile/parser"
2327
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
@@ -31,6 +35,7 @@ import (
3135
networkingv1 "k8s.io/api/networking/v1"
3236
"k8s.io/apimachinery/pkg/api/resource"
3337
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
38+
psaapi "k8s.io/pod-security-admission/api"
3439
)
3540

3641
const (
@@ -71,6 +76,8 @@ func GetObjectMeta(name, namespace string, labels, annotations map[string]string
7176
}
7277

7378
// GetContainers iterates through all container components, filters out init containers and returns corresponding containers
79+
//
80+
// Deprecated: in favor of GetPodTemplateSpec
7481
func GetContainers(devfileObj parser.DevfileObj, options common.DevfileOptions) ([]corev1.Container, error) {
7582
allContainers, err := getAllContainers(devfileObj, options)
7683
if err != nil {
@@ -117,6 +124,8 @@ func GetContainers(devfileObj parser.DevfileObj, options common.DevfileOptions)
117124
}
118125

119126
// GetInitContainers gets the init container for every preStart devfile event
127+
//
128+
// Deprecated: in favor of GetPodTemplateSpec
120129
func GetInitContainers(devfileObj parser.DevfileObj) ([]corev1.Container, error) {
121130
containers, err := getAllContainers(devfileObj, common.DevfileOptions{})
122131
if err != nil {
@@ -167,30 +176,49 @@ func GetInitContainers(devfileObj parser.DevfileObj) ([]corev1.Container, error)
167176

168177
// DeploymentParams is a struct that contains the required data to create a deployment object
169178
type DeploymentParams struct {
170-
TypeMeta metav1.TypeMeta
171-
ObjectMeta metav1.ObjectMeta
172-
InitContainers []corev1.Container
173-
Containers []corev1.Container
179+
TypeMeta metav1.TypeMeta
180+
ObjectMeta metav1.ObjectMeta
181+
// Deprecated: InitContainers, Containers and Volumes are deprecated and are replaced by PodTemplateSpec.
182+
// A PodTemplateSpec value can be obtained calling GetPodTemplateSpec function, instead of calling GetContainers and GetInitContainers
183+
InitContainers []corev1.Container
184+
// Deprecated: see InitContainers
185+
Containers []corev1.Container
186+
// Deprecated: see InitContainers
174187
Volumes []corev1.Volume
188+
PodTemplateSpec *corev1.PodTemplateSpec
175189
PodSelectorLabels map[string]string
176190
Replicas *int32
177191
}
178192

179193
// GetDeployment gets a deployment object
180194
func GetDeployment(devfileObj parser.DevfileObj, deployParams DeploymentParams) (*appsv1.Deployment, error) {
181195

182-
podTemplateSpecParams := podTemplateSpecParams{
183-
ObjectMeta: deployParams.ObjectMeta,
184-
InitContainers: deployParams.InitContainers,
185-
Containers: deployParams.Containers,
186-
Volumes: deployParams.Volumes,
187-
}
188-
189196
deploySpecParams := deploymentSpecParams{
190-
PodTemplateSpec: *getPodTemplateSpec(podTemplateSpecParams),
191197
PodSelectorLabels: deployParams.PodSelectorLabels,
192198
Replicas: deployParams.Replicas,
193199
}
200+
if deployParams.PodTemplateSpec == nil {
201+
// Deprecated
202+
podTemplateSpecParams := podTemplateSpecParams{
203+
ObjectMeta: deployParams.ObjectMeta,
204+
InitContainers: deployParams.InitContainers,
205+
Containers: deployParams.Containers,
206+
Volumes: deployParams.Volumes,
207+
}
208+
podTemplateSpec, err := getPodTemplateSpec(podTemplateSpecParams)
209+
if err != nil {
210+
return nil, err
211+
}
212+
deploySpecParams.PodTemplateSpec = *podTemplateSpec
213+
} else {
214+
if len(deployParams.InitContainers) > 0 ||
215+
len(deployParams.Containers) > 0 ||
216+
len(deployParams.Volumes) > 0 {
217+
return nil, errors.New("InitContainers, Containers and Volumes cannot be set when PodTemplateSpec is set in parameters")
218+
}
219+
220+
deploySpecParams.PodTemplateSpec = *deployParams.PodTemplateSpec
221+
}
194222

195223
containerAnnotations, err := getContainerAnnotations(devfileObj, common.DevfileOptions{})
196224
if err != nil {
@@ -207,6 +235,116 @@ func GetDeployment(devfileObj parser.DevfileObj, deployParams DeploymentParams)
207235
return deployment, nil
208236
}
209237

238+
// PodTemplateParams is a struct that contains the required data to create a podtemplatespec object
239+
type PodTemplateParams struct {
240+
ObjectMeta metav1.ObjectMeta
241+
// PodSecurityAdmissionPolicy is the policy to be respected by the created pod
242+
// The pod will be patched, if necessary, to respect the policies
243+
PodSecurityAdmissionPolicy psaapi.Policy
244+
}
245+
246+
// GetPodTemplateSpec returns a pod template
247+
// The function:
248+
// - iterates through all container components, filters out init containers and gets corresponding containers
249+
// - gets the init container for every preStart devfile event
250+
// - patches the pod template and containers to satisfy PodSecurityAdmissionPolicy
251+
// - patches the pod template and containers to apply pod and container overrides
252+
func GetPodTemplateSpec(devfileObj parser.DevfileObj, podTemplateParams PodTemplateParams) (*corev1.PodTemplateSpec, error) {
253+
containers, err := GetContainers(devfileObj, common.DevfileOptions{})
254+
if err != nil {
255+
return nil, err
256+
}
257+
initContainers, err := GetInitContainers(devfileObj)
258+
if err != nil {
259+
return nil, err
260+
}
261+
262+
podTemplateSpecParams := podTemplateSpecParams{
263+
ObjectMeta: podTemplateParams.ObjectMeta,
264+
InitContainers: initContainers,
265+
Containers: containers,
266+
}
267+
var globalAttributes attributes.Attributes
268+
// attributes is not supported in versions less than 2.1.0, so we skip it
269+
if devfileObj.Data.GetSchemaVersion() > string(data.APISchemaVersion200) {
270+
// the only time GetAttributes will return an error is if DevfileSchemaVersion is 2.0.0, a case we've already covered;
271+
// so we'll skip checking for error here
272+
globalAttributes, _ = devfileObj.Data.GetAttributes()
273+
}
274+
components, err := devfileObj.Data.GetDevfileContainerComponents(common.DevfileOptions{})
275+
if err != nil {
276+
return nil, err
277+
}
278+
279+
podTemplateSpec, err := getPodTemplateSpec(podTemplateSpecParams)
280+
if err != nil {
281+
return nil, err
282+
}
283+
284+
podTemplateSpec, err = patchForPolicy(podTemplateSpec, podTemplateParams.PodSecurityAdmissionPolicy)
285+
if err != nil {
286+
return nil, err
287+
}
288+
289+
if needsPodOverrides(globalAttributes, components) {
290+
patchedPodTemplateSpec, err := applyPodOverrides(globalAttributes, components, podTemplateSpec)
291+
if err != nil {
292+
return nil, err
293+
}
294+
patchedPodTemplateSpec.ObjectMeta = podTemplateSpecParams.ObjectMeta
295+
podTemplateSpec = patchedPodTemplateSpec
296+
}
297+
298+
podTemplateSpec.Spec.Containers, err = applyContainerOverrides(devfileObj, podTemplateSpec.Spec.Containers)
299+
if err != nil {
300+
return nil, err
301+
}
302+
podTemplateSpec.Spec.InitContainers, err = applyContainerOverrides(devfileObj, podTemplateSpec.Spec.InitContainers)
303+
if err != nil {
304+
return nil, err
305+
}
306+
307+
return podTemplateSpec, nil
308+
}
309+
310+
func applyContainerOverrides(devfileObj parser.DevfileObj, containers []corev1.Container) ([]corev1.Container, error) {
311+
containerComponents, err := devfileObj.Data.GetComponents(common.DevfileOptions{
312+
ComponentOptions: common.ComponentOptions{
313+
ComponentType: v1.ContainerComponentType,
314+
},
315+
})
316+
if err != nil {
317+
return nil, err
318+
}
319+
320+
getContainerByName := func(name string) (*corev1.Container, bool) {
321+
for _, container := range containers {
322+
if container.Name == name {
323+
return &container, true
324+
}
325+
}
326+
return nil, false
327+
}
328+
329+
result := make([]corev1.Container, 0, len(containers))
330+
for _, comp := range containerComponents {
331+
container, found := getContainerByName(comp.Name)
332+
if !found {
333+
continue
334+
}
335+
if comp.Attributes.Exists(ContainerOverridesAttribute) {
336+
patched, err := containerOverridesHandler(comp, container)
337+
if err != nil {
338+
return nil, err
339+
}
340+
result = append(result, *patched)
341+
} else {
342+
result = append(result, *container)
343+
}
344+
}
345+
return result, nil
346+
}
347+
210348
// PVCParams is a struct to create PVC
211349
type PVCParams struct {
212350
TypeMeta metav1.TypeMeta

0 commit comments

Comments
 (0)