Search in sources :

Example 66 with RoleBinding

use of io.fabric8.kubernetes.api.model.rbac.RoleBinding in project devspaces-images by redhat-developer.

the class OpenShiftProject method prepare.

/**
 * Prepare a project for using.
 *
 * <p>Preparing includes creating if needed and waiting for default service account.
 *
 * @param canCreate defines what to do when the project is not found. The project is created when
 *     {@code true}, otherwise an exception is thrown.
 * @param initWithCheServerSa defines condition whether the project should be created with Che
 *     Server SA.
 * @throws InfrastructureException if any exception occurs during project preparation or if the
 *     project doesn't exist and {@code canCreate} is {@code false}.
 */
void prepare(boolean canCreate, boolean initWithCheServerSa, Map<String, String> labels, Map<String, String> annotations) throws InfrastructureException {
    String workspaceId = getWorkspaceId();
    String projectName = getName();
    KubernetesClient kubeClient = clientFactory.create(workspaceId);
    OpenShiftClient osClient = clientFactory.createOC(workspaceId);
    Project project = get(projectName, osClient);
    if (project == null) {
        if (!canCreate) {
            throw new InfrastructureException(format("Creating the namespace '%s' is not allowed, yet" + " it was not found.", projectName));
        }
        if (initWithCheServerSa) {
            OpenShiftClient openshiftClient = cheServerOpenshiftClientFactory.createOC();
            create(projectName, openshiftClient);
            waitDefaultServiceAccount(projectName, openshiftClient);
            openshiftClient.roleBindings().inNamespace(projectName).createOrReplace(new RoleBindingBuilder().withNewMetadata().withName("admin").endMetadata().addToUserNames(osClient.currentUser().getMetadata().getName()).withNewRoleRef().withApiVersion("rbac.authorization.k8s.io").withKind("RoleBinding").withName("admin").endRoleRef().build());
        } else {
            create(projectName, osClient);
            waitDefaultServiceAccount(projectName, kubeClient);
        }
    }
    label(osClient.namespaces().withName(projectName).get(), labels);
    annotate(osClient.namespaces().withName(projectName).get(), annotations);
}
Also used : Project(io.fabric8.openshift.api.model.Project) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) RoleBindingBuilder(io.fabric8.openshift.api.model.RoleBindingBuilder) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 67 with RoleBinding

use of io.fabric8.kubernetes.api.model.rbac.RoleBinding in project devspaces-images by redhat-developer.

the class OpenShiftStopWorkspaceRoleConfigurator method configure.

@Override
public void configure(NamespaceResolutionContext namespaceResolutionContext, String projectName) throws InfrastructureException {
    if (isNullOrEmpty(oAuthIdentityProvider)) {
        return;
    }
    if (stopWorkspaceRoleEnabled && installationLocation != null) {
        OpenShiftClient osClient = clientFactory.createOC();
        String stopWorkspacesRoleName = "workspace-stop";
        if (osClient.roles().inNamespace(projectName).withName(stopWorkspacesRoleName).get() == null) {
            osClient.roles().inNamespace(projectName).createOrReplace(createStopWorkspacesRole(stopWorkspacesRoleName));
        }
        osClient.roleBindings().inNamespace(projectName).createOrReplace(createStopWorkspacesRoleBinding(projectName));
    } else {
        LOG.warn("Stop workspace Role and RoleBinding will not be provisioned to the '{}' namespace. 'che.workspace.stop.role.enabled' property is set to '{}'", installationLocation, stopWorkspaceRoleEnabled);
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient)

Aggregations

RoleBinding (io.fabric8.kubernetes.api.model.rbac.RoleBinding)33 RoleRefBuilder (io.fabric8.kubernetes.api.model.rbac.RoleRefBuilder)20 RoleBinding (io.fabric8.openshift.api.model.RoleBinding)19 Test (org.junit.jupiter.api.Test)19 RoleBindingBuilder (io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder)16 RoleRef (io.fabric8.kubernetes.api.model.rbac.RoleRef)16 SubjectBuilder (io.fabric8.kubernetes.api.model.rbac.SubjectBuilder)14 RoleBindingBuilder (io.fabric8.openshift.api.model.RoleBindingBuilder)13 Role (io.fabric8.kubernetes.api.model.rbac.Role)10 Subject (io.fabric8.kubernetes.api.model.rbac.Subject)10 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)10 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)10 List (java.util.List)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)6 Kafka (io.strimzi.api.kafka.model.Kafka)6 KafkaBuilder (io.strimzi.api.kafka.model.KafkaBuilder)6 CertManager (io.strimzi.certs.CertManager)6 KubernetesVersion (io.strimzi.operator.KubernetesVersion)6 PlatformFeaturesAvailability (io.strimzi.operator.PlatformFeaturesAvailability)6