apiVersion: core.oam.dev/v1beta1
kind: TraitDefinition
metadata:
  annotations:
    definition.oam.dev/description: "add an init container with a shared volume."
  name: init-container
  namespace: vela-system
spec:
  appliesToWorkloads:
    - webservice
    - worker
  schematic:
    cue:
      template: |-
        patch: {
          spec: template: spec: {
              // +patchKey=name
              containers: [{
                  name: context.name
                  // +patchKey=name
                  volumeMounts: [{
                      name:      parameter.mountName
                      mountPath: parameter.appMountPath
                  }]
              }]
              initContainers: [{
                  name:    parameter.name
                  image:   parameter.image
                  command: parameter.command
                  // +patchKey=name
                  volumeMounts: [{
                      name:      parameter.mountName
                      mountPath: parameter.initMountPath
                  }]
              }]
              // +patchKey=name
              volumes: [{
                  name:     parameter.mountName
                  emptyDir: {}
              }]
          }
        }

        parameter: {
          name:  string
          image: string
          command?: [...string]
          mountName:     *"workdir" | string
          appMountPath:  string
          initMountPath: string
        }
