-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sanitization of resources for matching
Signed-off-by: Attila Mészáros <[email protected]>
- Loading branch information
Showing
16 changed files
with
215 additions
and
155 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...io/javaoperatorsdk/operator/processing/dependent/kubernetes/DesiredResourceSanitizer.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,50 @@ | ||
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; | ||
|
||
import static io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource.useSSA; | ||
|
||
public class DesiredResourceSanitizer { | ||
|
||
private DesiredResourceSanitizer() {} | ||
|
||
public static <R, P extends HasMetadata> void sanitizeDesired(R desired, R actual, P primary, | ||
Context<P> context) { | ||
if (useSSA(context)) { | ||
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.setStatus(new PersistentVolumeClaimStatus()); | ||
t.getStatus().setPhase("pending"); | ||
} | ||
}); | ||
} | ||
} | ||
} |
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
43 changes: 0 additions & 43 deletions
43
...tor-framework/src/test/java/io/javaoperatorsdk/operator/SSAStatefulSetMatcherIssueIT.java
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
...or-framework/src/test/java/io/javaoperatorsdk/operator/StatefulSetDesiredSanitizerIT.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,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 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
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
20 changes: 0 additions & 20 deletions
20
...rsdk/operator/sample/ssastatefulsetmatcherissue/SSAStatefulSetMatcherIssueReconciler.java
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
...aoperatorsdk/operator/sample/ssastatefulsetmatcherissue/StatefulSetDependentResource.java
This file was deleted.
Oops, something went wrong.
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
43 changes: 43 additions & 0 deletions
43
...ator/sample/statefulsetdesiredsanitizer/StatefulSetDesiredSanitizerDependentResource.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,43 @@ | ||
package io.javaoperatorsdk.operator.sample.statefulsetdesiredsanitizer; | ||
|
||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.apps.StatefulSet; | ||
import io.javaoperatorsdk.operator.ReconcilerUtils; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; | ||
|
||
public class StatefulSetDesiredSanitizerDependentResource | ||
extends | ||
CRUDKubernetesDependentResource<StatefulSet, StatefulSetDesiredSanitizerCustomResource> { | ||
|
||
public static volatile Boolean nonMatchedAtLeastOnce; | ||
|
||
public StatefulSetDesiredSanitizerDependentResource() { | ||
super(StatefulSet.class); | ||
} | ||
|
||
@Override | ||
protected StatefulSet desired(StatefulSetDesiredSanitizerCustomResource primary, | ||
Context<StatefulSetDesiredSanitizerCustomResource> context) { | ||
var template = | ||
ReconcilerUtils.loadYaml(StatefulSet.class, getClass(), "statefulset.yaml"); | ||
template.setMetadata(new ObjectMetaBuilder() | ||
.withName(primary.getMetadata().getName()) | ||
.withNamespace(primary.getMetadata().getNamespace()) | ||
.build()); | ||
return template; | ||
} | ||
|
||
@Override | ||
public Result<StatefulSet> match(StatefulSet actualResource, | ||
StatefulSetDesiredSanitizerCustomResource primary, | ||
Context<StatefulSetDesiredSanitizerCustomResource> context) { | ||
var res = super.match(actualResource, primary, context); | ||
if (!res.matched()) { | ||
nonMatchedAtLeastOnce = true; | ||
} else if (nonMatchedAtLeastOnce == null) { | ||
nonMatchedAtLeastOnce = false; | ||
} | ||
return res; | ||
} | ||
} |
Oops, something went wrong.