apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
  annotations:
    definition.oam.dev/description: ""
  labels: {}
  name: base-service
spec:
  schematic:
    cue:
      template: |
        output: {
          apiVersion: "apps/v1"
          kind:       "Deployment"
          metadata: {
            labels: {
              appId:   "BASE-SERVICE"
              appName: "base-service"
              version: "0.0.1"
            }
            name: "base-service"
          }
          spec: {
            replicas:             2
            revisionHistoryLimit: 5
            selector: matchLabels: app: "base-service"
            template: {
              metadata: labels: {
                antiAffinity: "none"
                app:          "base-service"
                appId:        "BASE-SERVICE"
                version:      "0.0.1"
                cluster:      parameter.cluster
              }
              spec: {
                affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: [
                  {
                    podAffinityTerm: {
                      labelSelector: matchExpressions: [
                        {
                          key:      "app"
                          operator: "In"
                          values: ["base-service"]
                        },
                        {
                          key:      "antiAffinity"
                          operator: "In"
                          values: ["none"]
                        },
                      ]
                      topologyKey: "kubernetes.io/hostname"
                    }
                    weight: 100
                  },
                ]
                containers: [
                  {
                    env: [
                      {name: "NODE_IP", valueFrom: fieldRef: fieldPath:       "status.hostIP"},
                      {name: "POD_IP", valueFrom: fieldRef: fieldPath:        "status.podIP"},
                      {name: "POD_NAME", valueFrom: fieldRef: fieldPath:      "metadata.name"},
                      {name: "POD_NAMESPACE", valueFrom: fieldRef: fieldPath: "metadata.namespace"},
                      {name: "APP_NAME", value:                               "base-service"},
                      {name: "LOG_BASE", value:                               "/data/log"},
                      {name: "RUNTIME_CLUSTER", value:                        parameter.cluster},
                    ]
                    image:           parameter.image
                    imagePullPolicy: "Always"
                    name:            "base-service"
                    ports: [
                      {containerPort: 11223, protocol: "TCP"},
                      {containerPort: 11224, protocol: "TCP"},
                    ]
                    volumeMounts: [
                      {mountPath: "/tmp/data/log/base-service", name: "log-volume"},
                      {mountPath: "/data", name:                      "sidecar-sre"},
                      {mountPath: "/app/skywalking", name:            "skywalking"},
                    ]
                  },
                ]
                initContainers: [
                  {
                    args: ["echo \"do something\""]
                    command: ["/bin/sh", "-c"]
                    env: [
                      {name: "NODE_IP", valueFrom: fieldRef: fieldPath: "status.hostIP"},
                      {name: "POD_IP", valueFrom: fieldRef: fieldPath:  "status.podIP"},
                      {name: "APP_NAME", value:                         "base-service"},
                    ]
                    image:           "busybox"
                    imagePullPolicy: "Always"
                    name:            "sidecar-sre"
                    resources: limits: {
                      cpu:    "100m"
                      memory: "100Mi"
                    }
                    volumeMounts: [
                      {mountPath: "/tmp/data/log/base-service", name: "log-volume"},
                      {mountPath: "/scratch", name:                   "sidecar-sre"},
                    ]
                  },
                ]
                terminationGracePeriodSeconds: 120
                volumes: [
                  {hostPath: {path:    "/logs/dev/base-service", type: "DirectoryOrCreate"}, name: "log-volume"},
                  {emptyDir: {}, name: "sidecar-sre"},
                  {emptyDir: {}, name: "skywalking"},
                ]
              }
            }
          }
        }
        parameter: {
          image:   string
          cluster: string
        }
  workload:
    definition:
      apiVersion: apps/v1
      kind: Deployment
---
apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
  annotations:
    definition.oam.dev/description: ""
  labels: {}
  name: proxy-service
