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): Register k8s namer for assigning monikers to manifests #1910

Merged
merged 1 commit into from
Sep 20, 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
3 changes: 3 additions & 0 deletions clouddriver-core/clouddriver-core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ dependencies {
compile spinnaker.dependency('eurekaClient')
compile spinnaker.dependency("jedis")
compile spinnaker.dependency("frigga")
compile spinnaker.dependency("lombok")

compile project(':cats:cats-core')
compile project(':cats:cats-redis')
compile project(':cats:cats-dynomite')
compile project(':clouddriver-security')
testCompile project(':cats:cats-test')
testCompile spinnaker.dependency('korkJedisTest')

compile 'com.netflix.spinnaker.moniker:moniker:0.2.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.names;

import com.netflix.spinnaker.moniker.Namer;
import com.netflix.spinnaker.moniker.frigga.FriggaReflectiveNamer;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;

/**
* The idea is each provider can register (per-account) based on config naming
* strategy. This assigns a `moniker` to any named resource which is then pushed
* through the rest of Spinnaker and can be handled without prior knowledge of what
* naming strategy was used. This is the only place the mapping from (provider, account, resource) -> namer
* must happen within Spinnaker.
*/
public class NamerRegistry {
private static Namer defaultNamer = new FriggaReflectiveNamer();
private static ProviderLookup providerLookup = new ProviderLookup();

public static Namer getDefaultNamer() {
return defaultNamer;
}

public static ProviderLookup lookup() {
return providerLookup;
}

@Slf4j
public static class ResourceLookup {
private ConcurrentHashMap<Class, Namer> map = new ConcurrentHashMap<>();

public Namer withResource(Class resource) {
if (!map.containsKey(resource)) {
log.debug("Looking up a namer for a non-registered resource");
return getDefaultNamer();
} else {
return map.get(resource);
}
}

public void setNamer(Class resource, Namer namer) {
map.put(resource, namer);
}
}

@Slf4j
public static class AccountLookup {
private ConcurrentHashMap<String, ResourceLookup> map = new ConcurrentHashMap<>();

public ResourceLookup withAccount(String accountName) {
if (!map.containsKey(accountName)) {
log.debug("Looking up a namer for a non-registered account");
ResourceLookup result = new ResourceLookup();
map.put(accountName, result);
return result;
} else {
return map.get(accountName);
}
}
}

@Slf4j
public static class ProviderLookup {
private ConcurrentHashMap<String, AccountLookup> map = new ConcurrentHashMap<>();

public AccountLookup withProvider(String providerName) {
if (!map.containsKey(providerName)) {
log.debug("Looking up a namer for a non-registered provider");
AccountLookup result = new AccountLookup();
map.put(providerName, result);
return result;
} else {
return map.get(providerName);
}
}
}
}
1 change: 1 addition & 0 deletions clouddriver-kubernetes/clouddriver-kubernetes.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ dependencies {
// TODO(lwander) move to spinnaker-dependencies when library stabilizes
compile 'io.kubernetes:client-java-util:0.1'
compile 'com.github.fge:json-patch:1.9'
compile 'com.netflix.spinnaker.moniker:moniker:0.2.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package com.netflix.spinnaker.clouddriver.kubernetes.security;

import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.clouddriver.kubernetes.KubernetesCloudProvider;
import com.netflix.spinnaker.clouddriver.kubernetes.config.LinkedDockerRegistryConfiguration;
import com.netflix.spinnaker.clouddriver.kubernetes.v1.security.KubernetesV1Credentials;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.KubernetesManifest;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.names.KubernetesManifestNamer;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.security.KubernetesV2Credentials;
import com.netflix.spinnaker.clouddriver.names.NamerRegistry;
import com.netflix.spinnaker.clouddriver.security.AccountCredentials;
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsRepository;
import com.netflix.spinnaker.clouddriver.security.ProviderVersion;
Expand Down Expand Up @@ -283,6 +287,10 @@ private C buildCredentials() {
accountCredentialsRepository
);
case v2:
NamerRegistry.lookup()
.withProvider(KubernetesCloudProvider.getID())
.withAccount(name)
.setNamer(KubernetesManifest.class, new KubernetesManifestNamer());
return (C) new KubernetesV2Credentials(name, spectatorRegistry);
default:
throw new IllegalArgumentException("Unknown provider type: " + version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class KubernetesAugmentedManifest {
Metadata metadata;

@Data
public class Metadata {
public static class Metadata {
KubernetesManifestSpinnakerRelationships relationships;
Artifact artifact;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.names;

import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.KubernetesManifest;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.KubernetesManifestAnnotater;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.KubernetesManifestSpinnakerRelationships;
import com.netflix.spinnaker.moniker.Moniker;
import com.netflix.spinnaker.moniker.Namer;
import org.apache.commons.lang3.NotImplementedException;

public class KubernetesManifestNamer implements Namer<KubernetesManifest> {
@Override
public void applyMoniker(KubernetesManifest obj, Moniker moniker) {
throw new NotImplementedException("TODO(lwander)");
}

@Override
public Moniker deriveMoniker(KubernetesManifest obj) {
KubernetesManifestSpinnakerRelationships relationships = KubernetesManifestAnnotater.getManifestRelationships(obj);

return Moniker.builder()
.app(relationships.getApplication())
.cluster(relationships.getCluster())
.build();
}
}