Search in sources :

Example 1 with V1Service

use of io.kubernetes.client.openapi.models.V1Service in project heron by twitter.

the class V1Controller method submit.

/**
 * Configures all components required by a <code>topology</code> and submits it to the Kubernetes scheduler.
 * @param packingPlan Used to configure the StatefulSets <code>Resource</code>s and replica count.
 * @return Success indicator.
 */
@Override
boolean submit(PackingPlan packingPlan) {
    final String topologyName = getTopologyName();
    if (!topologyName.equals(topologyName.toLowerCase())) {
        throw new TopologySubmissionException("K8S scheduler does not allow upper case topology's.");
    }
    final Resource containerResource = getContainerResource(packingPlan);
    final V1Service topologyService = createTopologyService();
    try {
        coreClient.createNamespacedService(getNamespace(), topologyService, null, null, null);
    } catch (ApiException e) {
        KubernetesUtils.logExceptionWithDetails(LOG, "Error creating topology service", e);
        throw new TopologySubmissionException(e.getMessage());
    }
    // Find the max number of instances in a container so that we can open
    // enough ports if remote debugging is enabled.
    int numberOfInstances = 0;
    for (PackingPlan.ContainerPlan containerPlan : packingPlan.getContainers()) {
        numberOfInstances = Math.max(numberOfInstances, containerPlan.getInstances().size());
    }
    final V1StatefulSet executors = createStatefulSet(containerResource, numberOfInstances, true);
    final V1StatefulSet manager = createStatefulSet(containerResource, numberOfInstances, false);
    try {
        appsClient.createNamespacedStatefulSet(getNamespace(), executors, null, null, null);
        appsClient.createNamespacedStatefulSet(getNamespace(), manager, null, null, null);
    } catch (ApiException e) {
        final String message = String.format("Error creating topology: %s%n", e.getResponseBody());
        KubernetesUtils.logExceptionWithDetails(LOG, message, e);
        throw new TopologySubmissionException(message);
    }
    return true;
}
Also used : TopologySubmissionException(org.apache.heron.scheduler.TopologySubmissionException) V1StatefulSet(io.kubernetes.client.openapi.models.V1StatefulSet) PackingPlan(org.apache.heron.spi.packing.PackingPlan) Resource(org.apache.heron.spi.packing.Resource) V1Service(io.kubernetes.client.openapi.models.V1Service) ApiException(io.kubernetes.client.openapi.ApiException)

Example 2 with V1Service

use of io.kubernetes.client.openapi.models.V1Service in project heron by twitter.

the class V1Controller method createTopologyService.

/**
 * Creates a headless <code>Service</code> to facilitate communication between Pods in a <code>topology</code>.
 * @return A fully configured <code>Service</code> to be used by a <code>topology</code>.
 */
