Puppet Class: profile::kafka::configurator
- Defined in:
- modules/profile/manifests/kafka/configurator.pp
Summary
converge live Kafka topic configuration to Puppet-declared stateOverview
SPDX-License-Identifier: Apache-2.0 Renders the desired topic configuration from hieradata and runs kafka-configurator against the local cluster to converge it.
The tool is a reconcile-and-exit CLI, not a daemon. Divergence has two sources -- the declared state changed (hieradata), or the live cluster was mutated out-of-band -- and each gets its own trigger into the same systemd oneshot unit:
* Puppet notifies the unit whenever the rendered desired state
changes, so a merged hieradata change converges within one agent
run rather than waiting for the next tick
* A timer runs it on a schedule, bounding the lifetime of drift
Puppet cannot see: a hand-run `kafka configs --alter` changes the
cluster without changing any file on any host
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 |
# File 'modules/profile/manifests/kafka/configurator.pp', line 52
class profile::kafka::configurator (
Optional[Stdlib::Fqdn] $runner_host = lookup('profile::kafka::configurator::runner_host', {'default_value' => undef}),
Hash[String, Hash] $topics = lookup('profile::kafka::configurator::topics', {'default_value' => {}}),
Hash[String, Hash[String, Boolean]] $features = lookup('profile::kafka::configurator::features'),
Boolean $dry_run = lookup('profile::kafka::configurator::dry_run', {'default_value' => true}),
Systemd::Timer::Datetime $interval = lookup('profile::kafka::configurator::interval', {'default_value' => '*-*-* *:00/30:00'}),
Optional[String[1]] $bootstrap = lookup('profile::kafka::configurator::bootstrap', {'default_value' => undef}),
Stdlib::Absolutepath $tool = lookup('profile::kafka::configurator::tool', {'default_value' => '/usr/bin/kafka-configurator'}),
) {
# We need the broker profile evaluated first because we depend on
# its listener and cert variables below
require profile::kafka::broker
$config_dir = '/etc/kafka-configurator'
$config_file = "${config_dir}/config.yaml"
# Client identity: the broker's own PKI certificate, whose CN is
# already in the cluster's super.users, so no ACL has to exist
# before the tool can read anything. When it's time to run this
# in production rather than the cloud test bed, provision a
# dedicated PKI client certificate (T276088). broker.pp only
# obtains a certificate when ssl_enabled, so a plaintext-only
# broker gets a plaintext-only client.
#
# librdkafka will not present a bare leaf (it silently connects
# with no client identity), so this is the chained file
if $profile::kafka::broker::ssl_enabled {
$ssl_cert = $profile::kafka::broker::ssl_cert
$cluster = {
'bootstrap' => pick($bootstrap, "${facts['networking']['fqdn']}:${profile::kafka::broker::ssl_port}"),
'ssl' => {
'ca' => '/etc/ssl/certs/wmf-ca-certificates.crt',
'certificate' => $ssl_cert['chained'],
'key' => $ssl_cert['key'],
},
}
} else {
$cluster = {
'bootstrap' => pick($bootstrap, "${facts['networking']['fqdn']}:${profile::kafka::broker::plaintext_port}"),
}
}
# Installed on every broker, not just the runner, so moving
# runner_host doesn't depend on a package install
stdlib::ensure_packages('kafka-configurator')
if $runner_host == $facts['networking']['fqdn'] {
$config = {
'cluster' => $cluster,
'features' => $features,
'topics' => $topics,
}
file { $config_dir:
ensure => directory,
owner => 'root',
group => 'root',
mode => '0555',
}
file { $config_file:
ensure => file,
owner => 'root',
group => 'root',
mode => '0444',
content => stdlib::to_yaml($config),
require => File[$config_dir],
# If the desired state declared in hiera changed, run the
# reconciliation as part of the puppet run
notify => Exec['kafka-configurator reconcile'],
}
$dry_run_flag = $dry_run.bool2str(' --dry-run', '')
# The broker's private key is 0440 and owned by the kafka user,
# so kafka is the least privilege that can read it
systemd::timer::job { 'kafka-configurator':
ensure => present,
description => 'Converge Kafka topic configuration to Puppet-declared state',
command => "${tool} --config ${config_file}${dry_run_flag}",
user => 'kafka',
interval => {'start' => 'OnCalendar', 'interval' => $interval},
logging_enabled => true,
require => [File[$config_file], Package['kafka-configurator']],
# dry_run and the tool path live in the unit's command line,
# not in config.yaml, so a change to them rewrites only the
# unit. Run the reconciliation now rather than leaving the
# new command unused until the next tick
notify => Exec['kafka-configurator reconcile'],
}
exec { 'kafka-configurator reconcile':
command => '/bin/systemctl start kafka-configurator.service',
refreshonly => true,
require => Systemd::Timer::Job['kafka-configurator'],
}
} else {
# A moved runner_host or a disabled cluster must tear down the
# old runner: an orphaned timer would keep converging the
# cluster with a frozen desired state
systemd::timer::job { 'kafka-configurator':
ensure => absent,
description => 'Converge Kafka topic configuration to Puppet-declared state',
command => "${tool} --config ${config_file}",
user => 'kafka',
interval => {'start' => 'OnCalendar', 'interval' => $interval},
}
file { $config_dir:
ensure => absent,
recurse => true,
force => true,
}
}
}
|