Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor InitWriterConfig #9922

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 63 additions & 45 deletions kafka-init/src/main/java/io/strimzi/kafka/init/InitWriterConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,123 @@
*/
package io.strimzi.kafka.init;

import io.strimzi.operator.common.config.ConfigParameter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static io.strimzi.operator.common.config.ConfigParameterParser.BOOLEAN;
import static io.strimzi.operator.common.config.ConfigParameterParser.NON_EMPTY_STRING;
import static io.strimzi.operator.common.config.ConfigParameterParser.STRING;

/**
* Init Writer configuration
*/
public class InitWriterConfig {
static final String INIT_FOLDER = "INIT_FOLDER";
static final String RACK_TOPOLOGY_KEY = "RACK_TOPOLOGY_KEY";
static final String NODE_NAME = "NODE_NAME";
static final String EXTERNAL_ADDRESS = "EXTERNAL_ADDRESS";
static final String EXTERNAL_ADDRESS_TYPE = "EXTERNAL_ADDRESS_TYPE";

static final String DEFAULT_INIT_FOLDER = "/opt/kafka/init";
private static final Map<String, ConfigParameter<?>> CONFIG_VALUES = new HashMap<>();
/**
* Folder where the rackid file is written
*/
public static final ConfigParameter<String> INIT_FOLDER = new ConfigParameter<>("INIT_FOLDER", STRING, "/opt/kafka/init", CONFIG_VALUES);
/**
* Kubernetes cluster node name from which getting the rack related label
*/
public static final ConfigParameter<String> NODE_NAME = new ConfigParameter<>("NODE_NAME", NON_EMPTY_STRING, CONFIG_VALUES);
/**
* Kubernetes cluster node label to use as topology key for rack definition
*/
public static final ConfigParameter<String> RACK_TOPOLOGY_KEY = new ConfigParameter<>("RACK_TOPOLOGY_KEY", STRING, null, CONFIG_VALUES);
/**
* Whether external address should be acquired
*/
public static final ConfigParameter<Boolean> EXTERNAL_ADDRESS = new ConfigParameter<>("EXTERNAL_ADDRESS", BOOLEAN, "false", CONFIG_VALUES);
/**
* The address type which should be preferred in the selection
*/
public static final ConfigParameter<String> EXTERNAL_ADDRESS_TYPE = new ConfigParameter<>("EXTERNAL_ADDRESS_TYPE", STRING, null, CONFIG_VALUES);
private final Map<String, Object> map;

private final String nodeName;
private final String rackTopologyKey;
private final boolean externalAddress;
private final String addressType;
private final String initFolder;
/**
* @return Set of configuration key/names
*/
public static Set<String> keyNames() {
return Collections.unmodifiableSet(CONFIG_VALUES.keySet());
}

private InitWriterConfig(Map<String, Object> map) {
this.map = map;
}
/**
* Load configuration parameters from a related map
*
* @param map map from which loading configuration parameters
* @return Rack Writer configuration instance
*/
static InitWriterConfig fromMap(Map<String, String> map) {
Map<String, String> envMap = new HashMap<>(map);
envMap.keySet().retainAll(InitWriterConfig.keyNames());

String nodeName = map.get(InitWriterConfig.NODE_NAME);
if (nodeName == null || nodeName.equals("")) {
throw new IllegalArgumentException(InitWriterConfig.NODE_NAME + " cannot be null or empty");
}

String rackTopologyKey = map.get(InitWriterConfig.RACK_TOPOLOGY_KEY);
Map<String, Object> generatedMap = ConfigParameter.define(envMap, CONFIG_VALUES);

boolean externalAddress = map.containsKey(InitWriterConfig.EXTERNAL_ADDRESS);

String initFolder = DEFAULT_INIT_FOLDER;
String initFolderEnvVar = map.get(InitWriterConfig.INIT_FOLDER);
if (initFolderEnvVar != null) {
initFolder = initFolderEnvVar;
}

String externalAddressType = map.get(InitWriterConfig.EXTERNAL_ADDRESS_TYPE);

return new InitWriterConfig(nodeName, rackTopologyKey, externalAddress, initFolder, externalAddressType);
return new InitWriterConfig(generatedMap);
}

InitWriterConfig(String nodeName, String rackTopologyKey, boolean externalAddress, String initFolder, String externalAddressType) {
this.nodeName = nodeName;
this.rackTopologyKey = rackTopologyKey;
this.externalAddress = externalAddress;
this.initFolder = initFolder;
this.addressType = externalAddressType;
/**
* Gets the configuration value corresponding to the key
* @param <T> Type of value
* @param value Instance of Config Parameter class
* @return Configuration value w.r.t to the key
*/
@SuppressWarnings("unchecked")
public <T> T get(ConfigParameter<T> value) {
return (T) this.map.get(value.key());
}

/**
* @return Kubernetes cluster node name from which getting the rack related label
*/
public String getNodeName() {
return nodeName;
return get(NODE_NAME);
}

/**
* @return the Kubernetes cluster node label to use as topology key for rack definition
*/
public String getRackTopologyKey() {
return rackTopologyKey;
return get(RACK_TOPOLOGY_KEY);
}

/**
* @return folder where the rackid file is written
*/
public String getInitFolder() {
return initFolder;
return get(INIT_FOLDER);
}

/**
* @return Return whether external address should be acquired
*/
public boolean isExternalAddress() {
return externalAddress;
return get(EXTERNAL_ADDRESS);
}

/**
* @return The address type which should be preferred in the selection
*/
public String getAddressType() {
return addressType;
return get(EXTERNAL_ADDRESS_TYPE);
}

@Override
public String toString() {
return "InitWriterConfig(" +
"nodeName=" + nodeName +
",rackTopologyKey=" + rackTopologyKey +
",externalAddress=" + externalAddress +
",initFolder=" + initFolder +
",addressType=" + addressType +
"nodeName=" + getNodeName() +
",rackTopologyKey=" + getRackTopologyKey() +
",externalAddress=" + isExternalAddress() +
",initFolder=" + getInitFolder() +
",addressType=" + getAddressType() +
")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package io.strimzi.kafka.init;

import io.strimzi.operator.common.InvalidConfigurationException;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
Expand All @@ -15,11 +16,10 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

public class InitWriterConfigTest {

private static final Map<String, String> ENV_VARS = Map.of(
InitWriterConfig.NODE_NAME, "localhost",
InitWriterConfig.RACK_TOPOLOGY_KEY, "failure-domain.beta.kubernetes.io/zone",
InitWriterConfig.EXTERNAL_ADDRESS, "TRUE"
InitWriterConfig.NODE_NAME.key(), "localhost",
InitWriterConfig.RACK_TOPOLOGY_KEY.key(), "failure-domain.beta.kubernetes.io/zone",
InitWriterConfig.EXTERNAL_ADDRESS.key(), "TRUE"
);

@Test
Expand All @@ -35,22 +35,22 @@ public void testFromMap() {
@Test
public void testFromMapWithAddressTypeEnvVar() {
Map<String, String> envs = new HashMap<>(ENV_VARS);
envs.put(InitWriterConfig.EXTERNAL_ADDRESS_TYPE, "InternalDNS");
envs.put(InitWriterConfig.EXTERNAL_ADDRESS_TYPE.key(), "InternalDNS");

InitWriterConfig config = InitWriterConfig.fromMap(envs);
assertThat(config.getAddressType(), is("InternalDNS"));
}

@Test
public void testFromMapEmptyEnvVarsThrows() {
assertThrows(IllegalArgumentException.class, () -> InitWriterConfig.fromMap(Map.of()));
assertThrows(InvalidConfigurationException.class, () -> InitWriterConfig.fromMap(Map.of()));
}

@Test
public void testFromMapMissingNodeNameThrows() {
Map<String, String> envVars = new HashMap<>(ENV_VARS);
envVars.remove(InitWriterConfig.NODE_NAME);
envVars.remove(InitWriterConfig.NODE_NAME.key());

assertThrows(IllegalArgumentException.class, () -> InitWriterConfig.fromMap(envVars));
assertThrows(InvalidConfigurationException.class, () -> InitWriterConfig.fromMap(envVars));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public class InitWriterTest {
public File tempDir;

private static final Map<String, String> ENV_VARS = Map.of(
InitWriterConfig.NODE_NAME, "localhost",
InitWriterConfig.RACK_TOPOLOGY_KEY, "failure-domain.beta.kubernetes.io/zone",
InitWriterConfig.EXTERNAL_ADDRESS, "true"
InitWriterConfig.NODE_NAME.key(), "localhost",
InitWriterConfig.RACK_TOPOLOGY_KEY.key(), "failure-domain.beta.kubernetes.io/zone",
InitWriterConfig.EXTERNAL_ADDRESS.key(), "true"
);
// metadata labels related to the Kubernetes cluster node
private static final Map<String, String> LABELS = Map.of(
Expand All @@ -60,7 +60,7 @@ public void testWriteRackId() throws IOException {
new File(rackFolder).mkdirs();

Map<String, String> envVars = new HashMap<>(ENV_VARS);
envVars.put(InitWriterConfig.INIT_FOLDER, rackFolder);
envVars.put(InitWriterConfig.INIT_FOLDER.key(), rackFolder);

InitWriterConfig config = InitWriterConfig.fromMap(envVars);

Expand All @@ -80,7 +80,7 @@ public void testWriteExternalAddress() throws IOException {
new File(addressFolder).mkdirs();

Map<String, String> envVars = new HashMap<>(ENV_VARS);
envVars.put(InitWriterConfig.INIT_FOLDER, addressFolder);
envVars.put(InitWriterConfig.INIT_FOLDER.key(), addressFolder);

InitWriterConfig config = InitWriterConfig.fromMap(envVars);

Expand Down Expand Up @@ -116,7 +116,7 @@ public void testWriteRackFailsWhenInitFolderDoesNotExist() {

// specify a not existing folder for emulating IOException in the rack writer
Map<String, String> envVars = new HashMap<>(ENV_VARS);
envVars.put(InitWriterConfig.INIT_FOLDER, "/no-folder");
envVars.put(InitWriterConfig.INIT_FOLDER.key(), "/no-folder");

InitWriterConfig config = InitWriterConfig.fromMap(envVars);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static <T extends Number> ConfigParameterParser<T> strictlyPositive(ConfigParame
* A Java Boolean
*/
ConfigParameterParser<Boolean> BOOLEAN = configValue -> {
if (configValue.equals("true") || configValue.equals("false")) {
if (configValue.equalsIgnoreCase("true") || configValue.equalsIgnoreCase("false")) {
return Boolean.parseBoolean(configValue);
} else {
throw new InvalidConfigurationException("Failed to parse. Value " + configValue + " is not valid");
Expand Down
Loading