---
# KKCluster object referenced by the Cluster object
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: KKCluster
metadata:
  name: '${CLUSTER_NAME}'
spec:
  distribution: k3s
  component:
    zone: '${KKZONE}'
  nodes:
    auth:
      user: '${USER_NAME}'
      password: '${PASSWORD}'
    instances: '${INSTANCES}'
  controlPlaneLoadBalancer:
    host: '${CONTROL_PLANE_ENDPOINT_IP}'
---
# Cluster object with
# - Reference to the K3sControlPlane object
# - the label cni=${CLUSTER_NAME}-crs-0, so the cluster can be selected by the ClusterResourceSet.
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
  name: '${CLUSTER_NAME}'
  labels:
    cni: "${CLUSTER_NAME}-crs-0"
spec:
  clusterNetwork:
    services:
      cidrBlocks: ['${SERVICE_CIDRS}']
    pods:
      cidrBlocks: ['${POD_CIDRS}']
    serviceDomain: '${SERVICE_DOMAIN}'
  infrastructureRef:
    apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    kind: KKCluster
    name: '${CLUSTER_NAME}'
  controlPlaneRef:
    kind: K3sControlPlane
    apiVersion: controlplane.cluster.x-k8s.io/v1beta1
    name: "${CLUSTER_NAME}-control-plane"
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: KKMachineTemplate
metadata:
  name: "${CLUSTER_NAME}-control-plane"
spec:
  template:
    spec:
      roles:
        - control-plane
---
kind: K3sControlPlane
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
metadata:
  name: "${CLUSTER_NAME}-control-plane"
spec:
  replicas: ${CONTROL_PLANE_MACHINE_COUNT}
  machineTemplate:
    infrastructureRef:
      kind: KKMachineTemplate
      apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
      name: "${CLUSTER_NAME}-control-plane"
  k3sConfigSpec:
    serverConfiguration:
      listener:
        tlsSan: ${CONTROL_PLANE_ENDPOINT_IP}
      networking:
        flannelBackend: none
      kubernetesComponents:
        disable: "servicelb,traefik,metrics-server,local-storage"
        disableNetworkPolicy: true
    files:
      - content: |
          apiVersion: v1
          kind: ServiceAccount
          metadata:
            name: kube-vip
            namespace: kube-system
          ---
          apiVersion: rbac.authorization.k8s.io/v1
          kind: ClusterRole
          metadata:
            annotations:
              rbac.authorization.kubernetes.io/autoupdate: "true"
            name: system:kube-vip-role
          rules:
            - apiGroups: [""]
              resources: ["services", "services/status", "nodes", "endpoints"]
              verbs: ["list","get","watch", "update"]
            - apiGroups: ["coordination.k8s.io"]
              resources: ["leases"]
              verbs: ["list", "get", "watch", "update", "create"]
          ---
          kind: ClusterRoleBinding
          apiVersion: rbac.authorization.k8s.io/v1
          metadata:
            name: system:kube-vip-binding
          roleRef:
            apiGroup: rbac.authorization.k8s.io
            kind: ClusterRole
            name: system:kube-vip-role
          subjects:
          - kind: ServiceAccount
            name: kube-vip
            namespace: kube-system
          ---
          apiVersion: apps/v1
          kind: DaemonSet
          metadata:
            creationTimestamp: null
            labels:
              app.kubernetes.io/name: kube-vip-ds
              app.kubernetes.io/version: v0.5.0
            name: kube-vip-ds
            namespace: kube-system
          spec:
            selector:
              matchLabels:
                app.kubernetes.io/name: kube-vip-ds
            template:
              metadata:
                creationTimestamp: null
                labels:
                  app.kubernetes.io/name: kube-vip-ds
                  app.kubernetes.io/version: v0.5.0
              spec:
                affinity:
                  nodeAffinity:
                    requiredDuringSchedulingIgnoredDuringExecution:
                      nodeSelectorTerms:
                      - matchExpressions:
                        - key: node-role.kubernetes.io/master
                          operator: Exists
                      - matchExpressions:
                        - key: node-role.kubernetes.io/control-plane
                          operator: Exists
                containers:
                - args:
                  - manager
                  env:
                  - name: address
                    value: ${CONTROL_PLANE_ENDPOINT_IP}
                  - name: vip_interface
                    value: ${VIP_NETWORK_INTERFACE=""}
                  - name: vip_arp
                    value: "true"
                  - name: port
                    value: "6443"
                  - name: vip_cidr
                    value: "32"
                  - name: cp_enable
                    value: "true"
                  - name: cp_namespace
                    value: kube-system
                  - name: vip_ddns
                    value: "false"
                  - name: svc_enable
                    value: "true"
                  - name: vip_leaderelection
                    value: "true"
                  - name: vip_leaseduration
                    value: "5"
                  - name: vip_renewdeadline
                    value: "3"
                  - name: vip_retryperiod
                    value: "1"
                  - name: lb_enable
                    value: "true"
                  - name: lb_port
                    value: "6443"
                  - name: lb_fwdmethod
                    value: local
                  - name: prometheus_server
                    value: :2112
                  image: ghcr.io/kube-vip/kube-vip:v0.5.0
                  imagePullPolicy: IfNotPresent
                  name: kube-vip
                  resources: {}
                  securityContext:
                    capabilities:
                      add:
                      - NET_ADMIN
                      - NET_RAW
                hostNetwork: true
                serviceAccountName: kube-vip
                tolerations:
                - effect: NoSchedule
                  operator: Exists
                - effect: NoExecute
                  operator: Exists
            updateStrategy: {}
        owner: root:root
        path: /var/lib/rancher/k3s/server/manifests/kube-vip.yaml
  version: "${KUBERNETES_VERSION}"
