UserOperatorConfig { @SuppressWarnings({"checkstyle:CyclomaticComplexity", "checkstyle:NPathComplexity"}) public static UserOperatorConfig fromMap(Map<String, String> map) { String namespace = map.get(UserOperatorConfig.STRIMZI_NAMESPACE); if (namespace == null || namespace.isEmpty()) { throw new InvalidConfigurationException(UserOperatorConfig.STRIMZI_NAMESPACE + " cannot be null"); } long reconciliationInterval = DEFAULT_FULL_RECONCILIATION_INTERVAL_MS; String reconciliationIntervalEnvVar = map.get(UserOperatorConfig.STRIMZI_FULL_RECONCILIATION_INTERVAL_MS); if (reconciliationIntervalEnvVar != null) { reconciliationInterval = Long.parseLong(reconciliationIntervalEnvVar); } String kafkaBootstrapServers = DEFAULT_KAFKA_BOOTSTRAP_SERVERS; String kafkaBootstrapServersEnvVar = map.get(UserOperatorConfig.STRIMZI_KAFKA_BOOTSTRAP_SERVERS); if (kafkaBootstrapServersEnvVar != null && !kafkaBootstrapServersEnvVar.isEmpty()) { kafkaBootstrapServers = kafkaBootstrapServersEnvVar; } String zookeeperConnect = DEFAULT_ZOOKEEPER_CONNECT; String zookeeperConnectEnvVar = map.get(UserOperatorConfig.STRIMZI_ZOOKEEPER_CONNECT); if (zookeeperConnectEnvVar != null && !zookeeperConnectEnvVar.isEmpty()) { zookeeperConnect = zookeeperConnectEnvVar; } long zookeeperSessionTimeoutMs = DEFAULT_ZOOKEEPER_SESSION_TIMEOUT_MS; String zookeeperSessionTimeoutMsEnvVar = map.get(UserOperatorConfig.STRIMZI_ZOOKEEPER_SESSION_TIMEOUT_MS); if (zookeeperSessionTimeoutMsEnvVar != null) { zookeeperSessionTimeoutMs = Long.parseLong(zookeeperSessionTimeoutMsEnvVar); } Labels labels; try { labels = Labels.fromString(map.get(STRIMZI_LABELS)); } catch (Exception e) { throw new InvalidConfigurationException("Failed to parse labels from " + STRIMZI_LABELS, e); } String caCertSecretName = map.get(UserOperatorConfig.STRIMZI_CA_CERT_SECRET_NAME); if (caCertSecretName == null || caCertSecretName.isEmpty()) { throw new InvalidConfigurationException(UserOperatorConfig.STRIMZI_CA_CERT_SECRET_NAME + " cannot be null"); } String caKeySecretName = map.get(UserOperatorConfig.STRIMZI_CA_KEY_SECRET_NAME); if (caKeySecretName == null || caKeySecretName.isEmpty()) { throw new InvalidConfigurationException(UserOperatorConfig.STRIMZI_CA_KEY_SECRET_NAME + " cannot be null"); } String clusterCaCertSecretName = map.get(UserOperatorConfig.STRIMZI_CLUSTER_CA_CERT_SECRET_NAME); String eoKeySecretName = map.get(UserOperatorConfig.STRIMZI_EO_KEY_SECRET_NAME); String caNamespace = map.get(UserOperatorConfig.STRIMZI_CA_NAMESPACE); if (caNamespace == null || caNamespace.isEmpty()) { caNamespace = namespace; } return new UserOperatorConfig(namespace, reconciliationInterval, kafkaBootstrapServers, zookeeperConnect, zookeeperSessionTimeoutMs, labels, caCertSecretName, caKeySecretName, clusterCaCertSecretName, eoKeySecretName, caNamespace); } @SuppressWarnings({"checkstyle:ParameterNumber"}) //TODO: to remove when removing the zookeeper related parameters  UserOperatorConfig(String namespace,
                              long reconciliationIntervalMs,
                              String kafkaBootstrapServers,
                              String zookeperConnect,
                              long zookeeperSessionTimeoutMs,
                              Labels labels, String caCertSecretName,
                              String caKeySecretName,
                              String clusterCaCertSecretName,
                              String eoKeySecretName,
                              String caNamespace); @SuppressWarnings({"checkstyle:CyclomaticComplexity", "checkstyle:NPathComplexity"}) static UserOperatorConfig fromMap(Map<String, String> map); static int getClientsCaValidityDays(); static int getClientsCaRenewalDays(); String getNamespace(); long getReconciliationIntervalMs(); Labels getLabels(); String getCaCertSecretName(); String getCaKeySecretName(); String getClusterCaCertSecretName(); String getEoKeySecretName(); String getCaNamespace(); String getKafkaBootstrapServers(); String getZookeperConnect(); long getZookeeperSessionTimeoutMs(); @Override String toString(); static final String STRIMZI_NAMESPACE; static final String STRIMZI_FULL_RECONCILIATION_INTERVAL_MS; static final String STRIMZI_LABELS; static final String STRIMZI_CA_CERT_SECRET_NAME; static final String STRIMZI_CA_KEY_SECRET_NAME; static final String STRIMZI_CLUSTER_CA_CERT_SECRET_NAME; static final String STRIMZI_EO_KEY_SECRET_NAME; static final String STRIMZI_CA_NAMESPACE; static final String STRIMZI_KAFKA_BOOTSTRAP_SERVERS; static final String STRIMZI_ZOOKEEPER_CONNECT; static final String STRIMZI_ZOOKEEPER_SESSION_TIMEOUT_MS; static final String STRIMZI_CLIENTS_CA_VALIDITY; static final String STRIMZI_CLIENTS_CA_RENEWAL; static final long DEFAULT_FULL_RECONCILIATION_INTERVAL_MS; static final String DEFAULT_KAFKA_BOOTSTRAP_SERVERS; static final String DEFAULT_ZOOKEEPER_CONNECT; static final long DEFAULT_ZOOKEEPER_SESSION_TIMEOUT_MS; }
