Skip to content

Commit

Permalink
Use meaningful value for plugin broker container name instead of gene…
Browse files Browse the repository at this point in the history
…rated one
  • Loading branch information
sleshchenko committed Jan 16, 2019
1 parent 4ad10f2 commit 2eb4589
Showing 1 changed file with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public abstract class BrokerEnvironmentFactory<E extends KubernetesEnvironment>
private static final String BROKER_VOLUME = "broker-config-volume";
private static final String CONF_FOLDER = "/broker-config";
private static final String CONFIG_FILE = "config.json";
private static final String CONTAINER_NAME_SUFFIX = "broker";
private static final String PLUGINS_VOLUME_NAME = "plugins";
private static final String BROKERS_POD_NAME = "che-plugin-broker";
private static final Gson GSON = new GsonBuilder().disableHtmlEscaping().create();
Expand Down Expand Up @@ -126,12 +125,13 @@ public E create(
.map(this::asEnvVar)
.collect(Collectors.toList());

Multimap<String, PluginMeta> brokersImageToMetas = sortByBrokerImage(pluginsMeta);
for (Entry<String, Collection<PluginMeta>> brokerImageToMetas :
brokersImageToMetas.asMap().entrySet()) {
Multimap<String, PluginMeta> typeToMetas = groupByTypes(pluginsMeta);
for (Entry<String, Collection<PluginMeta>> pluginTypeToMetas : typeToMetas.asMap().entrySet()) {
String pluginsType = pluginTypeToMetas.getKey();
String brokerImage = pluginTypeToImage.get(pluginsType);
BrokerConfig brokerConfig =
createBrokerConfig(
runtimeID, brokerImageToMetas.getValue(), envVars, brokerImageToMetas.getKey(), pod);
runtimeID, pluginsType, pluginTypeToMetas.getValue(), envVars, brokerImage, pod);

brokersConfigs.machines.put(brokerConfig.machineName, brokerConfig.machineConfig);
brokersConfigs.configMaps.put(brokerConfig.configMapName, brokerConfig.configMap);
Expand All @@ -149,7 +149,8 @@ public E create(
}

// Add init broker that cleans up /plugins
BrokerConfig brokerConfig = createBrokerConfig(runtimeID, null, envVars, initBrokerImage, pod);
BrokerConfig brokerConfig =
createBrokerConfig(runtimeID, "init-plugin-broker", null, envVars, initBrokerImage, pod);
pod.getSpec().getInitContainers().add(brokerConfig.container);
brokersConfigs.machines.put(brokerConfig.machineName, brokerConfig.machineConfig);

Expand All @@ -164,13 +165,14 @@ private String generateUniqueName(String suffix) {
}

private Container newContainer(
String pluginsType,
RuntimeIdentity runtimeId,
List<EnvVar> envVars,
String image,
@Nullable String brokerVolumeName) {
final ContainerBuilder cb =
new ContainerBuilder()
.withName(generateUniqueName(CONTAINER_NAME_SUFFIX))
.withName(pluginsType.toLowerCase().replaceAll("[^\\d\\w-]", "-"))
.withImage(image)
.withArgs(
"-push-endpoint",
Expand Down Expand Up @@ -219,11 +221,11 @@ private EnvVar asEnvVar(Pair<String, String> envVar) {

private BrokerConfig createBrokerConfig(
RuntimeIdentity runtimeId,
String pluginsType,
@Nullable Collection<PluginMeta> pluginsMeta,
List<EnvVar> envVars,
String image,
Pod pod) {

BrokerConfig brokerConfig = new BrokerConfig();
String configMapVolume = null;
if (pluginsMeta != null) {
Expand All @@ -232,7 +234,7 @@ private BrokerConfig createBrokerConfig(
brokerConfig.configMap = newConfigMap(brokerConfig.configMapName, pluginsMeta);
configMapVolume = brokerConfig.configMapVolume;
}
brokerConfig.container = newContainer(runtimeId, envVars, image, configMapVolume);
brokerConfig.container = newContainer(pluginsType, runtimeId, envVars, image, configMapVolume);
brokerConfig.machineName = Names.machineName(pod, brokerConfig.container);
brokerConfig.machineConfig = new InternalMachineConfig();
brokerConfig
Expand All @@ -243,9 +245,9 @@ private BrokerConfig createBrokerConfig(
return brokerConfig;
}

private Multimap<String, PluginMeta> sortByBrokerImage(Collection<PluginMeta> pluginMetas)
private Multimap<String, PluginMeta> groupByTypes(Collection<PluginMeta> pluginMetas)
throws InfrastructureException {
Multimap<String, PluginMeta> sortedPlugins = ArrayListMultimap.create();
Multimap<String, PluginMeta> groupedPlugins = ArrayListMultimap.create();
for (PluginMeta pluginMeta : pluginMetas) {
String type = pluginMeta.getType();
if (isNullOrEmpty(type)) {
Expand All @@ -254,17 +256,16 @@ private Multimap<String, PluginMeta> sortByBrokerImage(Collection<PluginMeta> pl
"Plugin '%s:%s' has invalid type '%s'",
pluginMeta.getId(), pluginMeta.getVersion(), type));
}
String image = pluginTypeToImage.get(type);
if (isNullOrEmpty(image)) {
if (!pluginTypeToImage.containsKey(type)) {
throw new InfrastructureException(
format(
"Plugin '%s:%s' has unsupported type '%s'",
pluginMeta.getId(), pluginMeta.getVersion(), type));
}
sortedPlugins.put(image, pluginMeta);
groupedPlugins.put(type, pluginMeta);
}

return sortedPlugins;
return groupedPlugins;
}

private static class BrokerConfig {
Expand Down

0 comments on commit 2eb4589

Please sign in to comment.