# Copyright (c) 2022 Yunshan Networks

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

##################################################################################################
# This file defines the services, deployments, configmap, statefulsets 
#
# To deploy full demo, you can :
# kubectl apply -f deepflow-otel-skywalking-demo.yaml
##################################################################################################

##################################################################################################
# Namespace
##################################################################################################
apiVersion: v1
kind: Namespace
metadata:
  name: deepflow-otel-skywalking-demo
---
##################################################################################################
# Deployments
##################################################################################################
apiVersion: apps/v1
kind: Deployment
metadata:
  name: svc-item
  namespace: deepflow-otel-skywalking-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: svc-item
  template:
    metadata:
      labels:
        app: svc-item
        version: latest
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - svc-item
                  - svc-order
                  - svc-stock
                  - svc-user
                  - web-shop
                  - loadgenerator
                  - db-demo
                  - nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
      - name: svc-item
        image: registry.cn-beijing.aliyuncs.com/deepflow-demo/skywalking-demo_mydemo-item:1.0
        imagePullPolicy: IfNotPresent
        env:
        - name: SERVICE_HOST
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: SERVICE_PORT
          value: "20880"
        - name: SW_AGENT_NAME
          value: spring-svc-item
        - name: SKYWALKING_HOST
          value: otel-agent.open-telemetry
        - name: SKYWALKING_PORT
          value: '11800'
        ports:
        - containerPort: 20880
        volumeMounts:
          - name: sidecar
            mountPath: /sidecar
          - name: config 
            mountPath: /home/application.yml
            subPath: application.yml
      initContainers:
        - name: sidecar
          image: apache/skywalking-java-agent:8.11.0-java8
          imagePullPolicy: IfNotPresent
          command:
            - /bin/sh
          args:
            - '-c'
            - cp -R /skywalking/agent /sidecar/
          volumeMounts:
            - name: sidecar
              mountPath: /sidecar
      volumes:
        - name: sidecar
          emptyDir: {}
        - configMap:
            name: item-service
          name: config
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: svc-order
  namespace: deepflow-otel-skywalking-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: svc-order
  template:
    metadata:
      labels:
        app: svc-order
        version: latest
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - svc-item
                  - svc-order
                  - svc-stock
                  - svc-user
                  - web-shop
                  - loadgenerator
                  - db-demo
                  - nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
      - name: svc-order
        image: registry.cn-beijing.aliyuncs.com/deepflow-demo/skywalking-demo_mydemo-order:1.0
        imagePullPolicy: IfNotPresent
        env:
        - name: MYSQL_HOST
          value: "db-demo"
        - name: SERVICE_HOST
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: SERVICE_PORT
          value: "20880"
        - name: SW_AGENT_NAME
          value: spring-svc-order
        - name: SKYWALKING_HOST
          value: otel-agent.open-telemetry
        - name: SKYWALKING_PORT
          value: '11800'
        ports:
        - containerPort: 20880
        volumeMounts:
          - name: sidecar
            mountPath: /sidecar
      initContainers:
        - name: sidecar
          image: apache/skywalking-java-agent:8.11.0-java8
          imagePullPolicy: IfNotPresent
          command:
            - /bin/sh
          args:
            - '-c'
            - cp -R /skywalking/agent /sidecar/
          volumeMounts:
            - name: sidecar
              mountPath: /sidecar
      volumes:
        - name: sidecar
          emptyDir: {}
        - configMap:
            name: order-service
          name: config
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: svc-stock
  namespace: deepflow-otel-skywalking-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: svc-stock
  template:
    metadata:
      labels:
        app: svc-stock
        version: latest
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - svc-item
                  - svc-order
                  - svc-stock
                  - svc-user
                  - web-shop
                  - loadgenerator
                  - db-demo
                  - nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
      - name: svc-stock
        image: registry.cn-beijing.aliyuncs.com/deepflow-demo/skywalking-demo_mydemo-stock:1.0
        imagePullPolicy: IfNotPresent
        env:
        - name: SERVICE_HOST
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: SERVICE_PORT
          value: "20880"
        - name: SW_AGENT_NAME
          value: spring-svc-stock
        - name: SKYWALKING_HOST
          value: otel-agent.open-telemetry
        - name: SKYWALKING_PORT
          value: '11800'
        ports:
        - containerPort: 20880
        volumeMounts:
          - name: sidecar
            mountPath: /sidecar
          - name: config
            mountPath: /home/application.yml
            subPath: application.yml
      initContainers:
        - name: sidecar
          image: apache/skywalking-java-agent:8.11.0-java8
          imagePullPolicy: IfNotPresent
          command:
            - /bin/sh
          args:
            - '-c'
            - cp -R /skywalking/agent /sidecar/
          volumeMounts:
            - name: sidecar
              mountPath: /sidecar
      volumes:
        - name: sidecar
          emptyDir: {}
        - configMap:
            name: stock-service
          name: config
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: svc-user
  namespace: deepflow-otel-skywalking-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: svc-user
  template:
    metadata:
      labels:
        app: svc-user
        version: latest
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - svc-item
                  - svc-order
                  - svc-stock
                  - svc-user
                  - web-shop
                  - loadgenerator
                  - db-demo
                  - nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
      - name: svc-user
        image: registry.cn-beijing.aliyuncs.com/deepflow-demo/skywalking-demo_mydemo-user:1.0
        imagePullPolicy: IfNotPresent
        env:
        - name: MYSQL_HOST
          value: "db-demo"
        - name: SERVICE_HOST
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: SERVICE_PORT
          value: "20880"
        - name: SW_AGENT_NAME
          value: spring-svc-user
        - name: SKYWALKING_HOST
          value: otel-agent.open-telemetry
        - name: SKYWALKING_PORT
          value: '11800'
        ports:
        - containerPort: 20880
        volumeMounts:
          - name: sidecar
            mountPath: /sidecar
          - name: config
            mountPath: /home/application.yml
            subPath: application.yml
      initContainers:
        - name: sidecar
          image: apache/skywalking-java-agent:8.11.0-java8
          imagePullPolicy: IfNotPresent
          command:
            - /bin/sh
          args:
            - '-c'
            - cp -R /skywalking/agent /sidecar/
          volumeMounts:
            - name: sidecar
              mountPath: /sidecar
      volumes:
        - name: sidecar
          emptyDir: {}
        - configMap:
            name: user-service
          name: config
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-shop
  namespace: deepflow-otel-skywalking-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web-shop
  template:
    metadata:
      labels:
        app: web-shop
        version: latest
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - svc-item
                  - svc-order
                  - svc-stock
                  - svc-user
                  - web-shop
                  - loadgenerator
                  - db-demo
                  - nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
      - name: web-shop
        image: registry.cn-beijing.aliyuncs.com/deepflow-demo/skywalking-demo_mydemo-shopweb:1.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8090
        env:
        - name: SW_AGENT_NAME
          value: spring-svc-webshop
        - name: SKYWALKING_HOST
          value: otel-agent.open-telemetry
        - name: SKYWALKING_PORT
          value: '11800'
        volumeMounts:
          - name: sidecar
            mountPath: /sidecar
      initContainers:
        - name: sidecar
          image: apache/skywalking-java-agent:8.11.0-java8
          imagePullPolicy: IfNotPresent
          command:
            - /bin/sh
          args:
            - '-c'
            - cp -R /skywalking/agent /sidecar/
          volumeMounts:
            - name: sidecar
              mountPath: /sidecar
      volumes:
        - name: sidecar
          emptyDir: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: loadgenerator
  namespace: deepflow-otel-skywalking-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: loadgenerator
  template:
    metadata:
      labels:
        app: loadgenerator
        version: latest
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - svc-item
                  - svc-order
                  - svc-stock
                  - svc-user
                  - web-shop
                  - loadgenerator
                  - db-demo
                  - nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
        - name: loadgenerator
          image: registry.cn-beijing.aliyuncs.com/deepflow-demo/skywalking-demo_loadgenerator:latest
          env:
            - name: FRONTEND_ADDR
              value: web-shop:8090
            - name: OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
              value: http://otel-agent.open-telemetry:4317
            - name: OTEL_RESOURCE_ATTRIBUTES
              value: service.name=loadgenerator
            - name: USERS
              value: '1'
