KubernetesNamespaceFactory { protected String getNamespaceName(Workspace workspace) throws InfrastructureException { String namespace = workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE); if (namespace == null) { NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext( workspace.getId(), EnvironmentContext.getCurrent().getSubject().getUserId(), EnvironmentContext.getCurrent().getSubject().getUserName()); namespace = evaluateLegacyNamespaceName(resolutionCtx); LOG.warn( "Workspace '{}' doesn't have an explicit namespace assigned." + " The legacy namespace resolution resolved it to '{}'.", workspace.getId(), namespace); } if (!NamespaceNameValidator.isValid(namespace)) { Subject subj = EnvironmentContext.getCurrent().getSubject(); if (!subj.isAnonymous()) { NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext(workspace.getId(), subj.getUserId(), subj.getUserName()); String defaultNamespace = evaluateNamespaceName(resolutionCtx); LOG.warn( "The namespace '{}' of the workspace '{}' is not valid. Trying to recover" + " from this situation using a default namespace which resolved to '{}'.", namespace, workspace.getId(), defaultNamespace); namespace = defaultNamespace; } else { LOG.warn( "The namespace '{}' of the workspace '{}' is not valid but we currently don't have" + " an active user to try an recover from this situation. We're letting the parent" + " workflow continue, but it may fail at some later point in time because of" + " the incorrect namespace name in use.", namespace, workspace.getId(), new Throwable()); } } return namespace; } @Inject  KubernetesNamespaceFactory(
      @Nullable @Named("che.infra.kubernetes.namespace") String legacyNamespaceName,
      @Nullable @Named("che.infra.kubernetes.service_account_name") String serviceAccountName,
      @Nullable @Named("che.infra.kubernetes.workspace_sa_cluster_roles") String clusterRoleNames,
      @Nullable @Named("che.infra.kubernetes.namespace.default") String defaultNamespaceName,
      @Named("che.infra.kubernetes.namespace.allow_user_defined")
          boolean allowUserDefinedNamespaces,
      @Named("che.infra.kubernetes.namespace.creation_allowed") boolean namespaceCreationAllowed,
      KubernetesClientFactory clientFactory,
      UserManager userManager,
      PreferenceManager preferenceManager,
      KubernetesSharedPool sharedPool); KubernetesNamespace access(String workspaceId, String namespace); void checkIfNamespaceIsAllowed(String namespaceName); List<KubernetesNamespaceMeta> list(); KubernetesNamespace getOrCreate(RuntimeIdentity identity); KubernetesNamespace get(RuntimeIdentity identity); KubernetesNamespace get(Workspace workspace); String evaluateLegacyNamespaceName(NamespaceResolutionContext resolutionCtx); String evaluateNamespaceName(NamespaceResolutionContext resolutionCtx); void deleteIfManaged(Workspace workspace);  }
@Test public void testEvalNamespaceUsesLegacyNamespaceIfWorkspaceDoesntRecordNamespaceAndLegacyNamespaceExists() throws Exception { namespaceFactory = new KubernetesNamespaceFactory( "blabol-<userid>-<username>-<userid>-<username>", "", "", "che-<userid>", true, true, clientFactory, userManager, preferenceManager, pool); WorkspaceImpl workspace = new WorkspaceImplBuilder().build(); EnvironmentContext.getCurrent().setSubject(new SubjectImpl("jondoe", "123", null, false)); String namespace = namespaceFactory.getNamespaceName(workspace); assertEquals(namespace, "blabol-123-jondoe-123-jondoe"); }
@Test public void testEvalNamespaceUsesWorkspaceRecordedNamespaceIfWorkspaceRecordsIt() throws Exception { namespaceFactory = new KubernetesNamespaceFactory( "blabol-<userid>-<username>-<userid>-<username>", "", "", "che-<userid>", false, true, clientFactory, userManager, preferenceManager, pool); WorkspaceImpl workspace = new WorkspaceImplBuilder() .setAttributes( ImmutableMap.of( Constants.WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "wkspcnmspc")) .build(); String namespace = namespaceFactory.getNamespaceName(workspace); assertEquals(namespace, "wkspcnmspc"); }
@Test public void testEvalNamespaceTreatsWorkspaceRecordedNamespaceLiterally() throws Exception { namespaceFactory = new KubernetesNamespaceFactory( "blabol-<userid>-<username>-<userid>-<username>", "", "", "che-<userid>", false, true, clientFactory, userManager, preferenceManager, pool); WorkspaceImpl workspace = new WorkspaceImplBuilder() .setAttributes( ImmutableMap.of(Constants.WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "<userid>")) .build(); String namespace = namespaceFactory.getNamespaceName(workspace); assertEquals(namespace, "<userid>"); }