Puppet Class: profile::kafka::configurator

Defined in:
modules/profile/manifests/kafka/configurator.pp

Summary

converge live Kafka topic configuration to Puppet-declared state

Overview

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

Parameters:

  • runner_host (Optional[Stdlib::Fqdn]) (defaults to: lookup('profile::kafka::configurator::runner_host', {'default_value' => undef})) —

    The single host allowed to converge this cluster. Every other host ensures the runner artifacts are absent, so only one broker converges the cluster, apart from the old and new runner overlapping until the old host's next Puppet run. Moving runner_host (or unsetting it) tears down the old runner rather than leaving its timer converging a frozen desired state. The broker roles include this class on every broker, so the default is unset: nobody runs.

  • topics (Hash[String, Hash]) (defaults to: lookup('profile::kafka::configurator::topics', {'default_value' => {}})) —

    Desired per-topic configuration overrides, as => {config => {key => value}}. Topics absent from the cluster are skipped; overrides not listed here are left alone.

  • features (Hash[String, Hash[String, Boolean]]) (defaults to: lookup('profile::kafka::configurator::features')) —

    Feature settings passed to kafka-configurator.

  • dry_run (Boolean) (defaults to: lookup('profile::kafka::configurator::dry_run', {'default_value' => true})) —

    True (the default) passes --dry-run, so runs report the pending diff without writing; a cluster stays in this mode until its dry-run output has shown no unexpected changes for long enough to trust it. Set false to let the tool apply the diff.

  • interval (Systemd::Timer::Datetime) (defaults to: lookup('profile::kafka::configurator::interval', {'default_value' => '*-*-* *:00/30:00'})) —

    systemd OnCalendar expression for how often to reconcile actual state with desired state.

  • bootstrap (Optional[String[1]]) (defaults to: lookup('profile::kafka::configurator::bootstrap', {'default_value' => undef})) —

    librdkafka bootstrap.servers value, so host:port (or a comma separated list of them) rather than a bare hostname. Defaults to this host's own listener, TLS when the broker has ssl_enabled and plaintext otherwise; metadata discovery finds the rest of the cluster from there. Not localhost, whose name would not match the broker certificate.

  • tool (Stdlib::Absolutepath) (defaults to: lookup('profile::kafka::configurator::tool', {'default_value' => '/usr/bin/kafka-configurator'})) —

    Path to the kafka-configurator executable, as installed by the kafka-configurator package.



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,
        }
    }
}