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

Stop steps after they are done #2681

Merged
merged 10 commits into from
Nov 1, 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
1 change: 0 additions & 1 deletion .woodpecker/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ steps:
repo: woodpeckerci/woodpecker-server
dockerfile: docker/Dockerfile.server.multiarch
platforms: *platforms_preview
logins: *publish_logins
tag: pull_${CI_COMMIT_PULL_REQUEST}
when:
evaluate: 'not (CI_COMMIT_PULL_REQUEST_LABELS contains "build_pr_images")'
Expand Down
16 changes: 16 additions & 0 deletions pipeline/backend/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@
return rc, nil
}

func (e *docker) DestroyStep(ctx context.Context, step *backend.Step, taskUUID string) error {
log.Trace().Str("taskUUID", taskUUID).Msgf("stop step %s", step.Name)

containerName := toContainerName(step)

if err := e.client.ContainerKill(ctx, containerName, "9"); err != nil && !isErrContainerNotFoundOrNotRunning(err) {
return err
}

Check warning on line 301 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L294-L301

Added lines #L294 - L301 were not covered by tests

if err := e.client.ContainerRemove(ctx, containerName, removeOpts); err != nil && !isErrContainerNotFoundOrNotRunning(err) {
return err
}

Check warning on line 305 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L303-L305

Added lines #L303 - L305 were not covered by tests

return nil

Check warning on line 307 in pipeline/backend/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/docker/docker.go#L307

Added line #L307 was not covered by tests
}

func (e *docker) DestroyWorkflow(_ context.Context, conf *backend.Config, taskUUID string) error {
log.Trace().Str("taskUUID", taskUUID).Msgf("delete workflow environment")

Expand Down
27 changes: 24 additions & 3 deletions pipeline/backend/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,29 @@
// return rc, nil
}

func (e *kube) DestroyStep(ctx context.Context, step *types.Step, taskUUID string) error {
podName, err := dnsName(step.Name)
if err != nil {
return err
}

Check warning on line 332 in pipeline/backend/kubernetes/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/kubernetes.go#L328-L332

Added lines #L328 - L332 were not covered by tests

log.Trace().Str("taskUUID", taskUUID).Msgf("Stopping pod: %s", podName)

gracePeriodSeconds := int64(0) // immediately
dpb := metav1.DeletePropagationBackground

deleteOpts := metav1.DeleteOptions{
GracePeriodSeconds: &gracePeriodSeconds,
PropagationPolicy: &dpb,
}

if err := e.client.CoreV1().Pods(e.config.Namespace).Delete(ctx, podName, deleteOpts); err != nil && !errors.IsNotFound(err) {
return err
}

Check warning on line 346 in pipeline/backend/kubernetes/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/kubernetes.go#L334-L346

Added lines #L334 - L346 were not covered by tests

return nil

Check warning on line 348 in pipeline/backend/kubernetes/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/kubernetes.go#L348

Added line #L348 was not covered by tests
}

// Destroy the pipeline environment.
func (e *kube) DestroyWorkflow(_ context.Context, conf *types.Config, taskUUID string) error {
anbraten marked this conversation as resolved.
Show resolved Hide resolved
log.Trace().Str("taskUUID", taskUUID).Msg("Deleting Kubernetes primitives")
Expand All @@ -349,9 +372,7 @@
}
log.Trace().Msgf("Deleting pod: %s", stepName)
if err := e.client.CoreV1().Pods(e.config.Namespace).Delete(noContext, stepName, deleteOpts); err != nil {
if errors.IsNotFound(err) {
log.Trace().Err(err).Msgf("Unable to delete pod %s", stepName)
} else {
if !errors.IsNotFound(err) {

Check warning on line 375 in pipeline/backend/kubernetes/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/kubernetes.go#L375

Added line #L375 was not covered by tests
return err
}
}
Expand Down
5 changes: 5 additions & 0 deletions pipeline/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@

// execCommands use step.Image as shell and run the commands in it
func (e *local) execCommands(ctx context.Context, step *types.Step, state *workflowState, env []string) error {
// Prepare commands
args, err := genCmdByShell(step.Image, step.Commands)
if err != nil {
return fmt.Errorf("could not convert commands into args: %w", err)

Check warning on line 138 in pipeline/backend/local/local.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/local/local.go#L135-L138

Added lines #L135 - L138 were not covered by tests
}

// Use "image name" as run command (indicate shell)
cmd := exec.CommandContext(ctx, step.Image, args...)

Check warning on line 142 in pipeline/backend/local/local.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/local/local.go#L142

Added line #L142 was not covered by tests
cmd.Env = env
cmd.Dir = state.workspaceDir

Expand All @@ -147,11 +147,11 @@
e.output, _ = cmd.StdoutPipe()
cmd.Stderr = cmd.Stdout

if runtime.GOOS == "windows" {
// we get non utf8 output from windows so just sanitize it
// TODO: remove hack
e.output = io.NopCloser(transform.NewReader(e.output, unicode.UTF8.NewDecoder().Transformer))
}

Check warning on line 154 in pipeline/backend/local/local.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/local/local.go#L150-L154

Added lines #L150 - L154 were not covered by tests

state.stepCMDs[step.Name] = cmd

Expand Down Expand Up @@ -215,6 +215,11 @@
return e.output, nil
}

func (e *local) DestroyStep(_ context.Context, _ *types.Step, _ string) error {
// WaitStep already waits for the command to finish, so there is nothing to do here.
return nil

Check warning on line 220 in pipeline/backend/local/local.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/local/local.go#L218-L220

Added lines #L218 - L220 were not covered by tests
}

// DestroyWorkflow the pipeline environment.
func (e *local) DestroyWorkflow(_ context.Context, _ *types.Config, taskUUID string) error {
log.Trace().Str("taskUUID", taskUUID).Msgf("delete workflow environment")
Expand Down
15 changes: 9 additions & 6 deletions pipeline/backend/types/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@ type Engine interface {
// IsAvailable check if the backend is available.
IsAvailable(ctx context.Context) bool

// Load the backend engine.
// Load loads the backend engine.
Load(ctx context.Context) error

// SetupWorkflow the workflow environment.
// SetupWorkflow sets up the workflow environment.
SetupWorkflow(ctx context.Context, conf *Config, taskUUID string) error

// StartStep start the workflow step.
// StartStep starts the workflow step.
StartStep(ctx context.Context, step *Step, taskUUID string) error

// WaitStep for the workflow step to complete and returns
// WaitStep waits for the workflow step to complete and returns
// the completion results.
WaitStep(ctx context.Context, step *Step, taskUUID string) (*State, error)

// TailStep the workflow step logs.
// TailStep tails the workflow step logs.
TailStep(ctx context.Context, step *Step, taskUUID string) (io.ReadCloser, error)

// DestroyWorkflow the workflow environment.
// DestroyStep destroys the workflow step.
DestroyStep(ctx context.Context, step *Step, taskUUID string) error

// DestroyWorkflow destroys the workflow environment.
DestroyWorkflow(ctx context.Context, conf *Config, taskUUID string) error
}
4 changes: 4 additions & 0 deletions pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@
return nil, err
}

if err := r.engine.DestroyStep(r.ctx, step, r.taskUUID); err != nil {
return nil, err
}

Check warning on line 279 in pipeline/pipeline.go

View check run for this annotation

Codecov / codecov/patch

pipeline/pipeline.go#L277-L279

Added lines #L277 - L279 were not covered by tests

if waitState.OOMKilled {
return waitState, &OomError{
Name: step.Name,
Expand Down
Loading