-
Notifications
You must be signed in to change notification settings - Fork 217
feat: use ssa for status update by default from UpdateControl
#2278
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
+240
−50
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1142a88
feat: use SSA for resource status patching
csviri 9eccbf4
docs
csviri 3f358c4
managed fields fix
csviri 1b3626f
refactor: simplify
metacosm 871f5b7
format
csviri b4ce0cd
fix IT
csviri 0f02f86
fix on short name
csviri 6c99a78
Integration test skeleton for SSA handling
csviri 31fbd2c
Integration test
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
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
155 changes: 155 additions & 0 deletions
155
operator-framework/src/test/java/io/javaoperatorsdk/operator/StatusPatchSSAMigrationIT.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,155 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInfo; | ||
|
||
import io.fabric8.kubernetes.api.model.Namespace; | ||
import io.fabric8.kubernetes.api.model.NamespaceBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.statuspatchnonlocking.StatusPatchLockingCustomResource; | ||
import io.javaoperatorsdk.operator.sample.statuspatchnonlocking.StatusPatchLockingCustomResourceSpec; | ||
import io.javaoperatorsdk.operator.sample.statuspatchnonlocking.StatusPatchLockingReconciler; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class StatusPatchSSAMigrationIT { | ||
|
||
public static final String TEST_RESOURCE_NAME = "test"; | ||
|
||
private final KubernetesClient client = new KubernetesClientBuilder().build(); | ||
private String testNamespace; | ||
|
||
@BeforeEach | ||
void beforeEach(TestInfo testInfo) { | ||
LocallyRunOperatorExtension.applyCrd(StatusPatchLockingCustomResource.class, | ||
client); | ||
testInfo.getTestMethod() | ||
.ifPresent(method -> testNamespace = KubernetesResourceUtil.sanitizeName(method.getName())); | ||
client.namespaces().resource(testNamespace(testNamespace)).create(); | ||
} | ||
|
||
@AfterEach | ||
void afterEach() { | ||
client.namespaces().withName(testNamespace).delete(); | ||
await().untilAsserted(() -> { | ||
var ns = client.namespaces().withName(testNamespace).get(); | ||
assertThat(ns).isNull(); | ||
}); | ||
client.close(); | ||
} | ||
|
||
|
||
@Test | ||
void testMigratingToSSA() { | ||
var operator = startOperator(false); | ||
var testResource = client.resource(testResource()).create(); | ||
|
||
await().untilAsserted(() -> { | ||
var res = client.resource(testResource).get(); | ||
assertThat(res.getStatus()).isNotNull(); | ||
assertThat(res.getStatus().getMessage()).isEqualTo(StatusPatchLockingReconciler.MESSAGE); | ||
assertThat(res.getStatus().getValue()).isEqualTo(1); | ||
}); | ||
operator.stop(); | ||
|
||
// start operator with SSA | ||
operator = startOperator(true); | ||
await().untilAsserted(() -> { | ||
var res = client.resource(testResource).get(); | ||
assertThat(res.getStatus()).isNotNull(); | ||
assertThat(res.getStatus().getMessage()).isEqualTo(StatusPatchLockingReconciler.MESSAGE); | ||
assertThat(res.getStatus().getValue()).isEqualTo(2); | ||
}); | ||
|
||
var actualResource = client.resource(testResource()).get(); | ||
actualResource.getSpec().setMessageInStatus(false); | ||
client.resource(actualResource).update(); | ||
|
||
await().untilAsserted(() -> { | ||
var res = client.resource(testResource).get(); | ||
assertThat(res.getStatus()).isNotNull(); | ||
// !!! This is wrong, the message should be null, | ||
// see issue in Kubernetes: https://github.com/kubernetes/kubernetes/issues/99003 | ||
assertThat(res.getStatus().getMessage()).isNotNull(); | ||
assertThat(res.getStatus().getValue()).isEqualTo(3); | ||
}); | ||
|
||
client.resource(testResource()).delete(); | ||
operator.stop(); | ||
} | ||
|
||
@Test | ||
void workaroundMigratingFromToSSA() { | ||
var operator = startOperator(false); | ||
var testResource = client.resource(testResource()).create(); | ||
|
||
await().untilAsserted(() -> { | ||
var res = client.resource(testResource).get(); | ||
assertThat(res.getStatus()).isNotNull(); | ||
assertThat(res.getStatus().getMessage()).isEqualTo(StatusPatchLockingReconciler.MESSAGE); | ||
assertThat(res.getStatus().getValue()).isEqualTo(1); | ||
}); | ||
operator.stop(); | ||
|
||
// start operator with SSA | ||
operator = startOperator(true); | ||
await().untilAsserted(() -> { | ||
var res = client.resource(testResource).get(); | ||
assertThat(res.getStatus()).isNotNull(); | ||
assertThat(res.getStatus().getMessage()).isEqualTo(StatusPatchLockingReconciler.MESSAGE); | ||
assertThat(res.getStatus().getValue()).isEqualTo(2); | ||
}); | ||
|
||
var actualResource = client.resource(testResource()).get(); | ||
actualResource.getSpec().setMessageInStatus(false); | ||
// removing the managed field entry for former method works | ||
actualResource.getMetadata().setManagedFields(actualResource.getMetadata().getManagedFields() | ||
.stream().filter(r -> !r.getOperation().equals("Update") && r.getSubresource() != null) | ||
.toList()); | ||
client.resource(actualResource).update(); | ||
|
||
await().untilAsserted(() -> { | ||
var res = client.resource(testResource).get(); | ||
assertThat(res.getStatus()).isNotNull(); | ||
assertThat(res.getStatus().getMessage()).isNull(); | ||
assertThat(res.getStatus().getValue()).isEqualTo(3); | ||
}); | ||
|
||
client.resource(testResource()).delete(); | ||
operator.stop(); | ||
} | ||
|
||
|
||
private Operator startOperator(boolean patchStatusWithSSA) { | ||
var operator = new Operator(o -> o.withCloseClientOnStop(false) | ||
.withUseSSAForResourceStatusPatch(patchStatusWithSSA)); | ||
operator.register(new StatusPatchLockingReconciler(), | ||
o -> o.settingNamespaces(testNamespace)); | ||
|
||
operator.start(); | ||
return operator; | ||
} | ||
|
||
StatusPatchLockingCustomResource testResource() { | ||
StatusPatchLockingCustomResource res = new StatusPatchLockingCustomResource(); | ||
res.setSpec(new StatusPatchLockingCustomResourceSpec()); | ||
res.setMetadata(new ObjectMetaBuilder() | ||
.withName(TEST_RESOURCE_NAME) | ||
.withNamespace(testNamespace) | ||
.build()); | ||
return res; | ||
} | ||
|
||
private Namespace testNamespace(String name) { | ||
return new NamespaceBuilder().withMetadata(new ObjectMetaBuilder() | ||
.withName(name) | ||
.build()).build(); | ||
} | ||
} |
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.
Is there a reason why people who not want to use SSA for status updating?
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.
Will add an additional test for upgradint from non-SSA to SSA, just added for safety reasons: since there were issues with SSA in the past, also even the non-built in kubernetes controller are not using SSA. So it does not harm to add a feature flag.
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.
Well, it adds complexity for not much. I'd rather we only add the flag if we determine it's needed.
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.
I think the migration from non-SSA to SSA - is not trivial. At least I hit some strange issues today, but will add test. Especially actual users would be safer to have this flag.