-
Notifications
You must be signed in to change notification settings - Fork 219
feat: sanitization of resources for matching #2042
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 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5f60005
reproduce ssa issue
csviri c880974
fixes
csviri ddf02dc
test
csviri 3306281
feat: sanitization of resources for matching
csviri ee1970e
add default resources
csviri 5faf9d7
format
csviri 4266e6c
generic configs for default non SSA resources
csviri 128328a
format
csviri 62f6b91
docs
csviri 81f9830
remove unnecessary line
csviri 34874e0
fix it
csviri c2fbacf
fix IT
csviri ec9c355
refactor: make intent more explicit
metacosm 43056e0
fix: remove duplicated line
metacosm 0c4db58
docs: improve javadoc
metacosm 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
47 changes: 47 additions & 0 deletions
47
...io/javaoperatorsdk/operator/processing/dependent/kubernetes/DesiredResourceSanitizer.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,47 @@ | ||
package io.javaoperatorsdk.operator.processing.dependent.kubernetes; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.PersistentVolumeClaimStatus; | ||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.fabric8.kubernetes.api.model.apps.StatefulSet; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
|
||
public class DesiredResourceSanitizer { | ||
|
||
private DesiredResourceSanitizer() {} | ||
|
||
public static <R, P extends HasMetadata> void sanitizeDesired(R desired, R actual, P primary, | ||
Context<P> context, boolean useSSA) { | ||
if (useSSA) { | ||
if (desired instanceof StatefulSet) { | ||
fillDefaultsOnVolumeClaimTemplate((StatefulSet) desired); | ||
} | ||
if (desired instanceof Secret) { | ||
checkIfStringDataUsed((Secret) desired); | ||
} | ||
} | ||
} | ||
|
||
private static void checkIfStringDataUsed(Secret secret) { | ||
if (secret.getStringData() != null && !secret.getStringData().isEmpty()) { | ||
throw new IllegalStateException( | ||
"There is a known issue using StringData with SSA. Use data instead."); | ||
} | ||
} | ||
|
||
private static void fillDefaultsOnVolumeClaimTemplate(StatefulSet statefulSet) { | ||
if (!statefulSet.getSpec().getVolumeClaimTemplates().isEmpty()) { | ||
statefulSet.getSpec().getVolumeClaimTemplates().forEach(t -> { | ||
if (t.getSpec().getVolumeMode() == null) { | ||
t.getSpec().setVolumeMode("Filesystem"); | ||
} | ||
if (t.getStatus() == null) { | ||
t.setStatus(new PersistentVolumeClaimStatus()); | ||
} | ||
if (t.getStatus().getPhase() == null) { | ||
t.getStatus().setPhase("pending"); | ||
} | ||
}); | ||
} | ||
} | ||
} |
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
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
57 changes: 57 additions & 0 deletions
57
...or-framework/src/test/java/io/javaoperatorsdk/operator/StatefulSetDesiredSanitizerIT.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,57 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import java.time.Duration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.apps.StatefulSet; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.statefulsetdesiredsanitizer.StatefulSetDesiredSanitizerCustomResource; | ||
import io.javaoperatorsdk.operator.sample.statefulsetdesiredsanitizer.StatefulSetDesiredSanitizerDependentResource; | ||
import io.javaoperatorsdk.operator.sample.statefulsetdesiredsanitizer.StatefulSetDesiredSanitizerReconciler; | ||
import io.javaoperatorsdk.operator.sample.statefulsetdesiredsanitizer.StatefulSetDesiredSanitizerSpec; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class StatefulSetDesiredSanitizerIT { | ||
|
||
public static final String TEST_1 = "test1"; | ||
|
||
@RegisterExtension | ||
LocallyRunOperatorExtension extension = | ||
LocallyRunOperatorExtension.builder() | ||
.withReconciler(new StatefulSetDesiredSanitizerReconciler()) | ||
.build(); | ||
|
||
@Test | ||
void testSSAMatcher() { | ||
var resource = extension.create(testResource()); | ||
|
||
await().pollDelay(Duration.ofMillis(200)).untilAsserted(() -> { | ||
var statefulSet = extension.get(StatefulSet.class, TEST_1); | ||
assertThat(statefulSet).isNotNull(); | ||
}); | ||
// make sure reconciliation happens at least once more | ||
resource.getSpec().setValue("changed value"); | ||
extension.replace(resource); | ||
|
||
await().untilAsserted( | ||
() -> assertThat(StatefulSetDesiredSanitizerDependentResource.nonMatchedAtLeastOnce) | ||
.isFalse()); | ||
} | ||
|
||
StatefulSetDesiredSanitizerCustomResource testResource() { | ||
var res = new StatefulSetDesiredSanitizerCustomResource(); | ||
res.setMetadata(new ObjectMetaBuilder() | ||
.withName(TEST_1) | ||
.build()); | ||
res.setSpec(new StatefulSetDesiredSanitizerSpec()); | ||
res.getSpec().setValue("initial value"); | ||
|
||
return res; | ||
} | ||
|
||
} |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.