|
| 1 | +/* |
| 2 | + * Copyright 2013-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.kubernetes.client.leader.election; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Objects; |
| 21 | +import java.util.function.BooleanSupplier; |
| 22 | + |
| 23 | +import io.kubernetes.client.extended.leaderelection.LeaderElectionConfig; |
| 24 | +import io.kubernetes.client.extended.leaderelection.Lock; |
| 25 | +import io.kubernetes.client.extended.leaderelection.resourcelock.ConfigMapLock; |
| 26 | +import io.kubernetes.client.extended.leaderelection.resourcelock.LeaseLock; |
| 27 | +import io.kubernetes.client.openapi.ApiClient; |
| 28 | +import io.kubernetes.client.openapi.ApiException; |
| 29 | +import io.kubernetes.client.openapi.apis.CoreV1Api; |
| 30 | +import io.kubernetes.client.openapi.apis.CustomObjectsApi; |
| 31 | +import io.kubernetes.client.openapi.models.V1APIResource; |
| 32 | +import io.kubernetes.client.openapi.models.V1Pod; |
| 33 | +import io.kubernetes.client.openapi.models.V1PodCondition; |
| 34 | + |
| 35 | +import org.springframework.boot.actuate.autoconfigure.info.ConditionalOnEnabledInfoContributor; |
| 36 | +import org.springframework.boot.actuate.info.InfoContributor; |
| 37 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 38 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 39 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 40 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform; |
| 41 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 42 | +import org.springframework.boot.cloud.CloudPlatform; |
| 43 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 44 | +import org.springframework.cloud.kubernetes.commons.leader.election.ConditionalOnLeaderElectionEnabled; |
| 45 | +import org.springframework.cloud.kubernetes.commons.leader.election.LeaderElectionProperties; |
| 46 | +import org.springframework.context.annotation.Bean; |
| 47 | +import org.springframework.context.annotation.Configuration; |
| 48 | +import org.springframework.core.log.LogAccessor; |
| 49 | + |
| 50 | +import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.COORDINATION_GROUP; |
| 51 | +import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.COORDINATION_VERSION; |
| 52 | + |
| 53 | +/** |
| 54 | + * @author wind57 |
| 55 | + */ |
| 56 | +@Configuration(proxyBeanMethods = false) |
| 57 | +@EnableConfigurationProperties(LeaderElectionProperties.class) |
| 58 | +@ConditionalOnBean(ApiClient.class) |
| 59 | +@ConditionalOnLeaderElectionEnabled |
| 60 | +@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) |
| 61 | +@AutoConfigureAfter(KubernetesClientLeaderElectionCallbacksAutoConfiguration.class) |
| 62 | +class KubernetesClientLeaderElectionAutoConfiguration { |
| 63 | + |
| 64 | + private static final LogAccessor LOG = new LogAccessor(KubernetesClientLeaderElectionAutoConfiguration.class); |
| 65 | + |
| 66 | + @Bean |
| 67 | + @ConditionalOnClass(InfoContributor.class) |
| 68 | + @ConditionalOnEnabledInfoContributor("leader.election") |
| 69 | + KubernetesClientLeaderElectionInfoContributor kubernetesClientLeaderElectionInfoContributor( |
| 70 | + String candidateIdentity, LeaderElectionConfig leaderElectionConfig) { |
| 71 | + return new KubernetesClientLeaderElectionInfoContributor(candidateIdentity, leaderElectionConfig); |
| 72 | + } |
| 73 | + |
| 74 | + @Bean |
| 75 | + @ConditionalOnMissingBean |
| 76 | + KubernetesClientLeaderElectionInitiator kubernetesClientLeaderElectionInitiator(String candidateIdentity, |
| 77 | + String podNamespace, LeaderElectionConfig leaderElectionConfig, |
| 78 | + LeaderElectionProperties leaderElectionProperties, BooleanSupplier podReadySupplier, |
| 79 | + KubernetesClientLeaderElectionCallbacks callbacks) { |
| 80 | + return new KubernetesClientLeaderElectionInitiator(candidateIdentity, podNamespace, leaderElectionConfig, |
| 81 | + leaderElectionProperties, podReadySupplier, callbacks); |
| 82 | + } |
| 83 | + |
| 84 | + @Bean |
| 85 | + @ConditionalOnMissingBean |
| 86 | + BooleanSupplier kubernetesClientPodReadySupplier(CoreV1Api coreV1Api, String candidateIdentity, |
| 87 | + String podNamespace) { |
| 88 | + return () -> { |
| 89 | + try { |
| 90 | + V1Pod pod = coreV1Api.readNamespacedPod(candidateIdentity, podNamespace).execute(); |
| 91 | + return isPodReady(pod); |
| 92 | + } |
| 93 | + catch (ApiException e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + @Bean |
| 100 | + @ConditionalOnMissingBean |
| 101 | + LeaderElectionConfig kubernetesClientLeaderElectionConfig(LeaderElectionProperties properties, Lock lock) { |
| 102 | + return new LeaderElectionConfig(lock, properties.leaseDuration(), properties.renewDeadline(), |
| 103 | + properties.retryPeriod()); |
| 104 | + } |
| 105 | + |
| 106 | + @Bean |
| 107 | + @ConditionalOnMissingBean |
| 108 | + Lock kubernetesClientLeaderElectionLock(ApiClient apiClient, LeaderElectionProperties properties, |
| 109 | + String candidateIdentity) { |
| 110 | + |
| 111 | + CustomObjectsApi customObjectsApi = new CustomObjectsApi(apiClient); |
| 112 | + boolean leaseSupported; |
| 113 | + try { |
| 114 | + List<V1APIResource> resources = customObjectsApi.getAPIResources(COORDINATION_GROUP, COORDINATION_VERSION) |
| 115 | + .execute() |
| 116 | + .getResources(); |
| 117 | + |
| 118 | + leaseSupported = resources.stream().map(V1APIResource::getKind).anyMatch("Lease"::equals); |
| 119 | + } |
| 120 | + catch (ApiException e) { |
| 121 | + throw new RuntimeException(e); |
| 122 | + } |
| 123 | + |
| 124 | + if (leaseSupported) { |
| 125 | + if (properties.useConfigMapAsLock()) { |
| 126 | + LOG.info(() -> "leases are supported on the cluster, but config map will be used " |
| 127 | + + "(because 'spring.cloud.kubernetes.leader.election.use-config-map-as-lock=true')"); |
| 128 | + return new ConfigMapLock(properties.lockNamespace(), properties.lockName(), candidateIdentity); |
| 129 | + } |
| 130 | + else { |
| 131 | + LOG.info(() -> "will use lease as the lock for leader election"); |
| 132 | + return new LeaseLock(properties.lockNamespace(), properties.lockName(), candidateIdentity, apiClient); |
| 133 | + } |
| 134 | + } |
| 135 | + else { |
| 136 | + LOG.info(() -> "will use configmap as the lock for leader election"); |
| 137 | + return new ConfigMapLock(properties.lockNamespace(), properties.lockName(), candidateIdentity, apiClient); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // following two methods are a verbatim copy of the fabric8 implementation |
| 142 | + private static boolean isPodReady(V1Pod pod) { |
| 143 | + Objects.requireNonNull(pod, "Pod can't be null."); |
| 144 | + V1PodCondition condition = getPodReadyCondition(pod); |
| 145 | + |
| 146 | + if (condition == null) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + return condition.getStatus().equalsIgnoreCase("True"); |
| 150 | + } |
| 151 | + |
| 152 | + private static V1PodCondition getPodReadyCondition(V1Pod pod) { |
| 153 | + if (pod.getStatus() == null || pod.getStatus().getConditions() == null) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + for (V1PodCondition condition : pod.getStatus().getConditions()) { |
| 158 | + if ("Ready".equals(condition.getType())) { |
| 159 | + return condition; |
| 160 | + } |
| 161 | + } |
| 162 | + return null; |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments