From 023ce18d0534563dcb5f25dd5b1f872d7798a6e0 Mon Sep 17 00:00:00 2001 From: bovy89 Date: Mon, 28 Oct 2024 15:51:57 +0100 Subject: [PATCH] add --command-config options to kafka::topic to permit topic management if sasl or custom client configuration are required --- REFERENCE.md | 12 +++++++++++- manifests/topic.pp | 16 +++++++++++++++- spec/defines/topic_spec.rb | 19 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index f99d9cd7..1f0885b7 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -1648,7 +1648,7 @@ Default value: `$kafka::params::producer_log4j_opts` ### `kafka::topic` -This defined type handles the creation of Kafka topics. +  altering broker configs (e.g. specify sasl and ssl configs) #### Examples @@ -1674,6 +1674,7 @@ The following parameters are available in the `kafka::topic` defined type: * [`partitions`](#-kafka--topic--partitions) * [`bin_dir`](#-kafka--topic--bin_dir) * [`config`](#-kafka--topic--config) +* [`cmd_config`](#-kafka--topic--cmd_config) ##### `ensure` @@ -1735,3 +1736,12 @@ See the Kafka documentation for full details on the topic configs. Default value: `undef` +##### `cmd_config` + +Data type: `Optional[Stdlib::Absolutepath]` + +Property file containing configs to be passed to Admin Client. +This is used only with --bootstrap-server option for describing and + +Default value: `undef` + diff --git a/manifests/topic.pp b/manifests/topic.pp index 627b1cdf..df9a5405 100644 --- a/manifests/topic.pp +++ b/manifests/topic.pp @@ -34,6 +34,11 @@ # A topic configuration override for the topic being created or altered. # See the Kafka documentation for full details on the topic configs. # +# @param cmd_config +# Property file containing configs to be passed to Admin Client. +# This is used only with --bootstrap-server option for describing and +#  altering broker configs (e.g. specify sasl and ssl configs) +# define kafka::topic ( Optional[String[1]] $ensure = undef, Optional[String[1]] $zookeeper = undef, @@ -42,6 +47,7 @@ Integer $partitions = 1, String[1] $bin_dir = '/opt/kafka/bin', Optional[Hash[String[1],String[1]]] $config = undef, + Optional[Stdlib::Absolutepath] $cmd_config = undef, ) { $_zookeeper = "--zookeeper ${zookeeper}" $_bootstrap_server = "--bootstrap-server ${bootstrap_server}" @@ -52,10 +58,18 @@ fail('Either zookeeper or bootstrap_server parameter must be defined!') } + if $zookeeper and $cmd_config { + warn('cmd_config will be ignored: This is used only with bootstrap_server') + } + if $zookeeper { $_connection = $_zookeeper } else { - $_connection = $_bootstrap_server + if $cmd_config { + $_connection = "${_bootstrap_server} --command-config ${cmd_config}" + } else { + $_connection = $_bootstrap_server + } } if $config { diff --git a/spec/defines/topic_spec.rb b/spec/defines/topic_spec.rb index 3a2637d2..c4ad048a 100644 --- a/spec/defines/topic_spec.rb +++ b/spec/defines/topic_spec.rb @@ -78,6 +78,25 @@ ) } end + + context 'when create topic demo for kafka v3 and command-config' do + let(:title) { 'demo' } + let :params do + { + 'ensure' => 'present', + 'bootstrap_server' => 'localhost:9092', + 'replication_factor' => 1, + 'partitions' => 1, + 'cmd_config' => '/opt/kafka/config/admin.config', + } + end + + it { + is_expected.to contain_exec("create topic #{title.to_s}").with( + command: "kafka-topics.sh --create --bootstrap-server #{params[:bootstrap_server]} --command-config #{params[:cmd_config]} --replication-factor #{params[:replication_factor]} --partitions #{params[:partitions]} --topic #{title.to_s} " + ) + } + end end end end