Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(alicloud): added alicloud code #5737

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions clouddriver-alicloud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,25 @@
The clouddriver-alicloud module aims to deploy an application on Alibaba Cloud.

It is a work in progress

### Configuring clouddriver.yml

```yaml
alicloud:
enabled: true
accounts:
- name: aksk_test
accessKeyId: <accessKeyId>
accessSecretKey: <accessSecretKey>
regions:
- cn-hangzhou
- cn-shanghai
- name: assumeRole_test
accountId: <managed accountid>
assumeRole: role/spinnakermanaged
regions:
- cn-beijing
defaultRegion: cn-hangzhou
accessKeyId: <managing accesskey>
accessSecretKey: <managing accessSecretKey>
```
6 changes: 5 additions & 1 deletion clouddriver-alicloud/clouddriver-alicloud.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
dependencies {
implementation project(":cats:cats-core")
implementation project(":clouddriver-api")
implementation project(":clouddriver-core")
implementation project(":clouddriver-eureka")
implementation project(":clouddriver-security")
implementation project(":clouddriver-configserver")


compileOnly "org.projectlombok:lombok"
annotationProcessor "org.projectlombok:lombok"
Expand All @@ -22,10 +25,11 @@ dependencies {
implementation 'com.jcraft:jsch.agentproxy.jsch:0.0.9'
implementation 'com.jcraft:jsch.agentproxy.connector-factory:0.0.9'
implementation 'com.aliyun:aliyun-java-sdk-core:4.4.2'
implementation 'com.aliyun:aliyun-java-sdk-slb:3.2.9'
implementation 'com.aliyun:aliyun-java-sdk-slb:3.3.14'
implementation 'com.aliyun:aliyun-java-sdk-vpc:3.0.6'
implementation 'com.aliyun:aliyun-java-sdk-ecs:4.16.10'
implementation 'com.aliyun:aliyun-java-sdk-ess:2.3.2'
implementation 'com.aliyun:aliyun-java-sdk-sts:3.0.0'
implementation "org.springframework.boot:spring-boot-actuator"
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.codehaus.groovy:groovy-all"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.netflix.frigga.Names;
import com.netflix.spinnaker.clouddriver.alicloud.model.AliCloudServerGroup;
import com.netflix.spinnaker.clouddriver.cache.KeyParser;
import com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -52,7 +53,11 @@ public String toString() {
public static final String SEPARATOR = ":";

public static String getLoadBalancerKey(
String loadBalancerName, String account, String region, String vpcId) {
String loadBalancerName,
String account,
String region,
String vpcId,
String loadBalancerType) {
String key =
ID
+ SEPARATOR
Expand All @@ -62,10 +67,11 @@ public static String getLoadBalancerKey(
+ SEPARATOR
+ region
+ SEPARATOR
+ loadBalancerName;
if (!StringUtils.isEmpty(vpcId)) {
key = key + SEPARATOR + vpcId;
}
+ loadBalancerName
+ SEPARATOR
+ (vpcId == null ? "" : vpcId)
+ SEPARATOR
+ (loadBalancerType == null ? "" : loadBalancerType);
return key;
}

Expand Down Expand Up @@ -292,15 +298,33 @@ public static Map<String, String> parse(String key) {
result.put("region", parts[3]);
result.put("account", parts[2]);
result.put("vpcId", parts[6]);
return result;
} else {
return null;
}
case "clusters":
if (parts.length >= 5) {
result.put("application", parts[2]);
result.put("account", parts[3]);
result.put("clusterName", parts[4]);
return result;
} else {
return null;
}

case "health":
if (parts.length >= 8) {
result.put("loadBalancerId", parts[2]);
result.put("instanceId", parts[3]);
result.put("port", parts[4]);
result.put("account", parts[5]);
return result;
} else {
return null;
}
break;
default:
return null;
}

return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2022 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 com.netflix.spinnaker.cats.cache.CacheData;
import java.util.Map;
import org.jetbrains.annotations.Nullable;

public class CacheDataHelper {
@Nullable
public static CacheData merge(CacheData o1, CacheData o2) {
if (o2 == null) {
return o1;
}
if (o1 == null) {
return o1;
}

if (!o1.getId().equals(o2.getId())) {
throw new RuntimeException("Different ID cannot merge");
}

Map<String, Object> attributes = o1.getAttributes();
attributes.putAll(o2.getAttributes());
o2.getRelationships()
.forEach(
(k, v) -> {
if (o1.getRelationships().containsKey(k)) {
o1.getRelationships().get(k).addAll(v);
} else {
o1.getRelationships().put(k, v);
}
});
o1.getRelationships().forEach((k, v) -> v.addAll(o2.getRelationships().get(k)));
return o1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.auth.AlibabaCloudCredentialsProvider;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

@Component
public class ClientFactory {

public IAcsClient createClient(String region, String accessKeyId, String accessSecretKey) {
DefaultProfile profile = DefaultProfile.getProfile(region, accessKeyId, accessSecretKey);
DefaultAcsClient defaultAcsClient = new DefaultAcsClient(profile);
public IAcsClient createClient(String region, AlibabaCloudCredentialsProvider credentials) {
DefaultProfile profile = DefaultProfile.getProfile(region);
DefaultAcsClient defaultAcsClient = new DefaultAcsClient(profile, credentials);
defaultAcsClient.appendUserAgent("Spinnaker", "Clouddriver v1.0");
return defaultAcsClient;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2022 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 java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateParseHelper {
static SimpleDateFormat YYYY_MM_DD_HH_MM_SS_UTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");

public static Date parseUTCTime(String utcTime) throws ParseException {
if (utcTime == null || utcTime.length() == 0) {
return null;
}

if (utcTime.endsWith("UTC")) {
utcTime = utcTime.replace("Z", " UTC");
}
return YYYY_MM_DD_HH_MM_SS_UTC.parse(utcTime);
}

public static String format(Date date) {
if (date == null) {
return null;
}
return YYYY_MM_DD_HH_MM_SS_UTC.format(date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,43 @@

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.alicloud.cache.Keys;
import com.netflix.spinnaker.clouddriver.alicloud.model.alienum.LifecycleState;
import com.netflix.spinnaker.clouddriver.alicloud.model.alienum.ScalingInstanceHealthStatus;
import com.netflix.spinnaker.clouddriver.model.HealthState;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
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 + ":.*";
public static boolean healthyStateMatcher(
String key, Set<String> loadBalancerId, String instanceId) {

if (StringUtils.isBlank(key)) {
return false;
}
return Pattern.matches(regex, key);

Map<String, String> details = Keys.parse(key);
if (details == null || details.isEmpty()) {
return false;
}

boolean instanceMatch = StringUtils.equals(instanceId, details.get("instanceId"));
if (loadBalancerId == null || loadBalancerId.isEmpty()) {
return instanceMatch;
}
return loadBalancerId.contains(details.get("loadBalancerId"));
}

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);
private static HealthState judgeInstanceHealthyState(Collection<CacheData> healthData) {
if (CollectionUtils.isEmpty(healthData)) {
return HealthState.Unknown;
}
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();
Expand All @@ -84,4 +70,37 @@ public static HealthState judgeInstanceHealthyState(
return HealthState.Unknown;
}
}

public static ScalingInstanceHealthStatus loadBalancerInstanceHealthState(
String scalingGroupLifecycleState,
String scalingInstanceInstanceHealthStatus,
Collection<CacheData> healthData) {
if (!LifecycleState.Active.name().equals(scalingGroupLifecycleState)) {
return ScalingInstanceHealthStatus.Unhealthy;
}

if (!ScalingInstanceHealthStatus.Healthy.name().equals(scalingInstanceInstanceHealthStatus)) {
return ScalingInstanceHealthStatus.Unhealthy;
}
HealthState healthState = judgeInstanceHealthyState(healthData);
return ScalingInstanceHealthStatus.forState(healthState);
}

public static HealthState genInstanceHealthState(
String instanceStatus, Collection<CacheData> healthData) {
return "Running".equals(instanceStatus)
? judgeInstanceHealthyState(healthData)
: HealthState.Down;
}

public static HealthState genInstanceHealthState(
String scalingGroupLifecycleState, String instanceStatus, Collection<CacheData> healthData) {
if (!LifecycleState.Active.name().equals(scalingGroupLifecycleState)) {
return HealthState.Down;
}
if (!"Healthy".equals(instanceStatus)) {
return HealthState.Down;
}
return HealthHelper.judgeInstanceHealthyState(healthData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2022 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 java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Sets {
public static Set<String> ofModifiable(String... arr) {
return Stream.of(arr).collect(Collectors.toCollection(HashSet::new));
}
}
Loading