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

Support events.preStop #1168

Merged
merged 4 commits into from
Oct 12, 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
3 changes: 1 addition & 2 deletions docs/unsupported-devfile-api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ The following features of the Devfile API that are not yet supported by the DevW
| `components.container.dedicatedPod` |
| `components.image` | https://github.com/eclipse/che/issues/21186[Support Devfile v2 outer loop components of type image and kubernetes]
| `components.custom` |
| `events.postStop` |
| `events.preStop` |
| `events.postStop` |
|================================================================================================================================================================================================

If there is no corresponding issue for a Devfile feature you'd like to use, please feel free to submit a feature request.
4 changes: 4 additions & 0 deletions pkg/library/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func GetKubeContainersFromDevfile(workspace *dw.DevWorkspaceTemplateSpec, securi
return nil, err
}

if err := lifecycle.AddPreStopLifecycleHooks(workspace, podAdditions.Containers); err != nil {
return nil, err
}

for _, initComponent := range initComponents {
k8sContainer, err := convertContainerToK8s(initComponent, securityContext, pullPolicy, defaultResources)
if err != nil {
Expand Down
9 changes: 0 additions & 9 deletions pkg/library/lifecycle/poststart.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,3 @@ func processCommandsForPostStart(commands []dw.Command) (*corev1.LifecycleHandle
}
return handler, nil
}

func getContainerWithName(name string, containers []corev1.Container) (*corev1.Container, error) {
for idx, container := range containers {
if container.Name == name {
return &containers[idx], nil
}
}
return nil, fmt.Errorf("container component with name %s not found", name)
}
107 changes: 107 additions & 0 deletions pkg/library/lifecycle/prestop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) 2019-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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lifecycle

import (
"fmt"
"strings"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
corev1 "k8s.io/api/core/v1"
)

const preStopCommandFmt = `{
%s
}
`

func AddPreStopLifecycleHooks(wksp *dw.DevWorkspaceTemplateSpec, containers []corev1.Container) error {
if wksp.Events == nil || len(wksp.Events.PreStop) == 0 {
return nil
}

componentToCommands := map[string][]dw.Command{}
for _, commandName := range wksp.Events.PreStop {
command, err := getCommandByKey(commandName, wksp.Commands)
if err != nil {
return fmt.Errorf("could not resolve command for preStop event '%s': %w", commandName, err)
}
cmdType, err := getCommandType(*command)
if err != nil {
return fmt.Errorf("could not determine command type for '%s': %w", command.Key(), err)
}
if cmdType != dw.ExecCommandType {
return fmt.Errorf("can not use %s-type command in preStop lifecycle event", cmdType)
}
componentToCommands[command.Exec.Component] = append(componentToCommands[command.Exec.Component], *command)
}

for componentName, commands := range componentToCommands {
cmdContainer, err := getContainerWithName(componentName, containers)
amisevsk marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("failed to process preStop event '%s': %w", commands[0].Id, err)
}

preStopHandler, err := processCommandsForPreStop(commands)
if err != nil {
return fmt.Errorf("failed to process preStop event '%s': %w", commands[0].Id, err)
}

if cmdContainer.Lifecycle == nil {
cmdContainer.Lifecycle = &corev1.Lifecycle{}
}
cmdContainer.Lifecycle.PreStop = preStopHandler
}

return nil
}

// processCommandsForPreStop builds a lifecycle handler that runs the provided command(s)
// The command has the format
//
// exec:
// command:
// - "/bin/sh"
// - "-c"
// - |
// {
// cd <workingDir>
// <commandline>
// }
func processCommandsForPreStop(commands []dw.Command) (*corev1.LifecycleHandler, error) {
var dwCommands []string
for _, command := range commands {
execCmd := command.Exec
if len(execCmd.Env) > 0 {
return nil, fmt.Errorf("env vars in preStop command %s are unsupported", command.Id)
}
if execCmd.WorkingDir != "" {
dwCommands = append(dwCommands, fmt.Sprintf("cd %s", execCmd.WorkingDir))
}
dwCommands = append(dwCommands, execCmd.CommandLine)
}

joinedCommands := strings.Join(dwCommands, "\n")

handler := &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: []string{
"/bin/sh",
"-c",
fmt.Sprintf(preStopCommandFmt, joinedCommands),
},
},
}
return handler, nil
}
89 changes: 89 additions & 0 deletions pkg/library/lifecycle/prestop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) 2019-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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lifecycle

import (
"fmt"
"os"
"path/filepath"
"testing"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)

