You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by gy...@apache.org on 2022/10/27 13:59:44 UTC

[flink-kubernetes-operator] branch main updated: [FLINK-29708] Convert CR Error field to JSON with enriched exception metadata

This is an automated email from the ASF dual-hosted git repository.

gyfora pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/flink-kubernetes-operator.git


The following commit(s) were added to refs/heads/main by this push:
     new 3c636538 [FLINK-29708] Convert CR Error field to JSON with enriched exception metadata
3c636538 is described below

commit 3c63653820742993926ff8901cfaa618a2f855cb
Author: darenwkt <da...@amazon.co.uk>
AuthorDate: Wed Oct 26 21:34:44 2022 +0100

    [FLINK-29708] Convert CR Error field to JSON with enriched exception metadata
---
 .../kubernetes_operator_config_configuration.html  |  24 +++
 .../shortcodes/generated/system_section.html       |  24 +++
 .../config/FlinkOperatorConfiguration.java         |  21 +++
 .../config/KubernetesOperatorConfigOptions.java    |  32 ++++
 .../controller/FlinkDeploymentController.java      |  13 +-
 .../controller/FlinkSessionJobController.java      |  10 +-
 .../operator/exception/FlinkResourceException.java |  49 +++++
 .../exception/MissingJobManagerException.java      |  33 ++++
 .../exception/MissingSessionJobException.java      |  33 ++++
 .../operator/exception/UnknownJobException.java    |  33 ++++
 .../operator/exception/ValidationException.java    |  33 ++++
 .../operator/observer/JobStatusObserver.java       |  38 ++--
 .../AbstractFlinkDeploymentObserver.java           |   6 +-
 .../observer/deployment/ApplicationObserver.java   |  16 +-
 .../sessionjob/FlinkSessionJobObserver.java        |  15 +-
 .../operator/reconciler/ReconciliationUtils.java   |  26 +--
 .../utils/FlinkResourceExceptionUtils.java         | 156 ++++++++++++++++
 .../controller/FlinkDeploymentControllerTest.java  |  17 +-
 .../sessionjob/FlinkSessionJobObserverTest.java    |   7 +-
 .../utils/FlinkResourceExceptionUtilsTest.java     | 203 +++++++++++++++++++++
 .../flink-kubernetes-operator/conf/flink-conf.yaml |   4 +
 21 files changed, 737 insertions(+), 56 deletions(-)

diff --git a/docs/layouts/shortcodes/generated/kubernetes_operator_config_configuration.html b/docs/layouts/shortcodes/generated/kubernetes_operator_config_configuration.html
index acdcaccb..99550e98 100644
--- a/docs/layouts/shortcodes/generated/kubernetes_operator_config_configuration.html
+++ b/docs/layouts/shortcodes/generated/kubernetes_operator_config_configuration.html
@@ -68,6 +68,30 @@
             <td>Boolean</td>
             <td>Enables dynamic change of watched/monitored namespaces.</td>
         </tr>
+        <tr>
+            <td><h5>kubernetes.operator.exception.field.max.length</h5></td>
+            <td style="word-wrap: break-word;">2048</td>
+            <td>Integer</td>
+            <td>Maximum length of each exception field including stack trace to be included in CR status error field.</td>
+        </tr>
+        <tr>
+            <td><h5>kubernetes.operator.exception.stacktrace.enabled</h5></td>
+            <td style="word-wrap: break-word;">false</td>
+            <td>Boolean</td>
+            <td>Enable exception stacktrace to be included in CR status error field.</td>
+        </tr>
+        <tr>
+            <td><h5>kubernetes.operator.exception.stacktrace.max.length</h5></td>
+            <td style="word-wrap: break-word;">2048</td>
+            <td>Integer</td>
+            <td>Maximum length of stacktrace to be included in CR status error field.</td>
+        </tr>
+        <tr>
+            <td><h5>kubernetes.operator.exception.throwable.list.max.count</h5></td>
+            <td style="word-wrap: break-word;">2</td>
+            <td>Integer</td>
+            <td>Maximum number of throwable to be included in CR status error field.</td>
+        </tr>
         <tr>
             <td><h5>kubernetes.operator.flink.client.cancel.timeout</h5></td>
             <td style="word-wrap: break-word;">1 min</td>
diff --git a/docs/layouts/shortcodes/generated/system_section.html b/docs/layouts/shortcodes/generated/system_section.html
index a4428aa7..3baf1715 100644
--- a/docs/layouts/shortcodes/generated/system_section.html
+++ b/docs/layouts/shortcodes/generated/system_section.html
@@ -14,6 +14,30 @@
             <td>Boolean</td>
             <td>Enables dynamic change of watched/monitored namespaces.</td>
         </tr>
