Skip to content

Commit 36f0be2

Browse files
[Optimization][dinky-getaway] Add Deployment status monitoring. (#3989)
Co-authored-by: yuhang2.zhang <yuhang2.zhang@ly.com>
1 parent 4585355 commit 36f0be2

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

dinky-gateway/src/main/java/org/dinky/gateway/kubernetes/utils/K8sClientHelper.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.dinky.gateway.kubernetes.utils;
2121

2222
import org.dinky.gateway.kubernetes.decorate.DinkySqlConfigMapDecorate;
23+
import org.dinky.gateway.kubernetes.watcher.DeploymentStatusWatcher;
2324
import org.dinky.utils.TextUtil;
2425

2526
import org.apache.flink.configuration.Configuration;
@@ -49,6 +50,7 @@
4950
import io.fabric8.kubernetes.client.Config;
5051
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
5152
import io.fabric8.kubernetes.client.KubernetesClient;
53+
import io.fabric8.kubernetes.client.dsl.RollableScalableResource;
5254
import io.fabric8.kubernetes.client.utils.Serialization;
5355
import lombok.Data;
5456
import lombok.extern.slf4j.Slf4j;
@@ -65,9 +67,11 @@ public class K8sClientHelper {
6567
private KubernetesClient kubernetesClient;
6668
protected Configuration configuration;
6769
private DinkySqlConfigMapDecorate sqlFileDecorate;
70+
private DeploymentStatusWatcher deploymentStatusWatch;
6871

6972
public K8sClientHelper(Configuration configuration, String kubeConfig) {
7073
this.configuration = configuration;
74+
deploymentStatusWatch = new DeploymentStatusWatcher();
7175
initKubeClient(kubeConfig);
7276
}
7377

@@ -113,12 +117,12 @@ private void initKubeClient(String kubeConfig) {
113117
*/
114118
public Deployment createDinkyResource() {
115119
log.info("createDinkyResource");
116-
Deployment deployment = kubernetesClient
120+
RollableScalableResource<Deployment> deploymentRollableScalableResource = kubernetesClient
117121
.apps()
118122
.deployments()
119123
.inNamespace(configuration.get(KubernetesConfigOptions.NAMESPACE))
120-
.withName(configuration.get(KubernetesConfigOptions.CLUSTER_ID))
121-
.get();
124+
.withName(configuration.get(KubernetesConfigOptions.CLUSTER_ID));
125+
Deployment deployment = deploymentRollableScalableResource.get();
122126
List<HasMetadata> resources = getSqlFileDecorate().buildResources();
123127
// set owner reference
124128
OwnerReference deploymentOwnerReference = new OwnerReferenceBuilder()
@@ -134,13 +138,15 @@ public Deployment createDinkyResource() {
134138
resource.getMetadata().setOwnerReferences(Collections.singletonList(deploymentOwnerReference)));
135139
// create resources
136140
resources.forEach(resource -> log.info(Serialization.asYaml(resource)));
141+
deploymentRollableScalableResource.watch(deploymentStatusWatch);
137142
kubernetesClient.resourceList(resources).createOrReplace();
138143
return deployment;
139144
}
140145

141146
/**
142147
* initPodTemplate
143148
* Preprocess the pod template
149+
*
144150
* @param sqlStatement
145151
* @return
146152
*/
@@ -166,8 +172,7 @@ public Pod decoratePodTemplate(String sqlStatement, String podTemplate) {
166172

167173
/**
168174
* dumpPod2Str
169-
*
170-
* */
175+
*/
171176
public String dumpPod2Str(Pod pod) {
172177
// use snakyaml to serialize the pod
173178
Representer representer = new IgnoreNullRepresenter();
@@ -179,9 +184,11 @@ public String dumpPod2Str(Pod pod) {
179184
Yaml yaml = new Yaml(representer, options);
180185
return yaml.dump(pod);
181186
}
187+
182188
/**
183189
* close
184190
* delete the temporary directory and close the client
191+
*
185192
* @return
186193
*/
187194
public boolean close() {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
*
3+
* Licensed to the Apache Software Foundation (ASF) under one or more
4+
* contributor license agreements. See the NOTICE file distributed with
5+
* this work for additional information regarding copyright ownership.
6+
* The ASF licenses this file to You under the Apache License, Version 2.0
7+
* (the "License"); you may not use this file except in compliance with
8+
* the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*/
19+
20+
package org.dinky.gateway.kubernetes.watcher;
21+
22+
import org.apache.hadoop.util.StringUtils;
23+
24+
import java.util.List;
25+
26+
import cn.hutool.core.collection.CollectionUtil;
27+
import cn.hutool.core.util.ObjectUtil;
28+
import io.fabric8.kubernetes.api.model.apps.Deployment;
29+
import io.fabric8.kubernetes.api.model.apps.DeploymentCondition;
30+
import io.fabric8.kubernetes.client.Watcher;
31+
import io.fabric8.kubernetes.client.WatcherException;
32+
import lombok.extern.slf4j.Slf4j;
33+
34+
@Slf4j
35+
public class DeploymentStatusWatcher implements Watcher<Deployment> {
36+
37+
@Override
38+
public void eventReceived(Action action, Deployment deployment) {
39+
String deploymentName = deployment.getMetadata().getName();
40+
log.info("deployment name: {}, deployment action: {}", deploymentName, action);
41+
if (ObjectUtil.isNotNull(deployment.getStatus())
42+
&& CollectionUtil.isNotEmpty(deployment.getStatus().getConditions())) {
43+
List<DeploymentCondition> conditions = deployment.getStatus().getConditions();
44+
conditions.forEach(condition -> {
45+
if (StringUtils.equalsIgnoreCase(condition.getStatus(), "true")) {
46+
log.info(
47+
"deployment name: {}, deployment status: {}, message: {}",
48+
deploymentName,
49+
condition.getStatus(),
50+
condition.getMessage());
51+
} else {
52+
log.warn(
53+
"deployment name: {}, deployment status: {}, message: {}",
54+
deploymentName,
55+
condition.getStatus(),
56+
condition.getMessage());
57+
}
58+
});
59+
}
60+
}
61+
62+
@Override
63+
public void onClose(WatcherException cause) {
64+
if (cause != null) {
65+
log.error("Watcher closed due to exception: {}", cause.getMessage());
66+
} else {
67+
log.info("Watcher closed gracefully.");
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)