spec:
  schematic:
    cue:
      template: |
        output: {
          apiVersion: "argoproj.io/v1alpha1"
          kind:       "Rollout"
          metadata: {
            labels: {
              appId:   "PROXY-SERVICE"
              appName: "proxy-service"
              version: "0.0.0"
            }
            name: "proxy-service"
          }
          spec: {
            replicas:             1
            revisionHistoryLimit: 1
            selector: matchLabels: app: "proxy-service"
            strategy: canary: steps: [
              {setWeight: 50},
              {pause: {}},
            ]
            template: {
              metadata: labels: {
                app:     "proxy-service"
                appId:   "PROXY-SERVICE"
                cluster: parameter.cluster
                version: "0.0.1"
              }
              spec: {
                affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: [
                  {
                    podAffinityTerm: {
                      labelSelector: matchExpressions: [
                        {
                          key:      "app"
                          operator: "In"
                          values: ["proxy-service"]
                        },
                      ]
                      topologyKey: "kubernetes.io/hostname"
                    }
                    weight: 100
                  },
                ]
                containers: [
                  {
                    env: [
                      {name: "NODE_IP", valueFrom: fieldRef: fieldPath:       "status.hostIP"},
                      {name: "POD_IP", valueFrom: fieldRef: fieldPath:        "status.podIP"},
                      {name: "POD_NAME", valueFrom: fieldRef: fieldPath:      "metadata.name"},
                      {name: "POD_NAMESPACE", valueFrom: fieldRef: fieldPath: "metadata.namespace"},
                      {name: "APP_NAME", value:                               "proxy-service"},
                      {name: "LOG_BASE", value:                               "/app/data/log"},
                      {name: "RUNTIME_CLUSTER", value:                        parameter.cluster},
                    ]
                    #image:          "proxy-service:0.1"
                    image:           parameter.image
                    imagePullPolicy: "Always"
                    name:            "proxy-service"
                    ports: [
                      {containerPort: 11024, protocol: "TCP"},
                      {containerPort: 11025, protocol: "TCP"},
                    ]
                    volumeMounts: [
                      {mountPath: "/tmp/data/log/proxy-service", name: "log-volume"},
                      {mountPath: "/app/data", name:                   "sidecar-sre"},
                      {mountPath: "/app/skywalking", name:             "skywalking"},
                    ]
                  },
                ]
                initContainers: [
                  {
                    args: ["echo \"do something\" "]
                    command: ["/bin/sh", "-c"]
                    env: [
                      {name: "NODE_IP", valueFrom: fieldRef: fieldPath: "status.hostIP"},
                      {name: "POD_IP", valueFrom: fieldRef: fieldPath:  "status.podIP"},
                      {name: "APP_NAME", value:                         "proxy-service"},
                    ]
                    image:           "busybox"
                    imagePullPolicy: "Always"
                    name:            "sidecar-sre"
                    resources: limits: {
                      cpu:    "100m"
                      memory: "100Mi"
                    }
                    volumeMounts: [
                      {mountPath: "/tmp/data/log/proxy-service", name: "log-volume"},
                      {mountPath: "/scratch", name:                    "sidecar-sre"},
                    ]
                  },
                ]
                terminationGracePeriodSeconds: 120
                volumes: [
                  {hostPath: {path:    "/app/logs/dev/proxy-service", type: "DirectoryOrCreate"}, name: "log-volume"},
                  {emptyDir: {}, name: "sidecar-sre"},
                  {emptyDir: {}, name: "skywalking"},
                ]
              }
            }
          }
        }
        parameter: {
          image:   string
          cluster: string
        }
  workload:
    definition:
      apiVersion: argoproj.io/v1alpha1
      kind: Rollout

---
apiVersion: core.oam.dev/v1beta1
kind: TraitDefinition
metadata:
  name: ingress
spec:
  schematic:
    cue:
      template: |
        parameter: {
          domain: string
          http: [string]: int
        }
        outputs: {
          "service": {
            apiVersion: "v1"
            kind: "Service"
            metadata: {
              name: context.name
              namespace: context.namespace
            }
            spec: {
              selector: app: context.name
              ports: [for ph, pt in parameter.http{
                protocol: "TCP"
                port: pt
                targetPort: pt
              }]
            }
          }
          "ingress": {
            apiVersion: "networking.k8s.io/v1"
            kind: "Ingress"
            metadata: {
              name: "\(context.name)-ingress"
              namespace: context.namespace
            }
            spec: rules: [{
              host: parameter.domain
              http: paths: [for ph, pt in parameter.http {
                  path: ph
                  pathType: "Prefix"
                  backend: service: {
                      name: context.name
                      port: number: pt
                  }
              }]
            }]
           }
        }
