Skip to content

Commit

Permalink
fix(AliCloud): Fix health check bug from load balancing to backend se…
Browse files Browse the repository at this point in the history
…rvice (#4371)

Co-authored-by: wb-wyh625345 <wb-wyh625345@alibaba-inc.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 11, 2020
1 parent 79414f8 commit 3730d66
Show file tree
Hide file tree
Showing 19 changed files with 542 additions and 134 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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
* 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,
Expand Down Expand Up @@ -165,11 +165,12 @@ public static String getServerGroupKey(
String autoScalingGroupName, String account, String region) {
AliCloudServerGroup serverGroup = new AliCloudServerGroup();
serverGroup.setName(autoScalingGroupName);
// Names names = Names.parseName(autoScalingGroupName);
return getServerGroupKey(
serverGroup.getMoniker().getCluster(), autoScalingGroupName, account, region);
}

static String getServerGroupKey(
public static String getServerGroupKey(
String cluster, String autoScalingGroupName, String account, String region) {
return ID
+ SEPARATOR
Expand Down Expand Up @@ -236,6 +237,32 @@ public static String getInstanceKey(String instanceId, String account, String re
return key;
}

public static String getInstanceHealthKey(
String loadBalancerId,
String instanceId,
String port,
String account,
String region,
String provider) {
String key =
ID
+ SEPARATOR
+ com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.HEALTH
+ SEPARATOR
+ loadBalancerId
+ SEPARATOR
+ instanceId
+ SEPARATOR
+ port
+ SEPARATOR
+ account
+ SEPARATOR
+ region
+ SEPARATOR
+ provider;
return key;
}

@Override
public Map<String, String> parseKey(String key) {
return parse(key);
Expand All @@ -257,13 +284,17 @@ public static Map<String, String> parse(String key) {

switch (result.get("type")) {
case "securityGroups":
Names names = Names.parseName(parts[4]);
result.put("application", names.getApp());
result.put("name", parts[4]);
result.put("id", parts[5]);
result.put("region", parts[3]);
result.put("account", parts[2]);
result.put("vpcId", parts[6] == "null" ? null : parts[6]);
if (parts.length >= 7 && !"null".equals(parts[6])) {
Names names = Names.parseName(parts[4]);
result.put("application", names.getApp());
result.put("name", parts[4]);
result.put("id", parts[5]);
result.put("region", parts[3]);
result.put("account", parts[2]);
result.put("vpcId", parts[6]);
} else {
return null;
}
break;
default:
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2019 Alibaba Group.
*
* Licensed 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 com.netflix.spinnaker.clouddriver.alicloud.common;

import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.HEALTH;

import com.netflix.spinnaker.cats.cache.Cache;
import com.netflix.spinnaker.cats.cache.CacheData;
import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider;
import com.netflix.spinnaker.clouddriver.model.HealthState;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

public class HealthHelper {

private static boolean healthyStateMatcher(String key, String loadBalancerId, String instanceId) {
String regex;
if (StringUtils.isNotBlank(loadBalancerId)) {
regex = AliCloudProvider.ID + ":.*:" + loadBalancerId + ":" + instanceId + ":.*";
return Pattern.matches(regex, key);
} else {
regex = AliCloudProvider.ID + ":.*:" + instanceId + ":.*";
}
return Pattern.matches(regex, key);
}

public static HealthState judgeInstanceHealthyState(
Collection<String> allHealthyKeys,
List<String> loadBalancerIds,
String instanceId,
Cache cacheView) {
Set<String> healthyKeys = new HashSet<>();
if (loadBalancerIds != null) {
for (String loadBalancerId : loadBalancerIds) {
List<String> collect =
allHealthyKeys.stream()
.filter(tab -> HealthHelper.healthyStateMatcher(tab, loadBalancerId, instanceId))
.collect(Collectors.toList());
Collection<CacheData> healthData = cacheView.getAll(HEALTH.ns, collect, null);
if (CollectionUtils.isEmpty(healthData)) {
return HealthState.Unknown;
}
healthyKeys.addAll(collect);
}
} else {
List<String> collect =
allHealthyKeys.stream()
.filter(tab -> HealthHelper.healthyStateMatcher(tab, null, instanceId))
.collect(Collectors.toList());
healthyKeys.addAll(collect);
}
Collection<CacheData> healthData = cacheView.getAll(HEALTH.ns, healthyKeys, null);
Map<String, Integer> healthMap = new HashMap<>(16);
for (CacheData cacheData : healthData) {
String serverHealthStatus = cacheData.getAttributes().get("serverHealthStatus").toString();
healthMap.put(serverHealthStatus, healthMap.getOrDefault(serverHealthStatus, 0) + 1);
}
Integer normal = healthMap.get("normal");
Integer abnormal = healthMap.get("abnormal");
if (normal != null && normal > 0 && abnormal == null) {
return HealthState.Up;
} else if (abnormal != null && abnormal > 0 && normal == null) {
return HealthState.Down;
} else if (abnormal == null && normal == null) {
return HealthState.Down;
} else {
return HealthState.Unknown;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public Map operate(List priorOutputs) {
if (!StringUtils.isEmpty(description.getVSwitchId())) {
loadBalancerRequest.setVSwitchId(description.getVSwitchId());
}
if ("internet".equalsIgnoreCase(loadBalancerRequest.getAddressType())) {
loadBalancerRequest.setVSwitchId("");
}

// Instance delete protection off
loadBalancerRequest.setDeleteProtection("off");
CreateLoadBalancerResponse loadBalancerResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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
* 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,
Expand All @@ -19,7 +19,6 @@
import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider;
import com.netflix.spinnaker.clouddriver.model.LoadBalancer;
import com.netflix.spinnaker.clouddriver.model.LoadBalancerServerGroup;
import com.netflix.spinnaker.moniker.Moniker;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -45,8 +44,6 @@ public class AliCloudLoadBalancer implements LoadBalancer {

Map<String, String> labels = new HashMap<>();

Moniker moniker = new Moniker();

public AliCloudLoadBalancer(
String account, String region, String name, String vpcId, String loadBalancerId) {
this.account = account;
Expand All @@ -61,11 +58,6 @@ public String getName() {
return name;
}

@Override
public Moniker getMoniker() {
return moniker;
}

@Override
public String getType() {
return type;
Expand Down Expand Up @@ -102,4 +94,8 @@ public String getVpcId() {
public String getLoadBalancerId() {
return loadBalancerId;
}

public void setServerGroups(Set<LoadBalancerServerGroup> serverGroups) {
this.serverGroups = serverGroups;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.netflix.spinnaker.cats.agent.Agent;
import com.netflix.spinnaker.clouddriver.alicloud.AliCloudProvider;
import com.netflix.spinnaker.clouddriver.alicloud.common.ClientFactory;
import com.netflix.spinnaker.clouddriver.alicloud.provider.agent.AliCloudLoadBalancerCachingAgent;
import com.netflix.spinnaker.clouddriver.alicloud.provider.agent.*;
import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudClientProvider;
import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentials;
import com.netflix.spinnaker.clouddriver.alicloud.security.AliCloudCredentialsProvider;
Expand All @@ -32,6 +32,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
Expand All @@ -49,6 +50,7 @@ public AliProvider aliProvider(
Registry registry,
ObjectMapper objectMapper,
AliCloudProvider aliCloudProvider,
ApplicationContext ctx,
ClientFactory clientFactory) {
AliProvider provider =
new AliProvider(
Expand All @@ -62,6 +64,7 @@ public AliProvider aliProvider(
registry,
objectMapper,
aliCloudProvider,
ctx,
clientFactory);
return provider;
}
Expand All @@ -76,6 +79,7 @@ public AliProviderSynchronizer synchronizeAliProvider(
Registry registry,
ObjectMapper objectMapper,
AliCloudProvider aliCloudProvider,
ApplicationContext ctx,
ClientFactory clientFactory) {

Set<String> scheduledAccounts = ProviderUtils.getScheduledAccounts(aliProvider);
Expand All @@ -101,6 +105,14 @@ public AliProviderSynchronizer synchronizeAliProvider(
credentials,
clientFactory.createClient(
region, credentials.getAccessKeyId(), credentials.getAccessSecretKey())));
newAgents.add(
new AliCloudLoadBalancerInstanceStateCachingAgent(
ctx,
credentials,
region,
objectMapper,
clientFactory.createClient(
region, credentials.getAccessKeyId(), credentials.getAccessSecretKey())));
}
}
}
Expand Down
Loading

0 comments on commit 3730d66

Please sign in to comment.