##################################################################################################
# StatefulSets
##################################################################################################
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: db-demo
  namespace: deepflow-otel-skywalking-demo
spec:
  replicas: 1
  serviceName: db-demo
  selector:
    matchLabels:
      app: db-demo
  template:
    metadata:
      labels:
        app: db-demo
        version: 5.7.18
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - svc-item
                  - svc-order
                  - svc-stock
                  - svc-user
                  - web-shop
                  - loadgenerator
                  - db-demo
                  - nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
      - name: db-demo
        image: registry.cn-beijing.aliyuncs.com/deepflow-demo/skywalking-demo_mydemo-mysql:1.0
        imagePullPolicy: IfNotPresent
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123"
        ports:
        - containerPort: 3306
        readinessProbe:
          exec:
            command: ["mysql", "-hlocalhost", "-uroot", "-p123", "-P3306", "--database", "skywalking", "-e", "SELECT 1"]
          initialDelaySeconds: 15
          periodSeconds: 5
          timeoutSeconds: 2
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: deepflow-otel-skywalking-demo
spec:
  replicas: 1
  serviceName: nacos
  selector:
    matchLabels:
      app: nacos
  template:
    metadata:
      labels:
        app: nacos
        version: 1.1.4
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - svc-item
                  - svc-order
                  - svc-stock
                  - svc-user
                  - web-shop
                  - loadgenerator
                  - db-demo
                  - nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
      - name: nacos
        image: registry.cn-beijing.aliyuncs.com/deepflow-demo/skywalking-demo_mydemo-nacos-server:1.1.4
        imagePullPolicy: IfNotPresent
        env:
        - name: MYSQL_HOST
          value: "db-demo"
        - name: MODE
          value: standalone
        ports:
        - containerPort: 8848
        readinessProbe:
          httpGet:
            path: /nacos
            port: 8848
          initialDelaySeconds: 3
          periodSeconds: 5
          timeoutSeconds: 2
