Search in sources :

Example 1 with ServiceAccountConfig

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);
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServiceAccountConfig(org.eclipse.jkube.kit.config.resource.ServiceAccountConfig) ResourceConfig(org.eclipse.jkube.kit.config.resource.ResourceConfig) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Aggregations

ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)1 DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ResourceConfig (org.eclipse.jkube.kit.config.resource.ResourceConfig)1 ServiceAccountConfig (org.eclipse.jkube.kit.config.resource.ServiceAccountConfig)1