KubernetesEnvironmentProvisioner { public void provision( WorkspaceConfigImpl workspaceConfig, String environmentType, List<HasMetadata> componentObjects, Map<String, MachineConfigImpl> machines) throws DevfileException, DevfileRecipeFormatException { String defaultEnv = workspaceConfig.getDefaultEnv(); EnvironmentImpl environment = workspaceConfig.getEnvironments().get(defaultEnv); if (environment == null) { checkItemsHasUniqueKindToName(componentObjects); RecipeImpl recipe = new RecipeImpl(environmentType, YAML_CONTENT_TYPE, asYaml(componentObjects), null); String envName = "default"; EnvironmentImpl env = new EnvironmentImpl(recipe, emptyMap()); env.getMachines().putAll(machines); workspaceConfig.getEnvironments().put(envName, env); workspaceConfig.setDefaultEnv(envName); } else { RecipeImpl envRecipe = environment.getRecipe(); for (Entry<String, MachineConfigImpl> machineEntry : machines.entrySet()) { if (environment.getMachines().put(machineEntry.getKey(), machineEntry.getValue()) != null) { throw new DevfileException( format("Environment already contains machine '%s'", machineEntry.getKey())); } } environment.getMachines().putAll(machines); Set<String> allowedEnvTypeBases = allowedEnvironmentTypeUpgrades.get(environmentType); if (allowedEnvTypeBases != null) { envRecipe.setType(environmentType); } List<HasMetadata> envObjects = unmarshalObjects(envRecipe); mergeProjectsPVC(envObjects, componentObjects); envObjects.addAll(componentObjects); checkItemsHasUniqueKindToName(envObjects); envRecipe.setContent(asYaml(envObjects)); } } @Inject  KubernetesEnvironmentProvisioner(
      KubernetesRecipeParser objectsParser,
      @Named(ALLOWED_ENVIRONMENT_TYPE_UPGRADES_KEY_NAME)
          Map<String, Set<String>> allowedEnvironmentTypeUpgrades,
      @Named(KUBERNETES_BASED_ENVIRONMENTS_KEY_NAME) Set<String> k8sBasedEnvTypes); void provision(
      WorkspaceConfigImpl workspaceConfig,
      String environmentType,
      List<HasMetadata> componentObjects,
      Map<String, MachineConfigImpl> machines);  }
