@Override public Optional<AddressSpace> getAddressSpaceWithName(String namespace, String name) { ConfigMap map = client.configMaps().withName(getConfigMapName(namespace, name)).get(); if (map == null) { return Optional.empty(); } else { return Optional.of(getAddressSpaceFromConfig(map)); } }
@Override public Set<AddressSpace> listAllAddressSpacesWithLabels(Map<String, String> labels) { labels = new LinkedHashMap<>(labels); labels.put(LabelKeys.TYPE, "address-space"); return listAddressSpacesMatching(labels); }
@Override public Set<AddressSpace> listAddressSpaces(String namespace) { return listAddressSpacesWithLabels(namespace, Collections.emptyMap()); }
AddressSpaceApi addressSpaceApi = new ConfigMapAddressSpaceApi(client);
@Override public void createAddressSpace(AddressSpace addressSpace) { String name = getConfigMapName(addressSpace.getMetadata().getNamespace(), addressSpace.getMetadata().getName()); ConfigMap map = create(addressSpace); client.configMaps().withName(name).create(map); }
@Override public boolean deleteAddressSpace(AddressSpace addressSpace) { String name = getConfigMapName(addressSpace.getMetadata().getNamespace(), addressSpace.getMetadata().getName()); Boolean deleted = client.configMaps().withName(name).delete(); return deleted != null && deleted; }
@Override public Set<AddressSpace> listAllAddressSpaces() { return listAllAddressSpacesWithLabels(Collections.emptyMap()); }
private Set<AddressSpace> listAddressSpacesMatching(Map<String, String> labels) { Set<AddressSpace> instances = new LinkedHashSet<>(); ConfigMapList list = client.configMaps().withLabels(labels).list(); for (ConfigMap map : list.getItems()) { instances.add(getAddressSpaceFromConfig(map)); } return instances; }
@Override public void start(Future<Void> startPromise) throws Exception { SchemaApi schemaApi = KubeSchemaApi.create(client, client.getNamespace(), true); CachingSchemaProvider schemaProvider = new CachingSchemaProvider(); schemaApi.watchSchema(schemaProvider, options.getResyncInterval()); ensureRouteExists(client, options); ensureCredentialsExist(client, options); AddressSpaceApi addressSpaceApi = new ConfigMapAddressSpaceApi(client); AuthApi authApi = new KubeAuthApi(client, client.getConfiguration().getOauthToken()); UserApi userApi = createUserApi(options); ConsoleProxy consoleProxy = addressSpace -> { Route route = client.routes().withName(options.getConsoleProxyRouteName()).get(); if (route == null) { return null; } return String.format("https://%s/console/%s", route.getSpec().getHost(), addressSpace.getMetadata().getName()); }; vertx.deployVerticle(new HTTPServer(addressSpaceApi, schemaProvider, authApi, options.getCertDir(), options.getEnableRbac(), userApi, options.getListenPort(), consoleProxy), result -> { if (result.succeeded()) { log.info("EnMasse Service Broker started"); startPromise.complete(); } else { startPromise.fail(result.cause()); } }); }
@Override public boolean replaceAddressSpace(AddressSpace addressSpace) { ConfigMap newMap = null; try { String name = getConfigMapName(addressSpace.getMetadata().getNamespace(), addressSpace.getMetadata().getName()); newMap = create(addressSpace); ConfigMap result; if (addressSpace.getMetadata().getResourceVersion() != null) { result = client.configMaps() .withName(name) .lockResourceVersion(addressSpace.getMetadata().getResourceVersion()) .replace(newMap); } else { result = client.configMaps() .withName(name) .replace(newMap); } cache.replace(newMap); return result != null; } catch (KubernetesClientException e) { if (e.getStatus().getCode() == 404) { return false; } else { throw e; } } }
private ConfigMap create(AddressSpace addressSpace) { Map<String, String> labels = addressSpace.getMetadata().getLabels(); if ( labels == null ) { labels = new HashMap<>(); addressSpace.getMetadata().setLabels(labels); } String name = getConfigMapName(addressSpace.getMetadata().getNamespace(), addressSpace.getMetadata().getName()); labels.put(LabelKeys.TYPE, "address-space"); labels.put(LabelKeys.NAMESPACE, addressSpace.getMetadata().getNamespace()); ConfigMapBuilder builder = new ConfigMapBuilder() .editOrNewMetadata() .withName(name) .addToLabels(labels) .addToAnnotations(addressSpace.getMetadata().getAnnotations()) .endMetadata(); if (addressSpace.getMetadata().getResourceVersion() != null) { builder.editOrNewMetadata() .withResourceVersion(addressSpace.getMetadata().getResourceVersion()) .endMetadata(); } try { // Reset resource version to avoid unneeded extra writes AddressSpace newAddressSpace = new AddressSpaceBuilder(addressSpace).editOrNewMetadata().withResourceVersion(null).endMetadata().build(); builder.addToData("config.json", mapper.writeValueAsString(newAddressSpace)); return builder.build(); } catch (IOException e) { log.info("Error serializing addressspace for {}", addressSpace, e); throw new UncheckedIOException(e); } }
AddressSpaceApi addressSpaceApi = new ConfigMapAddressSpaceApi(client);
@Override public Set<AddressSpace> listAddressSpacesWithLabels(String namespace, Map<String, String> labels) { labels = new LinkedHashMap<>(labels); labels.put(LabelKeys.TYPE, "address-space"); labels.put(LabelKeys.NAMESPACE, namespace); return listAddressSpacesMatching(labels); }
Kubernetes kubernetes = new KubernetesHelper(controllerClient.getNamespace(), controllerClient, controllerClient.getConfiguration().getOauthToken(), options.getTemplateDir(), isOpenShift); AddressSpaceApi addressSpaceApi = new ConfigMapAddressSpaceApi(controllerClient); EventLogger eventLogger = options.isEnableEventLogger() ? new KubeEventLogger(controllerClient, controllerClient.getNamespace(), Clock.systemUTC(), "enmasse-controller") : new LogEventLogger();