Skip to content

Commit 3db9e7f

Browse files
committed
modify async
1 parent 3c388a4 commit 3db9e7f

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.highgo.platform.operator.watcher;
19+
20+
import com.highgo.platform.operator.cr.DatabaseCluster;
21+
import io.fabric8.kubernetes.client.KubernetesClient;
22+
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
23+
import io.fabric8.kubernetes.client.informers.SharedInformerFactory;
24+
import org.springframework.stereotype.Component;
25+
26+
import javax.annotation.Resource;
27+
28+
/**
29+
* watcher工厂类
30+
*/
31+
@Component
32+
public class CloudAbstractWatcherFactory extends AbstractWatcherFactory {
33+
34+
@Resource
35+
private CloudClusterWatcher cloudClusterWatcher;
36+
37+
@Override
38+
public void registerInformers(KubernetesClient kubernetesClient, SharedInformerFactory sharedInformerFactory,
39+
String clusterId) {
40+
SharedIndexInformer<DatabaseCluster> clusterSharedIndexInformer =
41+
sharedInformerFactory.sharedIndexInformerForCustomResource(
42+
DatabaseCluster.class, 30 * 1000L);
43+
cloudClusterWatcher.setClusterId(clusterId);
44+
cloudClusterWatcher.setKubernetesClient(kubernetesClient);
45+
46+
clusterSharedIndexInformer.addEventHandler(cloudClusterWatcher.initResourceEventHandler());
47+
}
48+
49+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.highgo.platform.operator.watcher;
19+
20+
import com.highgo.cloud.util.CommonUtil;
21+
import com.highgo.platform.apiserver.model.vo.response.ClusterInfoVO;
22+
import com.highgo.platform.apiserver.service.K8sClusterService;
23+
import com.highgo.platform.operator.ElectLeader;
24+
import io.fabric8.kubernetes.api.model.Namespace;
25+
import org.springframework.beans.factory.annotation.Value;
26+
import org.springframework.boot.ApplicationArguments;
27+
import org.springframework.boot.ApplicationRunner;
28+
import org.springframework.stereotype.Component;
29+
30+
import javax.annotation.Resource;
31+
import java.util.List;
32+
import java.util.Optional;
33+
34+
@Component
35+
public class PostApplicationStart implements ApplicationRunner {
36+
37+
@Resource
38+
private K8sClusterService k8sClusterService;
39+
@Resource
40+
private ElectLeader electLeader;
41+
@Value("${common.namespace:ivory}")
42+
private String namespace;
43+
@Override
44+
public void run(ApplicationArguments args) throws Exception {
45+
List<ClusterInfoVO> list = k8sClusterService.list();
46+
if (CommonUtil.isEmpty(list)) {
47+
return;
48+
}
49+
50+
ClusterInfoVO clusterInfoVO = list.get(0);
51+
List<Namespace> namespaces = k8sClusterService.getNamespace(clusterInfoVO.getClusterId());
52+
53+
if (!CommonUtil.isEmpty(namespaces)) {
54+
Optional<Namespace> namespaceOptional =
55+
namespaces.stream().filter(item -> item.getMetadata().getName().equals(namespace)).findFirst();
56+
if (!namespaceOptional.isPresent()) {
57+
k8sClusterService.createNamespace(clusterInfoVO.getClusterId(), namespace);
58+
}
59+
}
60+
electLeader.initLeaderElector();
61+
}
62+
}

0 commit comments

Comments
 (0)