{{/*
Produces the kubeadm config spec for a controlplane object, with support for the skipPhases
option of InitConfiguration and specifying a KubeProxyConfiguration.
*/}}
{{- define "openstack-cluster.controlplane.kubeadmConfigSpec" -}}
{{- $ctx := index . 0 }}
{{- $kubeadmConfigSpec := omit
  (index . 1)
  "initConfiguration"
  "kubeProxyConfiguration"
  "files"
  "preKubeadmCommands"
}}
{{- $initConfiguration := omit (index . 1 | dig "initConfiguration" dict) "skipPhases" }}
{{- $skipPhases := index . 1 | dig "initConfiguration" "skipPhases" list }}
{{- $kubeProxyConfiguration := index . 1 | dig "kubeProxyConfiguration" dict }}
{{- $files := index . 1 | dig "files" list }}
{{- $preKubeadmCommands := index . 1 | dig "preKubeadmCommands" list }}

{{- if and $skipPhases (semverCompare "<1.22.0" $ctx.Values.global.kubernetesVersion) }}
{{- fail "skipPhases is only supported for Kubernetes 1.22 and higher" }}
{{- end }}

{{- with $kubeadmConfigSpec }}
{{- toYaml . }}
{{- end }}
{{- with $initConfiguration }}
initConfiguration: {{ toYaml $initConfiguration | nindent 2 }}
{{- end }}
{{- if or $files $skipPhases $kubeProxyConfiguration }}
files:
  {{- range $files }}
  - {{ toYaml . | nindent 4 }}
  {{- end }}
  {{- if $skipPhases }}
  - path: /run/kubeadm/skip-phases.yaml
    content: |
      skipPhases: {{ toYaml $skipPhases | nindent 8 }}
    owner: root:root
    permissions: "0644"
  {{- end }}
  {{- with $kubeProxyConfiguration }}
  - path: /run/kubeadm/kube-proxy-configuration.yaml
    content: |
      ---
      apiVersion: kubeproxy.config.k8s.io/v1alpha1
      kind: KubeProxyConfiguration
      {{- toYaml . | nindent 6 }}
    owner: root:root
    permissions: "0644"
  {{- end }}
{{- end }}
{{- if or $preKubeadmCommands $skipPhases $kubeProxyConfiguration }}
preKubeadmCommands:
  {{- range $preKubeadmCommands }}
  - {{ . }}
  {{- end }}
  {{- if $skipPhases }}
  - cat /run/kubeadm/skip-phases.yaml >> /run/kubeadm/kubeadm.yaml
  {{- end }}
  {{- if $kubeProxyConfiguration }}
  - cat /run/kubeadm/kube-proxy-configuration.yaml >> /run/kubeadm/kubeadm.yaml
  {{- end }}
{{- end }}
{{- end }}

---
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
kind: KubeadmControlPlane
metadata:
  name: {{ include "openstack-cluster.componentName" (list . "control-plane") }}
  labels: {{ include "openstack-cluster.componentLabels" (list . "control-plane") | nindent 4 }}
  annotations:
    # We let Cluster API clean up the cluster resources
    # Deleting them ourselves, which CAPI is not expecting, can cause some nasty race conditions
    helm.sh/resource-policy: keep
spec:
  version: {{ .Values.global.kubernetesVersion | required ".Values.global.kubernetesVersion is required" }}
  replicas: {{ .Values.controlPlane.machineCount }}
  rolloutStrategy: {{ toYaml .Values.controlPlane.rolloutStrategy | nindent 4 }}
  machineTemplate:
    metadata:
      labels: {{ include "openstack-cluster.componentSelectorLabels" (list . "control-plane") | nindent 8 }}
    infrastructureRef:
      kind: OpenStackMachineTemplate
      apiVersion: infrastructure.cluster.x-k8s.io/v1alpha6
      name: {{ include "openstack-cluster.controlplane.mt.name" . }}
    {{- with .Values.controlPlane.nodeDrainTimeout }}
    nodeDrainTimeout: {{ . }}
    {{- end }}
  kubeadmConfigSpec: {{
    include
      "openstack-cluster.controlplane.kubeadmConfigSpec"
      (list . .Values.controlPlane.kubeadmConfigSpec) |
    fromYaml |
    list . |
    include "openstack-cluster.kubeadmConfigSpec" |
    nindent 4
  }}