@Test public void testFromMap() { UserOperatorConfig config = UserOperatorConfig.fromMap(envVars); assertThat(config.getNamespace(), is(envVars.get(UserOperatorConfig.STRIMZI_NAMESPACE))); assertThat(config.getReconciliationIntervalMs(), is(Long.parseLong(envVars.get(UserOperatorConfig.STRIMZI_FULL_RECONCILIATION_INTERVAL_MS)))); assertThat(config.getLabels(), is(expectedLabels)); assertThat(config.getCaCertSecretName(), is(envVars.get(UserOperatorConfig.STRIMZI_CA_CERT_SECRET_NAME))); assertThat(config.getCaNamespace(), is(envVars.get(UserOperatorConfig.STRIMZI_CA_NAMESPACE))); assertThat(config.getZookeperConnect(), is(envVars.get(UserOperatorConfig.STRIMZI_ZOOKEEPER_CONNECT))); assertThat(config.getZookeeperSessionTimeoutMs(), is(Long.parseLong(envVars.get(UserOperatorConfig.STRIMZI_ZOOKEEPER_SESSION_TIMEOUT_MS)))); }
@Test public void testFromMapNamespaceEnvVarMissingThrows() { Map<String, String> envVars = new HashMap<>(UserOperatorConfigTest.envVars); envVars.remove(UserOperatorConfig.STRIMZI_NAMESPACE); assertThrows(InvalidConfigurationException.class, () -> UserOperatorConfig.fromMap(envVars)); }
@Test public void testFromMapCaNameEnvVarMissingThrows() { Map<String, String> envVars = new HashMap<>(UserOperatorConfigTest.envVars); envVars.remove(UserOperatorConfig.STRIMZI_CA_CERT_SECRET_NAME); assertThrows(InvalidConfigurationException.class, () -> UserOperatorConfig.fromMap(envVars)); }
@Test public void testFromMapInvalidReconciliationIntervalThrows() { Map<String, String> envVars = new HashMap<>(UserOperatorConfigTest.envVars); envVars.put(UserOperatorConfig.STRIMZI_FULL_RECONCILIATION_INTERVAL_MS, "not_an_long"); assertThrows(NumberFormatException.class, () -> UserOperatorConfig.fromMap(envVars)); }
@Test public void testFromMapInvalidLabelsStringThrows() { Map<String, String> envVars = new HashMap<>(UserOperatorConfigTest.envVars); envVars.put(UserOperatorConfig.STRIMZI_LABELS, ",label1="); assertThrows(InvalidConfigurationException.class, () -> UserOperatorConfig.fromMap(envVars)); }