|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * operators/operator_sdk/java/osdk-java-tutorial.adoc |
| 4 | + |
| 5 | +:_content-type: CONCEPT |
| 6 | +[id="osdk-java-controller-reconcile-loop_{context}"] |
| 7 | += Reconcile loop |
| 8 | + |
| 9 | +. Every controller has a reconciler object with a `Reconcile()` method that implements the reconcile loop. The reconcile loop is passed the `Deployment` argument, as shown in the following example: |
| 10 | ++ |
| 11 | +[source,java] |
| 12 | +---- |
| 13 | + Deployment deployment = client.apps() |
| 14 | + .deployments() |
| 15 | + .inNamespace(resource.getMetadata().getNamespace()) |
| 16 | + .withName(resource.getMetadata().getName()) |
| 17 | + .get(); |
| 18 | +---- |
| 19 | +
|
| 20 | +. As shown in the following example, if the `Deployment` is `null`, the deployment needs to be created. After you create the `Deployment`, you can determine if reconciliation is necessary. If there is no need of reconciliation, return the value of `UpdateControl.noUpdate()`, otherwise, return the value of `UpdateControl.updateStatus(resource): |
| 21 | ++ |
| 22 | +[source, java] |
| 23 | +---- |
| 24 | + if (deployment == null) { |
| 25 | + Deployment newDeployment = createMemcachedDeployment(resource); |
| 26 | + client.apps().deployments().create(newDeployment); |
| 27 | + return UpdateControl.noUpdate(); |
| 28 | + } |
| 29 | +---- |
| 30 | +
|
| 31 | +. After getting the `Deployment`, get the current and required replicas, as shown in the following example: |
| 32 | ++ |
| 33 | +[source,java] |
| 34 | +---- |
| 35 | + int currentReplicas = deployment.getSpec().getReplicas(); |
| 36 | + int requiredReplicas = resource.getSpec().getSize(); |
| 37 | +---- |
| 38 | +
|
| 39 | +. If `currentReplicas` does not match the `requiredReplicas`, you must update the `Deployment`, as shown in the following example: |
| 40 | ++ |
| 41 | +[source,java] |
| 42 | +---- |
| 43 | + if (currentReplicas != requiredReplicas) { |
| 44 | + deployment.getSpec().setReplicas(requiredReplicas); |
| 45 | + client.apps().deployments().createOrReplace(deployment); |
| 46 | + return UpdateControl.noUpdate(); |
| 47 | + } |
| 48 | +---- |
| 49 | +
|
| 50 | +. The following example shows how to obtain the list of pods and their names: |
| 51 | ++ |
| 52 | +[source,java] |
| 53 | +---- |
| 54 | + List<Pod> pods = client.pods() |
| 55 | + .inNamespace(resource.getMetadata().getNamespace()) |
| 56 | + .withLabels(labelsForMemcached(resource)) |
| 57 | + .list() |
| 58 | + .getItems(); |
| 59 | + |
| 60 | + List<String> podNames = |
| 61 | + pods.stream().map(p -> p.getMetadata().getName()).collect(Collectors.toList()); |
| 62 | +---- |
| 63 | +
|
| 64 | +. Check if resources were created and verify podnames with the Memcached resources. If a mismatch exists in either of these conditions, perform a reconciliation as shown in the following example: |
| 65 | ++ |
| 66 | +[source,java] |
| 67 | +---- |
| 68 | + if (resource.getStatus() == null |
| 69 | + || !CollectionUtils.isEqualCollection(podNames, resource.getStatus().getNodes())) { |
| 70 | + if (resource.getStatus() == null) resource.setStatus(new MemcachedStatus()); |
| 71 | + resource.getStatus().setNodes(podNames); |
| 72 | + return UpdateControl.updateResource(resource); |
| 73 | + } |
| 74 | +---- |
0 commit comments