io.fabric8.openshift.api.model.ProjectRequest Java Examples

The following examples show how to use io.fabric8.openshift.api.model.ProjectRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserImpersonationIT.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
public void should_be_able_to_create_a_project_impersonating_service_account() {
  RequestConfig requestConfig = new RequestConfigBuilder()
  .withImpersonateUsername(SERVICE_ACCOUNT)
  .withImpersonateGroups("system:authenticated", "system:authenticated:oauth")
  .withImpersonateExtras(Collections.singletonMap("scopes", Collections.singletonList("development")))
  .build();

  // Create a project
  ProjectRequest projectRequest = client.withRequestConfig(requestConfig).call(c -> c.projectrequests().createNew()
    .withNewMetadata()
    .withName(NEW_PROJECT)
    .endMetadata()
    .done());

  // Grab the requester annotation
  String requester = projectRequest.getMetadata().getAnnotations().get("openshift.io/requester");
  assertThat(requester).isEqualTo(SERVICE_ACCOUNT);
}
 
Example #2
Source File: NewProjectExamples.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
  String master = "https://localhost:8443/";
  if (args.length == 1) {
    master = args[0];
  }

  Config config = new ConfigBuilder().withMasterUrl(master).build();

  try (OpenShiftClient client = new DefaultOpenShiftClient(config)) {
    ProjectRequest request = null;
    try {
      request = client.projectrequests().createNew().withNewMetadata().withName("thisisatest").endMetadata().withDescription("Jimmi").withDisplayName("Jimmi").done();
    } finally {
      if (request != null) {
        client.projects().withName(request.getMetadata().getName()).delete();
      }
    }
  }
}
 
Example #3
Source File: ProjectRequestsOperationImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public ProjectRequest create(ProjectRequest... resources) {
  try {
    if (resources.length > 1) {
      throw new IllegalArgumentException("Too many items to create.");
    } else if (resources.length == 1) {
      return handleCreate(updateApiVersion(resources[0]), ProjectRequest.class);
    } else if (getItem() == null) {
      throw new IllegalArgumentException("Nothing to create.");
    } else {
      return handleCreate(updateApiVersion(getItem()), ProjectRequest.class);
    }
  } catch (InterruptedException | ExecutionException | IOException e) {
    throw KubernetesClientException.launderThrowable(e);
  }
}
 
Example #4
Source File: ProjectRequestTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreate() {
  ProjectRequest req1 = new ProjectRequestBuilder().withApiVersion("v1").withNewMetadata().withName("req1").and().build();

 server.expect().withPath("/apis/project.openshift.io/v1/projectrequests").andReturn(201, req1).once();

  OpenShiftClient client = server.getOpenshiftClient();

  ProjectRequest result = client.projectrequests().create(req1);
  assertNotNull(result);
  assertEquals(req1, result);
}
 
Example #5
Source File: ApplyService.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns true if the ProjectRequest is created
 */
public boolean applyProjectRequest(ProjectRequest entity) {
    // Check whether project creation attempted before
    if (projectsCreated.contains(getName(entity))) {
        return false;
    }
    String namespace = getOrCreateMetadata(entity).getName();
    log.info("Using project: " + namespace);
    String name = getName(entity);
    Objects.requireNonNull(name, "No name for " + entity);
    OpenShiftClient openshiftClient = getOpenShiftClient();
    if (openshiftClient == null) {
        log.warn("Cannot check for Project " + namespace + " as not running against OpenShift!");
        return false;
    }
    boolean exists = checkNamespace(name);
    // We may want to be more fine-grained on the phase of the project
    if (!exists) {
        try {
            Object answer = openshiftClient.projectrequests().create(entity);
            // Add project to created projects
            projectsCreated.add(name);
            logGeneratedEntity("Created ProjectRequest: ", namespace, entity, answer);
            return true;
        } catch (Exception e) {
            onApplyError("Failed to create ProjectRequest: " + name + " due " + e.getMessage(), e);
        }
    }
    return false;
}
 
Example #6
Source File: ProjectRequestHandler.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public ProjectRequest create(OkHttpClient client, Config config, String namespace, ProjectRequest item) {
    return new ProjectRequestsOperationImpl(client, OpenShiftConfig.wrap(config)).create(item);
}
 
Example #7
Source File: ProjectRequestHandler.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public ProjectRequest waitUntilCondition(OkHttpClient client, Config config, String namespace, ProjectRequest item, Predicate<ProjectRequest> condition, long amount, TimeUnit timeUnit) throws InterruptedException {
  throw new UnsupportedOperationException();
}
 
Example #8
Source File: ProjectRequestHandler.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public ProjectRequest waitUntilReady(OkHttpClient client, Config config, String namespace, ProjectRequest item, long amount, TimeUnit timeUnit) throws InterruptedException {
  throw new UnsupportedOperationException();
}
 
Example #9
Source File: ProjectRequestHandler.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public Watch watch(OkHttpClient client, Config config, String namespace, ProjectRequest item, ListOptions listOptions, Watcher<ProjectRequest> watcher) {
  throw new UnsupportedOperationException();
}
 
Example #10
Source File: ProjectRequestHandler.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public Watch watch(OkHttpClient client, Config config, String namespace, ProjectRequest item, String resourceVersion, Watcher<ProjectRequest> watcher) {
  throw new UnsupportedOperationException();
}
 