type preStopTestCase struct {
Name string `json:"name,omitempty"`
Input preStopTestInput `json:"input,omitempty"`
Output preStopTestOutput `json:"output,omitempty"`
testPath string
}

type preStopTestInput struct {
Devfile *dw.DevWorkspaceTemplateSpec `json:"devfile,omitempty"`
Containers []corev1.Container `json:"containers,omitempty"`
}

type preStopTestOutput struct {
Containers []corev1.Container `json:"containers,omitempty"`
ErrRegexp *string `json:"errRegexp,omitempty"`
}

func loadPreStopTestCaseOrPanic(t *testing.T, testPath string) preStopTestCase {
bytes, err := os.ReadFile(testPath)
if err != nil {
t.Fatal(err)
}
var test preStopTestCase
if err := yaml.Unmarshal(bytes, &test); err != nil {
t.Fatal(err)
}
test.testPath = testPath
return test
}

func loadAllPreStopTestCasesOrPanic(t *testing.T, fromDir string) []preStopTestCase {
files, err := os.ReadDir(fromDir)
if err != nil {
t.Fatal(err)
}
var tests []preStopTestCase
for _, file := range files {
if file.IsDir() {
tests = append(tests, loadAllPreStopTestCasesOrPanic(t, filepath.Join(fromDir, file.Name()))...)
} else {
tests = append(tests, loadPreStopTestCaseOrPanic(t, filepath.Join(fromDir, file.Name())))
}
}
return tests
}

func TestAddPreStopLifecycleHooks(t *testing.T) {
tests := loadAllPreStopTestCasesOrPanic(t, "./testdata/preStop")
for _, tt := range tests {
t.Run(fmt.Sprintf("%s (%s)", tt.Name, tt.testPath), func(t *testing.T) {
err := AddPreStopLifecycleHooks(tt.Input.Devfile, tt.Input.Containers)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
if !assert.NoError(t, err, "Should not return error") {
return
}
assert.Equal(t, tt.Output.Containers, tt.Input.Containers, "Containers should be updated to match expected output")
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: "Adds all preStop events to containers"

input:
devfile:
commands:
- id: test-preStop-1
exec:
component: test-component-1
commandLine: "echo 'hello world 1'"
- id: test-preStop-2
exec:
component: test-component-2
commandLine: "echo 'hello world 2'"
workingDir: "/tmp/test-dir"
events:
preStop:
- test-preStop-1
- test-preStop-2

containers:
- name: test-component-1
image: test-img
- name: test-component-2
image: test-img
- name: test-component-3
image: test-img

output:
containers:
- name: test-component-1
image: test-img
lifecycle:
preStop:
exec:
command:
- "/bin/sh"
- "-c"
- |
{
echo 'hello world 1'
}
- name: test-component-2
image: test-img
lifecycle:
preStop:
exec:
command:
- "/bin/sh"
- "-c"
- |
{
cd /tmp/test-dir
echo 'hello world 2'
}

- name: test-component-3
image: test-img
30 changes: 30 additions & 0 deletions pkg/library/lifecycle/testdata/preStop/basic_preStop.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: "Should add preStop lifecycle hook for basic event"

input:
devfile:
commands:
- id: test-preStop
exec:
component: test-component
commandLine: "echo 'hello world'"
events:
preStop:
- test-preStop
containers:
- name: test-component
image: test-img

output:
containers:
- name: test-component
image: test-img
lifecycle:
preStop:
exec:
command:
- "/bin/sh"
- "-c"
- |
{
echo 'hello world'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: "Returns error when preStop command requires env vars"

input:
devfile:
commands:
- id: test-cmd
exec:
component: test-component
commandLine: "echo hello world ${MY_ENV}"
env:
- name: MY_ENV
value: /projects
events:
preStop:
- test-cmd
containers:
- name: test-component
image: test-img

output:
errRegexp: ".*env vars in preStop command test-cmd are unsupported.*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: "Returns error when preStop command is not exec-type"

input:
devfile:
commands:
- id: test-command
apply:
component: my-component
events:
preStop:
- test-command

output:
errRegexp: "can not use Apply-type command in preStop lifecycle event"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: "Returns error when preStop command does not exist"

input:
devfile:
events:
preStop:
- test-cmd

output:
errRegexp: ".*could not resolve command for preStop event 'test-cmd'.*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Returns error when preStop command requires nonexistent container"

input:
devfile:
commands:
- id: test-cmd
exec:
component: test-component-wrong-name
commandLine: "echo hello world"
events:
preStop:
- test-cmd
containers:
- name: test-component
image: test-img

output:
errRegexp: ".*failed to process preStop event 'test-cmd':.*container component with name .* not found.*"
Loading
Loading