|
| 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 | +} |
0 commit comments