---
##################################################################################################
# ConfigMaps
##################################################################################################
apiVersion: v1
data:
  application.yml: |
    spring:
       main:
          allow-bean-definition-overriding: true
       output:
          ansi:
             enabled: always
    application:
      name: item

    nacos:
      address: ${NACOS_HOST:localhost}:8848

    dubbo:
        filter:
        application: # see com.alibaba.dubbo.config.ApplicationConfig
            id: ${application.name}-srv
            name: ${application.name}-srv
            qosEnable: false
        protocol: # see com.alibaba.dubbo.config.ProtocolConfig
            id: rest
            name: rest
            host: ${SERVICE_HOST:localhost} # Use POD IP to register Dubbo service
            port: ${SERVICE_PORT:20880}
            threads: 3
            iothreads: 1
            server: netty
            client: netty
            status: server
            serialization: fst
            queues: 0
            keepAlive: true
        registry: # see com.alibaba.dubbo.config.RegistryConfig
            address: nacos://${nacos.address}
            check: false
        provider:
            cluster: failfast
            retries: 0
            loadbalance: roundrobin
            timeout: 10000
            filter: ${dubbo.filter}

    zipkin:
       server: ${zipkin.base-url}/api/v2/spans
       connectTimeout: 5000
       readTimeout: 5000
kind: ConfigMap
metadata:
  name: item-service
  namespace: deepflow-otel-skywalking-demo
