-- CLOUD-20862 | Topology Agent | 리더 선출 로직 개선
-- Auto-generated SQL script #202310121749
UPDATE public.agent_install_file_info
	SET yaml='---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cloudmoa-cluster-role
rules:
  - nonResourceURLs:
      - "*"
    verbs:
      - get
  - apiGroups:
      - metrics.k8s.io
    resources:
      - pods
      - nodes
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - pods
    verbs:
      - get
      - list
      - watch
      - update
  - apiGroups:
      - ""
    resources:
      - services
    verbs:
      - get
      - list
      - watch
      - update
  - apiGroups:
      - ""
    resources:
      - nodes/stats
      - endpoints
      - namespaces
      - events
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - apps
    resources:
      - daemonsets
      - deployments
      - deployments/scale
      - replicasets
      - replicasets/scale
      - statefulsets
      - statefulsets/scale
    verbs:
      - get
      - list
      - watch
      - update
  - apiGroups:
      - batch
    resources:
      - jobs
    verbs:
      - get
      - list
      - watch
      - update
  - apiGroups:
      - batch
    resources:
      - cronjobs
    verbs:
      - get
      - list
      - update
  - apiGroups:
      - storage.j8s.io
    resources:
      - storageclasses
    verbs:
      - get
      - list
  - apiGroups:
      - networking.k8s.io
    resources:
      - ingresses
    verbs:
      - get
      - list
  - apiGroups:
      - extensions
    resources:
      - ingresses
    verbs:
      - get
      - list
  - apiGroups:
      - policy
    resources:
      - podsecuritypolicies
    verbs:
      - use
    resourceNames:
      - imxc-ps
  - apiGroups:
      - certificates.k8s.io
    resourceNames:
      - kubernetes.io/kube-apiserver-client-kubelet
    resources:
      - signers
    verbs:
      - approve
  - apiGroups:
      - certificates.k8s.io
    resourceNames:
      - kubernetes.io/kubelet-serving
    resources:
      - signers
    verbs:
      - approve
  - apiGroups:
      - ""
    resources:
      - nodes
    verbs:
      - get
      - list
      - watch
      - proxy
  - apiGroups:
      - ""
    resources:
      - nodes/log
      - nodes/metrics
      - nodes/proxy
      - nodes/spec
      - nodes/stats
    verbs:
      - ''*''
  - apiGroups:
      - ''*''
    resources:
      - ''*''
    verbs:
      - get
      - list
      - watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cloudmoa-restricted-rb
  namespace: $CLOUDMOA_NAMESPACE
subjects:
  - kind: ServiceAccount
    name: default
    namespace: $CLOUDMOA_NAMESPACE
roleRef:
  kind: ClusterRole
  name: cloudmoa-cluster-role
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: cloudmoa-psp
  namespace: $CLOUDMOA_NAMESPACE
spec:
  privileged: true
  seLinux:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  runAsUser:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny
  hostPorts:
    - max: 65535
      min: 0
  hostNetwork: true
  hostPID: true
  volumes:
    - configMap
    - secret
    - emptyDir
    - hostPath
    - projected
    - downwardAPI
    - persistentVolumeClaim
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: cloudmoa-topology-agent
  namespace: $CLOUDMOA_NAMESPACE
  labels:
    app: cloudmoa-topology-agent
