# Copyright 2022 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.
#########
# BigQuery Log Sink
apiVersion: logging.cnrm.cloud.google.com/v1beta1
kind: LoggingLogSink
metadata:
  name: bigquery-logsink
  namespace: config-control # kpt-set: ${management-namespace}
  annotations:
    config.kubernetes.io/depends-on: bigquery.cnrm.cloud.google.com/namespaces/guardrails/BigQueryDataset/bigquerylogginglogsink # kpt-set: bigquery.cnrm.cloud.google.com/namespaces/${management-namespace}/BigQueryDataset/bigquerylogginglogsink,resourcemanager.cnrm.cloud.google.com/namespaces/${management-namespace}/Project/${guardrails-project-id}
spec:
  destination:
    bigQueryDatasetRef:
      name: bigquerylogginglogsink
      namespace: guardrails # kpt-set: ${management-namespace}
  includeChildren: true
  filter: logName:cloudaudit.googleapis.com
  organizationRef:
    external: "0000000000" # kpt-set: ${org-id}
---
#########
# Big Query Instance
apiVersion: bigquery.cnrm.cloud.google.com/v1beta1
kind: BigQueryDataset
metadata:
  name: bigquerylogginglogsink
  namespace: config-control # kpt-set: ${management-namespace}
  annotations:
    cnrm.cloud.google.com/delete-contents-on-destroy: "true"
    config.kubernetes.io/depends-on: resourcemanager.cnrm.cloud.google.com/namespaces/config-control/Project/guardrails-project-id # kpt-set: resourcemanager.cnrm.cloud.google.com/namespaces/${management-namespace}/Project/${guardrails-project-id},serviceusage.cnrm.cloud.google.com/namespaces/${management-namespace}/Service/guardrails-project-services-bigquery
spec:
  location: northamerica-northeast1
  projectRef:
    external: project-id # kpt-set: ${guardrails-project-id}
---
# BigQuery Audit Log Viewer
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPartialPolicy
metadata:
  name: bq-sink-writer
  namespace: config-control # kpt-set: ${management-namespace}
  annotations:
    config.kubernetes.io/depends-on: logging.cnrm.cloud.google.com/namespaces/config-control/LoggingLogSink/bigquery-logsink # kpt-set: logging.cnrm.cloud.google.com/namespaces/${management-namespace}/LoggingLogSink/bigquery-logsink
spec:
  resourceRef:
    kind: Project
    name: guardrails-project-id # kpt-set: ${guardrails-project-id}
  bindings:
    - role: roles/bigquery.dataOwner
      members:
        - memberFrom:
            logSinkRef:
              name: bigquery-logsink
