# 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.
# Config Sync GCP ServiceAccount (GSA)
# This GSA can be used to grant Config Sync additional permissions with IAMPartialPolicy
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMServiceAccount
metadata:
  name: sync-cluster-name # kpt-set: sync-${cluster-name}
  namespace: config-control # kpt-set: ${namespace}
  annotations:
    cnrm.cloud.google.com/blueprint: cnrm/gitops/configsync/v0.6.1
    cnrm.cloud.google.com/project-id: project-id # kpt-set: ${project-id}
spec:
  displayName: sync-cluster-name # kpt-set: sync-${cluster-name}
---
# Allow Config Sync Kubernetes ServiceAccount (KSA) to use the Config Sync GSA
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPartialPolicy
metadata:
  name: sync-cluster-name # kpt-set: sync-${cluster-name}
  namespace: config-control # kpt-set: ${namespace}
  annotations:
    cnrm.cloud.google.com/blueprint: cnrm/gitops/configsync/v0.6.1
    cnrm.cloud.google.com/project-id: project-id # kpt-set: ${project-id}
spec:
  resourceRef:
    name: sync-cluster-name # kpt-set: sync-${cluster-name}
    apiVersion: iam.cnrm.cloud.google.com/v1beta1
    kind: IAMServiceAccount
  bindings:
    - role: roles/iam.workloadIdentityUser
      members:
        - member: serviceAccount:project-id.svc.id.goog[config-management-system/root-reconciler] # kpt-set: serviceAccount:${project-id}.svc.id.goog[config-management-system/root-reconciler]
---
# Allow Config Sync GSA to read from CSR repos in the CSR project
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPartialPolicy
metadata:
  name: source-reader-sync-cluster-name-project-id # kpt-set: source-reader-sync-${cluster-name}-${project-id}
  namespace: config-control # kpt-set: ${namespace}
  annotations:
    cnrm.cloud.google.com/blueprint: cnrm/gitops/configsync/v0.6.1
    cnrm.cloud.google.com/project-id: project-id # kpt-set: ${project-id}
spec:
  resourceRef:
    apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
    kind: Project
    external: project-id # kpt-set: ${project-id}
  bindings:
    - role: roles/source.reader
      members:
        - member: serviceAccount:sync-cluster-name@project-id.iam.gserviceaccount.com # kpt-set: serviceAccount:sync-${cluster-name}@${project-id}.iam.gserviceaccount.com
