Skip to content

Commit 159c2ab

Browse files
committed
improve: samples correctly create status object for SSA (#2365)
Signed-off-by: Attila Mészáros <[email protected]>
1 parent bd1f2c2 commit 159c2ab

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/MySQLSchemaReconciler.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55

6+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
67
import io.fabric8.kubernetes.api.model.Secret;
78
import io.javaoperatorsdk.operator.api.reconciler.*;
89
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent;
@@ -33,10 +34,10 @@ public UpdateControl<MySQLSchema> reconcile(MySQLSchema schema, Context<MySQLSch
3334
Secret secret = context.getSecondaryResource(Secret.class).orElseThrow();
3435

3536
return context.getSecondaryResource(Schema.class, SchemaDependentResource.NAME).map(s -> {
36-
updateStatusPojo(schema, s, secret.getMetadata().getName(),
37+
var statusUpdateResource = createForStatusUpdate(schema, s, secret.getMetadata().getName(),
3738
decode(secret.getData().get(MYSQL_SECRET_USERNAME)));
3839
log.info("Schema {} created - updating CR status", s.getName());
39-
return UpdateControl.patchStatus(schema);
40+
return UpdateControl.patchStatus(statusUpdateResource);
4041
}).orElseGet(UpdateControl::noUpdate);
4142
}
4243

@@ -54,8 +55,14 @@ public ErrorStatusUpdateControl<MySQLSchema> updateErrorStatus(MySQLSchema schem
5455
}
5556

5657

57-
private void updateStatusPojo(MySQLSchema mySQLSchema, Schema schema, String secretName,
58+
private MySQLSchema createForStatusUpdate(MySQLSchema mySQLSchema, Schema schema,
59+
String secretName,
5860
String userName) {
61+
MySQLSchema res = new MySQLSchema();
62+
res.setMetadata(new ObjectMetaBuilder()
63+
.withName(mySQLSchema.getMetadata().getName())
64+
.withNamespace(mySQLSchema.getMetadata().getNamespace())
65+
.build());
5966
SchemaStatus status = new SchemaStatus();
6067
status.setUrl(
6168
format(
@@ -64,6 +71,7 @@ private void updateStatusPojo(MySQLSchema mySQLSchema, Schema schema, String sec
6471
status.setUserName(userName);
6572
status.setSecretName(secretName);
6673
status.setStatus("CREATED");
67-
mySQLSchema.setStatus(status);
74+
res.setStatus(status);
75+
return res;
6876
}
6977
}

sample-operators/tomcat-operator/src/main/java/io/javaoperatorsdk/operator/sample/TomcatReconciler.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

8+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
89
import io.fabric8.kubernetes.api.model.apps.Deployment;
910
import io.fabric8.kubernetes.api.model.apps.DeploymentStatus;
1011
import io.javaoperatorsdk.operator.api.reconciler.*;
@@ -26,23 +27,28 @@ public class TomcatReconciler implements Reconciler<Tomcat> {
2627
@Override
2728
public UpdateControl<Tomcat> reconcile(Tomcat tomcat, Context<Tomcat> context) {
2829
return context.getSecondaryResource(Deployment.class).map(deployment -> {
29-
Tomcat updatedTomcat = updateTomcatStatus(tomcat, deployment);
30+
Tomcat updatedTomcat = createTomcatForStatusUpdate(tomcat, deployment);
3031
log.info(
3132
"Updating status of Tomcat {} in namespace {} to {} ready replicas",
3233
tomcat.getMetadata().getName(),
3334
tomcat.getMetadata().getNamespace(),
34-
tomcat.getStatus().getReadyReplicas());
35+
tomcat.getStatus() == null ? 0 : tomcat.getStatus().getReadyReplicas());
3536
return UpdateControl.patchStatus(updatedTomcat);
3637
}).orElseGet(UpdateControl::noUpdate);
3738
}
3839

39-
private Tomcat updateTomcatStatus(Tomcat tomcat, Deployment deployment) {
40+
private Tomcat createTomcatForStatusUpdate(Tomcat tomcat, Deployment deployment) {
41+
Tomcat res = new Tomcat();
42+
res.setMetadata(new ObjectMetaBuilder()
43+
.withName(tomcat.getMetadata().getName())
44+
.withNamespace(tomcat.getMetadata().getNamespace())
45+
.build());
4046
DeploymentStatus deploymentStatus =
4147
Objects.requireNonNullElse(deployment.getStatus(), new DeploymentStatus());
4248
int readyReplicas = Objects.requireNonNullElse(deploymentStatus.getReadyReplicas(), 0);
4349
TomcatStatus status = new TomcatStatus();
4450
status.setReadyReplicas(readyReplicas);
45-
tomcat.setStatus(status);
46-
return tomcat;
51+
res.setStatus(status);
52+
return res;
4753
}
4854
}

sample-operators/tomcat-operator/src/main/java/io/javaoperatorsdk/operator/sample/WebappReconciler.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.slf4j.Logger;
1515
import org.slf4j.LoggerFactory;
1616

17+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
1718
import io.fabric8.kubernetes.api.model.Pod;
1819
import io.fabric8.kubernetes.api.model.apps.Deployment;
1920
import io.fabric8.kubernetes.client.KubernetesClient;
@@ -101,12 +102,7 @@ public UpdateControl<Webapp> reconcile(Webapp webapp, Context<Webapp> context) {
101102

102103
String[] commandStatusInAllPods = executeCommandInAllPods(kubernetesClient, webapp, command);
103104

104-
if (webapp.getStatus() == null) {
105-
webapp.setStatus(new WebappStatus());
106-
}
107-
webapp.getStatus().setDeployedArtifact(webapp.getSpec().getUrl());
108-
webapp.getStatus().setDeploymentStatus(commandStatusInAllPods);
109-
return UpdateControl.patchStatus(webapp);
105+
return UpdateControl.patchStatus(createWebAppForStatusUpdate(webapp, commandStatusInAllPods));
110106
} else {
111107
log.info("WebappController invoked but Tomcat not ready yet ({}/{})",
112108
tomcat.getStatus() != null ? tomcat.getStatus().getReadyReplicas() : 0,
@@ -115,6 +111,18 @@ public UpdateControl<Webapp> reconcile(Webapp webapp, Context<Webapp> context) {
115111
}
116112
}
117113

114+
private Webapp createWebAppForStatusUpdate(Webapp actual, String[] commandStatusInAllPods) {
115+
var webapp = new Webapp();
116+
webapp.setMetadata(new ObjectMetaBuilder()
117+
.withName(actual.getMetadata().getName())
118+
.withNamespace(actual.getMetadata().getNamespace())
119+
.build());
120+
webapp.setStatus(new WebappStatus());
121+
webapp.getStatus().setDeployedArtifact(actual.getSpec().getUrl());
122+
webapp.getStatus().setDeploymentStatus(commandStatusInAllPods);
123+
return webapp;
124+
}
125+
118126
@Override
119127
public DeleteControl cleanup(Webapp webapp, Context<Webapp> context) {
120128

0 commit comments

Comments
 (0)