Skip to content

Commit

Permalink
add pod env and vault to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ssharaev committed Nov 16, 2024
1 parent dacfd3f commit 55eb9f9
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 108 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/ssharaev/k8s/env/plugin/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Utils {

public static final String CONFIGMAP_DELIMITER = ";";
public static final String DELIMITER = ";";

public static String joinIfNotNull(List<String> names) {
if (names == null || names.isEmpty()) {
return null;
}
return String.join(Utils.CONFIGMAP_DELIMITER, names);
return String.join(Utils.DELIMITER, names);
}

public static List<String> splitIfNotEmpty(String string) {
return string == null ? List.of() : List.of(string.split(Utils.CONFIGMAP_DELIMITER));
return string == null ? List.of() : List.of(string.split(Utils.DELIMITER));
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/ssharaev/k8s/env/plugin/model/EnvMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;

@Getter
@RequiredArgsConstructor
public enum EnvMode {
CONFIGMAP_AND_SECRET("Configmaps and secrets"),
POD_ENV("Pod environment"),
POD_VAULT("Pod vault environment");

private final String textName;
private final String beautyName;

public static String[] beautyNames() {
return Arrays.stream(values()).sequential()
.map(EnvMode::getBeautyName)
.toList()
.toArray(new String[]{});
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.intellij.execution.Executor;
import com.intellij.execution.application.ApplicationConfiguration;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.task.ExecuteRunConfigurationTask;
import com.ssharaev.k8s.env.plugin.services.PluginSettingsProvider;
Expand All @@ -18,7 +19,11 @@

public class GradleK8sEnvExecutionEnvironmentProvider implements GradleExecutionEnvironmentProvider {

private final EnvProvider envProvider = new CombinedEnvProvider();
private final EnvProvider envProvider;

public GradleK8sEnvExecutionEnvironmentProvider() {
envProvider = ApplicationManager.getApplication().getService(CombinedEnvProvider.class);
}

@Override
public boolean isApplicable(@NotNull ExecuteRunConfigurationTask task) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.intellij.execution.configurations.JavaParameters;
import com.intellij.execution.configurations.RunConfigurationBase;
import com.intellij.execution.configurations.RunnerSettings;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.ssharaev.k8s.env.plugin.services.PluginSettingsProvider;
import com.ssharaev.k8s.env.plugin.services.RunConfigurationEditorService;
import com.ssharaev.k8s.env.plugin.services.providers.CombinedEnvProvider;
import com.ssharaev.k8s.env.plugin.services.providers.EnvProvider;
import com.ssharaev.k8s.env.plugin.ui.RunConfigurationSettingsEditor;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -19,12 +21,18 @@

public class IdeaRunConfigurationExtension extends RunConfigurationExtension {

private final EnvProvider envProvider = new CombinedEnvProvider();
private final EnvProvider envProvider;
private final RunConfigurationEditorService runConfigurationEditorService;

public IdeaRunConfigurationExtension() {
envProvider = ApplicationManager.getApplication().getService(CombinedEnvProvider.class);
runConfigurationEditorService = ApplicationManager.getApplication().getService(RunConfigurationEditorService.class);
}

@Nullable
@Override
protected String getEditorTitle() {
return "Env from k8s";
return "Kubernetes Run Configuration Env";
}

@Nullable
Expand All @@ -36,29 +44,25 @@ protected <P extends RunConfigurationBase<?>> SettingsEditor<P> createEditor(@No
@NotNull
@Override
protected String getSerializationId() {
return EnvK8sRunConfigurationEditor.getSerializationId();
return runConfigurationEditorService.getSerializationId();
}

@Override
protected void writeExternal(@NotNull RunConfigurationBase runConfiguration, @NotNull Element element) throws WriteExternalException {
EnvK8sRunConfigurationEditor.writeExternal(runConfiguration, element);
runConfigurationEditorService.writeExternal(runConfiguration, element);
}

@Override
protected void readExternal(@NotNull RunConfigurationBase runConfiguration, @NotNull Element element) throws InvalidDataException {
EnvK8sRunConfigurationEditor.readExternal(runConfiguration, element);
runConfigurationEditorService.readExternal(runConfiguration, element);
}

@Override
protected void validateConfiguration(@NotNull RunConfigurationBase configuration, boolean isExecution) {
EnvK8sRunConfigurationEditor.validateConfiguration(configuration, isExecution);
protected void validateConfiguration(@NotNull RunConfigurationBase configuration, boolean isExecution) throws ConfigurationException {
runConfigurationEditorService.validateConfiguration(configuration, isExecution);
}

/**
* Unlike other extensions the IDEA extension
* calls this method instead of RunConfigurationExtensionBase#patchCommandLine method
* that we could have used to update environment variables.
*/

@Override
public <T extends RunConfigurationBase<?>> void updateJavaParameters(
@NotNull final T configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,56 @@
import com.intellij.execution.Executor;
import com.intellij.execution.application.ApplicationConfiguration;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.task.ExecuteRunConfigurationTask;
import com.ssharaev.k8s.env.plugin.services.PluginSettingsProvider;
import com.ssharaev.k8s.env.plugin.services.providers.CombinedEnvProvider;
import com.ssharaev.k8s.env.plugin.services.providers.EnvProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.execution.MavenRunConfiguration;
import org.jetbrains.idea.maven.execution.MavenRunnerSettings;
import org.jetbrains.idea.maven.execution.build.MavenExecutionEnvironmentProvider;

import java.util.Map;
import java.util.Optional;


public class MavenK8sEnvExecutionEnvironmentProvider implements MavenExecutionEnvironmentProvider {

private final EnvProvider envProvider;

public MavenK8sEnvExecutionEnvironmentProvider() {
envProvider = ApplicationManager.getApplication().getService(CombinedEnvProvider.class);
}

@Override
public boolean isApplicable(@NotNull ExecuteRunConfigurationTask task) {
return task.getRunProfile() instanceof ApplicationConfiguration;
}

@Override
public @Nullable ExecutionEnvironment createExecutionEnvironment(@NotNull Project project, @NotNull ExecuteRunConfigurationTask task, @Nullable Executor executor) {
return null;
final ExecutionEnvironment environment = delegateProvider(task)
.map(provider -> provider.createExecutionEnvironment(project, task, executor))
.orElse(null);

if (environment != null && environment.getRunProfile() instanceof MavenRunConfiguration targetConfig) {
final ApplicationConfiguration sourceConfig = (ApplicationConfiguration) task.getRunProfile();
Map<String, String> env = envProvider.getEnv(PluginSettingsProvider.getPluginSetting(sourceConfig));
Optional.ofNullable(targetConfig.getRunnerSettings())
.map(MavenRunnerSettings::getEnvironmentProperties)
.ifPresent(properties -> properties.putAll(env));
}
return environment;
}

private Optional<MavenExecutionEnvironmentProvider> delegateProvider(
final ExecuteRunConfigurationTask executeRunConfigurationTask
) {
return MavenExecutionEnvironmentProvider.EP_NAME.getExtensionList().stream()
.filter(provider -> provider != this && provider.isApplicable(executeRunConfigurationTask))
.findFirst();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.ssharaev.k8s.env.plugin.ui;
package com.ssharaev.k8s.env.plugin.runConfiguration;

import com.intellij.execution.configurations.RunConfigurationBase;
import com.intellij.openapi.options.SettingsEditor;
import com.ssharaev.k8s.env.plugin.model.PluginSettings;
import com.ssharaev.k8s.env.plugin.services.PluginSettingsProvider;
import com.ssharaev.k8s.env.plugin.ui.RunConfigurationPanelProvider;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
Expand All @@ -19,9 +20,7 @@ public RunConfigurationSettingsEditor() {
@Override
protected void resetEditorFrom(@NotNull T configuration) {
PluginSettings state = PluginSettingsProvider.getPluginSetting(configuration);
if (state != null) {
panelProvider.setState(state);
}
panelProvider.setState(state);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1ConfigMapList;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1SecretList;
import io.kubernetes.client.util.Config;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -35,14 +37,34 @@ public KubernetesService() {
client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
client.setHttpClient(client.getHttpClient().newBuilder()
.readTimeout(10, TimeUnit.SECONDS) // Infinite read timeout
.readTimeout(10, TimeUnit.SECONDS)
.build());
} catch (IOException e) {
throw new RuntimeException("Unable to connect to the k8s cluster", e);
}
this.api = new CoreV1Api(client);
}

public void connected() throws ApiException {
api.listNamespace().execute();
}

public List<String> getNamespaces() {
try {
return api.listNamespace().execute().getItems()
.stream()
.filter(Objects::nonNull)
.map(V1Namespace::getMetadata)
.filter(Objects::nonNull)
.map(V1ObjectMeta::getName)
.filter(Objects::nonNull)
.toList();
} catch (ApiException e) {
LOGGER.warn("Error while getting namespaces!", e);
return List.of();
}
}

public Map<String, String> getEnvFromConfigmaps(@NotNull String namespace, @NotNull Collection<String> configmapNames) {
LOGGER.debug("Start searching k8s env for namespace: " + namespace + " and configmap list: " + configmapNames);
if (configmapNames.isEmpty()) {
Expand Down Expand Up @@ -94,6 +116,7 @@ public Map<String, String> getEnvFromPod(@NotNull String namespace, @NotNull Str
return execInPod(namespace, podName, "printenv");
}

// TODO change command
public Map<String, String> getVaultEnvFromPod(@NotNull String namespace, @NotNull String podName) {
LOGGER.debug("Start searching pod k8s vault env for namespace: " + namespace + " and pod name: " + podName);
return execInPod(namespace, podName, "printenv");
Expand Down
Loading

0 comments on commit 55eb9f9

Please sign in to comment.