apiVersion: v1
kind: Namespace
metadata:
  name: cavisson

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sqlserver
  namespace: cavisson
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sqlserver
  template:
    metadata:
      labels:
        app: sqlserver
    spec:
      volumes:
        - name: sqldata
          emptyDir: {}
      containers:
        - name: sqlserver
          image: mcr.microsoft.com/azure-sql-edge
          ports:
            - containerPort: 1433
          env:
            - name: SA_PASSWORD
              value: "@someThingComplicated1234"
            - name: ACCEPT_EULA
              value: "Y"
          volumeMounts:
            - name: sqldata
              mountPath: /var/opt/mssql

---
apiVersion: v1
kind: Service
metadata:
  name: sqlserver
  namespace: cavisson
spec:
  type: ClusterIP
  selector:
    app: sqlserver
  ports:
    - name: sql
      port: 1433
      targetPort: 1433

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: eshopwebmvc-alpine
  namespace: cavisson
spec:
  replicas: 1
  selector:
    matchLabels:
      app: eshopwebmvc-alpine
  template:
    metadata:
      labels:
        app: eshopwebmvc-alpine
    spec:
      volumes:
        - name: app-secrets
          emptyDir: {}
        - name: aspnet-https
          emptyDir: {}
        - name: agent-volume
          emptyDir: {}

      initContainers:
        - name: agent-init-alpine
          image: gmfpoc.azurecr.io/agent-init-alpine:latest
          command: ["/bin/sh", "-c"]
          args:
            - |
              echo "Installing Cavisson .NET Agent..."

              mkdir -p /shared-agent/netdiagnostics/DotNetC/dll
              mkdir -p /shared-agent/netdiagnostics/DotNetC/lib
              mkdir -p /shared-agent/netdiagnostics/DotNetC/config
              mkdir -p /shared-agent/netdiagnostics/DotNetC/dll/deps

              cp /opt/cavisson/netdiagnostics/DotNetC/dll/* /shared-agent/netdiagnostics/DotNetC/dll/
              cp /opt/cavisson/netdiagnostics/DotNetC/dll/deps/* /shared-agent/netdiagnostics/DotNetC/dll/deps/
              cp /opt/cavisson/netdiagnostics/DotNetC/lib/* /shared-agent/netdiagnostics/DotNetC/lib/
              cp /opt/cavisson/netdiagnostics/DotNetC/config/* /shared-agent/netdiagnostics/DotNetC/config/
              cp /opt/cavisson/netdiagnostics/DotNetC/version /shared-agent/netdiagnostics/DotNetC/

              ln -sf /shared-agent/netdiagnostics/DotNetC/lib/libwebsockets.so.19 /shared-agent/netdiagnostics/DotNetC/lib/libwebsockets.so
              ln -sf /shared-agent/netdiagnostics/DotNetC/lib/libcavapr-1.so.0.7.4 /shared-agent/netdiagnostics/DotNetC/lib/libcavapr-1.so.0
              ln -sf /shared-agent/netdiagnostics/DotNetC/lib/libcavapr-1.so.0.7.4 /shared-agent/netdiagnostics/DotNetC/lib/libcavapr-1.so
              ln -sf /shared-agent/netdiagnostics/DotNetC/lib/libuuid.so.1.3.0 /shared-agent/netdiagnostics/DotNetC/lib/libuuid.so.1
              ln -sf /shared-agent/netdiagnostics/DotNetC/lib/libuuid.so.1.3.0 /shared-agent/netdiagnostics/DotNetC/lib/libuuid.so

              chmod -R 755 /shared-agent/netdiagnostics
              echo "DONE"

          volumeMounts:
            - name: agent-volume
              mountPath: /shared-agent

      containers:
        - name: eshopwebmvc-alpine
          image: gmfpoc.azurecr.io/eshopwebmvc-alpine:latest

          # Security
          securityContext:
            readOnlyRootFilesystem: true
            allowPrivilegeEscalation: false
            runAsNonRoot: true

          resources:
            requests:
             cpu: "1000m"
             memory: "500Mi"
            limits:
             cpu: "1000m"
             memory: "500Mi"

          env:
            - name: ASPNETCORE_ENVIRONMENT
              value: "Docker"
            - name: ASPNETCORE_URLS
              value: "http://+:8080"
            - name: ConnectionStrings__CatalogConnection
              value: "Server=sqlserver,1433;User Id=sa;Password=@someThingComplicated1234;TrustServerCertificate=true;"
            - name: DOTNET_SYSTEM_GLOBALIZATION_INVARIANT
              value: "false"
            - name: DOTNET_CULTURE
              value: "en-US"
            - name: NDHOME
              value: "/shared-agent/netdiagnostics"
            - name: LD_LIBRARY_PATH
              value: "$(NDHOME)/DotNetC/lib:$(LD_LIBRARY_PATH)"
            - name: CORECLR_ENABLE_PROFILING
              value: "1"
            - name: CORECLR_PROFILER
              value: "{918728DD-259F-4A6A-AC2B-B85E1B658318}"
            - name: CORECLR_PROFILER_PATH
              value: "/shared-agent/netdiagnostics/DotNetC/dll/ILRewriteProfiler.so"
            - name: DOTNET_STARTUP_HOOKS
              value: "/shared-agent/netdiagnostics/DotNetC/dll/ProfilerHelper.dll"
            - name: CAV_APP_AGENT_ENABLE_MONITORING
              value: "1"
            - name: CAV_APP_AGENT_PROXYIP
              value: "66.220.31.146"
            - name: CAV_APP_AGENT_PROXYPORT
              value: "7939"
            - name: CAV_APP_AGENT_PROXYMODE
              value: "1"
            - name: CAV_APP_AGENT_TIER
              value: "eShopAKSAlpineTier"
            - name: CAV_APP_AGENT_SERVER
              value: "eShopAKSAlpineServer"
            - name: CAV_APP_AGENT_INSTANCE
              value: "eShopAKSAlpineInstance"
          ports:
            - containerPort: 8080

          volumeMounts:
            - name: app-secrets
              mountPath: /root/.microsoft/usersecrets
            - name: aspnet-https
              mountPath: /root/.aspnet/https
            - name: agent-volume
              mountPath: /shared-agent

---
apiVersion: v1
kind: Service
metadata:
  name: eshopwebmvc-alpine
  namespace: cavisson
spec:
  type: LoadBalancer
  selector:
    app: eshopwebmvc-alpine
  ports:
    - name: http
      port: 5106
      targetPort: 8080

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: eshoppublicapi-alpine
  namespace: cavisson
spec:
  replicas: 1
  selector:
    matchLabels:
      app: eshoppublicapi-alpine
  template:
    metadata:
      labels:
        app: eshoppublicapi-alpine
    spec:
      volumes:
        - name: app-secrets
          emptyDir: {}
        - name: aspnet-https
          emptyDir: {}

      containers:
        - name: eshoppublicapi-alpine
          image: gmfpoc.azurecr.io/eshoppublicapi-alpine:latest

          # Security
          securityContext:
            readOnlyRootFilesystem: true
            allowPrivilegeEscalation: false
            runAsNonRoot: true

          resources:
            requests:
             cpu: "1000m"
             memory: "500Mi"
            limits:
             cpu: "1000m"
             memory: "500Mi"

          env:
            - name: ASPNETCORE_ENVIRONMENT
              value: "Docker"
            - name: ASPNETCORE_URLS
              value: "http://+:8080"
            - name: ConnectionStrings__IdentityConnection
              value: "Server=sqlserver,1433;User Id=sa;Password=@someThingComplicated1234;TrustServerCertificate=true;"

          ports:
            - containerPort: 8080

          volumeMounts:
            - name: app-secrets
              mountPath: /root/.microsoft/usersecrets
            - name: aspnet-https
              mountPath: /root/.aspnet/https

