{{- if index .Values "cluster-api-operator" "cluster-api" "enabled" }}
{{- $namespace := index .Values "cluster-api-operator" "cluster-api" "core" "namespace" }}
{{- if not (lookup "v1" "Namespace" "" $namespace) }}
---
apiVersion: v1
kind: Namespace
metadata:
  annotations:
    "helm.sh/hook": "post-install, post-upgrade"
    "helm.sh/hook-weight": "1"
  name: {{ index .Values "cluster-api-operator" "cluster-api" "core" "namespace" }}
{{- end }}
---
apiVersion: turtles-capi.cattle.io/v1alpha1
kind: CAPIProvider
metadata:
  name: cluster-api
  namespace: {{ index .Values "cluster-api-operator" "cluster-api" "core" "namespace" }}
  annotations:
    "helm.sh/hook": "post-install, post-upgrade"
    "helm.sh/hook-weight": "2"
spec:
  name: cluster-api
  type: core
  version: {{ index .Values "cluster-api-operator" "cluster-api" "version" }}
  additionalManifests:
    name: capi-additional-rbac-roles
    namespace: {{ index .Values "cluster-api-operator" "cluster-api" "core" "namespace" }}
  configSecret:
{{- if index .Values "cluster-api-operator" "cluster-api" "configSecret" "name" }}
    name: {{ index .Values "cluster-api-operator" "cluster-api" "configSecret" "name" }}
{{ else }}
    name: {{ index .Values "cluster-api-operator" "cluster-api" "configSecret" "defaultName" }}
{{- end }}
{{- if or (index .Values "cluster-api-operator" "cluster-api" "core" "fetchConfig" "url") (index .Values "cluster-api-operator" "cluster-api" "core" "fetchConfig" "selector") }}
  fetchConfig:
    {{- if index .Values "cluster-api-operator" "cluster-api" "core" "fetchConfig" "url" }}
    url: {{ index .Values "cluster-api-operator" "cluster-api" "core" "fetchConfig" "url" }}
    {{- end }}
    {{- if index .Values "cluster-api-operator" "cluster-api" "core" "fetchConfig" "selector" }}
    selector: {{ index .Values "cluster-api-operator" "cluster-api" "core" "fetchConfig" "selector" }}
    {{- end }}
{{- end }}
{{- if index .Values "cluster-api-operator" "cluster-api" "core" "imageUrl" }}
  deployment:
    containers:
      - name: manager
        imageUrl: {{ index .Values "cluster-api-operator" "cluster-api" "core" "imageUrl" }}
{{- end }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: capi-additional-rbac-roles
  namespace: {{ index .Values "cluster-api-operator" "cluster-api" "core" "namespace" }}
  annotations:
    "helm.sh/hook": "post-install, post-upgrade"
    "helm.sh/hook-weight": "2"
data: 
  manifests: |-
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: provisioning-rke-cattle-io
      labels:
        cluster.x-k8s.io/aggregate-to-manager: "true"
    rules:
      - apiGroups: ["rke.cattle.io"]
        resources: ["*"]
        verbs: ["*"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: provisioning-rke-machine-cattle-io
      labels:
        cluster.x-k8s.io/aggregate-to-manager: "true"
    rules:
      - apiGroups: ["rke-machine.cattle.io"]
        resources: ["*"]
        verbs: ["*"]
{{- end }}