apiVersion: v1
kind: Namespace
metadata:
  name: cloudpilot
  labels:
    app.kubernetes.io/name: cloudpilot
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cloudpilot-agent-cluster-role
rules:
  - apiGroups: ["batch"]
    resources: [jobs, cronjobs]
    verbs: [get, list, watch]
  - apiGroups: ["batch"]
    resources: [cronjobs]
    verbs: [patch]
  - apiGroups:
      - "karpenter.k8s.aws"
    resources:
      - "ec2nodeclasses"
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - "karpenter.k8s.gcp"
    resources:
      - "gcenodeclasses"
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - "karpenter.azure.com"
    resources:
      - "aksnodeclasses"
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - "karpenter.sh"
    resources:
      - "nodeclaims"
      - "nodepools"
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - "storage.k8s.io"
    resources:
      - "csinodes"
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - "namespaces"
      - "nodes"
      - "pods"
      - "events"
      - "persistentvolumeclaims"
      - "persistentvolumes"
    verbs:
      - get
      - list
      - watch
  # Disk and PVC usage is fetched through the kubelet stats proxy with GET.
  - apiGroups: [""]
    resources: ["nodes/proxy"]
    verbs: ["get"]
  - apiGroups:
      - ""
    resources:
      - "events"
    verbs:
      - create
  - apiGroups:
      - "apps"
    resources:
      - "deployments"
      - "daemonsets"
      - "statefulsets"
      - "replicasets"
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - "policy"
    resources:
      - "poddisruptionbudgets"
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - "metrics.k8s.io"
    resources:
      - "pods"
      - "nodes"
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - "apiextensions.k8s.io"
    resources:
      - "customresourcedefinitions"
    verbs:
      - get
      - list
      - watch
      - create
      - patch
  - apiGroups:
      - "evpa.cloudpilot.ai"
    resources:
      - "autoscalingpolicyconfigurations"
      - "autoscalingpolicyconfigurations/status"
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - "evpa.cloudpilot.ai"
    resources:
      - "autoscalingpolicies"
      - "autoscalingpolicies/status"
      - "recommendationpolicies"
    verbs:
      - create
      - update
      - list
      - watch
      - patch
      - delete
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: cloudpilot-agent-role
  namespace: cloudpilot
