io.fabric8.kubernetes.api.model.ServiceAccountBuilder Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.ServiceAccountBuilder. 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: KubernetesAppDeployerIntegrationTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeploymentServiceAccountName() {
	log.info("Testing {}...", "DeploymentServiceAccountName");

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

	this.kubernetesClient.serviceAccounts().create(deploymentServiceAccount);

	String serviceAccountName = deploymentServiceAccount.getMetadata().getName();

	KubernetesDeployerProperties deployProperties = new KubernetesDeployerProperties();
	deployProperties.setDeploymentServiceAccountName(serviceAccountName);

	ContainerFactory containerFactory = new DefaultContainerFactory(deployProperties);
	KubernetesAppDeployer appDeployer = new KubernetesAppDeployer(deployProperties, kubernetesClient,
			containerFactory);

	AppDefinition definition = new AppDefinition(randomName(), null);
	AppDeploymentRequest request = new AppDeploymentRequest(definition, testApplication());

	log.info("Deploying {}...", request.getDefinition().getName());
	String deploymentId = appDeployer.deploy(request);
	Timeout timeout = deploymentTimeout();
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));

	log.info("Undeploying {}...", deploymentId);
	timeout = undeploymentTimeout();
	appDeployer.undeploy(deploymentId);
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));

	kubernetesClient.serviceAccounts().delete(deploymentServiceAccount);
}
 
Example #2
Source File: AbstractModel.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * @return The service account.
 */
public ServiceAccount generateServiceAccount() {
    return new ServiceAccountBuilder()
            .withNewMetadata()
                .withName(getServiceAccountName())
                .withNamespace(namespace)
                .withOwnerReferences(createOwnerReference())
                .withLabels(labels.toMap())
            .endMetadata()
        .build();
}
 
Example #3
Source File: ServiceAccountOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Override
protected ServiceAccount resource() {
    return new ServiceAccountBuilder()
            .withNewMetadata()
                .withName(RESOURCE_NAME)
                .withNamespace(NAMESPACE)
                .withLabels(singletonMap("foo", "bar"))
            .endMetadata()
        .build();
}
 
Example #4
Source File: ServiceAccountEnricher.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
private ServiceAccount createServiceAccount(KubernetesListBuilder builder, String serviceAccountName) {
    return new ServiceAccountBuilder()
            .withNewMetadata().withName(serviceAccountName).endMetadata()
            .build();
}
 
Example #5
Source File: UserImpersonationIT.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Before
public void init() {
  currentNamespace = session.getNamespace();
  // Create impersonator cluster role
  impersonatorRole = new ClusterRoleBuilder()
    .withNewMetadata()
    .withName("impersonator")
    .endMetadata()
    .addToRules(new PolicyRuleBuilder()
      .addToApiGroups("")
      .addToResources("users", "groups", "userextras", "serviceaccounts")
      .addToVerbs("impersonate")
      .build()
    )
    .build();
  client.rbac().clusterRoles().inNamespace(currentNamespace).createOrReplace(impersonatorRole);

  // Create Service Account
  serviceAccount1 = new ServiceAccountBuilder()
    .withNewMetadata().withName(SERVICE_ACCOUNT).endMetadata()
    .build();
  client.serviceAccounts().inNamespace(currentNamespace).create(serviceAccount1);

  // Bind Impersonator Role to current user
  impersonatorRoleBinding = new ClusterRoleBindingBuilder()
    .withNewMetadata()
    .withName("impersonate-role")
    .endMetadata()
    .addToSubjects(new SubjectBuilder()
      .withApiGroup("rbac.authorization.k8s.io")
      .withKind("User")
      .withName(client.currentUser().getMetadata().getName())
      .withNamespace(currentNamespace)
      .build()
    )
    .withRoleRef(new RoleRefBuilder()
      .withApiGroup("rbac.authorization.k8s.io")
      .withKind("ClusterRole")
      .withName("impersonator")
      .build()
    )
    .build();

  client.rbac().clusterRoleBindings().inNamespace(currentNamespace).createOrReplace(impersonatorRoleBinding);
}
 
Example #6
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 #7
Source File: DeploymentExamples.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 client = new DefaultKubernetesClient(config);

  try {
    // Create a namespace for all our stuff
    Namespace ns = new NamespaceBuilder().withNewMetadata().withName("thisisatest").addToLabels("this", "rocks").endMetadata().build();
    log("Created namespace", client.namespaces().createOrReplace(ns));

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

    client.serviceAccounts().inNamespace("thisisatest").createOrReplace(fabric8);
    for (int i = 0; i < 2; i++) {
      System.err.println("Iteration:" + (i+1));
      Deployment deployment = new DeploymentBuilder()
        .withNewMetadata()
        .withName("nginx")
        .endMetadata()
        .withNewSpec()
        .withReplicas(1)
        .withNewTemplate()
        .withNewMetadata()
        .addToLabels("app", "nginx")
        .endMetadata()
        .withNewSpec()
        .addNewContainer()
        .withName("nginx")
        .withImage("nginx")
        .addNewPort()
        .withContainerPort(80)
        .endPort()
        .endContainer()
        .endSpec()
        .endTemplate()
        .withNewSelector()
        .addToMatchLabels("app", "nginx")
        .endSelector()
        .endSpec()
        .build();


      deployment = client.apps().deployments().inNamespace("thisisatest").create(deployment);
      log("Created deployment", deployment);

      System.err.println("Scaling up:" + deployment.getMetadata().getName());
      client.apps().deployments().inNamespace("thisisatest").withName("nginx").scale(2, true);
      log("Created replica sets:", client.apps().replicaSets().inNamespace("thisisatest").list().getItems());
      System.err.println("Deleting:" + deployment.getMetadata().getName());
      client.resource(deployment).delete();
    }
    log("Done.");

  }finally {
    client.namespaces().withName("thisisatest").delete();
    client.close();
  }
}
 
Example #8
Source File: Kubernetes.java    From enmasse with Apache License 2.0 3 votes vote down vote up
/**
 * Creates service account
 *
 * @param name      name of servcie account
 * @param namespace namespace
 * @return full name
 */
public String createServiceAccount(String name, String namespace) {
    log.info("Create serviceaccount {} in namespace {}", name, namespace);
    client.serviceAccounts().inNamespace(namespace)
            .create(new ServiceAccountBuilder().withNewMetadata().withName(name).endMetadata().build());
    return "system:serviceaccount:" + namespace + ":" + name;
}