+        <tr>
+            <td><h5>kubernetes.operator.exception.field.max.length</h5></td>
+            <td style="word-wrap: break-word;">2048</td>
+            <td>Integer</td>
+            <td>Maximum length of each exception field including stack trace to be included in CR status error field.</td>
+        </tr>
+        <tr>
+            <td><h5>kubernetes.operator.exception.stacktrace.enabled</h5></td>
+            <td style="word-wrap: break-word;">false</td>
+            <td>Boolean</td>
+            <td>Enable exception stacktrace to be included in CR status error field.</td>
+        </tr>
+        <tr>
+            <td><h5>kubernetes.operator.exception.stacktrace.max.length</h5></td>
+            <td style="word-wrap: break-word;">2048</td>
+            <td>Integer</td>
+            <td>Maximum length of stacktrace to be included in CR status error field.</td>
+        </tr>
+        <tr>
+            <td><h5>kubernetes.operator.exception.throwable.list.max.count</h5></td>
+            <td style="word-wrap: break-word;">2</td>
+            <td>Integer</td>
+            <td>Maximum number of throwable to be included in CR status error field.</td>
+        </tr>
         <tr>
             <td><h5>kubernetes.operator.flink.client.cancel.timeout</h5></td>
             <td style="word-wrap: break-word;">1 min</td>
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/config/FlinkOperatorConfiguration.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/config/FlinkOperatorConfiguration.java
index a4065bce..3a54847a 100644
--- a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/config/FlinkOperatorConfiguration.java
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/config/FlinkOperatorConfiguration.java
@@ -53,6 +53,10 @@ public class FlinkOperatorConfiguration {
     Integer savepointHistoryCountThreshold;
     Duration savepointHistoryAgeThreshold;
     RetryConfiguration retryConfiguration;
+    Boolean exceptionStackTraceEnabled;
+    int exceptionStackTraceLengthThreshold;
+    int exceptionFieldLengthThreshold;
+    int exceptionThrowableCountThreshold;
     String labelSelector;
 
     public static FlinkOperatorConfiguration fromConfiguration(Configuration operatorConfig) {
@@ -94,6 +98,19 @@ public class FlinkOperatorConfiguration {
                 operatorConfig.get(
                         KubernetesOperatorConfigOptions
                                 .OPERATOR_SAVEPOINT_HISTORY_MAX_AGE_THRESHOLD);
+        Boolean exceptionStackTraceEnabled =
+                operatorConfig.get(
+                        KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_STACK_TRACE_ENABLED);
+        int exceptionStackTraceLengthThreshold =
+                operatorConfig.get(
+                        KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_STACK_TRACE_MAX_LENGTH);
+        int exceptionFieldLengthThreshold =
+                operatorConfig.get(
+                        KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_FIELD_MAX_LENGTH);
+        int exceptionThrowableCountThreshold =
+                operatorConfig.get(
+                        KubernetesOperatorConfigOptions
+                                .OPERATOR_EXCEPTION_THROWABLE_LIST_MAX_COUNT);
 
         String flinkServiceHostOverride = null;
         if (EnvUtils.get(EnvUtils.ENV_KUBERNETES_SERVICE_HOST).isEmpty()) {
@@ -147,6 +164,10 @@ public class FlinkOperatorConfiguration {
                 savepointHistoryCountThreshold,
                 savepointHistoryAgeThreshold,
                 retryConfiguration,
+                exceptionStackTraceEnabled,
+                exceptionStackTraceLengthThreshold,
+                exceptionFieldLengthThreshold,
+                exceptionThrowableCountThreshold,
                 labelSelector);
     }
 
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/config/KubernetesOperatorConfigOptions.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/config/KubernetesOperatorConfigOptions.java
index 8c18f249..9ffbef7b 100644
--- a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/config/KubernetesOperatorConfigOptions.java
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/config/KubernetesOperatorConfigOptions.java
@@ -212,6 +212,38 @@ public class KubernetesOperatorConfigOptions {
                     .withDescription(
                             "Maximum age for savepoint history entries to retain. Due to lazy clean-up, the most recent savepoint may live longer than the max age.");
 
+    @Documentation.Section(SECTION_SYSTEM)
+    public static final ConfigOption<Boolean> OPERATOR_EXCEPTION_STACK_TRACE_ENABLED =
+            operatorConfig("exception.stacktrace.enabled")
+                    .booleanType()
+                    .defaultValue(false)
+                    .withDescription(
+                            "Enable exception stacktrace to be included in CR status error field.");
+
+    @Documentation.Section(SECTION_SYSTEM)
+    public static final ConfigOption<Integer> OPERATOR_EXCEPTION_STACK_TRACE_MAX_LENGTH =
+            operatorConfig("exception.stacktrace.max.length")
+                    .intType()
+                    .defaultValue(2048)
+                    .withDescription(
+                            "Maximum length of stacktrace to be included in CR status error field.");
+
+    @Documentation.Section(SECTION_SYSTEM)
+    public static final ConfigOption<Integer> OPERATOR_EXCEPTION_FIELD_MAX_LENGTH =
+            operatorConfig("exception.field.max.length")
+                    .intType()
+                    .defaultValue(2048)
+                    .withDescription(
+                            "Maximum length of each exception field including stack trace to be included in CR status error field.");
+
+    @Documentation.Section(SECTION_SYSTEM)
+    public static final ConfigOption<Integer> OPERATOR_EXCEPTION_THROWABLE_LIST_MAX_COUNT =
+            operatorConfig("exception.throwable.list.max.count")
+                    .intType()
+                    .defaultValue(2)
+                    .withDescription(
+                            "Maximum number of throwable to be included in CR status error field.");
+
     @Documentation.Section(SECTION_ADVANCED)
     public static final ConfigOption<Duration> OPERATOR_SAVEPOINT_HISTORY_MAX_AGE_THRESHOLD =
             ConfigOptions.key(OPERATOR_SAVEPOINT_HISTORY_MAX_AGE.key() + ".threshold")
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkDeploymentController.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkDeploymentController.java
index b08493ab..cfeabe37 100644
--- a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkDeploymentController.java
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkDeploymentController.java
@@ -143,7 +143,8 @@ public class FlinkDeploymentController
         LOG.error("Flink Deployment failed", dfe);
         flinkApp.getStatus().setJobManagerDeploymentStatus(JobManagerDeploymentStatus.ERROR);
         flinkApp.getStatus().getJobStatus().setState(JobStatus.RECONCILING.name());
-        ReconciliationUtils.updateForReconciliationError(flinkApp, dfe.getMessage());
+        ReconciliationUtils.updateForReconciliationError(
+                flinkApp, dfe, configManager.getOperatorConfiguration());
         eventRecorder.triggerEvent(
                 flinkApp,
                 EventRecorder.Type.Warning,
@@ -164,7 +165,11 @@ public class FlinkDeploymentController
     public ErrorStatusUpdateControl<FlinkDeployment> updateErrorStatus(
             FlinkDeployment flinkDeployment, Context<FlinkDeployment> context, Exception e) {
         return ReconciliationUtils.toErrorStatusUpdateControl(
-                flinkDeployment, context.getRetryInfo(), e, statusRecorder);
+                flinkDeployment,
+                context.getRetryInfo(),
+                e,
+                statusRecorder,
+                configManager.getOperatorConfiguration());
     }
 
     private boolean validateDeployment(FlinkDeployment deployment) {
@@ -178,7 +183,9 @@ public class FlinkDeploymentController
                         EventRecorder.Component.Operator,
                         validationError.get());
                 return ReconciliationUtils.applyValidationErrorAndResetSpec(
-                        deployment, validationError.get());
+                        deployment,
+                        validationError.get(),
+                        configManager.getOperatorConfiguration());
             }
         }
         return true;
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkSessionJobController.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkSessionJobController.java
index 61dc9512..6372a5e7 100644
--- a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkSessionJobController.java
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkSessionJobController.java
@@ -123,7 +123,11 @@ public class FlinkSessionJobController
     public ErrorStatusUpdateControl<FlinkSessionJob> updateErrorStatus(
             FlinkSessionJob sessionJob, Context<FlinkSessionJob> context, Exception e) {
         return ReconciliationUtils.toErrorStatusUpdateControl(
-                sessionJob, context.getRetryInfo(), e, statusRecorder);
+                sessionJob,
+                context.getRetryInfo(),
+                e,
+                statusRecorder,
+                configManager.getOperatorConfiguration());
     }
 
     @Override
