private void updateConfigMap(ConfigMap oldConfigMap, ConfigMap newConfigMap) { kubernetesClient.configMaps() .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace())) .withName(leaderProperties.getConfigMapName()) .lockResourceVersion(oldConfigMap.getMetadata().getResourceVersion()) .replace(newConfigMap); } }
public static void main(String[] args) throws InterruptedException { Config config = new ConfigBuilder().build(); KubernetesClient client = new DefaultKubernetesClient(config); String namespace = null; if (args.length > 0) { namespace = args[0]; } if (namespace == null) { namespace = client.getNamespace(); } if (namespace == null) { namespace = "default"; } String name = "cheese"; try { Resource<ConfigMap, DoneableConfigMap> configMapResource = client.configMaps().inNamespace(namespace).withName(name); ConfigMap configMap = configMapResource.createOrReplace(new ConfigMapBuilder(). withNewMetadata().withName(name).endMetadata(). addToData("foo", "" + new Date()). addToData("bar", "beer"). build()); log("Upserted ConfigMap at " + configMap.getMetadata().getSelfLink() + " data " + configMap.getData()); } finally { client.close(); } }
public ConfigMap getConfigMap(String namespace, String configMapName) { return getAllConfigMaps(namespace).getItems().stream() .filter(configMap -> configMap.getMetadata().getName().equals(configMapName)) .findFirst().get(); }
public void collectConfigMaps(String operation) { log.info("Collecting configmaps for namespace {}", namespace); kubernetes.getAllConfigMaps(namespace).getItems().forEach(configMap -> { try { Path confMapFile = resolveLogFile(configMap.getMetadata().getName() + "." + operation + ".configmap"); log.info("config map '{}' will be archived with path: '{}'", configMap.getMetadata().getName(), confMapFile); if (!Files.exists(confMapFile)) { try (BufferedWriter bf = Files.newBufferedWriter(confMapFile)) { bf.write(configMap.toString()); } } } catch (IOException e) { throw new UncheckedIOException(e); } }); }
@Override protected Future<ReconcileResult<ConfigMap>> internalPatch(String namespace, String name, ConfigMap current, ConfigMap desired) { try { if (compareObjects(current.getData(), desired.getData()) && compareObjects(current.getMetadata().getName(), desired.getMetadata().getName()) && compareObjects(current.getMetadata().getNamespace(), desired.getMetadata().getNamespace()) && compareObjects(current.getMetadata().getAnnotations(), desired.getMetadata().getAnnotations()) && compareObjects(current.getMetadata().getLabels(), desired.getMetadata().getLabels())) { // Checking some metadata. We cannot check entire metadata object because it contains // timestamps which would cause restarting loop log.debug("{} {} in namespace {} has not been patched because resources are equal", resourceKind, name, namespace); return Future.succeededFuture(ReconcileResult.noop(current)); } else { return super.internalPatch(namespace, name, current, desired); } } catch (Exception e) { log.error("Caught exception while patching {} {} in namespace {}", resourceKind, name, namespace, e); return Future.failedFuture(e); } }
private void deleteConfig(ConfigMap map) { String pid = map.getMetadata().getLabels().get(pidLabel); String[] p = parsePid(pid); try { Map<String, String> configMapData = map.getData(); Configuration config = getConfiguration(configAdmin.get(), pid, p[0], p[1]); if (configMapData != null) { boolean merge = configMapData.containsKey(FABRIC8_CONFIG_MERGE) ? Boolean.valueOf(configMapData.get(FABRIC8_CONFIG_MERGE)) : configMerge; if (!merge) { LOGGER.debug("Delete configuration {}", config.getPid()); config.delete(); } } } catch (Exception e) { LOGGER.warn("", e); } }
private void updateConfig(ConfigMap map) { Long ver = Long.parseLong(map.getMetadata().getResourceVersion()); String pid = map.getMetadata().getLabels().get(pidLabel); String[] p = parsePid(pid); configMapCfg.put(FABRIC8_PID, pid); configMapCfg.put(FABRIC8_K8S_META_RESOURCE_VERSION, ver); configMapCfg.put(FABRIC8_K8S_META_NAME, map.getMetadata().getName()); configMapCfg.put(FABRIC8_K8S_META_NAMESPACE, map.getMetadata().getNamespace());
@Override ConfigMap applyResource(ConfigMap original, ConfigMap current) { return client .configMaps() .inNamespace(getNamespace()) .withName(current.getMetadata().getName()) .edit() .withMetadata(current.getMetadata()) .withData(current.getData()) .done(); }
@Override protected Topic topicBuilder(ConfigMap cm) { try { return new Topic(getProperty(cm.getData(), TOPIC_NAME_KEY, cm.getMetadata().getName()), getProperty(cm.getData(), PARTITIONS_KEY, 0), getProperty(cm.getData(), REPLICATION_FACTOR_KEY, (short) 0), Config.propertiesFromString(getProperty(cm.getData(), PROPERTIES_KEY, "")), getProperty(cm.getData(), ACL_KEY, false)); } catch (IOException e) { log.error("Unable to parse properties from ConfigMap {}", cm.getMetadata().getName(), e); return null; } }
private AddressSpace getAddressSpaceFromConfig(ConfigMap map) { try { AddressSpace addressSpace = mapper.readValue(map.getData().get("config.json"), AddressSpace.class); AddressSpaceBuilder builder = new AddressSpaceBuilder(addressSpace); MetadataNested<AddressSpaceBuilder> metadataBuilder = builder.editOrNewMetadata(); if (addressSpace.getMetadata().getUid() == null) { metadataBuilder.withUid(map.getMetadata().getUid()); } metadataBuilder.withResourceVersion(map.getMetadata().getResourceVersion()); if (addressSpace.getMetadata().getCreationTimestamp() == null) { metadataBuilder.withCreationTimestamp(map.getMetadata().getCreationTimestamp()); } if (addressSpace.getMetadata().getSelfLink() == null) { metadataBuilder.withSelfLink("/apis/enmasse.io/v1beta1/namespaces/" + addressSpace.getMetadata().getNamespace() + "/addressspaces/" + addressSpace.getMetadata().getName()); } // commit changes metadataBuilder.endMetadata(); return builder.build(); } catch (IOException e) { log.error("Error decoding address space from configmap : {}", map, e); throw new UncheckedIOException(e); } }
private Address getAddressFromConfig(ConfigMap map) { Map<String, String> data = map.getData(); try { Address address = mapper.readValue(data.get("config.json"), Address.class); AddressBuilder builder = new AddressBuilder(address); MetadataNested<AddressBuilder> metadataBuilder = builder.editOrNewMetadata(); if (address.getMetadata().getUid() == null) { metadataBuilder.withUid(map.getMetadata().getUid()); } metadataBuilder.withResourceVersion(map.getMetadata().getResourceVersion()); if (address.getMetadata().getCreationTimestamp() == null) { metadataBuilder.withCreationTimestamp(map.getMetadata().getCreationTimestamp()); } if (address.getMetadata().getSelfLink() == null) { metadataBuilder.withSelfLink("/apis/enmasse.io/v1beta1/namespaces/" + address.getMetadata().getNamespace() + "/addressspaces/" + Address.extractAddressSpace(address)); } // commit changes to metadata metadataBuilder.endMetadata(); return builder.build(); } catch (IOException e) { log.error("Error decoding address from configmap : {}", map, e); throw new UncheckedIOException(e); } }
public ConfigMapFluentImpl(ConfigMap instance){ this.withApiVersion(instance.getApiVersion()); this.withData(instance.getData()); this.withKind(instance.getKind()); this.withMetadata(instance.getMetadata()); }
public ConfigMapBuilder(ConfigMap instance,Boolean validationEnabled){ this.fluent = this; this.withApiVersion(instance.getApiVersion()); this.withData(instance.getData()); this.withKind(instance.getKind()); this.withMetadata(instance.getMetadata()); this.validationEnabled = validationEnabled; }
public ConfigMapBuilder(ConfigMapFluent<?> fluent,ConfigMap instance,Boolean validationEnabled){ this.fluent = fluent; fluent.withApiVersion(instance.getApiVersion()); fluent.withData(instance.getData()); fluent.withKind(instance.getKind()); fluent.withMetadata(instance.getMetadata()); this.validationEnabled = validationEnabled; } public ConfigMapBuilder(ConfigMap instance){
.inNamespace(this.lockConfiguration.getKubernetesResourcesNamespaceOrDefault(kubernetesClient)) .withName(this.lockConfiguration.getConfigMapName()) .lockResourceVersion(configMap.getMetadata().getResourceVersion()) .replace(updatedConfigMap);
} else if (object instanceof ConfigMap) { ConfigMap configMap = (ConfigMap) object; configMaps.put(configMap.getMetadata().getName(), configMap); } else { throw new ValidationException(