# Copyright 2023 Google LLC
# 
# 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.

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
  namespace: sample
data:
  nginx.conf: |
     events {}
     http {
       server {
         listen 80;
         root /var/www/html;
         location / {
            return 200 'Hello World!';
         }
       }
       server {
         listen 8080;
         location /stub_status {
            stub_status on;
         }
       }
     }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: sample
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
          name: web
        - containerPort: 8080
          name: status
        volumeMounts:
          - name: nginx-config
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
        readinessProbe:
          httpGet:
            path: /stub_status
            port: 8080
          initialDelaySeconds: 2
          periodSeconds: 2
          failureThreshold: 1 
        resources:
          requests:
            cpu: 10m
            memory: 10Mi
          limits:
            memory: 10Mi            
      - name: nginx-prometheus-exporter
        image: nginx/nginx-prometheus-exporter:0.10.0
        ports:
        - containerPort: 9113
          name: metrics          
        env:
        - name: SCRAPE_URI
          value: http://localhost:8080/stub_status
        resources:
          requests:
            cpu: 5m
            memory: 5Mi
          limits:
            memory: 5Mi            
      volumes:
      - name: nginx-config
        configMap:
          name: nginx-config
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: sample
  annotations:
    annotations:
    cloud.google.com/neg: '{"ingress": true}'
    cloud.google.com/app-protocols: '{"web":"HTTP"}'
    cloud.google.com/backend-config: '{"default": "backendconfig"}'
  labels:
    app: nginx
spec:
  ports:
  - name: web
    port: 80
    protocol: TCP
  selector:
    app: nginx
---
apiVersion: monitoring.googleapis.com/v1
kind: ClusterPodMonitoring
metadata:
  name: nginx
  namespace: sample
spec:
  selector:
    matchLabels:
      app: nginx
  endpoints:
  - port: metrics
    interval: 30s    