@Test public void shouldProvisionEnvironmentWithCorrectRecipeTypeAndContentFromK8SList() throws Exception { String yamlRecipeContent = getResource("devfile/petclinic.yaml"); List<HasMetadata> componentsObjects = toK8SList(yamlRecipeContent).getItems(); k8sEnvProvisioner.provision( workspaceConfig, KubernetesEnvironment.TYPE, componentsObjects, emptyMap()); String defaultEnv = workspaceConfig.getDefaultEnv(); assertNotNull(defaultEnv); EnvironmentImpl environment = workspaceConfig.getEnvironments().get(defaultEnv); assertNotNull(environment); RecipeImpl recipe = environment.getRecipe(); assertNotNull(recipe); assertEquals(recipe.getType(), KUBERNETES_COMPONENT_TYPE); assertEquals(recipe.getContentType(), YAML_CONTENT_TYPE); KubernetesList expectedKubernetesList = new KubernetesListBuilder().withItems(toK8SList(yamlRecipeContent).getItems()).build(); assertEquals(toK8SList(recipe.getContent()).getItems(), expectedKubernetesList.getItems()); }
@Test( expectedExceptions = DevfileException.class, expectedExceptionsMessageRegExp = "Environment already contains machine 'machine'") public void shouldThrowAnExceptionIfEnvironmentContainMachineWithSpecifiedName() throws Exception { workspaceConfig.setDefaultEnv("default"); workspaceConfig.setEnvironments( ImmutableMap.of( "default", new EnvironmentImpl(null, ImmutableMap.of("machine", new MachineConfigImpl())))); k8sEnvProvisioner.provision( workspaceConfig, KubernetesEnvironment.TYPE, emptyList(), ImmutableMap.of("machine", new MachineConfigImpl())); }
@Test public void shouldUpgradeKubernetesEnvironmentToOpenShiftTypeOnOpenShiftComponentProvisioning() throws Exception { workspaceConfig.setDefaultEnv("default"); RecipeImpl existingRecipe = new RecipeImpl(KubernetesEnvironment.TYPE, "yaml", "existing-content", null); workspaceConfig .getEnvironments() .put("default", new EnvironmentImpl(existingRecipe, emptyMap())); List<HasMetadata> componentsObject = new ArrayList<>(); Deployment componentDeployment = new DeploymentBuilder() .withNewMetadata() .withName("web-app") .endMetadata() .withNewSpec() .endSpec() .build(); componentsObject.add(new DeploymentBuilder(componentDeployment).build()); doReturn(new ArrayList<>()).when(k8sRecipeParser).parse(anyString()); k8sEnvProvisioner.provision(workspaceConfig, "openshift", componentsObject, emptyMap()); EnvironmentImpl resultEnv = workspaceConfig.getEnvironments().get(workspaceConfig.getDefaultEnv()); RecipeImpl resultRecipe = resultEnv.getRecipe(); assertEquals(resultRecipe.getType(), "openshift"); }
@Test public void shouldProvisionComponentObjectsIntoExistingKubernetesRecipe() throws Exception { workspaceConfig.setDefaultEnv("default"); RecipeImpl existingRecipe = new RecipeImpl(KUBERNETES_COMPONENT_TYPE, "yaml", "existing-content", null); workspaceConfig .getEnvironments() .put("default", new EnvironmentImpl(existingRecipe, emptyMap())); List<HasMetadata> recipeObjects = new ArrayList<>(); Deployment recipeDeployment = new DeploymentBuilder() .withNewMetadata() .withName("db") .endMetadata() .withNewSpec() .endSpec() .build(); recipeObjects.add(new DeploymentBuilder(recipeDeployment).build()); doReturn(recipeObjects).when(k8sRecipeParser).parse(anyString()); List<HasMetadata> componentsObject = new ArrayList<>(); Deployment componentDeployment = new DeploymentBuilder() .withNewMetadata() .withName("web-app") .endMetadata() .withNewSpec() .endSpec() .build(); componentsObject.add(new DeploymentBuilder(componentDeployment).build()); k8sEnvProvisioner.provision( workspaceConfig, KubernetesEnvironment.TYPE, componentsObject, emptyMap()); KubernetesList expectedKubernetesList = new KubernetesListBuilder() .withItems(Arrays.asList(recipeDeployment, componentDeployment)) .build(); EnvironmentImpl resultEnv = workspaceConfig.getEnvironments().get(workspaceConfig.getDefaultEnv()); assertEquals( toK8SList(resultEnv.getRecipe().getContent()).getItems(), expectedKubernetesList.getItems()); }
@Test( expectedExceptions = DevfileException.class, expectedExceptionsMessageRegExp = "Kubernetes component can only be applied to a workspace with any of kubernetes or openshift " + "recipe type but workspace has a recipe of type 'any'") public void shouldThrowAnExceptionIfWorkspaceAlreadyContainNonK8sNorOSRecipe() throws Exception { workspaceConfig.setDefaultEnv("default"); RecipeImpl existingRecipe = new RecipeImpl("any", "yaml", "existing-content", null); workspaceConfig .getEnvironments() .put("default", new EnvironmentImpl(existingRecipe, emptyMap())); List<HasMetadata> componentsObject = new ArrayList<>(); Deployment componentDeployment = new DeploymentBuilder() .withNewMetadata() .withName("db") .endMetadata() .withNewSpec() .endSpec() .build(); componentsObject.add(new DeploymentBuilder(componentDeployment).build()); k8sEnvProvisioner.provision( workspaceConfig, KubernetesEnvironment.TYPE, componentsObject, emptyMap()); }
@Test( expectedExceptions = DevfileFormatException.class, expectedExceptionsMessageRegExp = "Components can not have objects with the same name and kind " + "but there are multiple objects with kind 'Service' and name 'db'") public void shouldThrowExceptionIfDifferentComponentsHaveObjectsWithTheSameKindAndName() throws Exception { List<HasMetadata> componentsObject = new ArrayList<>(); Service service1 = new ServiceBuilder() .withNewMetadata() .withName("db") .endMetadata() .withNewSpec() .endSpec() .build(); Service service2 = new ServiceBuilder() .withNewMetadata() .withName("db") .endMetadata() .withNewSpec() .endSpec() .build(); componentsObject.add(new ServiceBuilder(service1).build()); componentsObject.add(new ServiceBuilder(service2).build()); k8sEnvProvisioner.provision( workspaceConfig, KubernetesEnvironment.TYPE, componentsObject, emptyMap()); }
@Test( expectedExceptions = DevfileFormatException.class, expectedExceptionsMessageRegExp = "Components can not have objects with the same name and kind but there are multiple objects with kind 'Service' and name 'db'") public void shouldThrowExceptionIfComponentHasMultipleObjectsWithTheSameKindAndName() throws Exception { List<HasMetadata> objects = new ArrayList<>(); Service service = new ServiceBuilder() .withNewMetadata() .withName("db") .endMetadata() .withNewSpec() .endSpec() .build(); objects.add(new ServiceBuilder(service).build()); objects.add(new ServiceBuilder(service).build()); k8sEnvProvisioner.provision(workspaceConfig, KubernetesEnvironment.TYPE, objects, emptyMap()); }