<p>I am using fabric8.io to orchestrate application containers in Kubernetes. I am looking to create a Service that manages a pod with a certain label on a certain port. Is there a specific example of the API that does this. I couldnt find it in the examples </p>

<p><a href="https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/FullExample.java#L75" rel="nofollow">https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/FullExample.java#L75</a></p>

<p>There dont seem to be javadocs available???</p>

<p>Fabric8's Kubernetes Client is using a generated model and DSL that has the <strong>exact</strong> same structure as as the JSON and YAML configuration.</p>

<p>So in order to create a Service instance that looks like:</p>

<pre><code> {
   "kind": "Service",
   "apiVersion": "v1",
   "metadata": {
       "name": "myservice"
   },
   "spec": {
       "ports": [
           {
              "protocol": "TCP",
              "port": 80,
              "targetPort": 8080,
          }
      ],
      "selector": {
          "key": "value1",
      },¬
      "portalIP": "172.30.234.134",
      "type": "ClusterIP",
  }
</code></pre>

<p>}</p>

<p>You can use the following code:</p>

<pre><code>Service service = new ServiceBuilder()
          .withNewMetadata()
              .withName("myservice")
          .endMetadata()
          .withNewSpec()
            .addNewPort()
              .withProtocol("TCP")
              .withPort(80)
              .withNewTargetPort(8080)
            .endPort()
            .addToSelector("key1", "value1")
            .withPortalIP("172.30.234.134")
            .withType("ClusterIP")
          .endSpec()
          .build();
</code></pre>

<p>If don't need to hold a reference of the service object and you just want to create it, you can inline it like:</p>

<pre><code>client.services().createNew()
          .withNewMetadata()
              .withName("myservice")
          .endMetadata()
          .withNewSpec()
            .addNewPort()
              .withProtocol("TCP")
              .withPort(80)
              .withNewTargetPort(8080)
            .endPort()
            .addToSelector("key1", "value1")
            .withPortalIP("172.30.234.134")
            .withType("ClusterIP")
          .endSpec()
          .done();
</code></pre>

<p>It's even more compact that the JSON equivalent, because default value can be committed and also some stuff like selector can be optionally inlined in a single line.</p>

<p>This is something that not only applies to Service, but to every-single Kubernetes/Openshift resource.</p>

<p>If you have the JSON or YAML in place you can load them easily by providing an input stream to the client:</p>

<pre><code>Service service = client.services().load(inputStream).get();
</code></pre>

<p>There are more options here, like directly creating the service:</p>

<pre><code>Service newService = client.services().load(inputStream).create();
</code></pre>

<p>It's always help to remember that structure is always the same regardless of lang, format. Pretty much anything can be inlined, so tab completion in your IDE can be really really helpful.</p>
