Puppet Class: profile::kubernetes::deployment_server::helmfile

Defined in:
modules/profile/manifests/kubernetes/deployment_server/helmfile.pp

Overview

Parameters:

  • services (Hash[String, Any]) (defaults to: hiera('profile::kubernetes::deployment_server::services', {}))
  • services_secrets (Hash[String, Any]) (defaults to: hiera('profile::kubernetes::deployment_server_secrets::services', {}))
  • admin_services_secrets (Hash[String, Any]) (defaults to: hiera('profile::kubernetes::deployment_server_secrets::admin_services', {}))
  • prometheus_nodes (Array[Stdlib::Fqdn]) (defaults to: lookup('prometheus_all_nodes'))
  • service_listeners (Array[Profile::Service_listener]) (defaults to: lookup('profile::services_proxy::envoy::listeners', {'default_value' => []}))


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'modules/profile/manifests/kubernetes/deployment_server/helmfile.pp', line 1

class profile::kubernetes::deployment_server::helmfile(
    Hash[String, Any] $services=hiera('profile::kubernetes::deployment_server::services', {}),
    Hash[String, Any] $services_secrets=hiera('profile::kubernetes::deployment_server_secrets::services', {}),
    Hash[String, Any] $admin_services_secrets=hiera('profile::kubernetes::deployment_server_secrets::admin_services', {}),
    Array[Stdlib::Fqdn] $prometheus_nodes = lookup('prometheus_all_nodes'),
    Array[Profile::Service_listener] $service_listeners = lookup('profile::services_proxy::envoy::listeners', {'default_value' => []}),
){

    require_package('helmfile')
    require_package('helm-diff')

    # logging script needed for sal on helmfile
    file { '/usr/local/bin/helmfile_log_sal':
        ensure => file,
        owner  => 'root',
        group  => 'root',
        mode   => '0555',
        source => 'puppet:///modules/profile/kubernetes/helmfile_log_sal.sh',
    }

    git::clone { 'operations/deployment-charts':
        ensure    => 'present',
        directory => '/srv/deployment-charts',
    }

    systemd::timer::job { 'git_pull_charts':
        ensure                    => present,
        description               => 'Pull changes on deployment-charts repo',
        command                   => '/bin/bash -c "cd /srv/deployment-charts && /usr/bin/git pull >/dev/null 2>&1"',
        interval                  => {
            'start'    => 'OnCalendar',
            'interval' => '*-*-* *:*:00', # every minute
        },
        logging_enabled           => false,
        monitoring_enabled        => true,
        monitoring_contact_groups => 'admins',
        user                      => 'root',
    }

    $merged_services = deep_merge($services, $services_secrets)
    $clusters = ['staging', 'eqiad', 'codfw']
    $clusters.each |String $environment| {
        # populate .hfenv is a temporary workaround for hemlfile checkout T212130 for context
        $merged_services.map |String $svcname, Hash $data| {
          if $svcname == 'admin' {
              $hfenv="/srv/deployment-charts/helmfile.d/admin/${environment}/.hfenv"
              $hfdir="/srv/deployment-charts/helmfile.d/admin/${environment}"
          }elsif $svcname != 'admin' and size($svcname) > 1 {
              $hfenv="/srv/deployment-charts/helmfile.d/services/${environment}/${svcname}/.hfenv"
              $hfdir="/srv/deployment-charts/helmfile.d/services/${environment}/${svcname}"
          }else {
              fail("unexpected servicename ${svcname}")
          }
          file { $hfdir:
              ensure => directory,
              owner  => $data['owner'],
              group  => $data['group'],
          }
          file { $hfenv:
              ensure  => present,
              owner   => $data['owner'],
              group   => $data['group'],
              mode    => $data['mode'],
              content => "kube_env \"${svcname}\" \"${environment}\"",
              require => File[$hfdir]
          }
        }
        $merged_services.map |String $svcname, Hash $data| {
            unless $svcname == 'admin' {
                $secrets_dir="/srv/deployment-charts/helmfile.d/services/${environment}/${svcname}/private"
                file { $secrets_dir:
                    ensure  => directory,
                    owner   => $data['owner'],
                    group   => $data['group'],
                    require => Git::Clone['operations/deployment-charts'],
                }
                if $environment == 'staging' {
                    $dc = 'eqiad'
                }
                else {
                    $dc = $environment
                }

                $puppet_ca_data = file($facts['puppet_config']['localcacert'])

                $filtered_prometheus_nodes = $prometheus_nodes.filter |$node| { "${dc}.wmnet" in $node }.map |$node| { ipresolve($node) }

                unless empty($filtered_prometheus_nodes) {
                  $deployment_config_opts = {
                    'tls' => {
                      'telemetry' => {
                        'prometheus_nodes' => $filtered_prometheus_nodes
                      }
                    },
                    'puppet_ca_crt' => $puppet_ca_data,
                  }
                } else {
                  $deployment_config_opts = {
                    'puppet_ca_crt' => $puppet_ca_data
                  }
                }

                # Add here values provided by puppet, like the IPs of the prometheus nodes.
                file { "${secrets_dir}/general.yaml":
                    ensure  => present,
                    owner   => $data['owner'],
                    group   => $data['group'],
                    mode    => $data['mode'],
                    content => to_yaml($deployment_config_opts)
                }
                # write private section only if there is any secret defined.
                $raw_data = $data[$environment]
                if $raw_data {
                    # Substitute the value of any key in the form <somekey>: secret__<somevalue>
                    # with <somekey>: secret(<somevalue>)
                    # This allows to avoid having to copy/paste certs inside of yaml files directly,
                    # for example.
                    $secret_data = wmflib::inject_secret($raw_data)

                    file { "${secrets_dir}/secrets.yaml":
                        owner   => $data['owner'],
                        group   => $data['group'],
                        mode    => $data['mode'],
                        content => ordered_yaml($secret_data),
                        require => [ Git::Clone['operations/deployment-charts'], File[$secrets_dir], ]
                    }
                }
            }
        }
        $admin_services_secrets.map |String $svcname, Hash $data| {
          if $data[$environment] {
            $secrets_dir="/srv/deployment-charts/helmfile.d/admin/${environment}/${svcname}"
            file { $secrets_dir:
                ensure  => directory,
                owner   => $data['owner'],
                group   => $data['group'],
                require => Git::Clone['operations/deployment-charts'],
            }
            file { "${secrets_dir}/private":
                ensure  => directory,
                owner   => $data['owner'],
                group   => $data['group'],
                require => Git::Clone['operations/deployment-charts'],
            }
            file { "${secrets_dir}/private/secrets.yaml":
                owner   => $data['owner'],
                group   => $data['group'],
                mode    => $data['mode'],
                content => ordered_yaml($data[$environment]),
                require => [ Git::Clone['operations/deployment-charts'], File[$secrets_dir], File["${secrets_dir}/private"] ]
            }
          }
        }
    } # end clusters
    # Global file defining the services proxy upstreams
    file { '/etc/helmfile-defaults':
        ensure => directory
    }
    # Services proxy list of definitions to use by our helm charts.
    # They come from two hiera data structures:
    # - profile::services_proxy::envoy::listeners
    # - service::catalog
    $services_proxy = wmflib::service::fetch()
    $proxies = $service_listeners.map |$listener| {
        $address = $listener['upstream'] ? {
            undef   => "${listener['service']}.discovery.wmnet",
            default => $listener['upstream'],
        }
        $svc = $services_proxy[$listener['service']]
        $upstream_port = $svc['port']
        $encryption = $svc['encryption']
        $retval = {
            $listener['name'] => {
                'keepalive' => $listener['keepalive'],
                'port' => $listener['port'],
                'http_host' => $listener['http_host'],
                'timeout'   => $listener['timeout'],
                'retry_policy' => $listener['retry'],
                'xfp' => $listener['xfp'],
                'upstream' => {
                    'address' => $address,
                    'port' => $upstream_port,
                    'encryption' => $encryption,
                }
            }.filter |$key, $val| { $val =~ NotUndef }
        }
    }.reduce({}) |$mem, $val| { $mem.merge($val) }
    file { '/etc/helmfile-defaults/service-proxy.yaml':
        ensure  => present,
        content => to_yaml({'services_proxy' => $proxies}),
    }
}