com.instaclustr.kubernetes.SecretReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
Common classes and utilities integrated with various projects
package com.instaclustr.kubernetes;
import static com.instaclustr.kubernetes.KubernetesSecretsReader.readNamespace;
import static java.lang.String.format;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import com.google.inject.Provider;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1Secret;
import io.kubernetes.client.models.V1SecretList;
public class SecretReader {
private final Provider coreV1ApiProvider;
public SecretReader(final Provider coreV1ApiProvider) {
this.coreV1ApiProvider = Objects.requireNonNull(coreV1ApiProvider);
}
public Optional read(final String secretName, final String key) throws Exception {
return read(readNamespace(), secretName, key);
}
public Optional read(final String namespace, final String secretName, final String key) throws Exception {
return readIntoObject(namespace, secretName, v1Secret -> Optional.ofNullable(v1Secret.getData().get(key)));
}
public T readIntoObject(final String namespace, final String secretName, final Function mappingFunction) throws Exception {
final V1SecretList v1SecretList = coreV1ApiProvider.get().listNamespacedSecret(namespace, null, null, null, null, null, null, null, null);
for (final V1Secret v1Secret : v1SecretList.getItems()) {
if (v1Secret.getMetadata().getName().equals(secretName)) {
return mappingFunction.apply(v1Secret);
}
}
throw new IllegalStateException(format("Secret with name %s was not found in namespace %s", secretName, namespace));
}
}