class qdii::k8s {
  # Install and configure containerd.
  if ($::operatingsystem == 'Debian') or ($::operatingsystem == 'Ubuntu') {
    $containerd_pkg = 'containerd.io'
    $nfs_pkg        = 'nfs-common'
    kmod::load { 'br_netfilter': }
    kmod::load { 'vxlan': }
    $swapoff = '/usr/sbin/swapoff'
  } else {
    $containerd_pkg = 'containerd'
    $nfs_pkg        = 'nfs-utils'
    $swapoff = '/usr/bin/swapoff'
  }
  package { [
    "$containerd_pkg",
  ]:
    ensure => installed
  }
  file { 'containerd-config-dir':
    path   => '/etc/containerd',
    ensure => directory,
  } ->
  file { 'containerd-config':
    path   => '/etc/containerd/config.toml',
    source => 'puppet:///modules/qdii/misc/containerd-config.toml',
  }

  # Install the required packages for Kubernetes.
  package { [
    'kubeadm',
    'kubelet',
    "$nfs_pkg",  # To create NFS PersistentVolumeClaims.
  ]:
    ensure => installed
  }

  # Used instead of iptables for load-balancing.
  kmod::load { 'ip_vs': }
  kmod::load { 'ip_vs_rr': }
  kmod::load { 'ip_vs_wrr': }
  kmod::load { 'ip_vs_sh': }

  # Kubelet hates swap.
  exec { 'swap_off':
    command => "$swapoff -a",
    user    => 'root',
  }

  # Required by kubeadm precondition.
  sysctl { 'net.ipv6.conf.default.forwarding':
    ensure     => 'present',
    value      => 1,
    defined_in => ['/etc/sysctl.d/80-puppet-ipv6-forwarding.conf'],
  }
  sysctl { 'net.ipv4.ip_forward':
    ensure     => 'present',
    value      => 1,
    defined_in => ['/etc/sysctl.d/80-puppet-ipv4-forward.conf'],
  }
}
