Lifecycle

The lifecycle trait permits to attach operations to two stages of a container. The postStart hook will be triggered once the container is started, while the preStop hook will be triggered when the container is going to be deleted. The hook can either execute a command in the attached container, or send an HTTP request to a given URL. Use cases for hooks include:

  • Notifying another system of the available container so that it can be discovered by other applications or components.
  • Sending a message to another system when the container is deleted.
  • Executing a graceful stop script for a container.

The following snippet shows an example application with a lifecycle trait.

apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  name: my-app
  annotations: 
    version: "v0.0.1"
    description: "My application"
spec:
  components:
    - name:  my-component
      type: worker
      properties:
        image: my-app-image:v1.0.0
        cmd: ["cmd"]     
        cpu: "0.25"
        memory: "200Mi"
      traits:
        - type: lifecycle # Set to lifecycle
          properties:
            postStart: # (Optional) action post start the container
              # exec or httpGet option
              exec: # (Optional) command to execute
                command: ["cmd"]
              httpGet: # (Optional) http get action
                host: "host"                
                scheme: "HTTP" # (Optional) schema (HTTP or HTTPS)
                port: 80
            preStop: # (Optional) action prevous to stop the container
              # exec or httpGet option
              exec: # (Optional) command to execute
                command: ["cmd"]
              httpGet: # (Optional) http get action
                host: "host"                
                scheme: "HTTP" # (Optional) schema (HTTP or HTTPS)
                port: 80