static void upsertSecret(AccountDeploymentDetails<KubernetesAccount> details, Set<Pair<File, String>> files, String secretName, String namespace) { KubernetesClient client = getClient(details); if (client.secrets().inNamespace(namespace).withName(secretName).get() != null) { client.secrets().inNamespace(namespace).withName(secretName).delete(); } Map<String, String> secretContents = new HashMap<>(); files.forEach(pair -> { try { File file = pair.getLeft(); String name = pair.getRight(); String data = new String(Base64.getEncoder().encode(IOUtils.toByteArray(new FileInputStream(file)))); secretContents.putIfAbsent(name, data); } catch (IOException e) { throw new HalException(Severity.ERROR, "Unable to read contents of \"" + pair.getLeft() + "\": " + e); } }); SecretBuilder secretBuilder = new SecretBuilder(); secretBuilder = secretBuilder.withNewMetadata() .withName(secretName) .withNamespace(namespace) .endMetadata() .withData(secretContents); client.secrets().inNamespace(namespace).create(secretBuilder.build()); }
static void upsertSecret(AccountDeploymentDetails<KubernetesAccount> details, Set<Pair<File, String>> files, String secretName, String namespace) { KubernetesClient client = getClient(details); if (client.secrets().inNamespace(namespace).withName(secretName).get() != null) { client.secrets().inNamespace(namespace).withName(secretName).delete(); } Map<String, String> secretContents = new HashMap<>(); files.forEach(pair -> { try { File file = pair.getLeft(); String name = pair.getRight(); String data = new String(Base64.getEncoder().encode(IOUtils.toByteArray(new FileInputStream(file)))); secretContents.putIfAbsent(name, data); } catch (IOException e) { throw new HalException(Severity.ERROR, "Unable to read contents of \"" + pair.getLeft() + "\": " + e); } }); SecretBuilder secretBuilder = new SecretBuilder(); secretBuilder = secretBuilder.withNewMetadata() .withName(secretName) .withNamespace(namespace) .endMetadata() .withData(secretContents); client.secrets().inNamespace(namespace).create(secretBuilder.build()); }
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; }
/** * 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; }
.endMetadata() .withType(KUBERNETES_DOCKERCFG) .withData(ImmutableMap.of(".dockercfg", base64encoded)) .build(); try {
@Override protected Secret resource() { return new SecretBuilder() .withNewMetadata() .withName(RESOURCE_NAME) .withNamespace(NAMESPACE) .withLabels(singletonMap("foo", "bar")) .endMetadata() .withData(singletonMap("FOO", "BAR")) .build(); }
Secret secret = new SecretBuilder().withData(data).withMetadata(metadata).withType(type).build(); builder.addToSecretItems(i, secret);
public SecretBuilder( Secret instance ){ this.fluent = this; this.withApiVersion(instance.getApiVersion()); this.withData(instance.getData()); this.withKind(instance.getKind()); this.withMetadata(instance.getMetadata()); this.withType(instance.getType()); }
public SecretBuilder( Secret instance ){ this.fluent = this; this.withApiVersion(instance.getApiVersion()); this.withData(instance.getData()); this.withKind(instance.getKind()); this.withMetadata(instance.getMetadata()); this.withType(instance.getType()); }
/** * 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; }
.endMetadata() .withType("kubernetes.io/tls") .withData(data) .build();
public SecretBuilder(Secret instance,Boolean validationEnabled){ this.fluent = this; this.withApiVersion(instance.getApiVersion()); this.withData(instance.getData()); this.withKind(instance.getKind()); this.withMetadata(instance.getMetadata()); this.withStringData(instance.getStringData()); this.withType(instance.getType()); this.validationEnabled = validationEnabled; }
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(); } }
.withNamespace(kubernetesNamespace) .endMetadata() .withData(data) .withType("kubernetes.io/dockercfg") .build();
.withAnnotations(annotations) .endMetadata() .withData(keys) .build();
private void generate(SecretModel secretModel) throws KubernetesPluginException { Secret secret = new SecretBuilder() .withNewMetadata() .withNamespace(dataHolder.getNamespace()) .withName(secretModel.getName()) .endMetadata() .withData(secretModel.getData()) .build(); try { String secretContent = SerializationUtils.dumpWithoutRuntimeStateAsYaml(secret); KubernetesUtils.writeToFile(secretContent, SECRET_FILE_POSTFIX + YAML); } catch (IOException e) { String errorMessage = "Error while generating yaml file for secret: " + secretModel.getName(); throw new KubernetesPluginException(errorMessage, e); } }