-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(provider/kubernetes): Cache pods
- Loading branch information
Lars Wander
committed
Sep 6, 2017
1 parent
c0d3208
commit d146e8a
Showing
7 changed files
with
241 additions
and
31 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
.../netflix/spinnaker/clouddriver/kubernetes/v2/caching/agent/KubernetesPodCachingAgent.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,69 @@ | ||
/* | ||
* Copyright 2017 Google, 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.kubernetes.v2.caching.agent; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.netflix.spectator.api.Registry; | ||
import com.netflix.spinnaker.cats.agent.AgentDataType; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.security.KubernetesNamedAccountCredentials; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.Keys; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.KubernetesKind; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.security.KubernetesV2Credentials; | ||
import io.kubernetes.client.models.V1Pod; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE; | ||
import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.INFORMATIVE; | ||
|
||
@Slf4j | ||
public class KubernetesPodCachingAgent extends KubernetesV2CachingAgent<V1Pod> { | ||
KubernetesPodCachingAgent(KubernetesNamedAccountCredentials<KubernetesV2Credentials> namedAccountCredentials, | ||
ObjectMapper objectMapper, | ||
Registry registry, | ||
int agentIndex, | ||
int agentCount) { | ||
super(namedAccountCredentials, objectMapper, registry, agentIndex, agentCount); | ||
} | ||
|
||
@Getter | ||
final private Collection<AgentDataType> providedDataTypes = Collections.unmodifiableSet( | ||
new HashSet<>(Arrays.asList( | ||
INFORMATIVE.forType(Keys.LogicalKind.APPLICATION.toString()), | ||
INFORMATIVE.forType(Keys.LogicalKind.CLUSTER.toString()), | ||
INFORMATIVE.forType(KubernetesKind.DEPLOYMENT.toString()), | ||
INFORMATIVE.forType(KubernetesKind.REPLICA_SET.toString()), | ||
AUTHORITATIVE.forType(KubernetesKind.POD.toString()) | ||
)) | ||
); | ||
|
||
@Override | ||
protected List<V1Pod> loadPrimaryResource() { | ||
return namespaces.stream() | ||
.map(credentials::listAllPods) | ||
.flatMap(Collection::stream) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
...m/netflix/spinnaker/clouddriver/kubernetes/v2/caching/agent/KubernetesV2CachingAgent.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,68 @@ | ||
/* | ||
* Copyright 2017 Google, 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.kubernetes.v2.caching.agent; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.netflix.spectator.api.Registry; | ||
import com.netflix.spinnaker.cats.agent.CacheResult; | ||
import com.netflix.spinnaker.cats.agent.DefaultCacheResult; | ||
import com.netflix.spinnaker.cats.cache.CacheData; | ||
import com.netflix.spinnaker.cats.provider.ProviderCache; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.caching.KubernetesCachingAgent; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.security.KubernetesNamedAccountCredentials; | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.security.KubernetesV2Credentials; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public abstract class KubernetesV2CachingAgent<T> extends KubernetesCachingAgent<KubernetesV2Credentials> { | ||
protected KubernetesV2CachingAgent(KubernetesNamedAccountCredentials<KubernetesV2Credentials> namedAccountCredentials, | ||
ObjectMapper objectMapper, | ||
Registry registry, | ||
int agentIndex, | ||
int agentCount) { | ||
super(namedAccountCredentials, objectMapper, registry, agentIndex, agentCount); | ||
} | ||
|
||
@Override | ||
public CacheResult loadData(ProviderCache providerCache) { | ||
reloadNamespaces(); | ||
|
||
List<CacheData> resourceData = loadPrimaryResource().stream() | ||
.map(rs -> KubernetesCacheDataConverter.fromResource(accountName, objectMapper, rs)) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toList()); | ||
|
||
List<CacheData> invertedRelationships = resourceData.stream() | ||
.map(KubernetesCacheDataConverter::invertRelationships) | ||
.flatMap(Collection::stream) | ||
.collect(Collectors.toList()); | ||
|
||
resourceData.addAll(invertedRelationships); | ||
|
||
Map<String, Collection<CacheData>> entries = KubernetesCacheDataConverter.stratifyCacheDataByGroup(resourceData); | ||
KubernetesCacheDataConverter.logStratifiedCacheData(getAgentType(), entries); | ||
|
||
return new DefaultCacheResult(entries); | ||
} | ||
|
||
protected abstract List<T> loadPrimaryResource(); | ||
} |
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
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
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
74 changes: 74 additions & 0 deletions
74
...ix/spinnaker/clouddriver/kubernetes/v2/caching/agent/KubernetesPodCachingAgentSpec.groovy
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,74 @@ | ||
/* | ||
* Copyright 2017 Google, 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.kubernetes.v2.caching.agent | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.netflix.spinnaker.clouddriver.kubernetes.security.KubernetesNamedAccountCredentials | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.Keys | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.KubernetesApiVersion | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.KubernetesKind | ||
import com.netflix.spinnaker.clouddriver.kubernetes.v2.security.KubernetesV2Credentials | ||
import io.kubernetes.client.models.V1ObjectMeta | ||
import io.kubernetes.client.models.V1Pod | ||
import spock.lang.Specification | ||
|
||
class KubernetesPodCachingAgentSpec extends Specification { | ||
def ACCOUNT = "my-account" | ||
def CLUSTER = "my-cluster" | ||
def APPLICATION = "my-application" | ||
def NAME = "the-name" | ||
def NAMESPACE = "your-namespace" | ||
|
||
void "invokes caching agent on output pod"() { | ||
setup: | ||
def pod = new V1Pod() | ||
def annotations = [ | ||
'relationships.spinnaker.io/cluster': '"' + CLUSTER + '"', | ||
'relationships.spinnaker.io/application': '"' + APPLICATION + '"' | ||
] | ||
|
||
def metadata = new V1ObjectMeta() | ||
metadata.setAnnotations(annotations) | ||
metadata.setName(NAME) | ||
metadata.setNamespace(NAMESPACE) | ||
pod.setMetadata(metadata) | ||
pod.setKind(KubernetesKind.REPLICA_SET.name) | ||
pod.setApiVersion(KubernetesApiVersion.EXTENSIONS_V1BETA1.name) | ||
|
||
def credentials = Mock(KubernetesV2Credentials) | ||
credentials.getDeclaredNamespaces() >> [NAMESPACE] | ||
credentials.listAllPods(NAMESPACE) >> [pod] | ||
|
||
def namedAccountCredentials = Mock(KubernetesNamedAccountCredentials) | ||
namedAccountCredentials.getCredentials() >> credentials | ||
namedAccountCredentials.getName() >> ACCOUNT | ||
|
||
def cachingAgent = new KubernetesPodCachingAgent(namedAccountCredentials, new ObjectMapper(), null, 0, 1) | ||
|
||
when: | ||
def result = cachingAgent.loadData(null) | ||
|
||
then: | ||
result.cacheResults[KubernetesKind.REPLICA_SET.name].size() == 1 | ||
def cacheData = result.cacheResults[KubernetesKind.REPLICA_SET.name].iterator().next() | ||
cacheData.relationships.get(Keys.LogicalKind.CLUSTER.toString()) == [Keys.cluster(ACCOUNT, CLUSTER)] | ||
cacheData.relationships.get(Keys.LogicalKind.APPLICATION.toString()) == [Keys.application(APPLICATION)] | ||
cacheData.attributes.get("name") == NAME | ||
cacheData.attributes.get("namespace") == NAMESPACE | ||
} | ||
} |