This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
Allows user to define Secret from annotation #1498
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...her/standard/src/main/java/io/fabric8/maven/enricher/standard/FileDataSecretEnricher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* Copyright 2016 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you 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 io.fabric8.maven.enricher.standard; | ||
|
||
import io.fabric8.kubernetes.api.builder.TypedVisitor; | ||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.SecretBuilder; | ||
import io.fabric8.maven.core.util.Base64Util; | ||
import io.fabric8.maven.enricher.api.BaseEnricher; | ||
import io.fabric8.maven.enricher.api.MavenEnricherContext; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class FileDataSecretEnricher extends BaseEnricher { | ||
|
||
protected static final String PREFIX_ANNOTATION = "maven.fabric8.io/secret/"; | ||
|
||
public FileDataSecretEnricher(MavenEnricherContext buildContext) { | ||
super(buildContext, "fmp-secret-file"); | ||
} | ||
|
||
@Override | ||
public void addMissingResources(KubernetesListBuilder builder) { | ||
addAnnotations(builder); | ||
} | ||
|
||
private void addAnnotations(KubernetesListBuilder builder) { | ||
builder.accept(new TypedVisitor<SecretBuilder>() { | ||
|
||
@Override | ||
public void visit(SecretBuilder element) { | ||
final Map<String, String> annotations = element.buildMetadata().getAnnotations(); | ||
try { | ||
final Map<String, String> secretAnnotations = createSecretFromAnnotations(annotations); | ||
element.addToData(secretAnnotations); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private Map<String, String> createSecretFromAnnotations(final Map<String, String> annotations) throws IOException { | ||
final Set<Map.Entry<String, String>> entries = annotations.entrySet(); | ||
final Map<String, String> secretFileLocations = new HashMap<>(); | ||
|
||
for(Iterator<Map.Entry<String, String>> it = entries.iterator(); it.hasNext(); ) { | ||
Map.Entry<String, String> entry = it.next(); | ||
final String key = entry.getKey(); | ||
|
||
if(key.startsWith(PREFIX_ANNOTATION)) { | ||
byte[] bytes = readContent(entry.getValue()); | ||
secretFileLocations.put(getOutput(key), Base64Util.encodeToString(bytes)); | ||
it.remove(); | ||
} | ||
} | ||
|
||
return secretFileLocations; | ||
} | ||
|
||
private byte[] readContent(String location) throws IOException { | ||
return Files.readAllBytes(Paths.get(location)); | ||
} | ||
|
||
private String getOutput(String key) { | ||
return key.substring(PREFIX_ANNOTATION.length()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...standard/src/test/java/io/fabric8/maven/enricher/standard/FileDataSecretEnricherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* Copyright 2016 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you 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 io.fabric8.maven.enricher.standard; | ||
|
||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.fabric8.kubernetes.api.model.SecretBuilder; | ||
import io.fabric8.maven.core.config.ResourceConfig; | ||
import io.fabric8.maven.core.model.Configuration; | ||
import io.fabric8.maven.core.util.Base64Util; | ||
import io.fabric8.maven.enricher.api.MavenEnricherContext; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import mockit.Expectations; | ||
import mockit.Mocked; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class FileDataSecretEnricherTest { | ||
|
||
private static final String TEST_APPLICATION_PROPERTIES_PATH = "src/test/resources/test-application.properties"; | ||
private static final String TEST_APPLICATION_PROPERTIES = "test-application.properties"; | ||
@Mocked | ||
private MavenEnricherContext context; | ||
|
||
@Test | ||
public void shouldMaterializeFileContentFromAnnotation() throws IOException { | ||
|
||
// Given | ||
|
||
new Expectations() { | ||
{{ | ||
context.getConfiguration(); | ||
result = new Configuration.Builder() | ||
.resource(new ResourceConfig()) | ||
.build(); | ||
}} | ||
|
||
}; | ||
|
||
final FileDataSecretEnricher fileDataSecretEnricher = | ||
new FileDataSecretEnricher(context); | ||
final KubernetesListBuilder builder = new KubernetesListBuilder(); | ||
builder.addToSecretItems(createBaseSecret()); | ||
|
||
// When | ||
fileDataSecretEnricher.addMissingResources(builder); | ||
|
||
// Then | ||
final Secret secret = (Secret) builder.buildFirstItem(); | ||
|
||
final Map<String, String> data = secret.getData(); | ||
assertThat(data) | ||
.containsKey(TEST_APPLICATION_PROPERTIES); | ||
|
||
assertThat(data.get(TEST_APPLICATION_PROPERTIES)) | ||
.isEqualTo(Base64Util | ||
.encodeToString(Files.readAllBytes(Paths.get(TEST_APPLICATION_PROPERTIES_PATH)))); | ||
|
||
final Map<String, String> annotations = secret.getMetadata().getAnnotations(); | ||
assertThat(annotations) | ||
.isEmpty(); | ||
} | ||
|
||
private Secret createBaseSecret() { | ||
ObjectMetaBuilder metaBuilder = new ObjectMetaBuilder() | ||
.withNamespace("default"); | ||
|
||
Map<String, String> annotations = new HashMap<>(); | ||
annotations.put(FileDataSecretEnricher.PREFIX_ANNOTATION + TEST_APPLICATION_PROPERTIES, | ||
TEST_APPLICATION_PROPERTIES_PATH); | ||
metaBuilder = metaBuilder.withAnnotations(annotations); | ||
|
||
Map<String, String> data = new HashMap<>(); | ||
return new SecretBuilder() | ||
.withData(data) | ||
.withMetadata(metaBuilder.build()) | ||
.build(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @bohmber why not just use
?
The FMP code base uses this way of iteration everywhere, so it would look uniform with the codebase if you change it to this style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just a modified copy of the ConfigMapEnricher and i think the iterator is needed for the remove a few lines later
it.remove();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh i see