private Secret createSecretFromCertAndKeyFiles(final String secretName, final Map<String, String> secretLabels, final String keyKey, final String certKey, final File keyFile, final File certFile, final OpenShiftClient client) throws IOException { Map<String, String> data = new LinkedHashMap<>(); Base64.Encoder encoder = Base64.getEncoder(); data.put(keyKey, encoder.encodeToString(FileUtils.readFileToByteArray(keyFile))); data.put(certKey, encoder.encodeToString(FileUtils.readFileToByteArray(certFile))); return client.secrets().inNamespace(namespace).withName(secretName).createOrReplaceWithNew() .editOrNewMetadata() .withName(secretName) .withLabels(secretLabels) .endMetadata() .addToData(data) .done(); }
@Override public Secret createSecret(Cert cert, Secret caSecret, Map<String, String> labels) { try { Map<String, String> data = new LinkedHashMap<>(); Base64.Encoder encoder = Base64.getEncoder(); data.put("tls.key", encoder.encodeToString(FileUtils.readFileToByteArray(cert.getKeyFile()))); data.put("tls.crt", encoder.encodeToString(FileUtils.readFileToByteArray(cert.getCertFile()))); data.put("ca.crt", caSecret.getData().get("tls.crt")); return client.secrets().inNamespace(namespace).createNew() .editOrNewMetadata() .withName(cert.getComponent().getSecretName()) .withLabels(labels) .endMetadata() .withType("kubernetes.io/tls") .addToData(data) .done(); } catch (IOException e) { throw new UncheckedIOException(e); } }
private Map.Entry<String, String> allocateUser(HasMetadata deployment) { Secret usernamePoolSecret = kubeClient().secrets().withName(usernamePoolSecretName).get(); Secret consumedUsersSecret = kubeClient().secrets().withName(consumedUsersSecretName).get(); Map<String, String> usernamePool = decodeMap(usernamePoolSecret.getData().get("username-pool")); Collection<String> consumedUsernames = decodeList(consumedUsersSecret.getData().get("consumed-usernames")); // How much of the pool is used userPoolAvailable = ((usernamePool.size() - consumedUsernames.size()) * 100) / usernamePool.size(); // Remove all consumed usernames consumedUsernames.forEach(k -> usernamePool.remove(k)); if (usernamePool.isEmpty()) { throw new IllegalStateException("Username pool is exhausted. Please check Secret " + usernamePoolSecretName + " and " + consumedUsersSecretName); } // Take first element Map.Entry<String, String> pair = usernamePool.entrySet().iterator().next(); consumedUsernames.add(pair.getKey()); Secret updatedSecret = new SecretBuilder() .withNewMetadata() .withAnnotations(consumedUsersSecret.getMetadata().getAnnotations()) .withLabels(consumedUsersSecret.getMetadata().getLabels()) .withName(consumedUsersSecret.getMetadata().getName()) .endMetadata() .withData(null) .withStringData(Collections.singletonMap("consumed-usernames", consumedUsernames.stream().collect(Collectors.joining("\n")))).build(); // TODO handle failure case kubeClient().secrets().createOrReplace(updatedSecret); return pair; }
Secret topicSecret = new SecretBuilder().withNewMetadata() .withName(secretName) .withLabels(labels) .withAnnotations(annotations) .endMetadata()
/** * Create a Kubernetes secret containing the provided secret data section * * @param namespace Namespace * @param name Secret name * @param data Map with secret data / files * @param labels Labels to add to the Secret * @param annotations annotations to add to the Secret * @param ownerReference owner of the Secret * @return the Secret */ public Secret createSecret(String namespace, String name, Map<String, String> data, Map<String, String> labels, Map<String, String> annotations, OwnerReference ownerReference) { List<OwnerReference> or = ownerReference != null ? singletonList(ownerReference) : emptyList(); Secret secret = new SecretBuilder() .withNewMetadata() .withName(name) .withNamespace(namespace) .withLabels(labels) .withAnnotations(annotations) .withOwnerReferences(or) .endMetadata() .withData(data) .build(); return secret; }
public static Secret createSecret(String name, String namespace, Labels labels, OwnerReference ownerReference, Map<String, String> data) { if (ownerReference == null) { return new SecretBuilder() .withNewMetadata() .withName(name) .withNamespace(namespace) .withLabels(labels.toMap()) .endMetadata() .withData(data).build(); } else { return new SecretBuilder() .withNewMetadata() .withName(name) .withOwnerReferences(ownerReference) .withNamespace(namespace) .withLabels(labels.toMap()) .endMetadata() .withData(data).build(); } }
/** * Creates secret with the data * @param data Map with the Secret content * @return */ protected Secret createSecret(Map<String, String> data) { Secret s = new SecretBuilder() .withNewMetadata() .withName(getSecretName()) .withNamespace(namespace) .withLabels(labels.toMap()) .endMetadata() .withData(data) .build(); return s; }
@Override protected Secret resource() { return new SecretBuilder() .withNewMetadata() .withName(RESOURCE_NAME) .withNamespace(NAMESPACE) .withLabels(singletonMap("foo", "bar")) .endMetadata() .withData(singletonMap("FOO", "BAR")) .build(); }