|
| 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