@@ -146,7 +150,9 @@ public class FlinkSessionJobController
                         EventRecorder.Component.Operator,
                         validationError.get());
                 return ReconciliationUtils.applyValidationErrorAndResetSpec(
-                        sessionJob, validationError.get());
+                        sessionJob,
+                        validationError.get(),
+                        configManager.getOperatorConfiguration());
             }
         }
         return true;
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/FlinkResourceException.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/FlinkResourceException.java
new file mode 100644
index 00000000..69167f4b
--- /dev/null
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/FlinkResourceException.java
@@ -0,0 +1,49 @@
+/*
+ * 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.exception;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import lombok.Builder;
+import lombok.Data;
+import lombok.extern.jackson.Jacksonized;
+
+import java.util.List;
+import java.util.Map;
+
+/** Exception to display as JSON in CRD. */
+@Data
+@Jacksonized
+@Builder
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class FlinkResourceException {
+
+    /** Exception Type. */
+    String type;
+
+    /** Exception Message. */
+    String message;
+
+    /** Exception StackTrace. */
+    String stackTrace;
+
+    /** Custom Exception Metadata. */
+    Map<String, Object> additionalMetadata;
+
+    /** List of Throwables converted to FlinkResourceException Type. */
+    List<FlinkResourceException> throwableList;
+}
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/MissingJobManagerException.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/MissingJobManagerException.java
new file mode 100644
index 00000000..e3e042c2
--- /dev/null
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/MissingJobManagerException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception;
+
+/** Exception to signal missing job manager. */
+public class MissingJobManagerException extends RuntimeException {
+    public MissingJobManagerException(Throwable cause) {
+        super(cause);
+    }
+
+    public MissingJobManagerException(String msg) {
+        super(msg);
+    }
+
+    public MissingJobManagerException(String msg, Throwable cause) {
+        super(msg, cause);
+    }
+}
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/MissingSessionJobException.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/MissingSessionJobException.java
new file mode 100644
index 00000000..ab926195
--- /dev/null
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/MissingSessionJobException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception;
+
+/** Exception to signal missing session job. */
+public class MissingSessionJobException extends RuntimeException {
+    public MissingSessionJobException(Throwable cause) {
+        super(cause);
+    }
+
+    public MissingSessionJobException(String msg) {
+        super(msg);
+    }
+
+    public MissingSessionJobException(String msg, Throwable cause) {
+        super(msg, cause);
+    }
+}
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/UnknownJobException.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/UnknownJobException.java
new file mode 100644
index 00000000..34bff05e
--- /dev/null
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/UnknownJobException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception;
+
+/** Exception to signal unrecognized job found. */
+public class UnknownJobException extends RuntimeException {
+    public UnknownJobException(Throwable cause) {
+        super(cause);
+    }
+
+    public UnknownJobException(String msg) {
+        super(msg);
+    }
+
+    public UnknownJobException(String msg, Throwable cause) {
+        super(msg, cause);
+    }
+}
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/ValidationException.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/ValidationException.java
new file mode 100644
index 00000000..f3964592
--- /dev/null
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/exception/ValidationException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception;
+
+/** Exception to signal validation failure. */
+public class ValidationException extends RuntimeException {
+    public ValidationException(Throwable cause) {
+        super(cause);
+    }
+
+    public ValidationException(String msg) {
+        super(msg);
+    }
+
+    public ValidationException(String msg, Throwable cause) {
+        super(msg, cause);
+    }
+}
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/JobStatusObserver.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/JobStatusObserver.java
index b6aa3283..5ad5a412 100644
--- a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/JobStatusObserver.java
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/JobStatusObserver.java
@@ -18,6 +18,7 @@
 package org.apache.flink.kubernetes.operator.observer;
 
 import org.apache.flink.configuration.Configuration;
+import org.apache.flink.kubernetes.operator.config.FlinkConfigManager;
 import org.apache.flink.kubernetes.operator.crd.AbstractFlinkResource;
 import org.apache.flink.kubernetes.operator.crd.status.JobStatus;
 import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils;
@@ -34,6 +35,8 @@ import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.TimeoutException;
 