spec:
  selector:
    matchLabels:
      app: cloudmoa-topology-agent
  template:
    metadata:
      labels:
        app: cloudmoa-topology-agent
    spec:
      hostNetwork: true
      hostPID: true
      tolerations:
      - effect: NoSchedule
        operator: Exists
      - effect: NoExecute
        operator: Exists
      containers:
      - image: $DOCKER_REGISTRY_URL/node-exporter
        name: node-agent
        resources:
          limits:
            cpu: 250m
            memory: 180Mi
          requests:
            cpu: 125m
            memory: 90Mi
        ports:
        - containerPort: 9110
          hostPort: 9110
          name: scrape
        args:
        - --path.procfs=/host/proc
        - --path.sysfs=/host/sys
        - --path.rootfs=/host/root
        - --collector.filesystem.ignored-mount-points=^/(dev|proc|sys|run|var/lib/docker/.+|var/lib/kubelet/pods/.+)($|/)
        - --collector.tcpstat
        - --web.listen-address=:9110
        # --log.level=debug
        env:
        - name: GOMAXPROCS
          value: "1"
        volumeMounts:
        - mountPath: /host/proc
          name: proc-volume
          readOnly: false
        - mountPath: /host/sys
          name: sys-volume
          readOnly: false
        - mountPath: /host/root
          mountPropagation: HostToContainer
          name: root-volume
          readOnly: true
      - name: cloudmoa-topology-agent
        image: $DOCKER_REGISTRY_URL/topology-agent:$IMAGE_TAG
        resources:
          requests:
            cpu: 200m
            memory: 512Mi
          limits:
            cpu: 500m
            memory: 600Mi
        securityContext:
          privileged: true
        volumeMounts:
        - mountPath: /host/usr/bin
          name: bin-volume
        - mountPath: /var/run/docker.sock
          name: docker-volume
        - mountPath: /host/proc
          name: proc-volume
        - mountPath: /root
          name: root-volume
        - mountPath: /log
          name: log-volume
        env:
          - name: DATAGATE
            value: $COLLTION_SERVER_DATAGATE_IP:$COLLTION_SERVER_DATAGATE_PORT
          - name: CLUSTER_ID
            value: $CLOUDMOA_CLUSTER_ID
          - name: ROOT_DIRECTORY
            value: /root
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: spec.nodeName
          - name: POD_ID
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: LOG_LEVEL
            value: "INFO"
        livenessProbe:
          httpGet:
            path: /healthz
            port: 18081
          initialDelaySeconds: 30
          timeoutSeconds: 5
      volumes:
      - name: bin-volume
        hostPath:
          path: /usr/bin
          type: Directory
      - name: docker-volume
        hostPath:
          path: /var/run/docker.sock
      - name: proc-volume
        hostPath:
          path: /proc
      - name: root-volume
        hostPath:
          path: /
      - hostPath:
          path: /sys
        name: sys-volume
      - name: log-volume
        hostPath:
          path: /home'
	WHERE id=2;

-- CLOUD-18687 otel-trace agent 추가
INSERT INTO public.agent_install_file_info (id, name, type, description, version, yaml, use_yn, created_date,
                                            modified_date)
VALUES (8, 'otel-trace', 'agent',
        'CloudMOA에서는 고객사에서 운영 중인 application의 TPS, 서비스 연관관계 등의 데이터를 얻기 위해서 Opentelemetry를 사용하며, Opentelemetry 사용을 위해 애플리케이션의 instrumentation이 필요합니다.',
        null, '---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: otel-trace-agent
  namespace: $CLOUDMOA_NAMESPACE
  labels:
    app: otel-trace-agent
spec:
  selector:
    matchLabels:
      app: otel-trace-agent
  template:
    metadata:
      labels:
        app: otel-trace-agent
    spec:
      containers:
        - image: $DOCKER_REGISTRY_URL/otel-trace-agent:$IMAGE_TAG
          imagePullPolicy: Always
          name: otel-trace-agent
          resources:
            requests:
              cpu: 100m
              memory: 50Mi
            limits:
              cpu: 200m
              memory: 100Mi
          ports:
            - containerPort: 4317
              protocol: TCP
          env:
            - name: CMOA_LOG_LEVEL
              value: "INFO"
            - name: CMOA_CLUSTER_ID
              value: $CLOUDMOA_CLUSTER_ID
            - name: CMOA_DATAGATE
              value: $COLLTION_SERVER_DATAGATE_IP:$COLLTION_SERVER_DATAGATE_PORT
          volumeMounts:
            - name: config
              mountPath: /CloudMOA/default_config.yaml
              subPath: default_config.yaml
      volumes:
        - name: config
          configMap:
            name: otel-trace-agent-config
            items:
              - key: config.yaml
                path: default_config.yaml

---

apiVersion: v1
kind: Service
metadata:
  name: otel-trace-agent
  namespace: $CLOUDMOA_NAMESPACE
  labels:
    app: otel-trace-agent
spec:
  ports:
    - name: otel-port
      port: 4317
      protocol: TCP
      targetPort: 4317
  selector:
    app: otel-trace-agent
  type: ClusterIP

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: otel-trace-agent-config
  namespace: $CLOUDMOA_NAMESPACE
data:
  config.yaml: |
    receivers:
      otlp:
        protocols:
          grpc:
            endpoint: 0.0.0.0:4317
    processors:
      batch:
      probabilistic_sampler:
        hash_seed: 22
        sampling_percentage: 100
    exporters:
      # cmoaexporter 설정
      cmoaexporter:
        api:
          endpoint: "127.0.0.1:7070"
        traces:
          endpoint: "127.0.0.1:7070"
          tls:
            enable: false
            skip_host_verify: true
            ca_file: ""
            cert_file: ""
            key_file: ""
          queue:
            enabled: true
    service:
      pipelines:
        traces:
          receivers: [otlp]
          processors: [batch, probabilistic_sampler]
          exporters: [cmoaexporter]');

-- CLOUD-21306 | Redis Metric Meta 패치
-- Auto-generated SQL script #202310171617
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_keyspace_misses_total{{filter}}[5m]))'
	WHERE id='redis_keyspace_misses';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_keyspace_hits_total{{filter}}[5m]))'
	WHERE id='redis_keyspace_hits';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_connections_received_total{{filter}}[5m]))'
	WHERE id='redis_connections_received';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_cpu_sys_children_seconds_total{{filter}}[5m]))'
	WHERE id='redis_used_cpu_sys_children';