---
apiVersion: v1
data:
  application.yml: |
    spring:
      main:
          allow-bean-definition-overriding: true
      output:
          ansi:
            enabled: always
      datasource:
          driver-class-name: ${mysql.driver-class}
          url: jdbc:mysql://${mysql.host}:${mysql.port}/${db.order}?connectTimeout=3000&socketTimeout=10000&characterEncoding=utf8&useTimezone=true&serverTimezone=Asia/Shanghai&useSSL=false${jdbc.interceptors}
          username: ${mysql.user}  
          password: ${mysql.password}

    application:
      name: order 
    nacos:
      address: ${NACOS_HOST:localhost}:8848

    dubbo:
        filter:
        application: # see com.alibaba.dubbo.config.ApplicationConfig
            id: ${application.name}-srv
            name: ${application.name}-srv
            qosEnable: false
        protocol: # see com.alibaba.dubbo.config.ProtocolConfig
            id: rest
            name: rest
            host: ${SERVICE_HOST:localhost} # Use POD IP to register Dubbo service
            port: ${SERVICE_PORT:20883}
            threads: 3
            iothreads: 1
            server: netty
            client: netty
            status: server
            serialization: fst
            queues: 0
            keepAlive: true
        registry: # see com.alibaba.dubbo.config.RegistryConfig
            address: nacos://${nacos.address}
            check: false
        provider:
            cluster: failfast
            retries: 0
            loadbalance: roundrobin
            timeout: 10000
            filter: ${dubbo.filter}
        consumer:
            filter: ${dubbo.filter}
            # Fault tolerance. Options: failover/failfast/failsafe/failback/forking
            cluster: failfast
            # Load balancing strategy. Options: random, roundrobin(polling), leastactive(invoking least active service)
            loadbalance: roundrobin

    druid:
      defaultAutoCommit: true
      defaultTransactionIsolation: 2
      initialSize: 1
      maxActive: 3
      maxWait: 10000
      minIdle: 1
      validationQuery: /* ping */ select 1
      testOnBorrow: false
      testOnReturn: false
      testWhileIdle: true
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 1800000
      removeAbandoned: true
      removeAbandonedTimeout: 1800
      logAbandoned: true

    mybatis: # see MybatisProperties.class in mybatis-spring-boot-autoconfigure
      mapperLocations: classpath:mappers/*.xml

    seata:
      enabled: true
      application-id: ${application.name}
      tx-service-group: my_demo_gtx
      config:
        type: nacos
        nacos:
          namespace:
          serverAddr: ${nacos.address}
      registry:
        type: nacos
        nacos:
          cluster: default
          server-addr: ${nacos.address}
          namespace:

    zipkin:
      server: ${zipkin.base-url}/api/v2/spans
      connectTimeout: 5000
      readTimeout: 5000
kind: ConfigMap
metadata:
  name: order-service
  namespace: deepflow-otel-skywalking-demo
---
apiVersion: v1
data:
  application.yml: |
    spring:
        main:
            allow-bean-definition-overriding: true
        output:
            ansi:
                enabled: always

    application:
      name: stock
    nacos:
      address: ${NACOS_HOST:localhost}:8848

    dubbo:
        filter:
        application: # see com.alibaba.dubbo.config.ApplicationConfig
            id: ${application.name}-srv
            name: ${application.name}-srv
            qosEnable: false
        protocol: # see com.alibaba.dubbo.config.ProtocolConfig
            id: rest
            name: rest
            host: ${SERVICE_HOST:localhost} # Use POD IP to register Dubbo service
            port: ${SERVICE_PORT:20881}
            threads: 3
            iothreads: 1
            server: netty
            client: netty
            status: server
            serialization: fst
            queues: 0
            keepAlive: true
        registry: # see com.alibaba.dubbo.config.RegistryConfig
            address: nacos://${nacos.address}
            check: false
        provider:
            cluster: failfast
            retries: 0
            loadbalance: roundrobin
            timeout: 10000
            filter: ${dubbo.filter}

    zipkin:
       server: ${zipkin.base-url}/api/v2/spans
       connectTimeout: 5000
       readTimeout: 5000
kind: ConfigMap
metadata:
  name: stock-service
  namespace: deepflow-otel-skywalking-demo
---
apiVersion: v1
data:
  application.yml: |
    mysql:
      driver-class: com.mysql.jdbc.Driver
      host: ${MYSQL_HOST:localhost}
      port: ${MYSQL_PORT:3306}
      user: mydemo
      password: mydemo
    db:
      order: mydemo-dn1
      user: mydemo-dn1
    jdbc:
      interceptors:
    nacos:
      address: ${NACOS_HOST:localhost}:8848

    spring:
      main:
          allow-bean-definition-overriding: true
      output:
          ansi:
            enabled: always
      datasource:  
          driver-class-name: ${mysql.driver-class}
          url: jdbc:mysql://${mysql.host}:${mysql.port}/${db.user}?connectTimeout=3000&socketTimeout=10000&characterEncoding=utf8&useTimezone=true&serverTimezone=Asia/Shanghai&useSSL=false${jdbc.interceptors}
          username: ${mysql.user}  
          password: ${mysql.password}

    application:
      name: user

    dubbo:
        filter:
        application: # see com.alibaba.dubbo.config.ApplicationConfig
            id: ${application.name}-srv
            name: ${application.name}-srv
            qosEnable: false
        protocol: # see com.alibaba.dubbo.config.ProtocolConfig
            id: rest
            name: rest
            host: ${SERVICE_HOST:localhost} # Use POD IP to register Dubbo service
            port: ${SERVICE_PORT:20882}
            threads: 3
            iothreads: 1
            server: netty
            client: netty
            status: server
            serialization: fst
            queues: 0
            keepAlive: true
        registry: # see com.alibaba.dubbo.config.RegistryConfig
            address: nacos://${nacos.address}
            check: false
        provider:
            cluster: failfast
            retries: 0
            loadbalance: roundrobin
            timeout: 10000
            filter: ${dubbo.filter}

    druid:
      defaultAutoCommit: true
      defaultTransactionIsolation: 2
      initialSize: 1
      maxActive: 3
      maxWait: 5000
      minIdle: 1
      validationQuery: /* ping */ select 1
      testOnBorrow: false
      testOnReturn: false
      testWhileIdle: true
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 1800000 
      removeAbandoned: true
      removeAbandonedTimeout: 1800
      logAbandoned: true

    mybatis: # see MybatisProperties.class in mybatis-spring-boot-autoconfigure
      mapperLocations: classpath:mappers/*.xml

    seata:
      enabled: true
      application-id: ${application.name}
      tx-service-group: my_demo_gtx
      config:
        type: nacos
        nacos:
          namespace:
          serverAddr: ${nacos.address}
      registry:
        type: nacos
        nacos:
          cluster: default
          server-addr: ${nacos.address}
          namespace:

    zipkin:
      server: ${zipkin.base-url}/api/v2/spans
      connectTimeout: 5000
      readTimeout: 5000

    mydemo:
      hostname: ${HOSTNAME:localhost}
kind: ConfigMap
metadata:
  name: user-service
  namespace: deepflow-otel-skywalking-demo
---
##################################################################################################
# Services
##################################################################################################
kind: Service
apiVersion: v1
metadata:
  name: nacos
  namespace: deepflow-otel-skywalking-demo
spec:
  type: ClusterIP
  ports:
  - name: http-nacos
    port: 8848
    targetPort: 8848
  selector:
    app: nacos
---
kind: Service
apiVersion: v1
metadata:
  name: web-shop
  namespace: deepflow-otel-skywalking-demo
spec:
  type: ClusterIP
  ports:
    - name: http-shop
      port: 8090
      targetPort: 8090
  selector:
    app: web-shop
---
kind: Service
apiVersion: v1
metadata:
  name: db-demo
  namespace: deepflow-otel-skywalking-demo
spec:
  type: ClusterIP
  ports:
  - name: mysql
    port: 3306
    targetPort: 3306
  selector:
    app: db-demo