Search in sources :

Example 1 with IntOrString

use of io.kubernetes.client.custom.IntOrString 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 2 with IntOrString

use of io.kubernetes.client.custom.IntOrString in project twister2 by DSC-SPIDAL.

the class KubernetesUtils method createServiceObject.

public static V1Service createServiceObject(String serviceName, String serviceLabel, int port, int targetPort) {
    V1Service service = new V1Service();
    service.setKind("Service");
    service.setApiVersion("v1");
    // construct and set metadata
    V1ObjectMeta meta = new V1ObjectMeta();
    meta.setName(serviceName);
    service.setMetadata(meta);
    // construct and set service spec
    V1ServiceSpec serviceSpec = new V1ServiceSpec();
    // ClusterIP needs to be None for headless service
    serviceSpec.setClusterIP("None");
    // set selector
    HashMap<String, String> selectors = new HashMap<String, String>();
    selectors.put("app", serviceLabel);
    serviceSpec.setSelector(selectors);
    ArrayList<V1ServicePort> ports = new ArrayList<V1ServicePort>();
    V1ServicePort servicePort = new V1ServicePort();
    servicePort.setPort(port);
    servicePort.setTargetPort(new IntOrString(targetPort));
    servicePort.setProtocol("TCP");
    ports.add(servicePort);
    serviceSpec.setPorts(ports);
    service.setSpec(serviceSpec);
    return service;
}
Also used : V1ServicePort(io.kubernetes.client.models.V1ServicePort) HashMap(java.util.HashMap) V1ObjectMeta(io.kubernetes.client.models.V1ObjectMeta) IntOrString(io.kubernetes.client.custom.IntOrString) V1ServiceSpec(io.kubernetes.client.models.V1ServiceSpec) ArrayList(java.util.ArrayList) V1Service(io.kubernetes.client.models.V1Service) IntOrString(io.kubernetes.client.custom.IntOrString)

Aggregations

IntOrString (io.kubernetes.client.custom.IntOrString)2 V1ObjectMeta (io.kubernetes.client.models.V1ObjectMeta)1 V1Service (io.kubernetes.client.models.V1Service)1 V1ServicePort (io.kubernetes.client.models.V1ServicePort)1 V1ServiceSpec (io.kubernetes.client.models.V1ServiceSpec)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)1 V1DeleteOptions (io.kubernetes.client.openapi.models.V1DeleteOptions)1 V1Pod (io.kubernetes.client.openapi.models.V1Pod)1 V1PodBuilder (io.kubernetes.client.openapi.models.V1PodBuilder)1 V1Service (io.kubernetes.client.openapi.models.V1Service)1 V1ServiceBuilder (io.kubernetes.client.openapi.models.V1ServiceBuilder)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1