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 multiple postStart events attached to one container component #1081

Merged
merged 3 commits into from Apr 11, 2023

Conversation

amisevsk
Copy link
Collaborator

@amisevsk amisevsk commented Apr 6, 2023

What does this PR do?

Updates how postStart events are handled to allow multiple postStart events to run on one container

This includes a few changes:

  • Failing commands no longer fail workspace start, since the lifecycle hook succeeds as long as the last command returns 0. We can potentially work around this with set -e if necessary
  • Commands are executed in the order they appear in the postStart events list
  • In order to capture the stdout/stderr of poststart commands, all commands are grouped and stderr/stdour are redirected to files

The new format for postStart commands (as a lifecycle hook) is

lifecycle:
  postStart:
    exec:
      command:
        - "/bin/sh"
        - "-c"
        - |
          {
          cd command1.workingDir
          command1.commandLine
          cd command2.workingDir
          command2.commandLine
          } 1>/tmp/poststart-stdout.txt 2>/tmp/poststart-stderr.txt

What issues does this PR fix or reference?

Closes: #1011
Related:

Is it tested? How?

Testing with `code-latest` The following workspace should start normally, except with a file `post-start.log` in the project directory
kind: DevWorkspace
apiVersion: workspace.devfile.io/v1alpha2
metadata:
  name: code-latest
spec:
  started: true
  template:
    projects:
      - name: web-nodejs-sample
        git:
          remotes:
            origin: "https://github.com/che-samples/web-nodejs-sample.git"
    components:
      - name: dev
        container:
          image: quay.io/devfile/universal-developer-image:latest
    commands:
      - id: say-hello
        exec:
          component: dev
          commandLine: echo "Hello from $(pwd)"
          workingDir: ${PROJECT_SOURCE}/app
      - id: test-poststart
        exec:
          component: dev
          commandLine: echo "hello from poststart" > post-start.log
          workingDir: ${PROJECT_SOURCE}
    events:
      postStart:
        - test-poststart
  contributions:
    - name: che-code
      uri: https://eclipse-che.github.io/che-plugin-registry/main/v3/plugins/che-incubator/che-code/latest/devfile.yaml
      components:
        - name: che-code-runtime-description
          container:
            env:
              - name: CODE_HOST
                value: 0.0.0.0
Big devworkspace I used to test multiple combinations

The following workspace should

  1. Create file $PROJECT_SOURCE/output-to-file.log
  2. Create file $HOME/multi-line-command.log that shows it slept for 5 seconds
  3. Log an error to /tmp/poststart-stderr.txt
  4. Log various information to /tmp/poststart-stdout.txt, including the long-running background task (which should log increasing numbers)
kind: DevWorkspace
apiVersion: workspace.devfile.io/v1alpha2
metadata:
  name: plain
spec:
  started: true
  routingClass: 'basic'
  template:
    projects:
    - name: web-nodejs-sample
      git:
        remotes:
          origin: "https://github.com/che-samples/web-nodejs-sample.git"
    components:
      - name: web-terminal
        container:
          image: quay.io/wto/web-terminal-tooling:next
          memoryLimit: 512Mi
          mountSources: true
          command:
           - "tail"
           - "-f"
           - "/dev/null"
    commands:
    - id: failing-command
      exec:
        component: web-terminal
        commandLine: ./not-a-script.sh
        workingDir: /tmp/
    - id: list-in-project
      exec:
        component: web-terminal
        commandLine: ls -al
        workingDir: ${PROJECT_SOURCE}
    - id: long-command
      exec:
        component: web-terminal
        commandLine: |
          date
          pwd 
          sleep 5s
          echo "done sleeping"
        workingDir: $HOME
    - id: output-to-file
      exec:
        component: web-terminal
        commandLine: echo "writing to a file" > output-to-file.log
        workingDir: ${PROJECT_SOURCE}
    - id: multi-line-command
      exec:
        component: web-terminal
        commandLine: |
          echo "date: $(date)" > multi-line-command.log
          pwd >> multi-line-command.log
          sleep 5s
          echo "done sleeping: $(date)" >> multi-line-command.log
        workingDir: $HOME
    - id: background-longrunning
      exec:
        component: web-terminal
        commandLine: |
          nohup sh -c '{
            for i in {1..500}; do
              echo "background: $i"
              sleep 1
            done
          }' &
    events:
      postStart:
        - long-command
        - background-longrunning
        - output-to-file
        - failing-command
        - multi-line-command
        - list-in-project

