question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fabric8 client API version 1.2.2 order of create rep controller / create watch didn't matter. in 1.3.91 it does matter.

See original GitHub issue

Hi.

i filed an issue on stack overflow here -> http://stackoverflow.com/questions/37475114/does-fabric8-and-kubernetes-1-2-4-not-support-watches-on-replication-controllers

which illustrates the steps to reproduce this issue, using a small scala program. My colleague Vijay Samuel (mad respect to him) helped me find a solution to the problem, which is expressed via a working java program in that same stack overflow post.

Initially we thought the key tweak to making things work was to upgrade from 1.3.x of client API to 1.4-SNAPSHOT, but it turns out that this is immaterial. The main thing one must do is to make sure that watches are created before the object that is being watched is created.

For example, in Vijay’s if you switched the order of these statements

client.replicationControllers().inNamespace(namespace).withLabel("l", "v").watch(watcher);
createRc(client, namespace, podName, image);

to this:

createRc(client, namespace, podName, image);
client.replicationControllers().inNamespace(namespace).withLabel("l", "v").watch(watcher);

the program would cease to work. Switching the order would have been fine in 1.2.2 as far as i can tell from the testing i have done.

Vijay’s working example

import com.fasterxml.jackson.databind.ObjectMapper;
import com.typesafe.scalalogging.StrictLogging;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.ReplicationController;
import io.fabric8.kubernetes.client.Watcher.Action;
import io.fabric8.kubernetes.client.*;

import java.util.HashMap;
import java.util.Map;


public class Vijay {


  public static DefaultKubernetesClient getConnection ()  {
    ConfigBuilder
         configBuilder =   Config.builder() ;
    Config config =
      configBuilder.
        withMasterUrl("http://localhost:8080").
        build();
    return new DefaultKubernetesClient(config);
  }


  public static void main(String[] args) throws Exception {
    DefaultKubernetesClient client = getConnection();

    String namespace = "junk6";
    String podName = "prom";
    String image = "nginx";

    Watcher<ReplicationController> watcher = new Watcher<ReplicationController>() {

      @Override
      public void onClose(KubernetesClientException cause) {
        // TODO Auto-generated method stub

      }

      @Override
      public void eventReceived(Action action, ReplicationController resource) {
        System.out.println(action + ":" + resource);

      }
    };


    client.replicationControllers().inNamespace(namespace).withLabel("l", "v").watch(watcher);

    createRc(client, namespace, podName, image);

  }

  private static void createRc(DefaultKubernetesClient client, String namespace, String podName, String image) {
    try {
      Map<String, String> labels = new HashMap<String, String>();
      labels.put("l", "v");
      ReplicationController rc = client.replicationControllers().inNamespace(namespace)
          .createNew()
          .withNewMetadata()
          .withName(podName)
          .addToLabels(labels)
          .endMetadata()
          .withNewSpec()
          .withReplicas(1)
          .withSelector(labels)
          .withNewTemplate()
          .withNewMetadata()
          .addToLabels(labels)
          .endMetadata()
          .withNewSpec()
          .addNewContainer()
          .withName(podName)
          .withImage(image)
          .withImagePullPolicy("Always")
          .withNewResources()
          .addToLimits("cpu", new Quantity("100m"))
          .addToLimits("memory", new Quantity("100Mi"))
          .endResources()
          .endContainer()
          .endSpec()
          .endTemplate()
          .endSpec()
          .done();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
buildlackeycommented, May 27, 2016

… thanks very much for your help /cb

0reactions
jimmidysoncommented, May 27, 2016

Well I think is working correctly now… #433 stops sending an empty fieldSelector query param & now I can consistently watch events after creating objects (e.g. scaling an RC receives events properly). Closing now, but if this is still an issue with next release feel free to reopen.

Read more comments on GitHub >

github_iconTop Results From Across the Web

does fabric8 and kubernetes 1.2.4 not support watches on ...
The key issue (also mentioned in this bug report to fabric8 folks) is that order of creation of object and watch on the...
Read more >
API Help (Fabric8 :: Kubernetes :: Java Client 1.3.2 API) - javadoc.io
This API (Application Programming Interface) document has pages corresponding to the items in the navigation bar, described as follows. Overview. The Overview ...
Read more >
Difference between Fabric8 and Official Kubernetes Java Client
Now let's take a look at differences in usages of both clients in details, we'll be looking at common use cases in which...
Read more >
fabric8io/fabric8-maven-plugin
This plugin focus on two tasks: Building Docker images and creating Kubernetes and OpenShift resource descriptors. It can be configured very ...
Read more >
Writing a Kubernetes Operator in Java: Part 3 - Instana
In order to do that, the Kubernetes client API requires us to load the Custom Resource Definition (CRD) from the API server, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found