Skip to content

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
egoshard committed May 10, 2019
1 parent acf8060 commit f1d2596
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 72 deletions.
26 changes: 21 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
/*
* Copyright (c) 2019. Matt Trefethen
*
* 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.
*/

plugins {
id 'org.jetbrains.intellij' version '0.4.1'
id 'org.jetbrains.intellij' version '0.3.11'
}

group 'com.egoshard.intellij'
Expand All @@ -10,6 +26,9 @@ ext {
junit5PlatformVersion = '1.3.1'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
}
Expand All @@ -29,15 +48,12 @@ test {
}

intellij {
version '2018.3.3'
version '2018.2.6'
}

patchPluginXml {
sinceBuild '172'
untilBuild '183.*'
// changeNotes """
// Add change notes here.<br>
// <em>most HTML tags may be used</em>"""
}

wrapper {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/egoshard/intellij/k8s/ConfigSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.util.ArrayList;
import java.util.List;

/**
* Configuration settings wrapper
*/
public class ConfigSettings {

private final boolean enabled;
Expand Down
114 changes: 109 additions & 5 deletions src/main/java/com/egoshard/intellij/k8s/K8sRunConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,130 @@
/*
* Copyright (c) 2019. Matt Trefethen
*
* 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.egoshard.intellij.k8s;

import com.egoshard.intellij.k8s.ui.ConfigEditor;
import com.egoshard.intellij.k8s.ui.ConfigPanelFactory;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.RunConfigurationExtension;
import com.intellij.execution.configurations.JavaParameters;
import com.intellij.execution.configurations.RunConfigurationBase;
import com.intellij.execution.configurations.RunnerSettings;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.options.SettingsEditor;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class K8sRunConfiguration extends RunConfigurationExtension {
import java.util.HashMap;
import java.util.Map;

/**
* Run configuration extension for converting Kubernetes configuration files into runtime environment variables.
*/
class K8sRunConfiguration extends RunConfigurationExtension {

private static final Logger logger = Logger.getInstance(K8sRunConfiguration.class);

@Override
public <T extends RunConfigurationBase> void updateJavaParameters(T configuration, JavaParameters params, RunnerSettings runnerSettings) throws ExecutionException {
public boolean isEnabledFor(@NotNull RunConfigurationBase applicableConfiguration, @Nullable RunnerSettings runnerSettings) {
return true;
}

/**
* Returns the ID used to serialize the settings.
*
* @return the serialization ID (must be unique across all run configuration extensions).
*/
@NotNull
@Override
protected String getSerializationId() {
return ConfigEditor.getSerializationId();
}

/**
* Loads the settings of this extension from the run configuration XML element. In memory, the settings can be placed into the
* user data of the run configuration.
*
* @param runConfiguration the run configuration being deserialized.
* @param element the element with persisted settings.
*/
@Override
public boolean isEnabledFor(@NotNull RunConfigurationBase applicableConfiguration, @Nullable RunnerSettings runnerSettings) {
return true;
protected void readExternal(@NotNull RunConfigurationBase runConfiguration, @NotNull Element element) {
ConfigEditor.read(runConfiguration, element);
}

/**
* Saves the settings of this extension to the run configuration XML element.
*
* @param runConfiguration the run configuration being serialized.
* @param element the element into which the settings should be persisted,
*/
@Override
protected void writeExternal(@NotNull RunConfigurationBase runConfiguration, @NotNull Element element) {
ConfigEditor.write(runConfiguration, element);
}

/**
* Creates an editor for the settings of this extension. The editor is displayed as an additional tab of the run configuration options
* in the Run/Debug Configurations dialog.
*
* @param configuration the configuration being edited.
* @return the editor component, or null if this extension doesn't provide any UI for editing the settings.
*/
@Nullable
@Override
protected <P extends RunConfigurationBase> SettingsEditor<P> createEditor(@NotNull P configuration) {
return new ConfigEditor<>(new ConfigPanelFactory<>(), configuration);
}

/**
* Returns the title of the tab in which the settings editor is displayed.
*
* @return the editor tab title, or null if this extension doesn't provide any UI for editing the settings.
*/
@Nullable
@Override
protected String getEditorTitle() {
return ConfigEditor.getTitle();
}

/**
* Validate extensions after general configuration validation passed
*
* @param configuration run configuration implementation
* @param isExecution true if the configuration is about to be executed, false if the configuration settings are being edited.
*/
@Override
protected void validateConfiguration(@NotNull RunConfigurationBase configuration, boolean isExecution) {
ConfigEditor.validate(configuration);
}

/**
* Updates environment parameters based on parsed configuration files
*/
@Override
public <T extends RunConfigurationBase> void updateJavaParameters(T configuration, JavaParameters params, RunnerSettings runnerSettings) throws ExecutionException {
logger.info("Kubernetes configuration injection commencing.");
params.setEnv(ConfigEditor.parse(configuration, new HashMap<>(params.getEnv())));
StringBuilder builder = new StringBuilder();
builder.append("Injected parameters:\n");
for (Map.Entry<String, String> stringStringEntry : params.getEnv().entrySet()) {
builder.append(stringStringEntry.getKey()).append("=").append(stringStringEntry.getValue()).append("\n");
}
logger.info(builder.toString());
}

/**
Expand All @@ -29,7 +133,7 @@ public boolean isEnabledFor(@NotNull RunConfigurationBase applicableConfiguratio
* turned off in its settings. E.g. RCov in general available for given run configuration, but may be turned off.
*/
@Override
public boolean isApplicableFor(@NotNull RunConfigurationBase<?> configuration) {
public boolean isApplicableFor(@NotNull RunConfigurationBase configuration) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ void validate(@NotNull(MSG_SOURCE_MISSING) Map<String, Object> source) {
* @return map key/value pairs or an empty map if the key does not exist in the source map.
*/
@SuppressWarnings("unchecked")
Map<String, String> getData(@NotNull(MSG_SOURCE_MISSING) Map<String, Object> source, @NotNull(MSG_KEY_MISSING) String key) {
return (Map<String, String>) source.getOrDefault(key, new HashMap<>());
Map<String, Object> getData(@NotNull(MSG_SOURCE_MISSING) Map<String, Object> source, @NotNull(MSG_KEY_MISSING) String key) {
return (Map<String, Object>) source.getOrDefault(key, new HashMap<>());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.stream.Collectors;

/**
*
* Parses ConfigMap files into discrete environment variable key/value pairs.
*/
public class ConfigMapParser extends AbstractParser {

Expand All @@ -31,11 +31,9 @@ public class ConfigMapParser extends AbstractParser {
*/
@Override
public Map<String, String> parse(Map<String, Object> source) {
Map<String, String> data = super.getData(source, KEY_DATA);
Map<String, Object> data = super.getData(source, KEY_DATA);
return data.keySet().stream()
.collect(Collectors.toMap(
key -> key, data::get, (a, b) -> b)
);
.collect(Collectors.toMap(key -> key, key -> String.valueOf(data.get(key)), (a, b) -> b));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public interface ConfigParser {
Kind getSupportedKind();

/**
* Kind enumeration
* Configuration kind enumeration
*/
enum Kind {

CONFIGMAP("ConfigMap"),
SECRET("Secret");

private String key;
private final String key;

Kind(String key) {
this.key = key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static java.util.Base64.getDecoder;

/**
* Parsers secret configuration files into discrete environment variable key/value pairs.
* Parses secret configuration files into discrete environment variable key/value pairs.
*/
public class SecretParser extends AbstractParser {

Expand All @@ -40,15 +40,15 @@ public class SecretParser extends AbstractParser {
@Override
public Map<String, String> parse(Map<String, Object> source) {
Map<String, String> result;
Map<String, String> data = super.getData(source, KEY_DATA);
Map<String, String> stringData = super.getData(source, KEY_STRING_DATA);
Map<String, Object> data = super.getData(source, KEY_DATA);
Map<String, Object> stringData = super.getData(source, KEY_STRING_DATA);

Decoder decoder = getDecoder();
result = data.keySet().stream().collect(
Collectors.toMap(
s -> s,
s -> new String(decoder.decode(String.valueOf(data.get(s)))), (a, b) -> b));
stringData.keySet().forEach(key -> result.put(key, stringData.get(key)));
stringData.keySet().forEach(key -> result.put(key, String.valueOf(stringData.get(key))));

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
* Utility for file handling
*/
public class ConfigFileUtil {

private static final Logger logger = Logger.getInstance(ConfigFileUtil.class);
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/egoshard/intellij/k8s/ui/ConfigColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import javax.swing.table.TableCellRenderer;
import java.awt.*;

public class ConfigColumn extends ColumnInfo<ConfigEntry, String> {
/**
* IntelliJ UI column for rendering config entries.
*/
class ConfigColumn extends ColumnInfo<ConfigEntry, String> {

private static final String MSG_FILE_NOT_FOUND = "File not found.";
private static final String MSG_PATH = "Path";
Expand All @@ -43,6 +46,9 @@ public boolean isCellEditable(ConfigEntry entry) {
return true;
}

/**
* @see com.intellij.util.ui.table.ComboBoxTableCellEditor
*/
@Nullable
@Override
public TableCellEditor getEditor(ConfigEntry entry) {
Expand All @@ -60,6 +66,10 @@ public String valueOf(ConfigEntry entry) {
return entry.getPath();
}

/**
* @see com.intellij.util.ui.table.IconTableCellRenderer
* @see com.intellij.util.ui.LocalPathCellEditor
*/
@Override
public TableCellRenderer getRenderer(final ConfigEntry entry) {
return new DefaultTableCellRenderer() {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/egoshard/intellij/k8s/ui/ConfigEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.util.*;

/**
* @param <T>
* User interface editor provider.
*/
public class ConfigEditor<T extends RunConfigurationBase> extends SettingsEditor<T> {

Expand Down Expand Up @@ -147,9 +147,11 @@ public static void validate(RunConfigurationBase config) {


/**
* @param config
* @param params
* @return
* Parses all configuration entries in settings and builds a variable map.
*
* @param config run configuration
* @param params parameter map returned if settings are disable or null
* @return configuration map
*/
public static Map<String, String> parse(RunConfigurationBase config, Map<String, String> params) throws ExecutionException {
ConfigSettings settings = config.getUserData(SETTING_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.ArrayList;
import java.util.Map;

public class ConfigPanel<T extends RunConfigurationBase> extends JPanel {
class ConfigPanel<T extends RunConfigurationBase> extends JPanel {

private static final String MSG_NO_FILE_SELECTED = "No file selected";
private static final String MSG_SELECT_K8S_FILE = "Select Kubernetes ConfigMap or Secret File";
Expand Down
24 changes: 19 additions & 5 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
<!--
~ Copyright (c) 2019. Matt Trefethen
~
~ 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.
-->
<idea-plugin>
<id>com.egoshard.intellij.k8s-runtime-config-plugin</id>
<name>Kubernetes Runtime Configuration</name>
<vendor email="[email protected]" url="http://www.egoshard.com">EgoShard</vendor>
<vendor email="[email protected]" url="https://github.com/egoshard/intellij-k8s-runtime-config">Matt Trefethen</vendor>
<description><![CDATA[
This plugin loads Kubernetes ConfigMap and Secret data values as environment variables in an IntelliJ run configuration.<br>
This plugin loads Kubernetes ConfigMap, Secret and String data values as environment variables in an IntelliJ run configuration.
]]></description>
<version>1.0.0</version>
<idea-version since-build="172" until-build="183.*"/>
<depends>com.intellij.modules.lang</depends>
<extensions defaultExtensionNs="com.intellij">
<runConfigurationExtension id="k8s-config-plugin" implementation="com.egoshard.intellij.k8s.K8sRunConfiguration"/>
</extensions>
<actions>
<!-- Add your actions here -->
</actions>
</idea-plugin>
Loading

0 comments on commit f1d2596

Please sign in to comment.