Skip to content

Commit

Permalink
fixup! Use meaningful value for plugin broker container name instead …
Browse files Browse the repository at this point in the history
…of generated one
  • Loading branch information
sleshchenko committed Jan 16, 2019
1 parent bb0165e commit 1ada30a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ 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 @@ -125,13 +126,12 @@ public E create(
.map(this::asEnvVar)
.collect(Collectors.toList());

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

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

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

Expand All @@ -165,14 +164,13 @@ 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(pluginsType.toLowerCase().replaceAll("[^\\d\\w-]", "-"))
.withName(image.toLowerCase().replaceAll("[^\\d\\w-]", "-"))
.withImage(image)
.withArgs(
"-push-endpoint",
Expand Down Expand Up @@ -221,11 +219,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 @@ -234,7 +232,7 @@ private BrokerConfig createBrokerConfig(
brokerConfig.configMap = newConfigMap(brokerConfig.configMapName, pluginsMeta);
configMapVolume = brokerConfig.configMapVolume;
}
brokerConfig.container = newContainer(pluginsType, runtimeId, envVars, image, configMapVolume);
brokerConfig.container = newContainer(runtimeId, envVars, image, configMapVolume);
brokerConfig.machineName = Names.machineName(pod, brokerConfig.container);
brokerConfig.machineConfig = new InternalMachineConfig();
brokerConfig
Expand All @@ -245,9 +243,9 @@ private BrokerConfig createBrokerConfig(
return brokerConfig;
}

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

return groupedPlugins;
return sortedPlugins;
}

private static class BrokerConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void testInitBrokerContainer() throws Exception {
List<Container> initContainers = brokersConfigs.pod.getSpec().getInitContainers();
assertEquals(initContainers.size(), 1);
Container initContainer = initContainers.get(0);
assertEquals(initContainer.getName(), "init-plugin-broker");
assertEquals(initContainer.getName(), "init-image");
assertEquals(initContainer.getImage(), INIT_IMAGE);
assertEquals(initContainer.getImagePullPolicy(), IMAGE_PULL_POLICY);
assertEquals(
Expand All @@ -143,7 +143,7 @@ public void testInitBrokerContainer() throws Exception {
}

@Test
public void shouldNameContainersAfterPluginsTypes() throws Exception {
public void shouldNameContainersAfterPluginBrokerImage() throws Exception {
// given
Collection<PluginMeta> metas = singletonList(new PluginMeta().type(SUPPORTED_TYPE));
ArgumentCaptor<BrokersConfigs> captor = ArgumentCaptor.forClass(BrokersConfigs.class);
Expand All @@ -158,10 +158,10 @@ public void shouldNameContainersAfterPluginsTypes() throws Exception {

List<Container> initContainers = brokerPodSpec.getInitContainers();
assertEquals(initContainers.size(), 1);
assertEquals(initContainers.get(0).getName(), "init-plugin-broker");
assertEquals(initContainers.get(0).getName(), "init-image");

List<Container> containers = brokerPodSpec.getContainers();
assertEquals(containers.size(), 1);
assertEquals(containers.get(0).getName(), "test-type-1");
assertEquals(containers.get(0).getName(), "testrepo-image-tag");
}
}

0 comments on commit 1ada30a

Please sign in to comment.