Skip to content

Commit

Permalink
fix(kubernetes): Core caching agent is authoritative for artifacts (#…
Browse files Browse the repository at this point in the history
…4247)

There is currently no caching agent that is authoritative for kubernetes
artifacts; this is causing them to fail to be cached by the SQL provider
(which strictly enforces that only caching agents can only write types
for which they are authoritative).

The KubernetesCoreCachingAgent should be authoritative for artifacts,
just as it is for other logical kinds (clusters and applications).
  • Loading branch information
ezimanyi authored and mergify[bot] committed Jan 9, 2020
1 parent f0d22d6 commit 008e111
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -27,12 +28,10 @@
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesKind;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesKindProperties;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.security.KubernetesV2Credentials;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand All @@ -48,16 +47,14 @@ public KubernetesCoreCachingAgent(
}

public Collection<AgentDataType> getProvidedDataTypes() {
List<AgentDataType> types = new ArrayList<>();
types.add(AUTHORITATIVE.forType(Keys.LogicalKind.APPLICATIONS.toString()));
types.add(AUTHORITATIVE.forType(Keys.LogicalKind.CLUSTERS.toString()));
Stream<String> logicalTypes =
Stream.of(Keys.LogicalKind.APPLICATIONS, Keys.LogicalKind.CLUSTERS, Keys.Kind.ARTIFACT)
.map(Enum::toString);
Stream<String> kubernetesTypes = primaryKinds().stream().map(KubernetesKind::toString);

types.addAll(
primaryKinds().stream()
.map(k -> AUTHORITATIVE.forType(k.toString()))
.collect(Collectors.toList()));

return Collections.unmodifiableSet(new HashSet<>(types));
return Stream.concat(logicalTypes, kubernetesTypes)
.map(AUTHORITATIVE::forType)
.collect(toImmutableSet());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.agent;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
Expand All @@ -30,6 +31,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.netflix.spectator.api.NoopRegistry;
import com.netflix.spinnaker.cats.agent.AgentDataType;
import com.netflix.spinnaker.cats.agent.CacheResult;
import com.netflix.spinnaker.cats.cache.CacheData;
import com.netflix.spinnaker.cats.cache.DefaultCacheData;
Expand Down Expand Up @@ -547,4 +549,42 @@ private static class LoadDataResult {
this.cacheEntries = extractCacheEntries(providerCaches);
}
}

@ParameterizedTest
@ValueSource(ints = {1, 2, 10})
public void authoritativeForLogicalTypes(int numAgents) {
ImmutableCollection<KubernetesCoreCachingAgent> cachingAgents =
createCachingAgents(getNamedAccountCredentials(), numAgents);
cachingAgents.forEach(
cachingAgent ->
assertThat(getAuthoritativeTypes(cachingAgent.getProvidedDataTypes()))
.containsAll(
ImmutableList.of(
Keys.LogicalKind.APPLICATIONS.toString(),
Keys.LogicalKind.CLUSTERS.toString(),
Keys.Kind.ARTIFACT.toString())));
}

@ParameterizedTest
@ValueSource(ints = {1, 2, 10})
public void authoritativeForKubernetesKinds(int numAgents) {
ImmutableCollection<KubernetesCoreCachingAgent> cachingAgents =
createCachingAgents(getNamedAccountCredentials(), numAgents);
cachingAgents.forEach(
cachingAgent ->
assertThat(getAuthoritativeTypes(cachingAgent.getProvidedDataTypes()))
.containsAll(
ImmutableList.of(
KubernetesKind.NAMESPACE.toString(),
KubernetesKind.POD.toString(),
KubernetesKind.REPLICA_SET.toString())));
}

private static ImmutableList<String> getAuthoritativeTypes(
Collection<AgentDataType> agentDataTypes) {
return agentDataTypes.stream()
.filter(dataType -> dataType.getAuthority() == AUTHORITATIVE)
.map(AgentDataType::getTypeName)
.collect(toImmutableList());
}
}

0 comments on commit 008e111

Please sign in to comment.