-
Notifications
You must be signed in to change notification settings - Fork 1k
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(provider/ecs): ECS Cache base classes #2065
Merged
+597
−0
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
clouddriver-ecs/src/main/java/com/netflix/spinnaker/clouddriver/ecs/cache/Keys.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* * Copyright 2017 Lookout, Inc. | ||
* | ||
* 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.ecs.cache; | ||
|
||
import com.google.common.base.CaseFormat; | ||
import com.netflix.spinnaker.clouddriver.cache.KeyParser; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.HEALTH; | ||
import static com.netflix.spinnaker.clouddriver.ecs.EcsCloudProvider.ID; | ||
|
||
public class Keys implements KeyParser { | ||
public enum Namespace { | ||
IAM_ROLE, | ||
SERVICES, | ||
ECS_CLUSTERS, | ||
TASKS, | ||
CONTAINER_INSTANCES, | ||
TASK_DEFINITIONS; | ||
|
||
final String ns; | ||
|
||
Namespace() { | ||
ns = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, this.name()); | ||
} | ||
|
||
public String toString() { | ||
return ns; | ||
} | ||
} | ||
|
||
public static final String SEPARATOR = ":"; | ||
|
||
public static Map<String, String> parse(String key) { | ||
String[] parts = key.split(SEPARATOR); | ||
|
||
if (parts.length < 3 || !parts[0].equals(ID)) { | ||
return null; | ||
} | ||
|
||
Map<String, String> result = new HashMap<>(); | ||
result.put("provider", parts[0]); | ||
result.put("type", parts[1]); | ||
result.put("account", parts[2]); | ||
|
||
|
||
Namespace namespace = Namespace.valueOf(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, parts[1])); | ||
|
||
if (!namespace.equals(Namespace.IAM_ROLE)) { | ||
result.put("region", parts[3]); | ||
} | ||
|
||
switch (namespace) { | ||
case SERVICES: | ||
result.put("serviceName", parts[4]); | ||
break; | ||
case ECS_CLUSTERS: | ||
result.put("clusterName", parts[4]); | ||
break; | ||
case TASKS: | ||
result.put("taskName", parts[4]); | ||
break; | ||
case CONTAINER_INSTANCES: | ||
result.put("containerInstanceArn", parts[4]); | ||
break; | ||
case TASK_DEFINITIONS: | ||
result.put("taskDefinitionArn", parts[4]); | ||
break; | ||
case IAM_ROLE: | ||
result.put("roleName", parts[3]); | ||
default: | ||
break; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static String getServiceKey(String account, String region, String serviceName) { | ||
return ID + SEPARATOR + Namespace.SERVICES + SEPARATOR + account + SEPARATOR + region + SEPARATOR + serviceName; | ||
} | ||
|
||
public static String getClusterKey(String account, String region, String clusterName) { | ||
return ID + SEPARATOR + Namespace.ECS_CLUSTERS + SEPARATOR + account + SEPARATOR + region + SEPARATOR + clusterName; | ||
} | ||
|
||
public static String getTaskKey(String account, String region, String taskId) { | ||
return ID + SEPARATOR + Namespace.TASKS + SEPARATOR + account + SEPARATOR + region + SEPARATOR + taskId; | ||
} | ||
|
||
public static String getTaskHealthKey(String account, String region, String taskId) { | ||
return ID + SEPARATOR + HEALTH + SEPARATOR + account + SEPARATOR + region + SEPARATOR + taskId; | ||
} | ||
|
||
public static String getContainerInstanceKey(String account, String region, String containerInstanceArn) { | ||
return ID + SEPARATOR + Namespace.CONTAINER_INSTANCES + SEPARATOR + account + SEPARATOR + region + SEPARATOR + containerInstanceArn; | ||
} | ||
|
||
public static String getTaskDefinitionKey(String account, String region, String taskDefinitionArn) { | ||
return ID + SEPARATOR + Namespace.TASK_DEFINITIONS + SEPARATOR + account + SEPARATOR + region + SEPARATOR + taskDefinitionArn; | ||
} | ||
|
||
public static String getIamRoleKey(String account, String iamRoleName) { | ||
return ID + SEPARATOR + Namespace.IAM_ROLE + SEPARATOR + account + SEPARATOR + iamRoleName; | ||
} | ||
|
||
@Override | ||
public String getCloudProvider() { | ||
return ID; | ||
} | ||
|
||
@Override | ||
public Map<String, String> parseKey(String key) { | ||
return parse(key); | ||
} | ||
|
||
@Override | ||
public Boolean canParseType(String type) { | ||
for (Namespace key : Namespace.values()) { | ||
if (key.toString().equals(type)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Boolean canParseField(String type) { | ||
return false; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...src/main/java/com/netflix/spinnaker/clouddriver/ecs/cache/client/AbstractCacheClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.netflix.spinnaker.clouddriver.ecs.cache.client; | ||
|
||
import com.netflix.spinnaker.cats.cache.Cache; | ||
import com.netflix.spinnaker.cats.cache.CacheData; | ||
import com.netflix.spinnaker.clouddriver.ecs.cache.Keys; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
abstract class AbstractCacheClient<T> { | ||
|
||
private final String keyNamespace; | ||
private final Cache cacheView; | ||
|
||
/** | ||
* @param cacheView The Cache that the client will query. | ||
* @param keyNamespace The key namespace that the client is responsible for. | ||
*/ | ||
AbstractCacheClient(Cache cacheView, String keyNamespace) { | ||
this.cacheView = cacheView; | ||
this.keyNamespace = keyNamespace; | ||
} | ||
|
||
/** | ||
* @param cacheData CacheData that will be converted into an object. | ||
* @return An object of the generic type. | ||
*/ | ||
protected abstract T convert(CacheData cacheData); | ||
|
||
/** | ||
* @return A list of all generic type objects belonging to the key namespace. | ||
*/ | ||
public Collection<T> getAll() { | ||
Collection<CacheData> allData = cacheView.getAll(keyNamespace); | ||
return convertAll(allData); | ||
} | ||
|
||
/** | ||
* @param account name of the AWS account, as defined in clouddriver.yml | ||
* @param region region of the AWS account, as defined in clouddriver.yml | ||
* @return A list of all generic type objects belonging to the account and region in the key namespace. | ||
*/ | ||
public Collection<T> getAll(String account, String region) { | ||
Collection<CacheData> data = fetchFromCache(account, region); | ||
return convertAll(data); | ||
} | ||
|
||
/** | ||
* @param key A key within the key namespace that will be used to retrieve the object. | ||
* @return An object of the generic type that is associated to the key. | ||
*/ | ||
public T get(String key) { | ||
CacheData cacheData = cacheView.get(keyNamespace, key); | ||
if (cacheData != null) { | ||
return convert(cacheData); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @param cacheData A collection of CacheData that will be converted into a collection of generic typ objects. | ||
* @return A collection of generic typ objects. | ||
*/ | ||
private Collection<T> convertAll(Collection<CacheData> cacheData) { | ||
Set<T> itemSet = new HashSet<>(); | ||
for (CacheData cacheDatum : cacheData) { | ||
itemSet.add(convert(cacheDatum)); | ||
} | ||
return itemSet; | ||
} | ||
|
||
/** | ||
* @param account name of the AWS account, as defined in clouddriver.yml | ||
* @param region region of the AWS account, as defined in clouddriver.yml | ||
* @return | ||
*/ | ||
private Collection<CacheData> fetchFromCache(String account, String region) { | ||
String accountFilter = account != null ? account + Keys.SEPARATOR : "*" + Keys.SEPARATOR; | ||
String regionFilter = region != null ? region + Keys.SEPARATOR : "*" + Keys.SEPARATOR; | ||
Set<String> keys = new HashSet<>(); | ||
String pattern = "ecs" + Keys.SEPARATOR + keyNamespace + Keys.SEPARATOR + accountFilter + regionFilter + "*"; | ||
Collection<String> nameMatches = cacheView.filterIdentifiers(keyNamespace, pattern); | ||
|
||
keys.addAll(nameMatches); | ||
|
||
Collection<CacheData> allData = cacheView.getAll(keyNamespace, keys); | ||
|
||
if (allData == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
return allData; | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...ddriver-ecs/src/main/java/com/netflix/spinnaker/clouddriver/ecs/provider/EcsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* * Copyright 2017 Lookout, Inc. | ||
* | ||
* 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.ecs.provider; | ||
|
||
import com.netflix.spinnaker.cats.agent.Agent; | ||
import com.netflix.spinnaker.cats.agent.AgentSchedulerAware; | ||
import com.netflix.spinnaker.clouddriver.cache.SearchableProvider; | ||
import com.netflix.spinnaker.clouddriver.core.provider.agent.HealthProvidingCachingAgent; | ||
import com.netflix.spinnaker.clouddriver.ecs.cache.Keys; | ||
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsRepository; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.netflix.spinnaker.clouddriver.ecs.cache.Keys.Namespace.CONTAINER_INSTANCES; | ||
import static com.netflix.spinnaker.clouddriver.ecs.cache.Keys.Namespace.ECS_CLUSTERS; | ||
import static com.netflix.spinnaker.clouddriver.ecs.cache.Keys.Namespace.SERVICES; | ||
import static com.netflix.spinnaker.clouddriver.ecs.cache.Keys.Namespace.TASKS; | ||
import static com.netflix.spinnaker.clouddriver.ecs.cache.Keys.Namespace.TASK_DEFINITIONS; | ||
|
||
|
||
public class EcsProvider extends AgentSchedulerAware implements SearchableProvider { | ||
public static final String NAME = EcsProvider.class.getName(); | ||
|
||
private static final Set<String> defaultCaches = new HashSet<>(Arrays.asList( | ||
SERVICES.toString(), ECS_CLUSTERS.toString(), TASKS.toString(), | ||
CONTAINER_INSTANCES.toString(), TASK_DEFINITIONS.toString())); | ||
|
||
private static final Map<String, String> urlMappingTemplates = new HashMap<>(); | ||
|
||
private final Collection<Agent> agents; | ||
private final AccountCredentialsRepository accountCredentialsRepository; | ||
private final Keys keys = new Keys(); | ||
private Collection<HealthProvidingCachingAgent> healthAgents; | ||
|
||
public EcsProvider(AccountCredentialsRepository accountCredentialsRepository, Collection<Agent> agents) { | ||
this.agents = agents; | ||
this.accountCredentialsRepository = accountCredentialsRepository; | ||
} | ||
|
||
@Override | ||
public Set<String> getDefaultCaches() { | ||
return defaultCaches; | ||
} | ||
|
||
@Override | ||
public Map<String, String> getUrlMappingTemplates() { | ||
return urlMappingTemplates; | ||
} | ||
|
||
@Override | ||
public Map<SearchableProvider.SearchableResource, SearchableProvider.SearchResultHydrator> getSearchResultHydrators() { | ||
//TODO: Implement if needed - see InstanceSearchResultHydrator as an example. | ||
return Collections.emptyMap(); | ||
} | ||
|
||
@Override | ||
public Map<String, String> parseKey(String key) { | ||
return keys.parseKey(key); | ||
} | ||
|
||
@Override | ||
public String getProviderName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public Collection<Agent> getAgents() { | ||
return agents; | ||
} | ||
|
||
|
||
public void synchronizeHealthAgents() { | ||
healthAgents = Collections.unmodifiableCollection(agents.stream() | ||
.filter(a -> a instanceof HealthProvidingCachingAgent) | ||
.map(a -> (HealthProvidingCachingAgent) a).collect(Collectors.toList())); | ||
} | ||
|
||
public Collection<HealthProvidingCachingAgent> getHealthAgents() { | ||
List<HealthProvidingCachingAgent> newHealthAgents = new LinkedList<>(); | ||
newHealthAgents.addAll(healthAgents); | ||
return Collections.unmodifiableCollection(newHealthAgents); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't you just do
return Collections.unmodifiableCollection(healthAgents);
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.