UPDATE public.metric_meta2
	SET expr='sum by (data_type, xm_clst_id, xm_namespace, xm_node_id, instance) (
label_replace(rate(redis_cpu_sys_seconds_total{{filter}}[5m]), "data_type", "system", "", "") or
label_replace(rate(redis_cpu_user_seconds_total{{filter}}[5m]), "data_type", "user", "", "") )'
	WHERE id='redis_cpu_usage';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_cpu_user_seconds_total{{filter}}[5m]))'
	WHERE id='redis_used_cpu_user';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (redis_connected_slaves{{filter}})'
	WHERE id='redis_connected_slaves';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance, cmd) (rate(redis_commands_total{{filter}}[5m]))'
	WHERE id='redis_commands_total';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (redis_memory_used_bytes{{filter}}) / 1048576'
	WHERE id='redis_memory_used_size';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (redis_allocator_resident_bytes{{filter}}) / 1048576'
	WHERE id='redis_allocator_resident_size';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_cpu_sys_seconds_total{{filter}}[5m]))'
	WHERE id='redis_used_cpu_sys';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_cpu_user_children_seconds_total{{filter}} [5m]))'
	WHERE id='redis_used_cpu_user_children';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (redis_up{{filter}})'
	WHERE id='redis_up';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (redis_allocator_allocated_bytes{{filter}}) / 1048576'
	WHERE id='redis_allocator_allocated_size';
UPDATE public.metric_meta2
	SET expr='sum by (data_type, xm_clst_id, xm_namespace, xm_node_id, instance) (
label_replace(rate(redis_connections_received_total{{filter}} [5m]), "data_type", "received connections", "", "") or
label_replace(rate(redis_rejected_connections_total{{filter}} [5m]), "data_type", "rejected connections", "", "") or
label_replace(redis_connected_clients{{filter}}, "data_type", "connected clients", "", "") or
label_replace(redis_blocked_clients{{filter}}, "data_type", "blocked clients", "", "") )'
	WHERE id='count_by_connection_type';
UPDATE public.metric_meta2
	SET expr='sum by (data_type, xm_clst_id, xm_namespace, xm_node_id, instance) (
label_replace(redis_allocator_active_bytes{{filter}} / 1048576, "data_type", "active", "" ,  "") or
label_replace(redis_memory_used_bytes{{filter}} / 1048576, "data_type", "used", "" ,  "") or
label_replace(redis_allocator_allocated_bytes{{filter}} / 1048576, "data_type", "allocated", "" ,  "") or
label_replace(redis_allocator_resident_bytes{{filter}} / 1048576, "data_type", "resident", "" ,  "") )'
	WHERE id='redis_memory_total';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance, cmd) (rate(redis_commands_duration_seconds_total{{filter}}[5m]) * 1000)'
	WHERE id='redis_commands_duration_seconds';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_rejected_connections_total{{filter}}[5m]))'
	WHERE id='redis_rejected_connections';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (redis_blocked_clients{{filter}})'
	WHERE id='redis_blocked_clients';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance, slave_ip, slave_port) (rate(redis_connected_slave_offset_bytes{{filter}}[5m]))'
	WHERE id='redis_connected_slave_offset_bytes';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance, db) (redis_db_keys_expiring{{filter}})'
	WHERE id='redis_db_keys_expiring';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_evicted_keys_total{{filter}}[5m]))'
	WHERE id='redis_evicted_keys';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (redis_connected_clients{{filter}})'
	WHERE id='redis_connected_clients';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance, slave_ip, slave_port) (rate(redis_connected_slave_lag_seconds{{filter}}[5m]))'
	WHERE id='redis_connected_slave_lag_seconds';
UPDATE public.metric_meta2
	SET expr='sum by (data_type, xm_clst_id, xm_namespace, xm_node_id, instance) (
