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): adds hook for named per-account configuration #2288

Merged
merged 1 commit into from
Jan 12, 2018
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 @@ -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 = "kubernetesAnnotations"
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 "kubernetesAnnotations";
}

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