/*
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins;

import com.google.common.annotations.Beta;
import com.google.inject.Inject;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.Pod;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment;
import org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig;
import org.eclipse.che.api.workspace.server.wsplugins.model.PluginMeta;
import org.eclipse.che.workspace.infrastructure.kubernetes.Names;
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment;
import org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.brokerphases.BrokerEnvironmentFactory;

/**
 * Given a {@link InternalEnvironment} representing a workspace, adds the plugin broker as an init
 * container to the workspace Pod. This is necessary if the workspace is created using emptyDir
 * volumes (to allow the broker to add files to the workspace pod's volumes).
 *
 * <p>This API is in <b>Beta</b> and is subject to changes or removal.
 *
 * @author Angel Misevski
 */
@Beta
public class KubernetesBrokerInitContainerApplier<E extends KubernetesEnvironment> {

  private final BrokerEnvironmentFactory<E> brokerEnvironmentFactory;

  @Inject
  public KubernetesBrokerInitContainerApplier(
      BrokerEnvironmentFactory<E> brokerEnvironmentFactory) {
    this.brokerEnvironmentFactory = brokerEnvironmentFactory;
  }

  /**
   * Apply plugin broker as init container to workspace environment. Workspace environment will have
   * broker's configmap, machines, and volumes added in addition to the init container
   *
   * @param workspaceEnvironment
   * @param runtimeID
   * @param pluginsMeta
   * @throws InfrastructureException
   */
  public void apply(
      InternalEnvironment workspaceEnvironment,
      RuntimeIdentity runtimeID,
      Collection<PluginMeta> pluginsMeta)
      throws InfrastructureException {

    KubernetesEnvironment kubernetesEnvironment = (KubernetesEnvironment) workspaceEnvironment;
    E brokerEnvironment =
        brokerEnvironmentFactory.create(pluginsMeta, runtimeID, new BrokersResult());

    Map<String, Pod> workspacePods = kubernetesEnvironment.getPods();
    if (workspacePods.size() != 1) {
      throw new InfrastructureException(
          "Che plugins tooling configuration can be applied to a workspace with one pod only.");
    }
    Pod workspacePod = workspacePods.values().iterator().next();

    Map<String, Pod> brokerPods = brokerEnvironment.getPods();
    if (brokerPods.size() != 1) {
      throw new InfrastructureException("Broker environment must have only one Pod.");
    }
    Pod brokerPod = brokerPods.values().iterator().next();

    // Add broker machines to workspace environment so that the init containers can be provisioned.
    List<Container> brokerContainers = brokerPod.getSpec().getContainers();
    for (Container container : brokerContainers) {
      InternalMachineConfig brokerMachine =
          brokerEnvironment.getMachines().get(Names.machineName(brokerPod, container));
      if (brokerMachine == null) {
        throw new InfrastructureException(
            String.format("Could not find machine for broker container %s", container.getName()));
      }
      kubernetesEnvironment
          .getMachines()
          .put(Names.machineName(workspacePod, container), brokerMachine);
    }

    kubernetesEnvironment.getConfigMaps().putAll(brokerEnvironment.getConfigMaps());
    workspacePod.getSpec().setInitContainers(brokerPod.getSpec().getContainers());
    workspacePod.getSpec().getVolumes().addAll(brokerPod.getSpec().getVolumes());
  }
}

@@ -2233,129 +2233,8 @@
 ner%0A
-   *%0A   * @param workspaceEnvironment%0A   * @param runtimeID%0A   * @param pluginsMeta%0A   * @throws InfrastructureException%0A
    *
@@ -2262,35 +2262,17 @@
 (%0A      
-InternalEnvironment
+E
  workspa
@@ -2285,22 +2285,16 @@
 ronment,
-%0A     
  Runtime
@@ -2312,22 +2312,16 @@
 ntimeID,
-%0A     
  Collect
@@ -2393,104 +2393,8 @@
  %7B%0A%0A
-    KubernetesEnvironment kubernetesEnvironment = (KubernetesEnvironment) workspaceEnvironment;%0A
     
@@ -2539,26 +2539,25 @@
 ePods = 
-kubernetes
+workspace
 Environm
@@ -3614,34 +3614,33 @@
     %7D%0A      
-kubernetes
+workspace
 Environment%0A
@@ -3750,26 +3750,25 @@
  %7D%0A%0A    
-kubernetes
+workspace
 Environm

improve KubernetesBrokerInitContainerApplier