label_replace(rate(redis_keyspace_hits_total{{filter}} [5m]), "data_type", "hits", "" ,  "") or
label_replace(rate(redis_keyspace_misses_total{{filter}} [5m]), "data_type", "misses", "" ,  "") )'
	WHERE id='redis_key_hit_raito';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_commands_processed_total{{filter}}[5m]))'
	WHERE id='redis_commands_processed';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (rate(redis_expired_keys_total{{filter}}[5m]))'
	WHERE id='redis_expired_keys';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance, db) (redis_db_keys{{filter}})'
	WHERE id='redis_db_keys';
UPDATE public.metric_meta2
	SET expr='sum by (xm_clst_id, xm_namespace, xm_node_id, instance) (redis_allocator_active_bytes{{filter}}) / 1048576'
	WHERE id='redis_allocator_active_size';

-- Reason OOMKilled
INSERT INTO public.metric_meta2 (id,meta_name,description,expr,resource_type,entity_type,groupby_keys,in_use,message)
VALUES ('count_pod_oomkilled','OOMKilled of Pods By Workload','OOMKilled of Pods By Workload','count by (xm_clst_id, xm_pod_id, xm_cont_name, xm_namespace) (imxc_pod_container_status_terminated_reason_oom{{filter}})','State','Workload','xm_clst_id, xm_namespace, xm_pod_id',TRUE,'CLST:{{$labels.xm_clst_id}} POD:{{$labels.xm_pod_id}} NS:{{$labels.xm_namespace}}.');
-- Last Reason OOMKilled
INSERT INTO public.metric_meta2 (id,meta_name,description,expr,resource_type,entity_type,groupby_keys,in_use,message)
VALUES ('count_pod_last_oomkilled','Last OOMKilled of Pods By Workload','Last OOMKilled of Pods By Workload','count by (xm_clst_id, xm_pod_id, xm_cont_name, xm_namespace) (imxc_pod_container_status_last_terminated_reason_oom{{filter}})','State','Workload','xm_clst_id, xm_namespace, xm_pod_id', TRUE,'CLST:{{$labels.xm_clst_id}} POD:{{$labels.xm_pod_id}} NS:{{$labels.xm_namespace}}.');

-- Configuration Menu 추가
INSERT INTO public.auth_resource2 (access_type, name, parent_id, type) VALUES (4, 'Configuration',           (select id from auth_resource2 where type='menu' and name='Workloads'), 'menu');
INSERT INTO public.auth_resource3 (name, is_deleted, tenant_id) VALUES ('menu|Workloads|Configuration', false, null);
INSERT INTO public.menu_meta (id, description, icon, "position", url, auth_resource3_id, scope_level) VALUES (16, 'Pods', NULL, 9, 'configurationWorkloads', (select id from auth_resource3 where name='menu|Workloads|Configuration'), 3);


-- log parsing 관련 metric meta rate연산 삭제
UPDATE metric_meta2 	SET expr = 'count by (xm_clst_id, xm_node_id, xm_namespace, xm_pod_id, xm_regex) (log_metric_counter_cmoa_metric{{filter}})' WHERE id = 'count_pod_log_parsing';

CREATE TABLE "intermax" (
	"cluster_id" VARCHAR(128) NOT NULL,
	"namespace" VARCHAR(128) NOT NULL,
	"url" VARCHAR(256) NULL DEFAULT NULL,
	"service_name" VARCHAR(128) NULL DEFAULT NULL,
	PRIMARY KEY ("cluster_id", "namespace")
);

-- Configuration Table 생성

CREATE TABLE "cmoa_storageclass_base" (
	"kube_flatting_time" BIGINT NOT NULL,
	"cluster_id" VARCHAR(255) NOT NULL,
	"kind" VARCHAR(30) NOT NULL,
	"metadata_uid" VARCHAR(40) NOT NULL,
	"row_index" INTEGER NOT NULL,
	"metadata_name" TEXT NULL DEFAULT NULL,
	"metadata_creationtimestamp" VARCHAR(25) NULL DEFAULT NULL,
	"metadata_resourceversion" TEXT NULL DEFAULT NULL,
	"provisioner" TEXT NULL DEFAULT NULL,
	"reclaimpolicy" TEXT NULL DEFAULT NULL,
	"volumebindingmode" TEXT NULL DEFAULT NULL,
	PRIMARY KEY ("kube_flatting_time", "cluster_id", "kind", "metadata_uid", "row_index")
);

CREATE TABLE "cmoa_ingress_base" (
	"kube_flatting_time" BIGINT NOT NULL,
	"cluster_id" VARCHAR(255) NOT NULL,
	"kind" VARCHAR(30) NOT NULL,
	"metadata_uid" VARCHAR(40) NOT NULL,
	"row_index" INTEGER NOT NULL,
	"metadata_name" TEXT NULL DEFAULT NULL,
	"metadata_creationtimestamp" VARCHAR(25) NULL DEFAULT NULL,
	"metadata_resourceversion" TEXT NULL DEFAULT NULL,
	"metadata_namespace" TEXT NULL DEFAULT NULL,
	"spec_rules" TEXT NULL DEFAULT NULL,
	PRIMARY KEY ("kube_flatting_time", "cluster_id", "kind", "metadata_uid", "row_index")
);

