Skip to content

Commit

Permalink
feat(provider/kubernetes): remove dependency on api-client (#2113)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwander authored Nov 13, 2017
1 parent b3556c4 commit 408576a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.netflix.spinnaker.clouddriver.kubernetes.v1.security.KubernetesV1Credentials;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifest;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.names.KubernetesManifestNamer;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.op.job.KubectlJobExecutor;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.security.KubernetesV2Credentials;
import com.netflix.spinnaker.clouddriver.names.NamerRegistry;
import com.netflix.spinnaker.clouddriver.security.AccountCredentials;
Expand Down Expand Up @@ -171,6 +172,7 @@ static class Builder<C extends KubernetesCredentials> {
List<LinkedDockerRegistryConfiguration> dockerRegistries;
Registry spectatorRegistry;
AccountCredentialsRepository accountCredentialsRepository;
KubectlJobExecutor jobExecutor;
boolean debug;

Builder name(String name) {
Expand Down Expand Up @@ -276,6 +278,11 @@ Builder accountCredentialsRepository(AccountCredentialsRepository accountCredent
return this;
}

Builder jobExecutor(KubectlJobExecutor jobExecutor) {
this.jobExecutor = jobExecutor;
return this;
}

Builder debug(boolean debug) {
this.debug = debug;
return this;
Expand Down Expand Up @@ -313,6 +320,7 @@ private C buildCredentials() {
.omitNamespaces(omitNamespaces)
.registry(spectatorRegistry)
.debug(debug)
.jobExecutor(jobExecutor)
.build();
default:
throw new IllegalArgumentException("Unknown provider type: " + providerVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.netflix.spectator.api.Registry
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.security.AccountCredentialsRepository
import com.netflix.spinnaker.clouddriver.security.CredentialsInitializerSynchronizable
import com.netflix.spinnaker.clouddriver.security.ProviderUtils
Expand All @@ -37,6 +38,7 @@ class KubernetesNamedAccountCredentialsInitializer implements CredentialsInitial
private static final Integer DEFAULT_CACHE_THREADS = 1

@Autowired Registry spectatorRegistry
@Autowired KubectlJobExecutor jobExecutor

@Bean
List<? extends KubernetesNamedAccountCredentials> kubernetesNamedAccountCredentials(
Expand Down Expand Up @@ -91,6 +93,7 @@ class KubernetesNamedAccountCredentialsInitializer implements CredentialsInitial
.requiredGroupMembership(managedAccount.requiredGroupMembership)
.permissions(managedAccount.permissions.build())
.spectatorRegistry(spectatorRegistry)
.jobExecutor(jobExecutor)
.debug(managedAccount.debug)
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ private List<String> kubectlNamespacedAuthPrefix(KubernetesV2Credentials credent
namespace = credentials.getDefaultNamespace();
}

command.add("--namespace=" + namespace);
if (StringUtils.isNotEmpty(namespace)) {
command.add("--namespace=" + namespace);
}

return command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@
import com.netflix.spinnaker.clouddriver.kubernetes.security.KubernetesCredentials;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesApiVersion;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesKind;
import io.kubernetes.client.ApiClient;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.description.manifest.KubernetesManifest;
import com.netflix.spinnaker.clouddriver.kubernetes.v2.op.job.KubectlJobExecutor;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.apis.AppsV1beta1Api;
import io.kubernetes.client.apis.AppsV1beta2Api;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.apis.ExtensionsV1beta1Api;
import io.kubernetes.client.models.V1Service;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.KubeConfig;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -50,11 +46,7 @@
import java.util.stream.Collectors;

public class KubernetesV2Credentials implements KubernetesCredentials {
private final ApiClient client;
private final CoreV1Api coreV1Api;
private final ExtensionsV1beta1Api extensionsV1beta1Api;
private final AppsV1beta1Api appsV1beta1Api;
private final AppsV1beta2Api appsV1beta2Api;
private final KubectlJobExecutor jobExecutor;
private final Registry registry;
private final Clock clock;
private final String accountName;
Expand Down Expand Up @@ -93,6 +85,7 @@ public static class Builder {
List<String> namespaces = new ArrayList<>();
List<String> omitNamespaces = new ArrayList<>();
Registry registry;
KubectlJobExecutor jobExecutor;
boolean debug;

public Builder accountName(String accountName) {
Expand Down Expand Up @@ -130,6 +123,11 @@ public Builder registry(Registry registry) {
return this;
}

public Builder jobExecutor(KubectlJobExecutor jobExecutor) {
this.jobExecutor = jobExecutor;
return this;
}

public Builder debug(boolean debug) {
this.debug = debug;
return this;
Expand All @@ -151,18 +149,12 @@ public KubernetesV2Credentials build() {
kubeconfig.setContext(context);
}

ApiClient client = Config.fromConfig(kubeconfig);

if (!StringUtils.isEmpty(userAgent)) {
client.setUserAgent(userAgent);
}

namespaces = namespaces == null ? new ArrayList<>() : namespaces;
omitNamespaces = omitNamespaces == null ? new ArrayList<>() : omitNamespaces;

return new KubernetesV2Credentials(
accountName,
client,
jobExecutor,
namespaces,
omitNamespaces,
registry,
Expand All @@ -174,7 +166,7 @@ public KubernetesV2Credentials build() {
}

private KubernetesV2Credentials(@NotNull String accountName,
@NotNull ApiClient client,
@NotNull KubectlJobExecutor jobExecutor,
@NotNull List<String> namespaces,
@NotNull List<String> omitNamespaces,
@NotNull Registry registry,
Expand All @@ -186,34 +178,26 @@ private KubernetesV2Credentials(@NotNull String accountName,
this.accountName = accountName;
this.namespaces = namespaces;
this.omitNamespaces = omitNamespaces;
this.client = client;
this.client.setDebugging(debug);
this.jobExecutor = jobExecutor;
this.debug = debug;

this.coreV1Api = new CoreV1Api(this.client);
this.extensionsV1beta1Api = new ExtensionsV1beta1Api(this.client);
this.appsV1beta1Api = new AppsV1beta1Api(this.client);
this.appsV1beta2Api = new AppsV1beta2Api(this.client);

this.kubeconfigFile = kubeconfigFile;
this.context = context;
}

@Override
public List<String> getDeclaredNamespaces() {
List<String> result;
String labelSelector = null;
String fieldSelector = null;
if (!namespaces.isEmpty()) {
result = namespaces;
} else {
try {
result = coreV1Api.listNamespace(PRETTY, CONTINUE, fieldSelector, INCLUDE_UNINITIALIZED, labelSelector, LIMIT, DEFAULT_VERSION, TIMEOUT_SECONDS, WATCH)
.getItems()
.stream()
.map(n -> n.getMetadata().getName())
List<KubernetesManifest> namespaceManifests = jobExecutor.getAll(this, KubernetesKind.NAMESPACE, "");
result = namespaceManifests.stream()
.map(KubernetesManifest::getName)
.collect(Collectors.toList());
} catch (ApiException e) {

} catch (KubectlJobExecutor.KubectlException e) {
throw new RuntimeException(e);
}
}
Expand Down

0 comments on commit 408576a

Please sign in to comment.