private V1Service createTopologyService() {
    final String topologyName = getTopologyName();
    final V1Service service = new V1Service();
    // Setup service metadata.
    final V1ObjectMeta objectMeta = new V1ObjectMeta().name(topologyName).annotations(getServiceAnnotations()).labels(getServiceLabels());
    service.setMetadata(objectMeta);
    // Create the headless service.
    final V1ServiceSpec serviceSpec = new V1ServiceSpec().clusterIP("None").selector(getPodMatchLabels(topologyName));
    service.setSpec(serviceSpec);
    return service;
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1ServiceSpec(io.kubernetes.client.openapi.models.V1ServiceSpec) V1Service(io.kubernetes.client.openapi.models.V1Service)

Example 3 with V1Service

use of io.kubernetes.client.openapi.models.V1Service in project java by kubernetes-client.

the class YamlExample method main.

public static void main(String[] args) throws IOException, ApiException, ClassNotFoundException {
    V1Pod pod = new V1PodBuilder().withNewMetadata().withName("apod").endMetadata().withNewSpec().addNewContainer().withName("www").withImage("nginx").withNewResources().withLimits(new HashMap<>()).endResources().endContainer().endSpec().build();
    System.out.println(Yaml.dump(pod));
    V1Service svc = new V1ServiceBuilder().withNewMetadata().withName("aservice").endMetadata().withNewSpec().withSessionAffinity(V1ServiceSpec.SessionAffinityEnum.CLIENTIP).withType(V1ServiceSpec.TypeEnum.NODEPORT).addNewPort().withProtocol(V1ServicePort.ProtocolEnum.TCP).withName("client").withPort(8008).withNodePort(8080).withTargetPort(new IntOrString(8080)).endPort().endSpec().build();
    System.out.println(Yaml.dump(svc));
    // Read yaml configuration file, and deploy it
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    // See issue #474. Not needed at most cases, but it is needed if you are using war
    // packging or running this on JUnit.
    Yaml.addModelMap("v1", "Service", V1Service.class);
    // Example yaml file can be found in $REPO_DIR/test-svc.yaml
    File file = new File("test-svc.yaml");
    V1Service yamlSvc = (V1Service) Yaml.load(file);
    // Deployment and StatefulSet is defined in apps/v1, so you should use AppsV1Api instead of
    // CoreV1API
    CoreV1Api api = new CoreV1Api();
    V1Service createResult = api.createNamespacedService("default", yamlSvc, null, null, null, null);
    System.out.println(createResult);
    V1Service deleteResult = api.deleteNamespacedService(yamlSvc.getMetadata().getName(), "default", null, null, null, null, null, new V1DeleteOptions());
    System.out.println(deleteResult);
}
Also used : V1DeleteOptions(io.kubernetes.client.openapi.models.V1DeleteOptions) V1PodBuilder(io.kubernetes.client.openapi.models.V1PodBuilder) IntOrString(io.kubernetes.client.custom.IntOrString) V1ServiceBuilder(io.kubernetes.client.openapi.models.V1ServiceBuilder) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1Service(io.kubernetes.client.openapi.models.V1Service) ApiClient(io.kubernetes.client.openapi.ApiClient) File(java.io.File) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Example 4 with V1Service

use of io.kubernetes.client.openapi.models.V1Service in project java by kubernetes-client.

the class CoreV1Api method patchNamespacedServiceStatusWithHttpInfo.

/**
 * partially update status of the specified Service
 *
 * @param name name of the Service (required)
 * @param namespace object name and auth scope, such as for teams and projects (required)
 * @param body (required)
 * @param pretty If &#39;true&#39;, then the output is pretty printed. (optional)
 * @param dryRun When present, indicates that modifications should not be persisted. An invalid or
 *     unrecognized dryRun directive will result in an error response and no further processing of
 *     the request. Valid values are: - All: all dry run stages will be processed (optional)
 * @param fieldManager fieldManager is a name associated with the actor or entity that is making
 *     these changes. The value must be less than or 128 characters long, and only contain
 *     printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is
 *     required for apply requests (application/apply-patch) but optional for non-apply patch
 *     types (JsonPatch, MergePatch, StrategicMergePatch). (optional)
 * @param fieldValidation fieldValidation determines how the server should respond to
 *     unknown/duplicate fields in the object in the request. Introduced as alpha in 1.23, older
 *     servers or servers with the &#x60;ServerSideFieldValidation&#x60; feature disabled will
 *     discard valid values specified in this param and not perform any server side field
 *     validation. Valid values are: - Ignore: ignores unknown/duplicate fields. - Warn: responds
 *     with a warning for each unknown/duplicate field, but successfully serves the request. -
 *     Strict: fails the request on unknown/duplicate fields. (optional)
 * @param force Force is going to \&quot;force\&quot; Apply requests. It means user will
 *     re-acquire conflicting fields owned by other people. Force flag must be unset for non-apply
 *     patch requests. (optional)
 * @return ApiResponse&lt;V1Service&gt;
 * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the
 *     response body
 * @http.response.details
 *     <table summary="Response Details" border="1">
 * <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
 * <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
 * <tr><td> 201 </td><td> Created </td><td>  -  </td></tr>
 * <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
 * </table>
 */
public ApiResponse<V1Service> patchNamespacedServiceStatusWithHttpInfo(String name, String namespace, V1Patch body, String pretty, String dryRun, String fieldManager, String fieldValidation, Boolean force) throws ApiException {
    okhttp3.Call localVarCall = patchNamespacedServiceStatusValidateBeforeCall(name, namespace, body, pretty, dryRun, fieldManager, fieldValidation, force, null);
    Type localVarReturnType = new TypeToken<V1Service>() {
    }.getType();
    return localVarApiClient.execute(localVarCall, localVarReturnType);
}
Also used : Type(java.lang.reflect.Type) V1Service(io.kubernetes.client.openapi.models.V1Service)

Example 5 with V1Service

use of io.kubernetes.client.openapi.models.V1Service in project java by kubernetes-client.

the class YamlTest method testLoadAllFile.

@Test
public void testLoadAllFile() throws Exception {
    List<Object> list = Yaml.loadAll(new File(TEST_YAML_FILE_PATH));
    List<KubernetesType> k8ObjectList = new ArrayList<>();
    for (Object object : list) {
        String type = object.getClass().getSimpleName();
        if (type.equals("V1Service")) {
            V1Service svc = (V1Service) object;
            assertEquals("v1", svc.getApiVersion());
            assertEquals("Service", svc.getKind());
            assertEquals("mock", svc.getMetadata().getName());
            k8ObjectList.add(svc);
        } else if (type.equals("V1Deployment")) {
            V1Deployment deploy = (V1Deployment) object;
            assertEquals("apps/v1", deploy.getApiVersion());
            assertEquals("Deployment", deploy.getKind());
            assertEquals("helloworld", deploy.getMetadata().getName());
            k8ObjectList.add(deploy);
        } else if (type.equals("V1Secret")) {
            V1Secret secret = (V1Secret) object;
            assertEquals("Secret", secret.getKind());
            assertEquals("secret", secret.getMetadata().getName());
            assertEquals("Opaque", secret.getType());
            assertEquals("hello", new String(secret.getData().get("secret-data"), UTF_8));
            k8ObjectList.add(secret);
        } else {
            throw new Exception("some thing wrong happened");
        }
    }
    String result = Yaml.dumpAll(k8ObjectList.iterator());
    String expected = Resources.toString(EXPECTED_YAML_FILE, UTF_8).replace("\r\n", "\n");
    assertThat(result, equalTo(expected));
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) KubernetesType(io.kubernetes.client.common.KubernetesType) ArrayList(java.util.ArrayList) V1Service(io.kubernetes.client.openapi.models.V1Service) File(java.io.File) V1Secret(io.kubernetes.client.openapi.models.V1Secret) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

V1Service (io.kubernetes.client.openapi.models.V1Service)25 Type (java.lang.reflect.Type)16 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)5 V1ServiceSpec (io.kubernetes.client.openapi.models.V1ServiceSpec)5 HashMap (java.util.HashMap)4 IntOrString (io.kubernetes.client.custom.IntOrString)3 V1ServicePort (io.kubernetes.client.openapi.models.V1ServicePort)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 KubernetesType (io.kubernetes.client.common.KubernetesType)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 ApiException (io.kubernetes.client.openapi.ApiException)1 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)1 V1DeleteOptions (io.kubernetes.client.openapi.models.V1DeleteOptions)1 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)1 V1Pod (io.kubernetes.client.openapi.models.V1Pod)1 V1PodBuilder (io.kubernetes.client.openapi.models.V1PodBuilder)1 V1Secret (io.kubernetes.client.openapi.models.V1Secret)1 V1ServiceBuilder (io.kubernetes.client.openapi.models.V1ServiceBuilder)1 V1StatefulSet (io.kubernetes.client.openapi.models.V1StatefulSet)1