+import static org.apache.flink.kubernetes.operator.utils.FlinkResourceExceptionUtils.updateFlinkResourceException;
+
 /** An observer to observe the job status. */
 public abstract class JobStatusObserver<
         R extends AbstractFlinkResource<?, ?>, CTX extends ObserverContext> {
@@ -45,10 +48,15 @@ public abstract class JobStatusObserver<
 
     protected final FlinkService flinkService;
     protected final EventRecorder eventRecorder;
+    protected final FlinkConfigManager configManager;
 
-    public JobStatusObserver(FlinkService flinkService, EventRecorder eventRecorder) {
+    public JobStatusObserver(
+            FlinkService flinkService,
+            FlinkConfigManager configManager,
+            EventRecorder eventRecorder) {
         this.flinkService = flinkService;
         this.eventRecorder = eventRecorder;
+        this.configManager = configManager;
     }
 
     /**
@@ -198,32 +206,16 @@ public abstract class JobStatusObserver<
                 result.getSerializedThrowable()
                         .ifPresent(
                                 t -> {
-                                    var error = t.getFullStringifiedStackTrace();
-                                    var trimmedError = getErrorWithMaxLength(error);
-                                    trimmedError.ifPresent(
-                                            value -> {
-                                                if (!value.equals(
-                                                        resource.getStatus().getError())) {
-                                                    resource.getStatus().setError(value);
-                                                    LOG.error(
-                                                            "Job {} failed with error: {}",
-                                                            clusterJobStatus.getJobId(),
-                                                            error);
-                                                }
-                                            });
+                                    updateFlinkResourceException(
+                                            t, resource, configManager.getOperatorConfiguration());
+                                    LOG.error(
+                                            "Job {} failed with error: {}",
+                                            clusterJobStatus.getJobId(),
+                                            t.getFullStringifiedStackTrace());
                                 });
             } catch (Exception e) {
                 LOG.warn("Failed to request the job result", e);
             }
         }
     }
-
-    private Optional<String> getErrorWithMaxLength(String error) {
-        if (error == null) {
-            return Optional.empty();
-        } else {
-            return Optional.of(
-                    error.substring(0, Math.min(error.length(), MAX_ERROR_STRING_LENGTH)));
-        }
-    }
 }
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/deployment/AbstractFlinkDeploymentObserver.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/deployment/AbstractFlinkDeploymentObserver.java
index 62ace0a2..b5deb5ac 100644
--- a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/deployment/AbstractFlinkDeploymentObserver.java
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/deployment/AbstractFlinkDeploymentObserver.java
@@ -27,6 +27,7 @@ import org.apache.flink.kubernetes.operator.crd.spec.JobState;
 import org.apache.flink.kubernetes.operator.crd.status.FlinkDeploymentStatus;
 import org.apache.flink.kubernetes.operator.crd.status.JobManagerDeploymentStatus;
 import org.apache.flink.kubernetes.operator.exception.DeploymentFailedException;
+import org.apache.flink.kubernetes.operator.exception.MissingJobManagerException;
 import org.apache.flink.kubernetes.operator.observer.AbstractFlinkResourceObserver;
 import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils;
 import org.apache.flink.kubernetes.operator.service.FlinkService;
@@ -230,7 +231,10 @@ public abstract class AbstractFlinkDeploymentObserver
     private void onMissingDeployment(FlinkDeployment deployment) {
         String err = "Missing JobManager deployment";
         logger.error(err);
-        ReconciliationUtils.updateForReconciliationError(deployment, err);
+        ReconciliationUtils.updateForReconciliationError(
+                deployment,
+                new MissingJobManagerException(err),
+                configManager.getOperatorConfiguration());
         eventRecorder.triggerEvent(
                 deployment,
                 EventRecorder.Type.Warning,
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/deployment/ApplicationObserver.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/deployment/ApplicationObserver.java
index abb156fb..8a7ef957 100644
--- a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/deployment/ApplicationObserver.java
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/deployment/ApplicationObserver.java
@@ -22,6 +22,7 @@ import org.apache.flink.kubernetes.operator.config.FlinkConfigManager;
 import org.apache.flink.kubernetes.operator.crd.FlinkDeployment;
 import org.apache.flink.kubernetes.operator.crd.status.FlinkDeploymentStatus;
 import org.apache.flink.kubernetes.operator.crd.status.JobStatus;
+import org.apache.flink.kubernetes.operator.exception.UnknownJobException;
 import org.apache.flink.kubernetes.operator.observer.ClusterHealthObserver;
 import org.apache.flink.kubernetes.operator.observer.JobStatusObserver;
 import org.apache.flink.kubernetes.operator.observer.SavepointObserver;
@@ -53,7 +54,8 @@ public class ApplicationObserver extends AbstractFlinkDeploymentObserver {
         super(flinkService, configManager, eventRecorder);
         this.savepointObserver =
                 new SavepointObserver<>(flinkService, configManager, eventRecorder);
-        this.jobStatusObserver = new ApplicationJobObserver(flinkService, eventRecorder);
+        this.jobStatusObserver =
+                new ApplicationJobObserver(flinkService, configManager, eventRecorder);
         this.clusterHealthObserver = new ClusterHealthObserver(flinkService);
     }
 
@@ -76,8 +78,11 @@ public class ApplicationObserver extends AbstractFlinkDeploymentObserver {
 
     private class ApplicationJobObserver
             extends JobStatusObserver<FlinkDeployment, FlinkDeploymentObserverContext> {
-        public ApplicationJobObserver(FlinkService flinkService, EventRecorder eventRecorder) {
-            super(flinkService, eventRecorder);
+        public ApplicationJobObserver(
+                FlinkService flinkService,
+                FlinkConfigManager configManager,
+                EventRecorder eventRecorder) {
+            super(flinkService, configManager, eventRecorder);
         }
 
         @Override
@@ -117,7 +122,10 @@ public class ApplicationObserver extends AbstractFlinkDeploymentObserver {
                     .setState(org.apache.flink.api.common.JobStatus.RECONCILING.name());
             String err = "Unrecognized Job for Application deployment";
             logger.error(err);
-            ReconciliationUtils.updateForReconciliationError(deployment, err);
+            ReconciliationUtils.updateForReconciliationError(
+                    deployment,
+                    new UnknownJobException(err),
+                    configManager.getOperatorConfiguration());
             eventRecorder.triggerEvent(
                     deployment,
                     EventRecorder.Type.Warning,
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/sessionjob/FlinkSessionJobObserver.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/sessionjob/FlinkSessionJobObserver.java
index 1b9414e8..829e5938 100644
--- a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/sessionjob/FlinkSessionJobObserver.java
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/observer/sessionjob/FlinkSessionJobObserver.java
@@ -23,6 +23,7 @@ import org.apache.flink.kubernetes.operator.config.FlinkConfigManager;
 import org.apache.flink.kubernetes.operator.crd.FlinkSessionJob;
 import org.apache.flink.kubernetes.operator.crd.status.FlinkSessionJobStatus;
 import org.apache.flink.kubernetes.operator.crd.status.JobStatus;
+import org.apache.flink.kubernetes.operator.exception.MissingSessionJobException;
 import org.apache.flink.kubernetes.operator.observer.AbstractFlinkResourceObserver;
 import org.apache.flink.kubernetes.operator.observer.JobStatusObserver;
 import org.apache.flink.kubernetes.operator.observer.SavepointObserver;
@@ -80,7 +81,8 @@ public class FlinkSessionJobObserver
             Context<?> ctx,
             FlinkSessionJobObserverContext observerContext) {
 
-        var jobStatusObserver = new SessionJobStatusObserver(observerContext, eventRecorder);
+        var jobStatusObserver =
+                new SessionJobStatusObserver(observerContext, configManager, eventRecorder);
         var jobFound = jobStatusObserver.observe(flinkSessionJob, ctx, observerContext);
 
         if (jobFound) {
@@ -157,8 +159,10 @@ public class FlinkSessionJobObserver
             extends JobStatusObserver<FlinkSessionJob, FlinkSessionJobObserverContext> {
 
         public SessionJobStatusObserver(
-                FlinkSessionJobObserverContext observerContext, EventRecorder eventRecorder) {
-            super(observerContext.getFlinkService(), eventRecorder);
+                FlinkSessionJobObserverContext observerContext,
+                FlinkConfigManager configManager,
+                EventRecorder eventRecorder) {
+            super(observerContext.getFlinkService(), configManager, eventRecorder);
         }
 
         @Override
@@ -218,7 +222,10 @@ public class FlinkSessionJobObserver
                     .getJobStatus()
                     .setState(org.apache.flink.api.common.JobStatus.RECONCILING.name());
             LOG.error(MISSING_SESSION_JOB_ERR);
-            ReconciliationUtils.updateForReconciliationError(sessionJob, MISSING_SESSION_JOB_ERR);
+            ReconciliationUtils.updateForReconciliationError(
+                    sessionJob,
+                    new MissingSessionJobException(MISSING_SESSION_JOB_ERR),
+                    configManager.getOperatorConfiguration());
             eventRecorder.triggerEvent(
                     sessionJob,
                     EventRecorder.Type.Warning,
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/ReconciliationUtils.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/ReconciliationUtils.java
index cec8d1c2..9d9da074 100644
--- a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/ReconciliationUtils.java
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/ReconciliationUtils.java
@@ -31,7 +31,7 @@ import org.apache.flink.kubernetes.operator.crd.status.ReconciliationState;
 import org.apache.flink.kubernetes.operator.crd.status.SavepointInfo;
 import org.apache.flink.kubernetes.operator.crd.status.SavepointTriggerType;
 import org.apache.flink.kubernetes.operator.crd.status.TaskManagerInfo;
-import org.apache.flink.kubernetes.operator.exception.ReconciliationException;
+import org.apache.flink.kubernetes.operator.exception.ValidationException;
 import org.apache.flink.kubernetes.operator.utils.FlinkUtils;
 import org.apache.flink.kubernetes.operator.utils.StatusRecorder;
 import org.apache.flink.util.Preconditions;
@@ -52,6 +52,7 @@ import java.util.Optional;
 
 import static org.apache.flink.api.common.JobStatus.FINISHED;
 import static org.apache.flink.api.common.JobStatus.RUNNING;
+import static org.apache.flink.kubernetes.operator.utils.FlinkResourceExceptionUtils.updateFlinkResourceException;
 
 /** Reconciliation utilities. */
 public class ReconciliationUtils {
@@ -172,8 +173,8 @@ public class ReconciliationUtils {
     }
 
     public static void updateForReconciliationError(
-            AbstractFlinkResource<?, ?> target, String error) {
-        target.getStatus().setError(error);
+            AbstractFlinkResource<?, ?> target, Throwable error, FlinkOperatorConfiguration conf) {
+        updateFlinkResourceException(error, target, conf);
     }
 
     public static <T> T clone(T object) {
@@ -355,12 +356,15 @@ public class ReconciliationUtils {
      *     reconcile.
      */
     public static <SPEC extends AbstractFlinkSpec> boolean applyValidationErrorAndResetSpec(
-            AbstractFlinkResource<SPEC, ?> deployment, String validationError) {
+            AbstractFlinkResource<SPEC, ?> deployment,
+            String validationError,
+            FlinkOperatorConfiguration conf) {
 
         var status = deployment.getStatus();
         if (!validationError.equals(status.getError())) {
             LOG.error("Validation failed: " + validationError);
-            ReconciliationUtils.updateForReconciliationError(deployment, validationError);
+            ReconciliationUtils.updateForReconciliationError(
+                    deployment, new ValidationException(validationError), conf);
         }
 
         var lastReconciledSpec = status.getReconciliationStatus().deserializeLastReconciledSpec();
@@ -383,12 +387,13 @@ public class ReconciliationUtils {
      * Update the resource error status and metrics when the operator encountered an exception
      * during reconciliation.
      *
+     * @param <STATUS> Status type.
+     * @param <R> Resource type.
      * @param resource Flink Resource to be updated
      * @param retryInfo Current RetryInformation
      * @param e Exception that caused the retry
      * @param statusRecorder statusRecorder object for patching status
-     * @param <STATUS> Status type.
-     * @param <R> Resource type.
+     * @param operatorConfiguration
      * @return This always returns Empty optional currently, due to the status update logic
      */
     public static <STATUS extends CommonStatus<?>, R extends AbstractFlinkResource<?, STATUS>>
@@ -396,7 +401,8 @@ public class ReconciliationUtils {
                     R resource,
                     Optional<RetryInfo> retryInfo,
                     Exception e,
-                    StatusRecorder<R, STATUS> statusRecorder) {
+                    StatusRecorder<R, STATUS> statusRecorder,
+                    FlinkOperatorConfiguration operatorConfiguration) {
 
         retryInfo.ifPresent(
                 r ->
@@ -406,9 +412,7 @@ public class ReconciliationUtils {
                                 r.isLastAttempt()));
 
         statusRecorder.updateStatusFromCache(resource);
-        ReconciliationUtils.updateForReconciliationError(
-                resource,
-                (e instanceof ReconciliationException) ? e.getCause().toString() : e.toString());
+        ReconciliationUtils.updateForReconciliationError(resource, e, operatorConfiguration);
         statusRecorder.patchAndCacheStatus(resource);
 
         // Status was updated already, no need to return anything
diff --git a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/utils/FlinkResourceExceptionUtils.java b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/utils/FlinkResourceExceptionUtils.java
new file mode 100644
index 00000000..aedee902
--- /dev/null
+++ b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/utils/FlinkResourceExceptionUtils.java
@@ -0,0 +1,156 @@
+/*
+ * 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.utils;
+
+import org.apache.flink.kubernetes.operator.config.FlinkOperatorConfiguration;
+import org.apache.flink.kubernetes.operator.crd.AbstractFlinkResource;
+import org.apache.flink.kubernetes.operator.exception.DeploymentFailedException;
+import org.apache.flink.kubernetes.operator.exception.FlinkResourceException;
+import org.apache.flink.kubernetes.operator.exception.ReconciliationException;
+import org.apache.flink.runtime.rest.util.RestClientException;
+import org.apache.flink.util.Preconditions;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/** Flink Resource Exception utilities. */
+public final class FlinkResourceExceptionUtils {
+
+    private static final ObjectMapper objectMapper = new ObjectMapper();
+
+    public static <R extends AbstractFlinkResource> void updateFlinkResourceException(
+            Throwable throwable, R resource, FlinkOperatorConfiguration conf) {
+
+        boolean stackTraceEnabled = conf.getExceptionStackTraceEnabled();
+        int stackTraceLengthThreshold = conf.getExceptionStackTraceLengthThreshold();
+        int lengthThreshold = conf.getExceptionFieldLengthThreshold();
+        int throwableCountThreshold = conf.getExceptionThrowableCountThreshold();
+
+        Preconditions.checkNotNull(stackTraceEnabled);
+
+        FlinkResourceException flinkResourceException =
+                getFlinkResourceException(
+                        throwable,
+                        stackTraceEnabled,
+                        stackTraceLengthThreshold,
+                        lengthThreshold,
+                        throwableCountThreshold);
+
+        try {
+            ((AbstractFlinkResource<?, ?>) resource)
+                    .getStatus()
+                    .setError(convertToJson(flinkResourceException));
+        } catch (Exception e) {
+            // Rollback to setting error string/message to CRD
+            ((AbstractFlinkResource<?, ?>) resource)
+                    .getStatus()
+                    .setError(
+                            (e instanceof ReconciliationException)
+                                    ? e.getCause().toString()
+                                    : e.toString());
+        }
+    }
+
+    private static FlinkResourceException getFlinkResourceException(
+            Throwable throwable,
+            boolean isStackTraceEnabled,
+            int stackTraceLengthThreshold,
+            int lengthThreshold,
+            int throwableCountThreshold) {
+        FlinkResourceException flinkResourceException =
+                convertToFlinkResourceException(
+                        throwable, isStackTraceEnabled, stackTraceLengthThreshold, lengthThreshold);
+
+        flinkResourceException.setThrowableList(
+                ExceptionUtils.getThrowableList(throwable.getCause()).stream()
+                        .limit(throwableCountThreshold)
+                        .map(
+                                (t) ->
+                                        convertToFlinkResourceException(
+                                                t,
+                                                false,
+                                                stackTraceLengthThreshold,
+                                                lengthThreshold))
+                        .collect(Collectors.toList()));
+
+        return flinkResourceException;
+    }
+
+    private static FlinkResourceException convertToFlinkResourceException(
+            Throwable throwable,
+            boolean stackTraceEnabled,
+            int stackTraceLengthThreshold,
+            int lengthThreshold) {
+        FlinkResourceException flinkResourceException = FlinkResourceException.builder().build();
+
+        getSubstringWithMaxLength(throwable.getClass().getName(), lengthThreshold)
+                .ifPresent(flinkResourceException::setType);
+        getSubstringWithMaxLength(throwable.getMessage(), lengthThreshold)
+                .ifPresent(flinkResourceException::setMessage);
+
+        if (stackTraceEnabled) {
+            getSubstringWithMaxLength(ExceptionUtils.getStackTrace(throwable), lengthThreshold)
+                    .ifPresent(flinkResourceException::setStackTrace);
+        }
+
+        enrichMetadata(throwable, flinkResourceException, lengthThreshold);
+
+        return flinkResourceException;
+    }
+
+    public static Optional<String> getSubstringWithMaxLength(String str, int limit) {
+        if (str == null) {
+            return Optional.empty();
+        } else {
+            return Optional.of(str.substring(0, Math.min(str.length(), limit)));
+        }
+    }
+
+    private static void enrichMetadata(
+            Throwable throwable,
+            FlinkResourceException flinkResourceException,
+            int lengthThreshold) {
+        if (throwable instanceof RestClientException) {
+            flinkResourceException.setAdditionalMetadata(
+                    Map.of(
+                            "httpResponseCode",
+                            ((RestClientException) throwable).getHttpResponseStatus().code()));
+        }
+
+        if (throwable instanceof DeploymentFailedException) {
+            getSubstringWithMaxLength(
+                            ((DeploymentFailedException) throwable).getReason(), lengthThreshold)
+                    .ifPresent(
+                            reason ->
+                                    flinkResourceException.setAdditionalMetadata(
+                                            Map.of("reason", reason)));
+        }
+
+        // This section can be extended to enrich more metadata in the future.
+    }
+
+    private static String convertToJson(FlinkResourceException flinkResourceException)
+            throws JsonProcessingException {
+        return objectMapper.writeValueAsString(flinkResourceException);
+    }
+}
diff --git a/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/controller/FlinkDeploymentControllerTest.java b/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/controller/FlinkDeploymentControllerTest.java
index 6728e0cc..3d38cb83 100644
--- a/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/controller/FlinkDeploymentControllerTest.java
+++ b/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/controller/FlinkDeploymentControllerTest.java
@@ -209,8 +209,11 @@ public class FlinkDeploymentControllerTest {
         assertFalse(updateControl.isUpdateStatus());
 
         reconciliationStatus = appCluster.getStatus().getReconciliationStatus();
-        assertEquals(
-                "Cannot switch from job to session cluster", appCluster.getStatus().getError());
+        assertTrue(
+                appCluster
+                        .getStatus()
+                        .getError()
+                        .contains("Cannot switch from job to session cluster"));
         assertNotNull(reconciliationStatus.deserializeLastReconciledSpec().getJob());
 
         // Validate job status correct even with error
@@ -735,9 +738,11 @@ public class FlinkDeploymentControllerTest {
         appCluster.getSpec().getJobManager().setReplicas(-1);
         // Next reconcile will set error msg and observe with previous validated config
         updateControl = testController.reconcile(appCluster, context);
-        assertEquals(
-                "JobManager replicas should not be configured less than one.",
-                appCluster.getStatus().getError());
+        assertTrue(
+                appCluster
+                        .getStatus()
+                        .getError()
+                        .contains("JobManager replicas should not be configured less than one."));
         assertFalse(updateControl.isUpdateStatus());
         assertEquals(
                 JobManagerDeploymentStatus.DEPLOYED_NOT_READY,
@@ -948,7 +953,7 @@ public class FlinkDeploymentControllerTest {
         assertEquals(
                 JobManagerDeploymentStatus.ERROR,
                 appCluster.getStatus().getJobManagerDeploymentStatus());
-        assertEquals(crashLoopMessage, appCluster.getStatus().getError());
+        assertTrue(appCluster.getStatus().getError().contains(crashLoopMessage));
 
         // JobManager deployment becomes ready and successful observation should clear the errors
         testController.reconcile(appCluster, context);
diff --git a/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/observer/sessionjob/FlinkSessionJobObserverTest.java b/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/observer/sessionjob/FlinkSessionJobObserverTest.java
index d082ac74..427f74b1 100644
--- a/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/observer/sessionjob/FlinkSessionJobObserverTest.java
+++ b/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/observer/sessionjob/FlinkSessionJobObserverTest.java
@@ -164,8 +164,11 @@ public class FlinkSessionJobObserverTest {
         observer.observe(sessionJob2, readyContext);
         Assertions.assertEquals(
                 JobStatus.RECONCILING.name(), sessionJob2.getStatus().getJobStatus().getState());
-        Assertions.assertEquals(
-                JobStatusObserver.MISSING_SESSION_JOB_ERR, sessionJob2.getStatus().getError());
+        Assertions.assertTrue(
+                sessionJob2
+                        .getStatus()
+                        .getError()
+                        .contains(JobStatusObserver.MISSING_SESSION_JOB_ERR));
         Assertions.assertEquals(
                 JobStatusObserver.MISSING_SESSION_JOB_ERR,
                 eventCollector.events.peek().getMessage());
diff --git a/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/utils/FlinkResourceExceptionUtilsTest.java b/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/utils/FlinkResourceExceptionUtilsTest.java
new file mode 100644
index 00000000..33b06352
--- /dev/null
+++ b/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/utils/FlinkResourceExceptionUtilsTest.java
@@ -0,0 +1,203 @@
+/*
+ * 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.utils;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.kubernetes.operator.config.FlinkConfigManager;
+import org.apache.flink.kubernetes.operator.config.KubernetesOperatorConfigOptions;
+import org.apache.flink.kubernetes.operator.crd.AbstractFlinkResource;
+import org.apache.flink.kubernetes.operator.crd.FlinkDeployment;
+import org.apache.flink.kubernetes.operator.crd.FlinkSessionJob;
+import org.apache.flink.kubernetes.operator.crd.status.CommonStatus;
+import org.apache.flink.kubernetes.operator.exception.DeploymentFailedException;
+import org.apache.flink.kubernetes.operator.exception.FlinkResourceException;
+import org.apache.flink.kubernetes.operator.exception.MissingJobManagerException;
+import org.apache.flink.kubernetes.operator.exception.ReconciliationException;
+import org.apache.flink.runtime.rest.util.RestClientException;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Optional;
+
+import static org.apache.flink.kubernetes.operator.utils.FlinkResourceExceptionUtils.getSubstringWithMaxLength;
+import static org.apache.flink.kubernetes.operator.utils.FlinkResourceExceptionUtils.updateFlinkResourceException;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/** Test class for {@link FlinkResourceExceptionUtils}. */
+public class FlinkResourceExceptionUtilsTest {
+
+    @Test
+    public void testUpdateFlinkResourceExceptionWithConfig() throws JsonProcessingException {
+
+        Configuration testConfig = new Configuration();
+        FlinkConfigManager configManager = new FlinkConfigManager(testConfig);
+
+        assertEquals(
+                KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_STACK_TRACE_ENABLED
+                        .defaultValue(),
+                configManager.getOperatorConfiguration().getExceptionStackTraceEnabled());
+        assertEquals(
+                KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_STACK_TRACE_MAX_LENGTH
+                        .defaultValue(),
+                configManager.getOperatorConfiguration().getExceptionStackTraceLengthThreshold());
+        assertEquals(
+                KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_THROWABLE_LIST_MAX_COUNT
+                        .defaultValue(),
+                configManager.getOperatorConfiguration().getExceptionThrowableCountThreshold());
+        assertEquals(
+                KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_FIELD_MAX_LENGTH.defaultValue(),
+                configManager.getOperatorConfiguration().getExceptionFieldLengthThreshold());
+
+        testUpdateFlinkResourceException(configManager);
+
+        testConfig.set(
+                KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_STACK_TRACE_ENABLED, true);
+        testConfig.set(
+                KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_STACK_TRACE_MAX_LENGTH, 0);
+        testConfig.set(
+                KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_THROWABLE_LIST_MAX_COUNT, 0);
+        testConfig.set(KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_FIELD_MAX_LENGTH, 0);
+
+        configManager.updateDefaultConfig(testConfig);
+        testUpdateFlinkResourceException(configManager);
+
+        testConfig.set(
+                KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_STACK_TRACE_MAX_LENGTH, 100);
+        testConfig.set(
+                KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_THROWABLE_LIST_MAX_COUNT, 100);
+        testConfig.set(KubernetesOperatorConfigOptions.OPERATOR_EXCEPTION_FIELD_MAX_LENGTH, 100);
+
+        configManager.updateDefaultConfig(testConfig);
+        testUpdateFlinkResourceException(configManager);
+    }
+
+    private void testUpdateFlinkResourceException(FlinkConfigManager configManager)
+            throws JsonProcessingException {
+
+        ReconciliationException reconciliationException = getTestException();
+
+        List<AbstractFlinkResource> resources = getTestResources();
+
+        for (AbstractFlinkResource resource : resources) {
+
+            updateFlinkResourceException(
+                    reconciliationException, resource, configManager.getOperatorConfiguration());
+
+            String errorJson =
+                    Optional.ofNullable(resource)
+                            .map(r -> (CommonStatus) r.getStatus())
+                            .map(CommonStatus::getError)
+                            .orElseThrow(RuntimeException::new);
+
+            FlinkResourceException flinkResourceException =
+                    new ObjectMapper().readValue(errorJson, FlinkResourceException.class);
+
+            assertNull(flinkResourceException.getAdditionalMetadata());
+            assertEquals(
+                    getSubstringWithMaxLength(
+                                    "reconciliation exception message",
+                                    configManager
+                                            .getOperatorConfiguration()
+                                            .getExceptionFieldLengthThreshold())
+                            .get(),
+                    flinkResourceException.getMessage());
+            assertEquals(
+                    getSubstringWithMaxLength(
+                                    "org.apache.flink.kubernetes.operator.exception.ReconciliationException",
+                                    configManager
+                                            .getOperatorConfiguration()
+                                            .getExceptionFieldLengthThreshold())
+                            .get(),
+                    flinkResourceException.getType());
+
+            assertTrue(
+                    flinkResourceException.getThrowableList().size()
+                            <= configManager
+                                    .getOperatorConfiguration()
+                                    .getExceptionThrowableCountThreshold());
+
+            flinkResourceException
+                    .getThrowableList()
+                    .forEach(
+                            exception -> {
+                                if (exception.getType().contains("RestClientException")) {
+                                    assertEquals(
+                                            "rest client exception message",
+                                            exception.getMessage());
+                                    assertEquals(
+                                            400,
+                                            exception
+                                                    .getAdditionalMetadata()
+                                                    .get("httpResponseCode"));
+                                } else if (exception
+                                        .getType()
+                                        .contains("DeploymentFailedException")) {
+                                    assertEquals("dfe message", exception.getMessage());
+                                    assertEquals(
+                                            "dfe reason",
+                                            exception.getAdditionalMetadata().get("reason"));
+                                }
+                                assertTrue(
+                                        exception.getMessage().length()
+                                                <= configManager
+                                                        .getOperatorConfiguration()
+                                                        .getExceptionFieldLengthThreshold());
+                                assertTrue(
+                                        exception.getType().length()
+                                                <= configManager
+                                                        .getOperatorConfiguration()
+                                                        .getExceptionFieldLengthThreshold());
+                            });
+
+            if (!configManager.getOperatorConfiguration().getExceptionStackTraceEnabled()) {
+                assertNull(flinkResourceException.getStackTrace());
+            } else {
+                assertTrue(
+                        flinkResourceException.getStackTrace().length()
+                                <= configManager
+                                        .getOperatorConfiguration()
+                                        .getExceptionStackTraceLengthThreshold());
+            }
+        }
+    }
+
+    private ReconciliationException getTestException() {
+        return new ReconciliationException(
+                "reconciliation exception message",
+                new MissingJobManagerException(
+                        "missing job manager exception message",
+                        new RestClientException(
+                                "rest client exception message",
+                                new FlinkRuntimeException(
+                                        "flink runtime exception message",
+                                        new DeploymentFailedException("dfe message", "dfe reason")),
+                                new HttpResponseStatus(400, "http response status"))));
+    }
+
+    private List<AbstractFlinkResource> getTestResources() {
+        return List.of(new FlinkSessionJob(), new FlinkDeployment());
+    }
+}
diff --git a/helm/flink-kubernetes-operator/conf/flink-conf.yaml b/helm/flink-kubernetes-operator/conf/flink-conf.yaml
index b310cd6e..1c579c1c 100644
--- a/helm/flink-kubernetes-operator/conf/flink-conf.yaml
+++ b/helm/flink-kubernetes-operator/conf/flink-conf.yaml
@@ -39,6 +39,10 @@ parallelism.default: 2
 # kubernetes.operator.retry.initial.interval: 5 s
 # kubernetes.operator.retry.interval.multiplier: 2
 # kubernetes.operator.retry.max.attempts: 10
+# kubernetes.operator.exception.stacktrace.enabled: false
+# kubernetes.operator.exception.stacktrace.max.length: 2048
+# kubernetes.operator.exception.field.max.length: 2048
+# kubernetes.operator.exception.throwable.list.max.count: 2
 
 # kubernetes.operator.metrics.reporter.slf4j.factory.class: org.apache.flink.metrics.slf4j.Slf4jReporterFactory
 # kubernetes.operator.metrics.reporter.slf4j.interval: 5 MINUTE