Skip to content

preStart events support #4909

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

Merged
Merged
Changes from 4 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
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@

### Feature/Enhancements

- Support for `preStart` lifecycle events ([#4901](https://github.com/openshift/odo/issues/4901))

### Bug Fixes

### Tests
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ require (
github.com/Netflix/go-expect v0.0.0-20201125194554-85d881c3777e
github.com/blang/semver v3.5.1+incompatible
github.com/devfile/api/v2 v2.1.0
github.com/devfile/library v1.0.0
github.com/devfile/library v1.0.1-0.20210716161803-9de9d4cb6be7
github.com/devfile/registry-support/index/generator v0.0.0-20210407161420-cd279527f873
github.com/devfile/registry-support/registry-library v0.0.0-20210407161420-cd279527f873
github.com/docker/docker v17.12.0-ce-rc1.0.20200916142827-bd33bbf0497b+incompatible
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -228,11 +228,14 @@ github.com/deislabs/oras v0.8.1/go.mod h1:Mx0rMSbBNaNfY9hjpccEnxkOqJL6KGjtxNHPLC
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0=
github.com/devfile/api/v2 v2.0.0-20210211160219-33a78aec06af/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
github.com/devfile/api/v2 v2.0.0-20210713153530-f78ab9de1a30/go.mod h1:QNzaIVQnCsYfXed+QZOn1uvEQFzyhvpi/uc3g/b2ws0=
github.com/devfile/api/v2 v2.1.0 h1:hodMaQpa9onnx8uG5NBxIuzBjC7AUkLTGCcuvZyED7g=
github.com/devfile/api/v2 v2.1.0/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
github.com/devfile/library v1.0.0-alpha.2/go.mod h1:aGJSpcGrRiYwsQQJMQH1ChHuOptUf49n+j0RDBYyTIQ=
github.com/devfile/library v1.0.0 h1:/3sZgyw+3SAk3TASMLxogcqrdthEW1nK+H1WkfDaOT8=
github.com/devfile/library v1.0.0/go.mod h1:WCAJwFjlBC9+0TdF0XE7q3XBldJhkVhy5aWQPaSXqwU=
github.com/devfile/library v1.0.1-0.20210716161803-9de9d4cb6be7 h1:eWFBfXcGNiyh1J+1As3mN+NtAs8cHEEO4WAbQ104XnE=
github.com/devfile/library v1.0.1-0.20210716161803-9de9d4cb6be7/go.mod h1:CqGA79cpNxA2ew8q/DU1uvk1PMvOXr30p+0ESFOjGw8=
github.com/devfile/registry-support/index/generator v0.0.0-20201211145454-bc904934d987/go.mod h1:H8hBF8b+025XN+Wk70F5P1zeoJ28bAYkbQAeWyTkHEQ=
github.com/devfile/registry-support/index/generator v0.0.0-20210407161420-cd279527f873 h1:1PF19I6JXCAtD5Tr6qWRRYQ9kLn6DbGdGr6gz9jCWUI=
github.com/devfile/registry-support/index/generator v0.0.0-20210407161420-cd279527f873/go.mod h1:02/+7NmfHFJQ0C0S7akdfCeJSQSWmOvep8MmYcsSFko=
@@ -670,6 +673,7 @@ github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb/go.mod h1:5ELEyG+X8f+meRWHuqUOewBOhvHkl7M76pdGEansxW4=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
13 changes: 6 additions & 7 deletions pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
@@ -415,7 +415,6 @@ func (a *Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSp
if err != nil {
return err
}

if len(containers) == 0 {
return fmt.Errorf("no valid components found in the devfile")
}
@@ -428,11 +427,11 @@ func (a *Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSp
return err
}

var initContainers []corev1.Container
// odo currently does not support PreStart event or apply commands
// https://github.com/openshift/odo/issues/4187
// after odo support apply commands, should append result from generator.getInitContainers()
// initContainers := generator.GetInitContainers(a.Devfile)
initContainers, err := generator.GetInitContainers(a.Devfile)
if err != nil {
return err
}

initContainers = append(initContainers, kclient.GetBootstrapSupervisordInitContainer())

var odoSourcePVCName string
@@ -462,7 +461,7 @@ func (a *Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSp
}

// Get PVC volumes and Volume Mounts
pvcVolumes, err := storage.GetVolumesAndVolumeMounts(a.Devfile, containers, volumeNameToVolInfo, parsercommon.DevfileOptions{})
pvcVolumes, err := storage.GetVolumesAndVolumeMounts(a.Devfile, containers, initContainers, volumeNameToVolInfo, parsercommon.DevfileOptions{})
if err != nil {
return err
}
19 changes: 16 additions & 3 deletions pkg/devfile/adapters/kubernetes/storage/utils.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package storage

import (
"fmt"
"strings"

devfileParser "github.com/devfile/library/pkg/devfile/parser"
parsercommon "github.com/devfile/library/pkg/devfile/parser/data/v2/common"
@@ -28,7 +29,7 @@ type VolumeInfo struct {
// GetVolumesAndVolumeMounts gets the PVC volumes and updates the containers with the volume mounts.
// volumeNameToVolInfo is a map of the devfile volume name to the volume info containing the pvc name and the volume name.
// To be moved to devfile/library.
func GetVolumesAndVolumeMounts(devfileObj devfileParser.DevfileObj, containers []corev1.Container, volumeNameToVolInfo map[string]VolumeInfo, options parsercommon.DevfileOptions) ([]corev1.Volume, error) {
func GetVolumesAndVolumeMounts(devfileObj devfileParser.DevfileObj, containers []corev1.Container, initContainers []corev1.Container, volumeNameToVolInfo map[string]VolumeInfo, options parsercommon.DevfileOptions) ([]corev1.Volume, error) {

containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
if err != nil {
@@ -49,7 +50,7 @@ func GetVolumesAndVolumeMounts(devfileObj devfileParser.DevfileObj, containers [
}
}

addVolumeMountToContainers(containers, volInfo.VolumeName, containerNameToMountPaths)
addVolumeMountToContainers(containers, initContainers, volInfo.VolumeName, containerNameToMountPaths)
}
return pvcVols, nil
}
@@ -71,7 +72,7 @@ func getPVC(volumeName, pvcName string) corev1.Volume {
// addVolumeMountToContainers adds the Volume Mounts in containerNameToMountPaths to the containers for a given pvc and volumeName
// containerNameToMountPaths is a map of a container name to an array of its Mount Paths.
// To be moved to devfile/library.
func addVolumeMountToContainers(containers []corev1.Container, volumeName string, containerNameToMountPaths map[string][]string) {
func addVolumeMountToContainers(containers []corev1.Container, initContainers []corev1.Container, volumeName string, containerNameToMountPaths map[string][]string) {

for containerName, mountPaths := range containerNameToMountPaths {
for i := range containers {
@@ -86,6 +87,18 @@ func addVolumeMountToContainers(containers []corev1.Container, volumeName string
}
}
}
for i := range initContainers {
if strings.HasPrefix(initContainers[i].Name, containerName) {
for _, mountPath := range mountPaths {
initContainers[i].VolumeMounts = append(initContainers[i].VolumeMounts, corev1.VolumeMount{
Name: volumeName,
MountPath: mountPath,
SubPath: "",
},
)
}
}
}
}
}

8 changes: 5 additions & 3 deletions pkg/devfile/adapters/kubernetes/storage/utils_test.go
Original file line number Diff line number Diff line change
@@ -98,7 +98,8 @@ func TestAddVolumeMountToContainers(t *testing.T) {
for _, tt := range tests {
t.Run(tt.podName, func(t *testing.T) {
containers := []v1.Container{tt.container}
addVolumeMountToContainers(containers, tt.volumeName, tt.containerMountPathsMap)
initContainers := []v1.Container{}
addVolumeMountToContainers(containers, initContainers, tt.volumeName, tt.containerMountPathsMap)

mountPathCount := 0
for _, container := range containers {
@@ -293,7 +294,7 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
}

containers, err := generator.GetContainers(devObj, parsercommon.DevfileOptions{})
if err != nil {
if !tt.wantErr && err != nil {
t.Errorf("TestGetVolumesAndVolumeMounts error - %v", err)
return
}
@@ -307,7 +308,8 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
}
}

pvcVols, err := GetVolumesAndVolumeMounts(devObj, containers, tt.volumeNameToVolInfo, options)
initContainers := []v1.Container{}
pvcVols, err := GetVolumesAndVolumeMounts(devObj, containers, initContainers, tt.volumeNameToVolInfo, options)
if !tt.wantErr && err != nil {
t.Errorf("TestGetVolumesAndVolumeMounts unexpected error: %v", err)
return
4 changes: 2 additions & 2 deletions pkg/devfile/validate/commands.go
Original file line number Diff line number Diff line change
@@ -24,8 +24,8 @@ func validateCommands(commandsMap map[string]devfilev1.Command) (err error) {
// 2. if composite command, it should not be of kind run
func validateCommand(command devfilev1.Command) (err error) {

// devfile command type for odo must be exec or composite
if command.Exec == nil && command.Composite == nil {
// devfile command type for odo must be exec, apply or composite
if command.Exec == nil && command.Apply == nil && command.Composite == nil {
return &UnsupportedOdoCommandError{commandId: command.Id}
}

7 changes: 0 additions & 7 deletions pkg/devfile/validate/events.go
Original file line number Diff line number Diff line change
@@ -3,13 +3,6 @@ package validate
import "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"

func validatePreStart(preStart []string) (err error) {
// This is odo specific validation. There is still discussion about how PreStart should be implemented.
// https://github.com/devfile/api/issues/204
// https://github.com/openshift/odo/issues/4187
// This is here to prevent anyone from using PreStart event until we have a proper implementation
if len(preStart) != 0 {
return &UnsupportedFieldError{fieldName: "preStart"}
}
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this now, do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, I removed the associated code


2 changes: 1 addition & 1 deletion pkg/devfile/validate/events_test.go
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ func Test_validateEvents(t *testing.T) {
PreStart: []string{"asdf"},
},
},
wantErr: true,
wantErr: false,
},
}
for _, tt := range tests {

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading