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(provider/kubernetes): k8s-only search provider #2121

Merged
merged 1 commit into from
Nov 15, 2017
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,49 @@
* limitations under the License.
*/

package com.netflix.spinnaker.clouddriver.search

import groovy.transform.Canonical

@Canonical
class SearchResultSet {

package com.netflix.spinnaker.clouddriver.search;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SearchResultSet {
/**
* The total number of items matching the search criteria (query, platform, and type)
*/
Integer totalMatches
Integer totalMatches;

/**
* The page index (1-based) of the result set
*/
Integer pageNumber
Integer pageNumber;

/**
* The number of items per page
*/
Integer pageSize
Integer pageSize;

/**
* The platform of results the provider supplies - e.g. "aws", "gce", etc.
*/
String platform
String platform;

/**
* The original query string, used to sort results
*/
String query
String query;

/**
* The paginated list of objects matching the query
*/
List<Map<String, Object>> results = []

List<Map<String, Object>> results = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,23 @@ public static Kind fromString(String name) {
}

public enum LogicalKind {
APPLICATION,
CLUSTER;
APPLICATIONS,
CLUSTERS;

public static boolean isLogicalGroup(String group) {
return group.equals(APPLICATION.toString()) || group.equals(CLUSTER.toString());
return group.equals(APPLICATIONS.toString()) || group.equals(CLUSTERS.toString());
}

@Override
public String toString() {
return name().toLowerCase();
}

public String singular() {
String name = toString();
return name.substring(0, name.length() - 1);
}

public static LogicalKind fromString(String name) {
return Arrays.stream(values())
.filter(k -> k.toString().equalsIgnoreCase(name))
Expand All @@ -90,11 +95,11 @@ public static String artifact(String type, String name, String location, String
}

public static String application(String name) {
return createKey(Kind.LOGICAL, LogicalKind.APPLICATION, name);
return createKey(Kind.LOGICAL, LogicalKind.APPLICATIONS, name);
}

public static String cluster(String account, String application, String name) {
return createKey(Kind.LOGICAL, LogicalKind.CLUSTER, account, application, name);
return createKey(Kind.LOGICAL, LogicalKind.CLUSTERS, account, application, name);
}

public static String infrastructure(KubernetesKind kind, String account, String namespace, String name) {
Expand Down Expand Up @@ -136,9 +141,9 @@ private static CacheKey parseLogicalKey(String[] parts) {
LogicalKind logicalKind = LogicalKind.fromString(parts[2]);

switch (logicalKind) {
case APPLICATION:
case APPLICATIONS:
return new ApplicationCacheKey(parts);
case CLUSTER:
case CLUSTERS:
return new ClusterCacheKey(parts);
default:
throw new IllegalArgumentException("Unknown kind " + logicalKind);
Expand All @@ -154,6 +159,12 @@ public static abstract class CacheKey {
public abstract String getName();
}

@Data
public static abstract class LogicalKey extends CacheKey {
private Kind kind = Kind.LOGICAL;
public abstract LogicalKind getLogicalKind();
}

@EqualsAndHashCode(callSuper = true)
@Data
public static class ArtifactCacheKey extends CacheKey {
Expand Down Expand Up @@ -187,9 +198,8 @@ public String getGroup() {

@EqualsAndHashCode(callSuper = true)
@Data
public static class ApplicationCacheKey extends CacheKey {
private Kind kind = Kind.LOGICAL;
private LogicalKind logicalKind = LogicalKind.APPLICATION;
public static class ApplicationCacheKey extends LogicalKey {
private LogicalKind logicalKind = LogicalKind.APPLICATIONS;
private String name;

public ApplicationCacheKey(String[] parts) {
Expand All @@ -202,7 +212,7 @@ public ApplicationCacheKey(String[] parts) {

@Override
public String toString() {
return createKey(kind, logicalKind, name);
return createKey(getKind(), logicalKind, name);
}

@Override
Expand All @@ -213,9 +223,8 @@ public String getGroup() {

@EqualsAndHashCode(callSuper = true)
@Data
public static class ClusterCacheKey extends CacheKey {
private Kind kind = Kind.LOGICAL;
private LogicalKind logicalKind = LogicalKind.CLUSTER;
public static class ClusterCacheKey extends LogicalKey {
private LogicalKind logicalKind = LogicalKind.CLUSTERS;
private String account;
private String application;
private String name;
Expand All @@ -232,7 +241,7 @@ public ClusterCacheKey(String[] parts) {

@Override
public String toString() {
return createKey(kind, logicalKind, account, name);
return createKey(getKind(), logicalKind, account, name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,76 +17,29 @@

package com.netflix.spinnaker.clouddriver.kubernetes.v2.caching;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
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.cats.provider.Provider;
import com.netflix.spinnaker.clouddriver.kubernetes.KubernetesCloudProvider;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.KubernetesSpinnakerKindMap;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesKind;
import groovy.util.logging.Slf4j;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@EqualsAndHashCode(callSuper = true)
@Slf4j
@Data
class KubernetesV2Provider extends AgentSchedulerAware implements SearchableProvider {
class KubernetesV2Provider extends AgentSchedulerAware implements Provider {
public static final String PROVIDER_NAME = KubernetesCloudProvider.getID();

private final static ObjectMapper mapper = new ObjectMapper();
private final Map<String, String> urlMappingTemplates = Collections.emptyMap();
private final Collection<Agent> agents = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final KubernetesCloudProvider cloudProvider;
private final KubernetesSpinnakerKindMap kindMap;

KubernetesV2Provider(KubernetesCloudProvider cloudProvider, KubernetesSpinnakerKindMap kindMap) {
this.cloudProvider = cloudProvider;
this.kindMap = kindMap;
}

@Override
public String getProviderName() {
return PROVIDER_NAME;
}

@Override
public Set<String> getDefaultCaches() {
return kindMap.allKubernetesKinds()
.stream()
.map(KubernetesKind::toString)
.collect(Collectors.toSet());
}

@Override
public Map<SearchableProvider.SearchableResource, SearchableProvider.SearchResultHydrator> getSearchResultHydrators() {
return new HashMap<>();
}

@Override
public Map<String, String> parseKey(String key) {
return (Map<String, String>) Keys.parseKey(key)
.map(k -> {
String group = k.getGroup();
try {
KubernetesKind kind = KubernetesKind.fromString(group);
k.setType(kindMap.translateKubernetesKind(kind).toString());
} catch (Exception _ignored) {
k.setType(group);
}

return k;
})
.map(k -> mapper.convertValue(k, new TypeReference<Map<String, String>>() {}))
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.netflix.spinnaker.clouddriver.kubernetes.KubernetesCloudProvider;
import com.netflix.spinnaker.clouddriver.kubernetes.security.KubernetesNamedAccountCredentials;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.agent.KubernetesV2CachingAgentDispatcher;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.KubernetesSpinnakerKindMap;
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsRepository;
import com.netflix.spinnaker.clouddriver.security.ProviderUtils;
import com.netflix.spinnaker.clouddriver.security.ProviderVersion;
Expand All @@ -48,9 +47,8 @@ class KubernetesV2ProviderConfig implements Runnable {
@DependsOn("kubernetesNamedAccountCredentials")
KubernetesV2Provider kubernetesV2Provider(KubernetesCloudProvider kubernetesCloudProvider,
AccountCredentialsRepository accountCredentialsRepository,
KubernetesSpinnakerKindMap kindMap,
KubernetesV2CachingAgentDispatcher kubernetesV2CachingAgentDispatcher) {
this.kubernetesV2Provider = new KubernetesV2Provider(kubernetesCloudProvider, kindMap);
this.kubernetesV2Provider = new KubernetesV2Provider();
this.accountCredentialsRepository = accountCredentialsRepository;
this.kubernetesV2CachingAgentDispatcher = kubernetesV2CachingAgentDispatcher;

Expand Down
Loading