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 issueHi.
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:
- Created 7 years ago
- Comments:9 (5 by maintainers)
… thanks very much for your help /cb
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.