diff --git a/manifests/repos.pp b/manifests/repos.pp index b6fbc63f..64218091 100644 --- a/manifests/repos.pp +++ b/manifests/repos.pp @@ -3,6 +3,8 @@ # @param container_runtime # This is the runtime that the Kubernetes cluster will use. # It can only be set to "cri_containerd" or "docker". Defaults to cri_containerd +# @param kubernetes_version +# The kubernetes version used to determine major release version. # @param kubernetes_apt_location # The APT repo URL for the Kubernetes packages. Defaults to https://apt.kubernetes.io # @param kubernetes_apt_release @@ -40,6 +42,7 @@ # class kubernetes::repos ( String $container_runtime = $kubernetes::container_runtime, + Optional[String] $kubernetes_version = $kubernetes::kubernetes_version, Optional[String] $kubernetes_apt_location = $kubernetes::kubernetes_apt_location, Optional[String] $kubernetes_apt_release = $kubernetes::kubernetes_apt_release, Optional[String] $kubernetes_apt_repos = $kubernetes::kubernetes_apt_repos, @@ -60,19 +63,47 @@ ) { if $create_repos { + $parts = split($kubernetes_version, '[.]') + $minor_version = "${parts[0]}.${parts[1]}" + $_repos = $kubernetes_apt_repos ? { + undef => '', + default => $kubernetes_apt_repos + } case $facts['os']['family'] { 'Debian': { - $codename = fact('os.distro.codename') apt::source { 'kubernetes': - location => pick($kubernetes_apt_location,'https://apt.kubernetes.io'), - repos => pick($kubernetes_apt_repos,'main'), - release => pick($kubernetes_apt_release,'kubernetes-xenial'), - key => { - 'id' => pick($kubernetes_key_id,'A362B822F6DEDC652817EA46B53DC80D13EDEF05'), - 'source' => pick($kubernetes_key_source,'https://packages.cloud.google.com/apt/doc/apt-key.gpg'), - }, + location => pick($kubernetes_apt_location,"https://pkgs.k8s.io/core:/stable:/v${minor_version}/deb"), + release => pick($kubernetes_apt_release, '/'), + repos => $_repos, } + if $kubernetes_apt_location =~ String[1] { + Apt::Source<| title == 'kubernetes' |> { + key => { + 'id' => $kubernetes_key_id, + 'source' => $kubernetes_key_source, + } + } + } else { + # For pkgs.k8s.io use GPG siging key + $_keyring = '/usr/share/keyrings/kubernetes-apt-keyring.gpg' + # TODO: Switch to apt::keyring once supported by puppetlabs-apt + # see: https://github.com/puppetlabs/puppetlabs-apt/pull/1128 + archive { '/tmp/kubernetes-apt-keyring.gpg': + source => "https://pkgs.k8s.io/core:/stable:/v${minor_version}/deb/Release.key", + extract => true, + extract_path => '/usr/share/keyrings', + extract_command => 'gpg --dearmor < %s > kubernetes-apt-keyring.gpg', + creates => $_keyring, + } + + Apt::Source<| title == 'kubernetes' |> { + keyring => $_keyring, + require => Archive['/tmp/kubernetes-apt-keyring.gpg'], + } + } + + $codename = fact('os.distro.codename') if ($container_runtime == 'docker' and $manage_docker == true) or ($container_runtime == 'cri_containerd' and $containerd_install_method == 'package') { apt::source { 'docker': @@ -99,9 +130,10 @@ yumrepo { 'kubernetes': descr => 'Kubernetes', - baseurl => pick($kubernetes_yum_baseurl,'https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64'), - gpgkey => pick($kubernetes_yum_gpgkey,'https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg'), - gpgcheck => true, + baseurl => pick($kubernetes_yum_baseurl,"https://pkgs.k8s.io/core:/stable:/v${minor_version}/rpm/"), + gpgkey => pick($kubernetes_yum_gpgkey,"https://pkgs.k8s.io/core:/stable:/v${minor_version}/rpm/repodata/repomd.xml.key"), + enabled => 1, + gpgcheck => 1, } } diff --git a/spec/acceptance/kubernetes_spec.rb b/spec/acceptance/kubernetes_spec.rb index ad3ac867..71486ea4 100644 --- a/spec/acceptance/kubernetes_spec.rb +++ b/spec/acceptance/kubernetes_spec.rb @@ -16,8 +16,8 @@ case $facts['os']['family'] { 'RedHat', 'CentOS': { class {'kubernetes': - kubernetes_version => '1.22.0', - kubernetes_package_version => '1.22.0', + kubernetes_version => '1.28.2', + kubernetes_package_version => '1.28.2-1.1', controller_address => "#{int_ipaddr1}:6443", container_runtime => 'docker', manage_docker => false, diff --git a/spec/classes/repos_spec.rb b/spec/classes/repos_spec.rb index 027cc9e0..724578e7 100644 --- a/spec/classes/repos_spec.rb +++ b/spec/classes/repos_spec.rb @@ -2,6 +2,72 @@ require 'spec_helper' describe 'kubernetes::repos', type: :class do + context 'with Debian and default params' do + let(:facts) do + { + osfamily: 'Debian', # needed to run dependent tests from fixtures puppetlabs-apt + kernel: 'Linux', + os: { + family: 'Debian', + name: 'Ubuntu', + release: { + full: '22.04' + }, + distro: { + codename: 'jammy' + } + } + } + end + let(:params) do + { + 'container_runtime' => 'docker', + 'kubernetes_version' => '1.28.1', + 'kubernetes_apt_location' => '', + 'kubernetes_apt_release' => '', + 'kubernetes_apt_repos' => '', + 'kubernetes_key_id' => '', + 'kubernetes_key_source' => '', + 'kubernetes_yum_baseurl' => 'https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64', + 'kubernetes_yum_gpgkey' => 'https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg', + 'docker_apt_location' => 'https://download.docker.com/linux/ubuntu', + 'docker_apt_release' => 'jammy', + 'docker_apt_repos' => 'main', + 'docker_yum_baseurl' => 'https://download.docker.com/linux/centos/7/x86_64/stable', + 'docker_yum_gpgkey' => 'https://download.docker.com/linux/centos/gpg', + 'docker_key_id' => '9DC858229FC7DD38854AE2D88D81803C0EBFCD88', + 'docker_key_source' => 'https://download.docker.com/linux/ubuntu/gpg', + 'containerd_install_method' => 'archive', + 'create_repos' => true, + 'manage_docker' => true + } + end + + it { + expect(subject).to contain_apt__source('kubernetes').with( + ensure: 'present', + location: 'https://pkgs.k8s.io/core:/stable:/v1.28/deb', + release: '/', + keyring: '/usr/share/keyrings/kubernetes-apt-keyring.gpg', + ) + } + + it { + expect(subject).to contain_file('/etc/apt/sources.list.d/kubernetes.list') + .with_content(%r{^deb \[signed-by=/usr/share/keyrings/kubernetes-apt-keyring.gpg\] https://pkgs.k8s.io/core:/stable:/v1.28/deb /\s$}) + } + + it { + expect(subject).to contain_apt__source('docker').with( + ensure: 'present', + location: 'https://download.docker.com/linux/ubuntu', + repos: 'main', + release: 'jammy', + key: { 'id' => '9DC858229FC7DD38854AE2D88D81803C0EBFCD88', 'source' => 'https://download.docker.com/linux/ubuntu/gpg' }, + ) + } + end + context 'with osfamily => Ubuntu and manage_docker => true' do let(:facts) do { @@ -11,10 +77,10 @@ family: 'Debian', name: 'Ubuntu', release: { - full: '16.04' + full: '22.04' }, distro: { - codename: 'xenial' + codename: 'jammy' } } } @@ -22,15 +88,16 @@ let(:params) do { 'container_runtime' => 'docker', - 'kubernetes_apt_location' => 'http://apt.kubernetes.io', - 'kubernetes_apt_release' => 'kubernetes-xenial', + 'kubernetes_version' => '1.29.2', + 'kubernetes_apt_location' => 'http://myapt.example.org', + 'kubernetes_apt_release' => 'jammy', 'kubernetes_apt_repos' => 'main', 'kubernetes_key_id' => '54A647F9048D5688D7DA2ABE6A030B21BA07F4FB', 'kubernetes_key_source' => 'https://packages.cloud.google.com/apt/doc/apt-key.gpg', 'kubernetes_yum_baseurl' => 'https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64', 'kubernetes_yum_gpgkey' => 'https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg', 'docker_apt_location' => 'https://download.docker.com/linux/ubuntu', - 'docker_apt_release' => 'xenial', + 'docker_apt_release' => 'jammy', 'docker_apt_repos' => 'main', 'docker_yum_baseurl' => 'https://download.docker.com/linux/centos/7/x86_64/stable', 'docker_yum_gpgkey' => 'https://download.docker.com/linux/centos/gpg', @@ -45,9 +112,9 @@ it { expect(subject).to contain_apt__source('kubernetes').with( ensure: 'present', - location: 'http://apt.kubernetes.io', + location: 'http://myapt.example.org', repos: 'main', - release: 'kubernetes-xenial', + release: 'jammy', key: { 'id' => '54A647F9048D5688D7DA2ABE6A030B21BA07F4FB', 'source' => 'https://packages.cloud.google.com/apt/doc/apt-key.gpg' }, ) } @@ -57,7 +124,7 @@ ensure: 'present', location: 'https://download.docker.com/linux/ubuntu', repos: 'main', - release: 'xenial', + release: 'jammy', key: { 'id' => '9DC858229FC7DD38854AE2D88D81803C0EBFCD88', 'source' => 'https://download.docker.com/linux/ubuntu/gpg' }, ) } @@ -72,10 +139,10 @@ family: 'Debian', name: 'Ubuntu', release: { - full: '16.04' + full: '22.04' }, distro: { - codename: 'xenial' + codename: 'jammy' } } } @@ -83,6 +150,7 @@ let(:params) do { 'container_runtime' => 'cri_containerd', + 'kubernetes_version' => '1.28.1', 'kubernetes_apt_location' => 'http://apt.kubernetes.io', 'kubernetes_apt_release' => 'kubernetes-xenial', 'kubernetes_apt_repos' => 'main', @@ -124,6 +192,58 @@ } end + context 'with RedHat and default params' do + let(:facts) do + { + operatingsystem: 'RedHat', + osfamily: 'RedHat', + operatingsystemrelease: '7.0', + kernel: 'Linux', + os: { + family: 'RedHat', + name: 'RedHat', + release: { + full: '7.0' + } + } + } + end + + let(:params) do + { + 'container_runtime' => 'docker', + 'kubernetes_version' => '1.28.1', + 'kubernetes_apt_location' => '', + 'kubernetes_apt_release' => '', + 'kubernetes_apt_repos' => '', + 'kubernetes_key_id' => '', + 'kubernetes_key_source' => '', + 'kubernetes_yum_baseurl' => '', + 'kubernetes_yum_gpgkey' => '', + 'docker_apt_location' => 'https://download.docker.com/linux/ubuntu', + 'docker_apt_release' => 'xenial', + 'docker_apt_repos' => 'main', + 'docker_yum_baseurl' => 'https://download.docker.com/linux/centos/7/x86_64/stable', + 'docker_yum_gpgkey' => 'https://download.docker.com/linux/centos/gpg', + 'docker_key_id' => '9DC858229FC7DD38854AE2D88D81803C0EBFCD88', + 'docker_key_source' => 'https://download.docker.com/linux/ubuntu/gpg', + 'containerd_install_method' => 'archive', + 'create_repos' => true, + 'manage_docker' => false + } + end + + it { is_expected.not_to contain_yumrepo('docker') } + + it { + expect(subject).to contain_yumrepo('kubernetes').with( + 'enabled' => '1', + 'baseurl' => 'https://pkgs.k8s.io/core:/stable:/v1.28/rpm/', + 'gpgkey' => 'https://pkgs.k8s.io/core:/stable:/v1.28/rpm/repodata/repomd.xml.key', + ) + } + end + context 'with osfamily => RedHat and manage_epel => true and manage_docker => false' do let(:facts) do { @@ -144,6 +264,7 @@ let(:params) do { 'container_runtime' => 'docker', + 'kubernetes_version' => '1.28.1', 'kubernetes_apt_location' => 'http://apt.kubernetes.io', 'kubernetes_apt_release' => 'kubernetes-xenial', 'kubernetes_apt_repos' => 'main', @@ -165,7 +286,14 @@ end it { is_expected.not_to contain_yumrepo('docker') } - it { is_expected.to contain_yumrepo('kubernetes') } + + it { + expect(subject).to contain_yumrepo('kubernetes').with( + 'enabled' => '1', + 'baseurl' => 'https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64', + 'gpgkey' => 'https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg', + ) + } end context 'with osfamily => RedHat and container_runtime => cri_containerd and containerd_install_method => package' do @@ -187,6 +315,7 @@ let(:params) do { + 'kubernetes_version' => '1.28.1', 'container_runtime' => 'cri_containerd', 'kubernetes_apt_location' => 'http://apt.kubernetes.io', 'kubernetes_apt_release' => 'kubernetes-xenial', @@ -196,7 +325,7 @@ 'kubernetes_yum_baseurl' => 'https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64', 'kubernetes_yum_gpgkey' => 'https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg', 'docker_apt_location' => 'https://download.docker.com/linux/ubuntu', - 'docker_apt_release' => 'xenial', + 'docker_apt_release' => 'jammy', 'docker_apt_repos' => 'main', 'docker_yum_baseurl' => 'https://download.docker.com/linux/centos/7/x86_64/stable', 'docker_yum_gpgkey' => 'https://download.docker.com/linux/centos/gpg', @@ -209,6 +338,13 @@ end it { is_expected.to contain_yumrepo('docker') } - it { is_expected.to contain_yumrepo('kubernetes') } + + it { + expect(subject).to contain_yumrepo('kubernetes').with( + 'enabled' => '1', + 'baseurl' => 'https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64', + 'gpgkey' => 'https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg', + ) + } end end