# Control plane provider
{{- if .Values.controlPlane }}
{{- $controlPlanes := split ";" .Values.controlPlane }}
{{- $controlPlaneNamespace := "" }}
{{- $controlPlaneName := "" }}
{{- $controlPlaneVersion := "" }}
{{- range $controlPlane := $controlPlanes }}
{{- $controlPlaneArgs := split ":" $controlPlane }}
{{- $controlPlaneArgsLen :=  len $controlPlaneArgs }}
{{-  if eq $controlPlaneArgsLen 3 }}
  {{- $controlPlaneNamespace = $controlPlaneArgs._0 }}
  {{- $controlPlaneName = $controlPlaneArgs._1 }}
  {{- $controlPlaneVersion = $controlPlaneArgs._2 }}
{{-  else if eq $controlPlaneArgsLen 2 }}
  {{- $controlPlaneNamespace = print $controlPlaneArgs._0 "-control-plane-system" }}
  {{- $controlPlaneName = $controlPlaneArgs._0 }}
  {{- $controlPlaneVersion = $controlPlaneArgs._1 }}
{{-  else if eq $controlPlaneArgsLen 1 }}
  {{- $controlPlaneNamespace = print $controlPlaneArgs._0 "-control-plane-system" }}
  {{- $controlPlaneName = $controlPlaneArgs._0 }}
{{- else }}
  {{- fail "controlplane provider argument should have the following format kubeadm:v1.0.0 or mynamespace:kubeadm:v1.0.0" }}
{{-  end }}
---
apiVersion: v1
kind: Namespace
metadata:
  annotations:
    "helm.sh/hook": "post-install,post-upgrade"
    "helm.sh/hook-weight": "1"
  name: {{ $controlPlaneNamespace }}
---
apiVersion: operator.cluster.x-k8s.io/v1alpha2
kind: ControlPlaneProvider
metadata:
  name: {{ $controlPlaneName }}
  namespace: {{ $controlPlaneNamespace }}
  annotations:
    "helm.sh/hook": "post-install,post-upgrade"
    "helm.sh/hook-weight": "2"
{{- if or $controlPlaneVersion $.Values.configSecret.name $.Values.manager }}
spec:
{{- end}}
{{- if $controlPlaneVersion }}
  version: {{ $controlPlaneVersion }}
{{- end }}
{{- if $.Values.manager }}
{{- if hasKey $.Values.manager.featureGates $controlPlaneName }}
  manager:
{{- range $key, $value := $.Values.manager.featureGates }}
  {{- if eq $key $controlPlaneName }}
    featureGates:
    {{- range $k, $v := $value }}
      {{ $k }}: {{ $v }}
    {{- end }}
  {{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- if $.Values.configSecret.name }}
  configSecret:
    name: {{ $.Values.configSecret.name }}
    {{- if $.Values.configSecret.namespace }}
    namespace: {{ $.Values.configSecret.namespace }}
    {{- end }}
{{- end }}
{{- end }}
{{- end }}
