use of org.eclipse.jkube.kit.config.resource.ServiceAccountConfig in project jkube by eclipse.
the class ServiceAccountEnricher method create.
@Override
public void create(PlatformMode mode, KubernetesListBuilder builder) {
Map<String, String> deploymentToSaPair = new HashMap<>();
List<ServiceAccount> serviceAccounts = new ArrayList<>();
// Check XML config and see if there are any service accounts specified
ResourceConfig xmlResourceConfig = getConfiguration().getResource();
if (xmlResourceConfig != null && xmlResourceConfig.getServiceAccounts() != null) {
for (ServiceAccountConfig serviceAccountConfig : xmlResourceConfig.getServiceAccounts()) {
if (serviceAccountConfig.getName() != null) {
serviceAccounts.add(createServiceAccount(builder, serviceAccountConfig.getName()));
}
if (serviceAccountConfig.getDeploymentRef() != null) {
deploymentToSaPair.put(serviceAccountConfig.getDeploymentRef(), serviceAccountConfig.getName());
}
}
}
// If any service account is referenced in deployment spec, then
// create sa on fly.
builder.accept(new TypedVisitor<DeploymentBuilder>() {
@Override
public void visit(DeploymentBuilder deploymentBuilder) {
String serviceAccountName = getServiceAccountNameFromSpec(deploymentBuilder);
if (serviceAccountName != null && getServiceAccountFromList(builder, serviceAccountName) == null) {
serviceAccounts.add(createServiceAccount(builder, serviceAccountName));
}
if (deploymentToSaPair.containsKey(deploymentBuilder.buildMetadata().getName())) {
deploymentBuilder.editSpec().editTemplate().editSpec().withServiceAccountName(deploymentToSaPair.get(deploymentBuilder.buildMetadata().getName())).endSpec().endTemplate().endSpec();
}
}
});
builder.addAllToServiceAccountItems(serviceAccounts);
}
Aggregations