apiVersion: core.oam.dev/v1beta1
kind: WorkflowStepDefinition
metadata:
  name: canary-rollback
  namespace: vela-system
spec:
  schematic:
    cue:
      template: |-
        import ("vela/op")

        parameter: {...}

        comps: op.#Load
        compNames: [ for name, c in comps.value {name}]
        firstcomp: compNames[0]

        rolloutObj: op.#Read & {
          value: {
            apiVersion: "standard.oam.dev/v1alpha1"
            kind:       "Rollout"
            metadata: {
              name:      firstcomp
              namespace: context.namespace
            }
          }
        }

        _sourceRevision: rolloutObj.value.status.LastSourceRevision

        apply: op.#ApplyComponent & {
          value: comps.value[firstcomp]
          patch: {
            traits: "rollout": {
              spec: {
                targetRevisionName: _sourceRevision
              }
            }

            traits: "virtualService": {
              spec:
                // +patchStrategy=retainKeys
                http: [
                  {
                    route: [{
                      destination: {
                        host:   firstcomp
                        subset: _sourceRevision
                      }
                      weight: 100
                    }]
                  },
                ]
            }

            traits: "destinationRule": {
              // +patchStrategy=retainKeys
              spec: {
                host: firstcomp
                subsets: [
                  {
                    name: _sourceRevision
                    labels: {"app.oam.dev/revision": _sourceRevision}
                  },
                ]
              }
            }
          }
        }

        applyRemaining: op.#ApplyRemaining & {
            exceptions: [firstcomp]
        }