PR Checklist

  • E2E tests pass (when PR is ready, comment /test v8-devworkspace-operator-e2e, v8-che-happy-path to trigger)
    • v8-devworkspace-operator-e2e: DevWorkspace e2e test
    • v8-che-happy-path: Happy path for verification integration with Che

Allow multiple postStart lifecycle bindings attached to a single
container component. If a DevWorkspace contains postStart commands
'command1' and 'command2', the operator will add a lifecycle binding to
the container similar to

lifecycle:
  postStart:
    exec:
      command:
        - "/bin/sh"
        - "-c"
        - |
          cd command1.workingDir
          command2.commandLine
	  cd command2.workingDir
	  command2.commandLine

Signed-off-by: Angel Misevski <amisevsk@redhat.com>
Redirect stdout and stderr to /tmp/poststart-stdout.txt and
/tmp/poststart-stderr.txt in the workspace, as postStart events do not
log anything unless they fail.

Signed-off-by: Angel Misevski <amisevsk@redhat.com>
Signed-off-by: Angel Misevski <amisevsk@redhat.com>
@codecov
Copy link

codecov bot commented Apr 6, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.03 🎉

Comparison is base (ad3559a) 50.50% compared to head (72cf82f) 50.53%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1081      +/-   ##
==========================================
+ Coverage   50.50%   50.53%   +0.03%     
==========================================
  Files          82       82              
  Lines        7360     7359       -1     
==========================================
+ Hits         3717     3719       +2     
+ Misses       3358     3355       -3     
  Partials      285      285              
Impacted Files Coverage Δ
pkg/library/lifecycle/poststart.go 95.08% <100.00%> (-0.08%) ⬇️

... and 1 file with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@l0rd l0rd left a comment

Choose a reason for hiding this comment

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

I have tested with Che on an OpenShift cluster and it worked as expected! 👍

I had 5 commands + the editor startup script:

    lifecycle:
      postStart:
        exec:
          command:
          - /bin/sh
          - -c
          - |
            {
            cd ${PROJECT_SOURCE}/src/test/java/com/redhat/cloudnative
            badcommand
            cd ${PROJECT_SOURCE}/does/not/exist
            cat InventoryResourceTest.java
            cd ${PROJECT_SOURCE}/src/test/java/com/redhat/cloudnative
            cat InventoryResourceTest.java
            nohup /checode/entrypoint-volume.sh > /checode/entrypoint-logs.txt 2>&1 &
            echo "ping"
            echo "pong"
            } 1>/tmp/poststart-stdout.txt 2>/tmp/poststart-stderr.txt

@openshift-ci openshift-ci bot added the lgtm label Apr 7, 2023
Copy link
Collaborator

@AObuchow AObuchow left a comment

Choose a reason for hiding this comment

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

Code looks good to me, tested around on OpenShift 4.12 as well. Great work @amisevsk 👍

@openshift-ci
Copy link

openshift-ci bot commented Apr 11, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: amisevsk, AObuchow, l0rd

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@amisevsk amisevsk merged commit ef02ede into devfile:main Apr 11, 2023
8 checks passed
@amisevsk amisevsk deleted the multiple-poststart branch April 11, 2023 17:52
@AObuchow AObuchow mentioned this pull request Aug 23, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Running multiple postStart events is not allowed
3 participants