Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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 @@ -18,18 +18,26 @@
package org.apache.flink.kubernetes.operator.api.status;

import org.apache.flink.annotation.Experimental;
import org.apache.flink.api.common.JobStatus;
import org.apache.flink.kubernetes.operator.api.spec.FlinkDeploymentSpec;
import org.apache.flink.kubernetes.operator.api.utils.ConditionUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.fabric8.kubernetes.api.model.Condition;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.apache.flink.kubernetes.operator.api.utils.ConditionUtils.CONDITION_TYPE_RUNNING;

/** Last observed status of the Flink deployment. */
@Experimental
Expand All @@ -55,4 +63,146 @@ public class FlinkDeploymentStatus extends CommonStatus<FlinkDeploymentSpec> {

/** Information about the TaskManagers for the scale subresource. */
private TaskManagerInfo taskManager;

/** Condition of the CR . */
private List<Condition> conditions = new ArrayList<>();

public List<Condition> getConditions() {
if (reconciliationStatus != null

Choose a reason for hiding this comment

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

I am curious which parts of the code refer to application mode and which are session cluster. It would be good to have some comments to detail this and maybe use the mode name in method name or variable names to make this more intuitive to read.

Copy link
Author

Choose a reason for hiding this comment

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

I will add comments in the respective code.

&& reconciliationStatus.deserializeLastReconciledSpec() != null
&& reconciliationStatus.deserializeLastReconciledSpec().getJob() == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should avoid deserializing twice here, could we simply check the job status instead?

Copy link
Author

Choose a reason for hiding this comment

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

Sure will use JobStatus.

Copy link
Author

Choose a reason for hiding this comment

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

If we derive the mode from Jobstatus.state, it looks like , there is a chance of misleading information in the condition status.

For example, it is observed that, when an existing session cluster is updated with some information , for example with checkpoint interval changed to new value, we can see that state is updated with value as FINISHED as below

status:
clusterInfo:
jobManagerDeploymentStatus: READY
jobStatus:
checkpointInfo:
lastPeriodicCheckpointTimestamp: 0
savepointInfo:
lastPeriodicSavepointTimestamp: 0
savepointHistory: []
state: FINISHED

So, if we derive the running state from JobStatus, by looking at the state value , we end up condition for above as Running false. But if we look at the jobManagerDeploymentStatus: READY, we are supposed to say that condition as Running true as it is session cluster, and it's says that session is ready to receive the request.

// Populate conditions for SessionMode deployment
switch (jobManagerDeploymentStatus) {
Copy link

@davidradl davidradl Mar 21, 2025

Choose a reason for hiding this comment

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

can we have a datastructure, keyed off the jobManagerDeploymentStatus, that we interrogate to get the inserts for the updateConditions. Maybe a map with the key of the status and the value of an object RunningCondition, that has 2 fields the boolean and the description. Something like :


Map<JobManagerDeploymentStatus,String> jobmanagerDeploymentStatusMap = new HashMap<String,String>() {{
    put(READY, new RunningCondition(true, "JobManager is running and ready to receive REST API calls")),
    ....
}}; 

then the code just loops through the map updating conditions using the map values. Similar for jobstatus

Copy link
Author

Choose a reason for hiding this comment

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

Wondering will that add any value?. Any way to build the Map , we have to call ConditionUtils to build the Condition, so rather than have a Prebuild Map , we can directly call ConditionUtils to build them right. Your thoughts?.

Choose a reason for hiding this comment

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

I think you have pre built the map now - with map.of - looks good - thanks.

case READY:
Copy link

@davidradl davidradl Apr 10, 2025

Choose a reason for hiding this comment

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

instead of this switch can we just issue

updateCondition(
                           conditions,
                           ConditionUtils.crCondition(
                                   ConditionUtils.SESSION_MODE_CONDITION.get(
                                           jobManagerDeploymentStatus.name())));

Copy link
Author

Choose a reason for hiding this comment

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

@davidradl this has been revisited and made it simple by removing switch.

updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.SESSION_MODE_CONDITION.get(
JobManagerDeploymentStatus.READY.name())));
break;
case MISSING:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.SESSION_MODE_CONDITION.get(
JobManagerDeploymentStatus.MISSING.name())));
break;
case DEPLOYING:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.SESSION_MODE_CONDITION.get(
JobManagerDeploymentStatus.DEPLOYING.name())));
break;
case DEPLOYED_NOT_READY:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.SESSION_MODE_CONDITION.get(
JobManagerDeploymentStatus.DEPLOYED_NOT_READY.name())));
break;
case ERROR:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.SESSION_MODE_CONDITION.get(
JobManagerDeploymentStatus.ERROR.name())));
}
} else if (getJobStatus() != null && getJobStatus().getState() != null) {
// Populate conditions for ApplicationMode deployment
switch (getJobStatus().getState()) {
Copy link

@davidradl davidradl Apr 10, 2025

Choose a reason for hiding this comment

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

can this switch as-is be replaced with

 updateCondition(
                            conditions,
                            ConditionUtils.crCondition(
                                    ConditionUtils.APPLICATION_MODE_CONDITION.get(
                                            getJobStatus().getState().name())));
 

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking exactly the same here @lajith2006

Copy link
Author

Choose a reason for hiding this comment

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

Yes, @gyfora and @davidradl this has been revisited and addressed by removing switch and made it simple.

case RECONCILING:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.APPLICATION_MODE_CONDITION.get(
JobStatus.RECONCILING.name())));
break;
case CREATED:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.APPLICATION_MODE_CONDITION.get(
JobStatus.CREATED.name())));
break;
case RUNNING:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.APPLICATION_MODE_CONDITION.get(
JobStatus.RUNNING.name())));
break;
case FAILING:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.APPLICATION_MODE_CONDITION.get(
JobStatus.FAILING.name())));
break;
case RESTARTING:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.APPLICATION_MODE_CONDITION.get(
JobStatus.RESTARTING.name())));
break;
case FAILED:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.APPLICATION_MODE_CONDITION.get(
JobStatus.FAILED.name())));
break;
case FINISHED:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.APPLICATION_MODE_CONDITION.get(
JobStatus.FINISHED.name())));
break;

