-
Notifications
You must be signed in to change notification settings - Fork 219
feat: customize mapping annotation in kubernetes dependent resource #2075
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c1adeb0
feat: customize mapping annotation in kubernetes dependent resource
csviri 2016f8e
progress
csviri c87015d
format
csviri db49292
integration test
csviri c59aed2
IT fix
csviri fed53b5
improvement
csviri 0a0f469
naming
csviri 5b3263c
format
csviri a060844
fix
csviri 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 hidden or 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 hidden or 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
72 changes: 72 additions & 0 deletions
72
...amework/src/test/java/io/javaoperatorsdk/operator/DependentCustomMappingAnnotationIT.java
This file contains hidden or 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,72 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.CustomMappingConfigMapDependentResource; | ||
import io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.DependentCustomMappingCustomResource; | ||
import io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.DependentCustomMappingReconciler; | ||
import io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.DependentCustomMappingSpec; | ||
|
||
import static io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.CustomMappingConfigMapDependentResource.CUSTOM_NAMESPACE_KEY; | ||
import static io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.CustomMappingConfigMapDependentResource.CUSTOM_NAME_KEY; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
class DependentCustomMappingAnnotationIT { | ||
|
||
public static final String INITIAL_VALUE = "initial value"; | ||
public static final String CHANGED_VALUE = "changed value"; | ||
public static final String TEST_RESOURCE_NAME = "test1"; | ||
|
||
@RegisterExtension | ||
LocallyRunOperatorExtension extension = | ||
LocallyRunOperatorExtension.builder() | ||
.withReconciler(DependentCustomMappingReconciler.class) | ||
.build(); | ||
|
||
|
||
@Test | ||
void testCustomMappingAnnotationForDependent() { | ||
var cr = extension.create(testResource()); | ||
assertConfigMapData(INITIAL_VALUE); | ||
|
||
cr.getSpec().setValue(CHANGED_VALUE); | ||
cr = extension.replace(cr); | ||
assertConfigMapData(CHANGED_VALUE); | ||
|
||
extension.delete(cr); | ||
|
||
await().untilAsserted(() -> { | ||
var resource = extension.get(ConfigMap.class, TEST_RESOURCE_NAME); | ||
assertThat(resource).isNull(); | ||
}); | ||
} | ||
|
||
private void assertConfigMapData(String val) { | ||
await().untilAsserted(() -> { | ||
var resource = extension.get(ConfigMap.class, TEST_RESOURCE_NAME); | ||
assertThat(resource).isNotNull(); | ||
assertThat(resource.getMetadata().getAnnotations()) | ||
.containsKey(CUSTOM_NAME_KEY) | ||
.containsKey(CUSTOM_NAMESPACE_KEY); | ||
assertThat(resource.getData()).containsEntry(CustomMappingConfigMapDependentResource.KEY, | ||
val); | ||
}); | ||
} | ||
|
||
|
||
DependentCustomMappingCustomResource testResource() { | ||
var dr = new DependentCustomMappingCustomResource(); | ||
dr.setMetadata(new ObjectMetaBuilder().withName(TEST_RESOURCE_NAME).build()); | ||
dr.setSpec(new DependentCustomMappingSpec()); | ||
dr.getSpec().setValue(INITIAL_VALUE); | ||
|
||
return dr; | ||
} | ||
|
||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...ator/sample/dependentcustommappingannotation/CustomMappingConfigMapDependentResource.java
This file contains hidden or 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,52 @@ | ||
package io.javaoperatorsdk.operator.sample.dependentcustommappingannotation; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDNoGCKubernetesDependentResource; | ||
import io.javaoperatorsdk.operator.processing.event.ResourceID; | ||
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper; | ||
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers; | ||
|
||
public class CustomMappingConfigMapDependentResource | ||
extends CRUDNoGCKubernetesDependentResource<ConfigMap, DependentCustomMappingCustomResource> | ||
implements SecondaryToPrimaryMapper<ConfigMap> { | ||
|
||
public static final String CUSTOM_NAME_KEY = "customNameKey"; | ||
public static final String CUSTOM_NAMESPACE_KEY = "customNamespaceKey"; | ||
public static final String KEY = "key"; | ||
|
||
private SecondaryToPrimaryMapper<ConfigMap> mapper = | ||
Mappers.fromAnnotation(CUSTOM_NAME_KEY, CUSTOM_NAMESPACE_KEY); | ||
|
||
public CustomMappingConfigMapDependentResource() { | ||
super(ConfigMap.class); | ||
} | ||
|
||
@Override | ||
protected ConfigMap desired(DependentCustomMappingCustomResource primary, | ||
Context<DependentCustomMappingCustomResource> context) { | ||
return new ConfigMapBuilder() | ||
.withMetadata(new ObjectMetaBuilder() | ||
.withName(primary.getMetadata().getName()) | ||
.withNamespace(primary.getMetadata().getNamespace()) | ||
.build()) | ||
.withData(Map.of(KEY, primary.getSpec().getValue())) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Set<ResourceID> toPrimaryResourceIDs(ConfigMap resource) { | ||
return mapper.toPrimaryResourceIDs(resource); | ||
} | ||
|
||
@Override | ||
protected void addSecondaryToPrimaryMapperAnnotations(ConfigMap desired, | ||
DependentCustomMappingCustomResource primary) { | ||
addSecondaryToPrimaryMapperAnnotations(desired, primary, CUSTOM_NAME_KEY, CUSTOM_NAMESPACE_KEY); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...perator/sample/dependentcustommappingannotation/DependentCustomMappingCustomResource.java
This file contains hidden or 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,14 @@ | ||
package io.javaoperatorsdk.operator.sample.dependentcustommappingannotation; | ||
|
||
import io.fabric8.kubernetes.api.model.Namespaced; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.fabric8.kubernetes.model.annotation.Group; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
|
||
@Group("sample.javaoperatorsdk") | ||
@Version("v1") | ||
public class DependentCustomMappingCustomResource | ||
extends CustomResource<DependentCustomMappingSpec, Void> | ||
implements Namespaced { | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...dk/operator/sample/dependentcustommappingannotation/DependentCustomMappingReconciler.java
This file contains hidden or 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,20 @@ | ||
package io.javaoperatorsdk.operator.sample.dependentcustommappingannotation; | ||
|
||
import io.javaoperatorsdk.operator.api.reconciler.*; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent; | ||
|
||
@ControllerConfiguration( | ||
dependents = {@Dependent(type = CustomMappingConfigMapDependentResource.class)}) | ||
public class DependentCustomMappingReconciler | ||
implements Reconciler<DependentCustomMappingCustomResource> { | ||
|
||
@Override | ||
public UpdateControl<DependentCustomMappingCustomResource> reconcile( | ||
DependentCustomMappingCustomResource resource, | ||
Context<DependentCustomMappingCustomResource> context) throws Exception { | ||
|
||
return UpdateControl.noUpdate(); | ||
} | ||
|
||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...ratorsdk/operator/sample/dependentcustommappingannotation/DependentCustomMappingSpec.java
This file contains hidden or 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,15 @@ | ||
package io.javaoperatorsdk.operator.sample.dependentcustommappingannotation; | ||
|
||
public class DependentCustomMappingSpec { | ||
|
||
private String value; | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public DependentCustomMappingSpec setValue(String value) { | ||
this.value = value; | ||
return this; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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.
Why was the check on
SecondToPrimaryMapper
implementation removed?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.
(Made an improvement on naming.)
So it has now different definition a little, it is not just for the default annotation. Before it was using default annotation and that was checked. Now it check if it does not use owner reference for that.