CREATE TABLE "cmoa_clusterrole_base" (
	"kube_flatting_time" BIGINT NOT NULL,
	"cluster_id" VARCHAR(255) NOT NULL,
	"kind" VARCHAR(30) NOT NULL,
	"metadata_uid" VARCHAR(40) NOT NULL,
	"row_index" INTEGER NOT NULL,
	"metadata_name" TEXT NULL DEFAULT NULL,
	"metadata_creationtimestamp" VARCHAR(25) NULL DEFAULT NULL,
	"metadata_resourceversion" TEXT NULL DEFAULT NULL,
	"metadata_labels" TEXT NULL DEFAULT NULL,
	"rules" TEXT NULL DEFAULT NULL,
	PRIMARY KEY ("kube_flatting_time", "cluster_id", "kind", "metadata_uid", "row_index")
);

CREATE TABLE "cmoa_clusterrolebinding_base" (
	"kube_flatting_time" BIGINT NOT NULL,
	"cluster_id" VARCHAR(255) NOT NULL,
	"kind" VARCHAR(30) NOT NULL,
	"metadata_uid" VARCHAR(40) NOT NULL,
	"row_index" INTEGER NOT NULL,
	"metadata_name" TEXT NULL DEFAULT NULL,
	"metadata_creationtimestamp" VARCHAR(25) NULL DEFAULT NULL,
	"metadata_resourceversion" TEXT NULL DEFAULT NULL,
	"subjects" TEXT NULL DEFAULT NULL,
	"roleref" TEXT NULL DEFAULT NULL,
	PRIMARY KEY ("kube_flatting_time", "cluster_id", "kind", "metadata_uid", "row_index")
);

CREATE TABLE "cmoa_role_base" (
	"kube_flatting_time" BIGINT NOT NULL,
	"cluster_id" VARCHAR(255) NOT NULL,
	"kind" VARCHAR(30) NOT NULL,
	"metadata_uid" VARCHAR(40) NOT NULL,
	"row_index" INTEGER NOT NULL,
	"metadata_name" TEXT NULL DEFAULT NULL,
	"metadata_creationtimestamp" VARCHAR(25) NULL DEFAULT NULL,
	"metadata_resourceversion" TEXT NULL DEFAULT NULL,
	"metadata_namespace" TEXT NULL DEFAULT NULL,
	"rules" TEXT NULL DEFAULT NULL,
	PRIMARY KEY ("kube_flatting_time", "cluster_id", "kind", "metadata_uid", "row_index")
);

CREATE TABLE "cmoa_rolebinding_base" (
	"kube_flatting_time" BIGINT NOT NULL,
	"cluster_id" VARCHAR(255) NOT NULL,
	"kind" VARCHAR(30) NOT NULL,
	"metadata_uid" VARCHAR(40) NOT NULL,
	"row_index" INTEGER NOT NULL,
	"metadata_name" TEXT NULL DEFAULT NULL,
	"metadata_creationtimestamp" VARCHAR(25) NULL DEFAULT NULL,
	"metadata_resourceversion" TEXT NULL DEFAULT NULL,
	"metadata_namespace" TEXT NULL DEFAULT NULL,
	"subjects" TEXT NULL DEFAULT NULL,
	"roleref" TEXT NULL DEFAULT NULL,
	PRIMARY KEY ("kube_flatting_time", "cluster_id", "kind", "metadata_uid", "row_index")
);

CREATE TABLE "cmoa_serviceaccount_base" (
	"kube_flatting_time" BIGINT NOT NULL,
	"cluster_id" VARCHAR(255) NOT NULL,
	"kind" VARCHAR(30) NOT NULL,
	"metadata_uid" VARCHAR(40) NOT NULL,
	"row_index" INTEGER NOT NULL,
	"metadata_name" TEXT NULL DEFAULT NULL,
	"metadata_creationtimestamp" VARCHAR(25) NULL DEFAULT NULL,
	"metadata_resourceversion" TEXT NULL DEFAULT NULL,
	"metadata_namespace" TEXT NULL DEFAULT NULL,
	"secrets" TEXT NULL DEFAULT NULL,
	PRIMARY KEY ("kube_flatting_time", "cluster_id", "kind", "metadata_uid", "row_index")
);
commit;
