Skip to content

Commit 5a364e5

Browse files
committed
Add manifest for installing FluxCD
1 parent 229a90a commit 5a364e5

File tree

5 files changed

+301
-0
lines changed

5 files changed

+301
-0
lines changed

.fixtures.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ fixtures:
44
archive: https://github.com/voxpupuli/puppet-archive
55
augeasproviders_core: https://github.com/voxpupuli/puppet-augeasproviders_core
66
augeasproviders_sysctl: https://github.com/voxpupuli/puppet-augeasproviders_sysctl
7+
extlib: https://github.com/voxpupuli/puppet-extlib
78
kmod: https://github.com/voxpupuli/puppet-kmod
89
stdlib: https://github.com/puppetlabs/puppetlabs-stdlib
910
systemd: https://github.com/voxpupuli/puppet-systemd

REFERENCE.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [`k8s::install::cni_plugins`](#k8s--install--cni_plugins): Manages the installation of CNI plugins
1313
* [`k8s::install::container_runtime`](#k8s--install--container_runtime): Manages the installation of a container runtime / CRI
1414
* [`k8s::install::crictl`](#k8s--install--crictl): installs the crictl debugging tool
15+
* [`k8s::install::fluxcd`](#k8s--install--fluxcd): Installs the FluxCD CLI, and optionally also installs Flux into on the cluster
1516
* [`k8s::install::kubeadm`](#k8s--install--kubeadm): Installs the kubeadm binary
1617
* [`k8s::install::kubectl`](#k8s--install--kubectl): Installs the kubectl binary
1718
* [`k8s::node`](#k8s--node): Installs a Kubernetes node
@@ -690,6 +691,90 @@ template string for the URL to download tar.gz from
690691

691692
Default value: `'https://github.com/kubernetes-sigs/cri-tools/releases/download/%{version}/crictl-%{version}-linux-%{arch}.tar.gz'`
692693

694+
### <a name="k8s--install--fluxcd"></a>`k8s::install::fluxcd`
695+
696+
Installs the FluxCD CLI, and optionally also installs Flux into on the cluster
697+
698+
#### Examples
699+
700+
##### Install - and automatically update - latest version of Flux
701+
702+
```puppet
703+
class { 'k8s::install::fluxcd':
704+
ensure => latest,
705+
}
706+
```
707+
708+
##### Install flux with different components
709+
710+
```puppet
711+
class { 'k8s::install::fluxcd':
712+
install_options => {
713+
components => ['source-controller', 'kustomize-controller']
714+
components_extra => ['source-watcher', 'image-reflector-controller']
715+
}
716+
}
717+
```
718+
719+
#### Parameters
720+
721+
The following parameters are available in the `k8s::install::fluxcd` class:
722+
723+
* [`ensure`](#-k8s--install--fluxcd--ensure)
724+
* [`install`](#-k8s--install--fluxcd--install)
725+
* [`install_options`](#-k8s--install--fluxcd--install_options)
726+
* [`upgrade`](#-k8s--install--fluxcd--upgrade)
727+
* [`install_dir`](#-k8s--install--fluxcd--install_dir)
728+
* [`kubeconfig`](#-k8s--install--fluxcd--kubeconfig)
729+
730+
##### <a name="-k8s--install--fluxcd--ensure"></a>`ensure`
731+
732+
Data type: `Variant[Enum['absent', 'present', 'latest'], String[1]]`
733+
734+
The FluxCD version to install, or present/latest for the latest at the time
735+
736+
Default value: `'present'`
737+
738+
##### <a name="-k8s--install--fluxcd--install"></a>`install`
739+
740+
Data type: `Optional[Boolean]`
741+
742+
If FluxCD should be installed into the local cluster, will default to true on k8s::server nodes
743+
744+
Default value: `undef`
745+
746+
##### <a name="-k8s--install--fluxcd--install_options"></a>`install_options`
747+
748+
Data type: `Hash[String, Data]`
749+
750+
Additional options to provide to the `flux install` invocation
751+
752+
Default value: `{}`
753+
754+
##### <a name="-k8s--install--fluxcd--upgrade"></a>`upgrade`
755+
756+
Data type: `Boolean`
757+
758+
Upgrade FluxCD on the local cluster if the version changes
759+
760+
Default value: `true`
761+
762+
##### <a name="-k8s--install--fluxcd--install_dir"></a>`install_dir`
763+
764+
Data type: `Stdlib::Unixpath`
765+
766+
Where to install the FluxCD binary
767+
768+
Default value: `'/usr/local/bin'`
769+
770+
##### <a name="-k8s--install--fluxcd--kubeconfig"></a>`kubeconfig`
771+
772+
Data type: `Stdlib::Unixpath`
773+
774+
The kubeconfig file to use when installing/upgrading FluxCD
775+
776+
Default value: `'/root/.kube/config'`
777+
693778
### <a name="k8s--install--kubeadm"></a>`k8s::install::kubeadm`
694779

695780
Installs the kubeadm binary

manifests/install/fluxcd.pp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# @summary Installs the FluxCD CLI, and optionally also installs Flux into on the cluster
2+
#
3+
# @example Install - and automatically update - latest version of Flux
4+
# class { 'k8s::install::fluxcd':
5+
# ensure => latest,
6+
# }
7+
#
8+
# @example Install flux with different components
9+
# class { 'k8s::install::fluxcd':
10+
# install_options => {
11+
# components => ['source-controller', 'kustomize-controller']
12+
# components_extra => ['source-watcher', 'image-reflector-controller']
13+
# }
14+
# }
15+
#
16+
# @param ensure The FluxCD version to install, or present/latest for the latest at the time
17+
# @param install If FluxCD should be installed into the local cluster, will default to true on k8s::server nodes
18+
# @param install_options Additional options to provide to the `flux install` invocation
19+
# @param upgrade Upgrade FluxCD on the local cluster if the version changes
20+
# @param install_dir Where to install the FluxCD binary
21+
# @param kubeconfig The kubeconfig file to use when installing/upgrading FluxCD
22+
class k8s::install::fluxcd (
23+
Variant[Enum['absent', 'present', 'latest'], String[1]] $ensure = 'present',
24+
25+
Optional[Boolean] $install = undef,
26+
Hash[String, Data] $install_options = {},
27+
Boolean $upgrade = true,
28+
29+
Stdlib::Unixpath $install_dir = '/usr/local/bin',
30+
Stdlib::Unixpath $kubeconfig = '/root/.kube/config',
31+
) {
32+
if $ensure == 'absent' {
33+
file { '/usr/local/bin/flux':
34+
ensure => absent,
35+
}
36+
tidy { 'Old FluxCD installs':
37+
path => $install_dir,
38+
recurse => 1,
39+
matches => 'flux-*',
40+
}
41+
return()
42+
}
43+
44+
if $ensure == 'latest' or $ensure == 'present' {
45+
$latest = extlib::version_latest_github('fluxcd/flux2')
46+
$_version = $latest.regsubst('^v?(.+)$', '\1', 'I')
47+
} else {
48+
$_version = $ensure
49+
$latest = "v${_version}"
50+
}
51+
52+
if $_version !~ Pattern[/^\d+(\.\d+){2}$/] {
53+
fail("Version '${_version}' is not a valid FluxCD version")
54+
}
55+
56+
if $ensure == 'present' {
57+
$path = "${install_dir}/flux"
58+
$transform_command = ''
59+
} else {
60+
# Use version-specific flux binary, so that the archive resource detects version changes
61+
$path = "${install_dir}/flux-${_version}"
62+
$transform_command = "--transform='s/flux/flux-${_version}/'"
63+
64+
Archive['FluxCD CLI']
65+
-> file { "${install_dir}/flux":
66+
ensure => link,
67+
target => $path,
68+
replace => true,
69+
owner => 'root',
70+
group => 'root',
71+
mode => '0755',
72+
}
73+
~> tidy { 'Old FluxCD installs':
74+
path => $install_dir,
75+
recurse => 1,
76+
matches => 'flux-*',
77+
}
78+
}
79+
80+
archive { 'FluxCD CLI':
81+
ensure => present,
82+
path => '/tmp/fluxcd.tar.gz',
83+
source => "https://github.com/fluxcd/flux2/releases/download/${latest}/flux_${_version}_linux_amd64.tar.gz",
84+
extract => true,
85+
extract_command => "tar -C /usr/local/bin -xf %s ${transform_command} flux",
86+
extract_path => $install_dir,
87+
cleanup => true,
88+
creates => $path,
89+
}
90+
-> file { $path:
91+
ensure => file,
92+
replace => false,
93+
owner => 'root',
94+
group => 'root',
95+
mode => '0755',
96+
}
97+
98+
if pick($install, defined('k8s::server')) {
99+
$install_flags = $install_options.map |$flag, $value| {
100+
if $value =~ Array {
101+
$_value = $value.join(',')
102+
}
103+
"--${flag.regsubst('_', '-')}=${_value}"
104+
}
105+
106+
# Check if namespace exists, otherwise trigger install
107+
# TODO: trigger install if install_flags have changed?
108+
Exec <| title == 'k8s apiserver wait online' |>
109+
-> exec { 'Verify FluxCD install':
110+
path => $facts['path'],
111+
command => 'true',
112+
unless => "kubectl --kubeconfig ${kubeconfig} get namespace flux-system",
113+
}
114+
~> exec { 'FluxCD install':
115+
command => "flux install --export ${install_flags.join(' ')} | kubectl --kubeconfig ${kubeconfig} apply --server-side --force-conflicts -f -",
116+
refreshonly => true,
117+
path => $facts['path'],
118+
require => File['/usr/local/bin/flux'],
119+
}
120+
# If kubeconfig is managed, ensure it exists before Flux
121+
File <| title == $kubeconfig |> -> Exec['FluxCD install']
122+
Kubeconfig <| title == $kubeconfig |> -> Exec['FluxCD install']
123+
124+
if $upgrade {
125+
# Trigger (re)install on every version change
126+
Archive['FluxCD CLI'] ~> Exec['FluxCD install']
127+
}
128+
}
129+
}

metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
"name": "puppet-archive",
1313
"version_requirement": ">= 4.0.0 < 9.0.0"
1414
},
15+
{
16+
"name": "puppet-extlib",
17+
"version_requirement": ">= 7.5.2 < 8.0.0"
18+
},
1519
{
1620
"name": "puppetlabs-stdlib",
1721
"version_requirement": ">= 9.0.0 < 10.0.0"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'k8s::install::fluxcd' do
6+
let(:pre_condition) do
7+
<<~PUPPET
8+
include k8s
9+
10+
function extlib::version_latest_github(String[1] $pkg) {
11+
return 'v1.2.3'
12+
}
13+
PUPPET
14+
end
15+
16+
on_supported_os.each do |os, os_facts|
17+
context "on #{os}" do
18+
let(:facts) { os_facts }
19+
20+
it { is_expected.to compile }
21+
22+
context "with ensure => present" do
23+
let(:params) do
24+
{
25+
ensure: 'present'
26+
}
27+
end
28+
29+
it do
30+
is_expected.to contain_archive('FluxCD CLI')
31+
.with_source('https://github.com/fluxcd/flux2/releases/download/v1.2.3/flux_1.2.3_linux_amd64.tar.gz')
32+
.with_extract_command("tar -C /usr/local/bin -xf %s flux")
33+
.with_creates('/usr/local/bin/flux')
34+
end
35+
36+
it do
37+
is_expected.to contain_file('/usr/local/bin/flux')
38+
.with_ensure('file')
39+
.with_mode('0755')
40+
.that_requires('Archive[FluxCD CLI]')
41+
end
42+
end
43+
44+
context "with ensure => latest" do
45+
let(:params) do
46+
{
47+
ensure: 'latest'
48+
}
49+
end
50+
51+
it do
52+
is_expected.to contain_archive('FluxCD CLI')
53+
.with_source('https://github.com/fluxcd/flux2/releases/download/v1.2.3/flux_1.2.3_linux_amd64.tar.gz')
54+
.with_extract_command("tar -C /usr/local/bin -xf %s --transform='s/flux/flux-1.2.3/' flux")
55+
.with_creates('/usr/local/bin/flux-1.2.3')
56+
end
57+
58+
it do
59+
is_expected.to contain_file('/usr/local/bin/flux')
60+
.with_ensure('link')
61+
.with_target('/usr/local/bin/flux-1.2.3')
62+
.that_requires('Archive[FluxCD CLI]')
63+
end
64+
end
65+
66+
context "with install => true" do
67+
let(:params) do
68+
{
69+
install: true
70+
}
71+
end
72+
73+
it do
74+
is_expected.to contain_exec('FluxCD install')
75+
.with_command('flux install --export | kubectl --kubeconfig /root/.kube/config apply --server-side --force-conflicts -f -')
76+
.with_refreshonly(true)
77+
.that_requires('File[/usr/local/bin/flux]')
78+
end
79+
end
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)