Skip to content

Commit

Permalink
feat(provider/kubernetes): look up namer by class name
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeach committed Jan 12, 2018
1 parent 008254c commit 279a61c
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import com.netflix.spinnaker.clouddriver.model.SecurityGroupProvider
import com.netflix.spinnaker.clouddriver.model.ServerGroupManager
import com.netflix.spinnaker.clouddriver.model.ServerGroupManagerProvider
import com.netflix.spinnaker.clouddriver.model.SubnetProvider
import com.netflix.spinnaker.clouddriver.names.NamerRegistry
import com.netflix.spinnaker.clouddriver.names.NamingStrategy
import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperationConverter
import com.netflix.spinnaker.clouddriver.search.ApplicationSearchProvider
import com.netflix.spinnaker.clouddriver.search.NoopSearchProvider
Expand All @@ -66,6 +68,7 @@ import com.netflix.spinnaker.clouddriver.security.AccountCredentialsRepository
import com.netflix.spinnaker.clouddriver.security.DefaultAccountCredentialsProvider
import com.netflix.spinnaker.clouddriver.security.MapBackedAccountCredentialsRepository
import com.netflix.spinnaker.kork.core.RetrySupport
import com.netflix.spinnaker.moniker.Namer
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.context.properties.ConfigurationProperties
Expand Down Expand Up @@ -256,4 +259,9 @@ class CloudDriverConfig {
public RetrySupport retrySupport() {
return new RetrySupport();
}

@Bean
NamerRegistry namerRegistry(List<NamingStrategy> namingStrategies) {
new NamerRegistry(namingStrategies)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.netflix.spinnaker.moniker.frigga.FriggaReflectiveNamer;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand All @@ -31,6 +32,7 @@
* must happen within Spinnaker.
*/
public class NamerRegistry {
final private List<NamingStrategy> namingStrategies;
private static Namer defaultNamer = new FriggaReflectiveNamer();
private static ProviderLookup providerLookup = new ProviderLookup();

Expand All @@ -42,6 +44,17 @@ public static ProviderLookup lookup() {
return providerLookup;
}

public NamerRegistry(List<NamingStrategy> namingStrategies) {
this.namingStrategies = namingStrategies;
}

public Namer getNamingStrategy(String strategyName) {
return this.namingStrategies.stream()
.filter(strategy -> strategy.getName().equalsIgnoreCase(strategyName))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Could not find naming strategy '" + strategyName + "'"));
}

@Slf4j
public static class ResourceLookup {
private ConcurrentHashMap<Class, Namer> map = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.netflix.spinnaker.clouddriver.names;

import com.netflix.spinnaker.moniker.Namer;

public interface NamingStrategy<T> extends Namer<T> {
String getName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class KubernetesConfigurationProperties {
List<LinkedDockerRegistryConfiguration> dockerRegistries
List<String> requiredGroupMembership
Permissions.Builder permissions = new Permissions.Builder()
Boolean debug = false;
String namingStrategy = "annotations"
Boolean debug = false
}

List<ManagedAccount> accounts = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsRepository;
import com.netflix.spinnaker.clouddriver.security.ProviderVersion;
import com.netflix.spinnaker.fiat.model.resources.Permissions;
import com.netflix.spinnaker.moniker.Namer;
import groovy.util.logging.Slf4j;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -175,6 +176,7 @@ static class Builder<C extends KubernetesCredentials> {
Registry spectatorRegistry;
AccountCredentialsRepository accountCredentialsRepository;
KubectlJobExecutor jobExecutor;
Namer namer;
boolean debug;

Builder name(String name) {
Expand Down Expand Up @@ -233,7 +235,7 @@ Builder kubeconfigFile(String kubeconfigFile) {
}

Builder serviceAccount(Boolean serviceAccount) {
this.serviceAccount = serviceAccount;;
this.serviceAccount = serviceAccount;
return this;
}

Expand Down Expand Up @@ -300,6 +302,11 @@ Builder debug(boolean debug) {
return this;
}

Builder namer(Namer namer) {
this.namer = namer;
return this;
}

private C buildCredentials() {
switch (providerVersion) {
case v1:
Expand All @@ -322,7 +329,7 @@ private C buildCredentials() {
NamerRegistry.lookup()
.withProvider(KubernetesCloudProvider.getID())
.withAccount(name)
.setNamer(KubernetesManifest.class, new KubernetesManifestNamer());
.setNamer(KubernetesManifest.class, namer);
return (C) new KubernetesV2Credentials.Builder()
.accountName(name)
.kubeconfigFile(kubeconfigFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.netflix.spinnaker.cats.module.CatsModule
import com.netflix.spinnaker.cats.provider.ProviderSynchronizerTypeWrapper
import com.netflix.spinnaker.clouddriver.kubernetes.config.KubernetesConfigurationProperties
import com.netflix.spinnaker.clouddriver.kubernetes.v2.op.job.KubectlJobExecutor
import com.netflix.spinnaker.clouddriver.names.NamerRegistry
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsRepository
import com.netflix.spinnaker.clouddriver.security.CredentialsInitializerSynchronizable
import com.netflix.spinnaker.clouddriver.security.ProviderUtils
Expand All @@ -39,6 +40,7 @@ class KubernetesNamedAccountCredentialsInitializer implements CredentialsInitial

@Autowired Registry spectatorRegistry
@Autowired KubectlJobExecutor jobExecutor
@Autowired NamerRegistry namerRegistry

@Bean
List<? extends KubernetesNamedAccountCredentials> kubernetesNamedAccountCredentials(
Expand Down Expand Up @@ -96,6 +98,7 @@ class KubernetesNamedAccountCredentialsInitializer implements CredentialsInitial
.permissions(managedAccount.permissions.build())
.spectatorRegistry(spectatorRegistry)
.jobExecutor(jobExecutor)
.namer(namerRegistry.getNamingStrategy(managedAccount.namingStrategy))
.debug(managedAccount.debug)
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.ImmutableMap;
import com.netflix.spinnaker.cats.cache.CacheData;
import com.netflix.spinnaker.cats.cache.DefaultCacheData;
import com.netflix.spinnaker.clouddriver.kubernetes.KubernetesCloudProvider;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.Keys;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesApiVersion;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesCachingProperties;
Expand All @@ -29,8 +30,11 @@
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifestAnnotater;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifestMetadata;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifestSpinnakerRelationships;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.names.KubernetesManifestNamer;
import com.netflix.spinnaker.clouddriver.names.NamerRegistry;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.moniker.Moniker;
import com.netflix.spinnaker.moniker.Namer;
import io.kubernetes.client.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -152,7 +156,13 @@ public static CacheData convertAsResource(String account, KubernetesManifest man
KubernetesApiVersion apiVersion = manifest.getApiVersion();
String name = manifest.getName();
String namespace = manifest.getNamespace();
Moniker moniker = KubernetesManifestAnnotater.getMoniker(manifest);
Namer<KubernetesManifest> namer = account == null
? new KubernetesManifestNamer()
: NamerRegistry.lookup()
.withProvider(KubernetesCloudProvider.getID())
.withAccount(account)
.withResource(KubernetesManifest.class);
Moniker moniker = namer.deriveMoniker(manifest);

Map<String, Object> attributes = new ImmutableMap.Builder<String, Object>()
.put("kind", kind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@

import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifest;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifestAnnotater;
import com.netflix.spinnaker.clouddriver.names.NamingStrategy;
import com.netflix.spinnaker.moniker.Moniker;
import com.netflix.spinnaker.moniker.Namer;
import org.springframework.stereotype.Component;

@Component
public class KubernetesManifestNamer implements NamingStrategy<KubernetesManifest> {
@Override
public String getName() {
return "annotations";
}

public class KubernetesManifestNamer implements Namer<KubernetesManifest> {
@Override
public void applyMoniker(KubernetesManifest obj, Moniker moniker) {
KubernetesManifestAnnotater.annotateManifest(obj, moniker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ package com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.agent

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.cats.cache.DefaultCacheData
import com.netflix.spinnaker.clouddriver.kubernetes.KubernetesCloudProvider
import com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.Keys
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesApiVersion
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesKind
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifest
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifestAnnotater
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifestMetadata
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifestSpinnakerRelationships
import com.netflix.spinnaker.clouddriver.kubernetes.v2.names.KubernetesManifestNamer
import com.netflix.spinnaker.clouddriver.names.NamerRegistry
import com.netflix.spinnaker.kork.artifacts.model.Artifact
import com.netflix.spinnaker.moniker.Moniker
import org.apache.commons.lang3.tuple.Pair
Expand Down Expand Up @@ -58,6 +61,13 @@ metadata:
.cluster(cluster)
.build()

if (account != null) {
NamerRegistry.lookup()
.withProvider(KubernetesCloudProvider.ID)
.withAccount(account)
.setNamer(KubernetesManifest, new KubernetesManifestNamer())
}

def manifest = stringToManifest(rawManifest)
KubernetesManifestAnnotater.annotateManifest(manifest, moniker)

Expand Down

0 comments on commit 279a61c

Please sign in to comment.