rules:
  # Agent warning diagnostics only read logs from CloudPilot component Pods.
  - apiGroups: [""]
    resources: ["pods/log"]
    verbs: ["get"]
  - apiGroups: ["coordination.k8s.io"]
    resources: ["leases"]
    verbs: ["get", "list", "create", "update", "patch", "watch"]
  - apiGroups: ["apps"]
    resources: ["deployments"]
    resourceNames:
      - "cloudpilot-agent"
      - "cloudpilot-workload-autoscaler"
      - "cloudpilot-controller"
      - "cloudpilot-webhook"
      - "cloudpilot-workload-log-collector"
      - "cloudpilot-aws-optimizer"
      - "cloudpilot-alibabacloud-optimizer"
      - "cloudpilot-gcp-optimizer"
      - "cloudpilot-azure-optimizer"
    verbs: ["get", "update", "patch"]
  - apiGroups: ["apps"]
    resources: ["daemonsets"]
    resourceNames:
      - "cloudpilot-node-agent"
      - "cloudpilot-nri-plugin"
      - "cloudpilot-spot-handler"
      - "image-accelerator"
    verbs: ["get", "update", "patch"]
  - apiGroups: ["apps"]
    resources: ["statefulsets"]
    resourceNames: ["cloudpilot-victoria-metrics"]
    verbs: ["get", "update", "patch"]
  - apiGroups: [""]
    resources: ["pods", "events"]
    verbs: ["get", "list", "watch"]
  - apiGroups: ["apps"]
    resources: ["replicasets"]
    verbs: ["get", "list", "watch"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: cloudpilot-agent
  namespace: cloudpilot
  labels:
    app.kubernetes.io/name: cloudpilot
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cloudpilot-agent-cluster-rb
  labels:
    app.kubernetes.io/name: cloudpilot
subjects:
  - kind: ServiceAccount
    name: cloudpilot-agent
    namespace: cloudpilot
roleRef:
  kind: ClusterRole
  name: cloudpilot-agent-cluster-role
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: cloudpilot-agent-rb
  namespace: cloudpilot
  labels:
    app.kubernetes.io/name: cloudpilot
subjects:
  - kind: ServiceAccount
    name: cloudpilot-agent
    namespace: cloudpilot
roleRef:
  kind: Role
  name: cloudpilot-agent-role
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cloudpilot-agent
  namespace: cloudpilot
  labels:
    app.kubernetes.io/component: cloudpilot-agent
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cloudpilot-agent
  template:
    metadata:
      annotations:
        eks.amazonaws.com/compute-type: ec2
      labels:
        app: cloudpilot-agent
        goog-partner-solution: isol_plb32_001kf00001fxx2ciax_lo372gah4wffzo7zflvuu334rgv5iivl
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/os
                    operator: NotIn
                    values: [windows]
                  - key: type
                    operator: NotIn
                    values: [virtual-kubelet]
                  - key: node.kubernetes.io/instance-type
                    operator: NotIn
                    values: [eklet]
                  - key: eks.amazonaws.com/compute-type
                    operator: NotIn
                    values: [fargate]
                  - key: virtualization
                    operator: NotIn
                    values: [virtualnode2]
      hostNetwork: true # TODO: Waiting for clusterID to be revamped to turn this off, CLO-594
      serviceAccountName: cloudpilot-agent
      enableServiceLinks: false
      containers:
        - name: cloudpilot-agent
          image: public.ecr.aws/cloudpilotai/cloudpilot-agent:v1.23.1@sha256:baeb0923ad9c542c972fe628ed3303c98ef54265536e097a78925e0358e6d3a6
          imagePullPolicy: IfNotPresent
          args:
            - --v=4 # debug level log
          env:
            - name: CLOUDPILOT_API_KEY
              value: ${CLOUDPILOT_API_KEY}
            - name: CLOUDPILOT_API_ENDPOINT
              value: https://api.cloudpilot.ai
            - name: PREDICTOR_API_ENDPOINT
              value: https://predictor.cloudpilot.ai
            - name: CLUSTER_NAME
              value: ${CLUSTER_NAME}
            - name: CLOUD_PROVIDER
              value: ${CLOUD_PROVIDER}
            - name: REGION
              value: ${CLUSTER_REGION}
            - name: CLUSTER_LOCATION
              value: ${CLUSTER_LOCATION}
            - name: GCP_PROJECT_ID
              value: ${GCP_PROJECT_ID}
            - name: PRICE_API_ENDPOINT
              value: https://price.cloudpilot.ai
            - name: DISABLE_WORKLOAD_UPLOADING
              value: "${DISABLE_WORKLOAD_UPLOADING}"
            - name: LEADER_ELECTION_ID
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: ENABLE_MAINTENANCE_MODE
              value: "false"
            - name: PROXY_API_ENDPOINT
              value: https://proxy.cloudpilot.ai
            - name: SECURITY_PROXY
              value: ${SECURITY_PROXY}
            - name: ENABLE_DATA_PROTECTION_MODE
              value: "${ENABLE_DATA_PROTECTION_MODE}"
          resources:
            requests:
              cpu: 250m
              memory: 500Mi
            limits:
              cpu: 1000m
              memory: 1024Mi
      terminationGracePeriodSeconds: 30
      dnsPolicy: Default
---
apiVersion: v1
kind: Service
metadata:
  name: cloudpilot-agent
  namespace: cloudpilot
  labels:
    app.kubernetes.io/component: cloudpilot-agent
spec:
  ports:
  - name: agent
    port: 80
    protocol: TCP
    targetPort: 24909
  selector:
    app: cloudpilot-agent
  type: ClusterIP
---
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: cloudpilot-critical
value: 1000000000
globalDefault: false
preemptionPolicy: PreemptLowerPriority
description: "This priority class is used for CloudPilot AI critical pods."