case CANCELED:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.APPLICATION_MODE_CONDITION.get(
JobStatus.CANCELED.name())));
break;
case SUSPENDED:
updateCondition(
conditions,
ConditionUtils.crCondition(
ConditionUtils.APPLICATION_MODE_CONDITION.get(
JobStatus.SUSPENDED.name())));
break;
}
}
return conditions;
}

private static void updateCondition(List<Condition> conditions, Condition newCondition) {
if (newCondition.getType().equals(CONDITION_TYPE_RUNNING)) {
Optional<Condition> existingCondition =
conditions.stream()
.filter(
c ->
c.getType().equals(CONDITION_TYPE_RUNNING)
&& c.getReason()
.equals(newCondition.getReason())
&& c.getMessage()
.equals(newCondition.getMessage()))
.findFirst();
// Until there is a condition change which reflects the latest state, no need to add condition to list.
if (existingCondition.isPresent()) {
return;
}
// Remove existing Condition with type running and then add a new condition that reflects the current state.
conditions.removeIf(
c ->
c.getType().equals(CONDITION_TYPE_RUNNING)
&& !c.getMessage().equals(newCondition.getMessage())

Choose a reason for hiding this comment

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

we have got here because the new condition is not the same as the existing condition. So we should just remove the existing condition without any conditions.

As-is the condition will not be removed if existing condition message or reason matches the new condition; which is not correct.

Copy link
Author

@lajith2006 lajith2006 May 14, 2025

Choose a reason for hiding this comment

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

So we should just remove the existing condition without any conditions.

Right its fine for now, But I think, if in later point of time, if we are adding new conditions other than Running, just removing the existing condition without of any check will cause an issue.

As-is the condition will not be removed if existing condition message or reason matches the new condition; which is not correct.

That's right, but for Running type , we don't have any such case were reason or message can be matched with existing condition.

&& !c.getReason().equals(newCondition.getReason()));
}
conditions.add(newCondition);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.kubernetes.operator.api.utils;

import org.apache.flink.api.common.JobStatus;
import org.apache.flink.kubernetes.operator.api.status.JobManagerDeploymentStatus;

import io.fabric8.kubernetes.api.model.Condition;
import io.fabric8.kubernetes.api.model.ConditionBuilder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/** Creates a condition object with the type, status, message and reason. */
public class ConditionUtils {
public static final String CONDITION_TYPE_RUNNING = "Running";

public static Condition crCondition(Condition condition) {
return new ConditionBuilder(condition)
.withLastTransitionTime(
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(new Date()))
.build();
}

public static final Map<String, Condition> SESSION_MODE_CONDITION =
Map.of(
JobManagerDeploymentStatus.READY.name(),
new ConditionBuilder()

Choose a reason for hiding this comment

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

nit: you could refactor to a method and pass JobManagerDeploymentConditionStatus.READY, as the rest of the condition builder is the same apart from the status .
So

JobManagerDeploymentStatus.READY.name(),
createRunningConditionBuilder(JobManagerDeploymentConditionStatus.READY),
 JobManagerDeploymentStatus.MISSING.name(),
createRunningConditionBuilder(JobManagerDeploymentConditionStatus.MISSING),
...

Copy link
Author

Choose a reason for hiding this comment

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

It has been moved to method and reason and messages are getting from enum.

.withType(CONDITION_TYPE_RUNNING)
.withStatus("True")
.withReason("Ready")
.withMessage(
"JobManager is running and ready to receive REST API calls")
.build(),
JobManagerDeploymentStatus.MISSING.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("Missing")
.withMessage("JobManager deployment not found")
.build(),
JobManagerDeploymentStatus.DEPLOYING.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("Deploying")
.withMessage("JobManager process is starting up")
.build(),
JobManagerDeploymentStatus.DEPLOYED_NOT_READY.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("DeployedNotReady")
.withMessage(
"JobManager is running but not ready yet to receive REST API calls")
.build(),
JobManagerDeploymentStatus.ERROR.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("Error")
.withMessage("JobManager deployment failed")
.build());

public static final Map<String, Condition> APPLICATION_MODE_CONDITION =
Copy link
Contributor

Choose a reason for hiding this comment

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

To me this entire mapping feels like that it should be replaced with a method,

return new ConditionBuilder()
                            .withType(CONDITION_TYPE_RUNNING)
                            .withStatus(status == RUNNING ? "True" : "False")
                            .withReason(status.name())
                            .withMessage("Job state " + status.name)
                            .build() 

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I have a them in method. As reason is expected to be in camelcase, I have added a method to make it as camecase from status.name().

Map.of(
JobStatus.RECONCILING.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("Reconciling")
.withMessage("Job is currently reconciling")
.build(),
JobStatus.CREATED.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("JobCreated")
.withMessage("Job is created")
.build(),
JobStatus.RUNNING.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("True")
.withReason("JobRunning")
.withMessage("Job is running")
.build(),
JobStatus.FAILING.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("JobFailing")
.withMessage("Job has failed")
.build(),
JobStatus.RESTARTING.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("JobRestarting")
.withMessage("The job is currently restarting")
.build(),
JobStatus.FAILED.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("JobFailed")
.withMessage("The job has failed with a non-recoverable task failure")
.build(),
JobStatus.FINISHED.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("JobFinished")
.withMessage("Job's tasks have successfully finished")
.build(),
JobStatus.CANCELED.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("JobCancelled")
.withMessage("Job has been cancelled")
.build(),
JobStatus.SUSPENDED.name(),
new ConditionBuilder()
.withType(CONDITION_TYPE_RUNNING)
.withStatus("False")
.withReason("JobSuspended")
.withMessage("The job has been suspended")
.build());
}
Loading