# 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: Namespace
metadata:
  labels:
    app: autopsc
    control-plane: controller-manager
  name: autopsc-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: autopsc
  name: autopsc-controller-manager
  namespace: autopsc-system
  annotations:
    "iam.gke.io/gcp-service-account": "autopsc-system@GOOGLE_CLOUD_PROJECT.iam.gserviceaccount.com"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  labels:
    app: autopsc
  name: autopsc-leader-election-role
  namespace: autopsc-system
rules:
  - apiGroups:
      - ""
    resources:
      - configmaps
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - coordination.k8s.io
    resources:
      - leases
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - create
      - patch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  creationTimestamp: null
  labels:
    app: autopsc
  name: autopsc-manager-role
rules:
  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - create
      - patch
  - apiGroups:
      - "gateway.networking.k8s.io"
    resources:
      - gateways
    verbs:
      - get
      - list
      - patch
      - update
      - watch
  - apiGroups:
      - "gateway.networking.k8s.io"
    resources:
      - gateways/finalizers
    verbs:
      - update
  - apiGroups:
      - "gateway.networking.k8s.io"
    resources:
      - gateways/status
    verbs:
      - get
      - patch
      - update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
    app: autopsc
  name: autopsc-metrics-reader
rules:
  - nonResourceURLs:
      - /metrics
    verbs:
      - get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
    app: autopsc
  name: autopsc-proxy-role
rules:
  - apiGroups:
      - authentication.k8s.io
    resources:
      - tokenreviews
    verbs:
      - create
  - apiGroups:
      - authorization.k8s.io
    resources:
      - subjectaccessreviews
    verbs:
      - create
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
    app: autopsc
  name: autopsc-leader-election-rolebinding
  namespace: autopsc-system
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: autopsc-leader-election-role
subjects:
  - kind: ServiceAccount
    name: autopsc-controller-manager
    namespace: autopsc-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
    app: autopsc
  name: autopsc-manager-rolebinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: autopsc-manager-role
subjects:
  - kind: ServiceAccount
    name: autopsc-controller-manager
    namespace: autopsc-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
    app: autopsc
  name: autopsc-proxy-rolebinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: autopsc-proxy-role
subjects:
  - kind: ServiceAccount
    name: autopsc-controller-manager
    namespace: autopsc-system
---
apiVersion: v1
data:
  controller_manager_config.yaml: |
    # Copyright 2021 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: controller-runtime.sigs.k8s.io/v1alpha1
    kind: ControllerManagerConfig
    health:
      healthProbeBindAddress: :8081
    metrics:
      bindAddress: 127.0.0.1:8080
    webhook:
      port: 9443
    leaderElection:
      leaderElect: true
      resourceName: 9fe89c94.controller.autopsc.dev
kind: ConfigMap
metadata:
  labels:
    app: autopsc
  name: autopsc-manager-config
  namespace: autopsc-system
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: autopsc
    control-plane: controller-manager
  name: autopsc-controller-manager-metrics-service
  namespace: autopsc-system
spec:
  ports:
    - name: https
      port: 8443
      targetPort: https
  selector:
    app: autopsc
    control-plane: controller-manager
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: autopsc
    control-plane: controller-manager
  name: autopsc-controller-manager
  namespace: autopsc-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app: autopsc
      control-plane: controller-manager
  template:
    metadata:
      labels:
        app: autopsc
        control-plane: controller-manager
    spec:
      containers:
        - args:
            - --secure-listen-address=0.0.0.0:8443
            - --upstream=http://127.0.0.1:8080/
            - --logtostderr=true
            - --v=10
          image: gcr.io/kubebuilder/kube-rbac-proxy:v0.8.0
          name: kube-rbac-proxy
          ports:
            - containerPort: 8443
              name: https
          securityContext:
            allowPrivilegeEscalation: false
            privileged: false
        - args:
            - --health-probe-bind-address=:8081
            - --metrics-bind-address=127.0.0.1:8080
            - --leader-elect
          image: gcr.io/serverless-container-images/gke-autopsc-controller:v0.2.0
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8081
            initialDelaySeconds: 15
            periodSeconds: 20
          name: manager
          readinessProbe:
            httpGet:
              path: /readyz
              port: 8081
            initialDelaySeconds: 5
            periodSeconds: 10
          resources:
            limits:
              cpu: 100m
              memory: 30Mi
            requests:
              cpu: 100m
              memory: 20Mi
          securityContext:
            allowPrivilegeEscalation: false
            privileged: false
      securityContext:
        runAsNonRoot: true
      serviceAccountName: autopsc-controller-manager
      terminationGracePeriodSeconds: 10