Skip to content

improve: samples correctly create status object for SSA #2365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

return context.getSecondaryResource(Schema.class, SchemaDependentResource.NAME).map(s -> {
updateStatusPojo(schema, s, secret.getMetadata().getName(),
var statusUpdateResource = createForStatusUpdate(schema, s, secret.getMetadata().getName(),
decode(secret.getData().get(MYSQL_SECRET_USERNAME)));
log.info("Schema {} created - updating CR status", s.getName());
return UpdateControl.patchStatus(schema);
return UpdateControl.patchStatus(statusUpdateResource);
}).orElseGet(UpdateControl::noUpdate);
}

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


private void updateStatusPojo(MySQLSchema mySQLSchema, Schema schema, String secretName,
private MySQLSchema createForStatusUpdate(MySQLSchema mySQLSchema, Schema schema,
String secretName,
String userName) {
MySQLSchema res = new MySQLSchema();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, having to do that for SSA is quite annoying… I'm wondering if there's anything we can do about it…

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not sure, maybe an utility to clone a shallow class? this results certainly in a kind of a boilerplate code .

res.setMetadata(new ObjectMetaBuilder()
.withName(mySQLSchema.getMetadata().getName())
.withNamespace(mySQLSchema.getMetadata().getNamespace())
.build());
SchemaStatus status = new SchemaStatus();
status.setUrl(
format(
Expand All @@ -64,6 +71,7 @@ private void updateStatusPojo(MySQLSchema mySQLSchema, Schema schema, String sec
status.setUserName(userName);
status.setSecretName(secretName);
status.setStatus("CREATED");
mySQLSchema.setStatus(status);
res.setStatus(status);
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentStatus;
import io.javaoperatorsdk.operator.api.reconciler.*;
Expand All @@ -26,23 +27,28 @@ public class TomcatReconciler implements Reconciler<Tomcat> {
@Override
public UpdateControl<Tomcat> reconcile(Tomcat tomcat, Context<Tomcat> context) {
return context.getSecondaryResource(Deployment.class).map(deployment -> {
Tomcat updatedTomcat = updateTomcatStatus(tomcat, deployment);
Tomcat updatedTomcat = createTomcatForStatusUpdate(tomcat, deployment);
log.info(
"Updating status of Tomcat {} in namespace {} to {} ready replicas",
tomcat.getMetadata().getName(),
tomcat.getMetadata().getNamespace(),
tomcat.getStatus().getReadyReplicas());
tomcat.getStatus() == null ? 0 : tomcat.getStatus().getReadyReplicas());
return UpdateControl.patchStatus(updatedTomcat);
}).orElseGet(UpdateControl::noUpdate);
}

private Tomcat updateTomcatStatus(Tomcat tomcat, Deployment deployment) {
private Tomcat createTomcatForStatusUpdate(Tomcat tomcat, Deployment deployment) {
Tomcat res = new Tomcat();
res.setMetadata(new ObjectMetaBuilder()
.withName(tomcat.getMetadata().getName())
.withNamespace(tomcat.getMetadata().getNamespace())
.build());
DeploymentStatus deploymentStatus =
Objects.requireNonNullElse(deployment.getStatus(), new DeploymentStatus());
int readyReplicas = Objects.requireNonNullElse(deploymentStatus.getReadyReplicas(), 0);
TomcatStatus status = new TomcatStatus();
status.setReadyReplicas(readyReplicas);
tomcat.setStatus(status);
return tomcat;
res.setStatus(status);
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

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

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

private Webapp createWebAppForStatusUpdate(Webapp actual, String[] commandStatusInAllPods) {
var webapp = new Webapp();
webapp.setMetadata(new ObjectMetaBuilder()
.withName(actual.getMetadata().getName())
.withNamespace(actual.getMetadata().getNamespace())
.build());
webapp.setStatus(new WebappStatus());
webapp.getStatus().setDeployedArtifact(actual.getSpec().getUrl());
webapp.getStatus().setDeploymentStatus(commandStatusInAllPods);
return webapp;
}

@Override
public DeleteControl cleanup(Webapp webapp, Context<Webapp> context) {

Expand Down
Loading