Example #11
Source File: ProjectRequestHandler.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public Watch watch(OkHttpClient client, Config config, String namespace, ProjectRequest item, Watcher<ProjectRequest> watcher) {
  throw new UnsupportedOperationException();
}
 
Example #12
Source File: ProjectRequestHandler.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public Boolean delete(OkHttpClient client, Config config, String namespace, DeletionPropagation propagationPolicy, ProjectRequest item) {
  throw new UnsupportedOperationException();
}
 
Example #13
Source File: ProjectRequestHandler.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public ProjectRequestBuilder edit(ProjectRequest item) {
  return new ProjectRequestBuilder(item);
}
 
Example #14
Source File: ProjectRequestHandler.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public ProjectRequest reload(OkHttpClient client, Config config, String namespace, ProjectRequest item) {
  throw new UnsupportedOperationException();
}
 
Example #15
Source File: ProjectRequestHandler.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public ProjectRequest replace(OkHttpClient client, Config config, String namespace, ProjectRequest item) {
  throw new UnsupportedOperationException();
}
 
Example #16
Source File: ProjectRequestsOperationImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
public ProjectRequest getItem() {
  return (ProjectRequest) context.getItem();
}
 
Example #17
Source File: ProjectRequestsOperationImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public ProjectRequest create(ProjectRequest resource) {
  return create(new ProjectRequest[]{resource});
}
 
Example #18
Source File: ProjectRequestsOperationImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
private ProjectRequest updateApiVersion(ProjectRequest p) {
  if (p.getApiVersion() == null) {
    p.setApiVersion(this.apiGroupVersion);
  }
  return p;
}
 
Example #19
Source File: DeploymentConfigExamples.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
  Config config = new ConfigBuilder().build();
  KubernetesClient kubernetesClient = new DefaultKubernetesClient(config);
  OpenShiftClient client = kubernetesClient.adapt(OpenShiftClient.class);

  try {
    ProjectRequest  projectRequest = new ProjectRequestBuilder()
        .withNewMetadata()
          .withName("thisisatest")
          .addToLabels("project", "thisisatest")
        .endMetadata()
        .build();


    log("Created project", client.projectrequests().create(projectRequest));

    ServiceAccount fabric8 = new ServiceAccountBuilder().withNewMetadata().withName("fabric8").endMetadata().build();

    client.serviceAccounts().inNamespace("thisisatest").createOrReplace(fabric8);

    log("Created deployment", client.deploymentConfigs().inNamespace("thisisatest").createOrReplaceWithNew()
      .withNewMetadata()
        .withName("nginx")
      .endMetadata()
      .withNewSpec()
        .withReplicas(1)
        .addNewTrigger()
          .withType("ConfigChange")
        .endTrigger()
        .addToSelector("app", "nginx")
        .withNewTemplate()
          .withNewMetadata()
            .addToLabels("app", "nginx")
          .endMetadata()
          .withNewSpec()
            .addNewContainer()
              .withName("nginx")
              .withImage("nginx")
              .addNewPort()
                .withContainerPort(80)
              .endPort()
            .endContainer()
          .endSpec()
        .endTemplate()
      .endSpec()
      .done());


    client.deploymentConfigs().inNamespace("thisisatest").withName("nginx").scale(2, true);
    log("Created pods:", client.pods().inNamespace("thisisatest").list().getItems());
    client.deploymentConfigs().inNamespace("thisisatest").withName("nginx").delete();
    log("Pods:", client.pods().inNamespace("thisisatest").list().getItems());
    log("Replication Controllers:", client.replicationControllers().inNamespace("thisisatest").list().getItems());

    log("Done.");
  }finally {
   // client.projects().withName("thisisatest").delete();
    client.close();
  }
}
 
Example #20
Source File: Fabric8OpenShiftServiceImpl.java    From launchpad-missioncontrol with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public OpenShiftProject createProject(final String name) throws
        DuplicateProjectException,
        IllegalArgumentException {

    // Create
    final ProjectRequest projectRequest;
    try {
        projectRequest = client.projectrequests().createNew().
                withNewMetadata().
                withName(name).
                endMetadata().
                done();
    } catch (final KubernetesClientException kce) {
        // Detect if duplicate project
        if (kce.getCode() == CODE_DUPLICATE_PROJECT &&
                STATUS_REASON_DUPLICATE_PROJECT.equals(kce.getStatus().getReason())) {
            throw new DuplicateProjectException(name);
        }

        // Some other error, rethrow it
        throw kce;
    }

    // Block until exists
    int counter = 0;
    while (true) {
        counter++;
        if (projectExists(name)) {
            // We good
            break;
        }
        if (counter == 20) {
            throw new IllegalStateException("Newly-created project "
                                                    + name + " could not be found ");
        }
        log.finest("Couldn't find project " + name +
                           " after creating; waiting and trying again...");
        try {
            Thread.sleep(3000);
        } catch (final InterruptedException ie) {
            Thread.interrupted();
            throw new RuntimeException("Someone interrupted thread while finding newly-created project", ie);
        }
    }
    // Populate value object and return it
    final String roundtripDisplayName = projectRequest.getMetadata().getName();
    final OpenShiftProject project = new OpenShiftProjectImpl(roundtripDisplayName, consoleUrl.toString());

    return project;
}