KubernetesNamespaceFactory { public String evaluateNamespaceName(NamespaceResolutionContext resolutionCtx) throws InfrastructureException { String namespace; Optional<Pair<String, String>> storedNamespace = getPreferencesNamespaceName(resolutionCtx); if (storedNamespace.isEmpty() || !isStoredTemplateValid(storedNamespace.get().second)) { namespace = evalPlaceholders(defaultNamespaceName, resolutionCtx); } else { namespace = storedNamespace.get().first; } if (!NamespaceNameValidator.isValid(namespace)) { Optional<KubernetesNamespaceMeta> namespaceMetaOptional; String normalizedNamespace = normalizeNamespaceName(namespace); if (normalizedNamespace.isEmpty()) { throw new InfrastructureException( format( "Evaluated empty namespace name for workspace %s", resolutionCtx.getWorkspaceId())); } do { normalizedNamespace = normalizedNamespace .substring(0, Math.min(55, normalizedNamespace.length())) .concat(NameGenerator.generate("-", 6)); namespaceMetaOptional = fetchNamespace(normalizedNamespace); } while (namespaceMetaOptional.isPresent()); namespace = normalizedNamespace; } LOG.debug( "Evaluated the namespace for workspace {} using the namespace default to {}", resolutionCtx.getWorkspaceId(), namespace); if (resolutionCtx.isPersistAfterCreate() && !defaultNamespaceName.contains(WORKSPACEID_PLACEHOLDER)) { recordEvaluatedNamespaceName(namespace, resolutionCtx); } 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 testEvalNamespaceUsesNamespaceFromUserPreferencesIfExist() throws Exception { namespaceFactory = new KubernetesNamespaceFactory( "blabol-<userid>-<username>-<userid>-<username>--", "", "", "che-<userid>", true, true, clientFactory, userManager, preferenceManager, pool); Map<String, String> prefs = new HashMap<>(); prefs.put(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-123"); prefs.put(NAMESPACE_TEMPLATE_ATTRIBUTE, "che-<userid>"); when(preferenceManager.find(anyString())).thenReturn(prefs); String namespace = namespaceFactory.evaluateNamespaceName( new NamespaceResolutionContext("workspace123", "user123", "jondoe")); assertEquals(namespace, "che-123"); }
@Test public void testEvalNamespaceSkipsNamespaceFromUserPreferencesIfTemplateChanged() throws Exception { namespaceFactory = new KubernetesNamespaceFactory( "blabol-<userid>-<username>-<userid>-<username>--", "", "", "che-<userid>-<username>", true, true, clientFactory, userManager, preferenceManager, pool); Map<String, String> prefs = new HashMap<>(); prefs.put(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-123"); prefs.put(NAMESPACE_TEMPLATE_ATTRIBUTE, "che-<userid>"); when(preferenceManager.find(anyString())).thenReturn(prefs); String namespace = namespaceFactory.evaluateNamespaceName( new NamespaceResolutionContext("workspace123", "user123", "jondoe")); assertEquals(namespace, "che-user123-jondoe"); }
@Test public void testEvalNamespaceSkipsNamespaceFromUserPreferencesIfUserAllowedPropertySetFalse() throws Exception { namespaceFactory = new KubernetesNamespaceFactory( "blabol-<userid>-<username>-<userid>-<username>--", "", "", "che-<userid>-<username>", false, true, clientFactory, userManager, preferenceManager, pool); Map<String, String> prefs = new HashMap<>(); prefs.put(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-123"); prefs.put(NAMESPACE_TEMPLATE_ATTRIBUTE, "che-<userid>"); when(preferenceManager.find(anyString())).thenReturn(prefs); String namespace = namespaceFactory.evaluateNamespaceName( new NamespaceResolutionContext("workspace123", "user123", "jondoe")); assertEquals(namespace, "che-user123-jondoe"); }
@Test public void testEvalNamespaceSkipsNamespaceFromUserPreferencesIfTemplateContainsWorkspaceId() throws Exception { namespaceFactory = new KubernetesNamespaceFactory( "blabol-<userid>-<username>-<userid>-<username>--", "", "", "che-<workspaceid>-<username>", true, true, clientFactory, userManager, preferenceManager, pool); Map<String, String> prefs = new HashMap<>(); prefs.put(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-123"); prefs.put(NAMESPACE_TEMPLATE_ATTRIBUTE, "che-<workspaceid>-<username>"); String namespace = namespaceFactory.evaluateNamespaceName( new NamespaceResolutionContext("workspace123", "user123", "jondoe")); assertEquals(namespace, "che-workspace123-jondoe"); }