-
Notifications
You must be signed in to change notification settings - Fork 217
improve: blocklist of problematic resources for previous version annotation #2774
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
Open
csviri
wants to merge
5
commits into
main
Choose a base branch
from
blacklist-prev-annotation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a0548b2
feat: blacklist of problematic resources for previous version annotation
csviri 72fb838
rename to blocklist, added javadocs
csviri c46bc69
wip
csviri 53081c4
remove deamonSet since was not able to reproduce the behavior
csviri 2910c6a
Add 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
95 changes: 95 additions & 0 deletions
95
...rc/test/java/io/javaoperatorsdk/operator/dependent/prevblocklist/DeploymentDependent.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,95 @@ | ||
package io.javaoperatorsdk.operator.dependent.prevblocklist; | ||
|
||
import java.util.Map; | ||
|
||
import io.fabric8.kubernetes.api.model.ContainerBuilder; | ||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.LabelSelectorBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.PodSpecBuilder; | ||
import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder; | ||
import io.fabric8.kubernetes.api.model.Quantity; | ||
import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder; | ||
import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; | ||
import io.fabric8.kubernetes.api.model.apps.DeploymentSpecBuilder; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.GenericKubernetesResourceMatcher; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.SSABasedGenericKubernetesResourceMatcher; | ||
|
||
@KubernetesDependent | ||
public class DeploymentDependent | ||
extends CRUDKubernetesDependentResource<Deployment, PrevAnnotationBlockCustomResource> { | ||
|
||
public static final String RESOURCE_NAME = "test1"; | ||
|
||
public DeploymentDependent() { | ||
super(Deployment.class); | ||
} | ||
|
||
@Override | ||
protected Deployment desired( | ||
PrevAnnotationBlockCustomResource primary, | ||
Context<PrevAnnotationBlockCustomResource> context) { | ||
|
||
return new DeploymentBuilder() | ||
.withMetadata( | ||
new ObjectMetaBuilder() | ||
.withName(primary.getMetadata().getName()) | ||
.withNamespace(primary.getMetadata().getNamespace()) | ||
.build()) | ||
.withSpec( | ||
new DeploymentSpecBuilder() | ||
.withReplicas(1) | ||
.withSelector( | ||
new LabelSelectorBuilder().withMatchLabels(Map.of("app", "nginx")).build()) | ||
.withTemplate( | ||
new PodTemplateSpecBuilder() | ||
.withMetadata( | ||
new ObjectMetaBuilder().withLabels(Map.of("app", "nginx")).build()) | ||
.withSpec( | ||
new PodSpecBuilder() | ||
.withContainers( | ||
new ContainerBuilder() | ||
.withName("nginx") | ||
.withImage("nginx:1.14.2") | ||
.withResources( | ||
new ResourceRequirementsBuilder() | ||
.withLimits(Map.of("cpu", new Quantity("2000m"))) | ||
.build()) | ||
.build()) | ||
.build()) | ||
.build()) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
// for testing purposes replicating the matching logic but with the special matcher | ||
@Override | ||
public Result<Deployment> match( | ||
Deployment actualResource, | ||
Deployment desired, | ||
PrevAnnotationBlockCustomResource primary, | ||
Context<PrevAnnotationBlockCustomResource> context) { | ||
final boolean matches; | ||
addMetadata(true, actualResource, desired, primary, context); | ||
if (useSSA(context)) { | ||
matches = new SSAMatcherWithoutSanitization().matches(actualResource, desired, context); | ||
} else { | ||
matches = | ||
GenericKubernetesResourceMatcher.match(desired, actualResource, false, false, context) | ||
.matched(); | ||
} | ||
return Result.computed(matches, desired); | ||
} | ||
|
||
// using this matcher, so we are able to reproduce issue with resource limits | ||
static class SSAMatcherWithoutSanitization<R extends HasMetadata> | ||
extends SSABasedGenericKubernetesResourceMatcher<R> { | ||
|
||
@Override | ||
protected void sanitizeState(R actual, R desired, Map<String, Object> actualMap) {} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...o/javaoperatorsdk/operator/dependent/prevblocklist/PrevAnnotationBlockCustomResource.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,13 @@ | ||
package io.javaoperatorsdk.operator.dependent.prevblocklist; | ||
|
||
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.ShortNames; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
|
||
@Group("sample.javaoperatorsdk") | ||
@Version("v1") | ||
@ShortNames("pabc") | ||
public class PrevAnnotationBlockCustomResource extends CustomResource<PrevAnnotationBlockSpec, Void> | ||
implements Namespaced {} |
34 changes: 34 additions & 0 deletions
34
...va/io/javaoperatorsdk/operator/dependent/prevblocklist/PrevAnnotationBlockReconciler.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,34 @@ | ||
package io.javaoperatorsdk.operator.dependent.prevblocklist; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; | ||
import io.javaoperatorsdk.operator.api.reconciler.Reconciler; | ||
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; | ||
import io.javaoperatorsdk.operator.api.reconciler.Workflow; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent; | ||
import io.javaoperatorsdk.operator.support.TestExecutionInfoProvider; | ||
|
||
@Workflow(dependents = {@Dependent(type = DeploymentDependent.class)}) | ||
@ControllerConfiguration() | ||
public class PrevAnnotationBlockReconciler | ||
implements Reconciler<PrevAnnotationBlockCustomResource>, TestExecutionInfoProvider { | ||
|
||
private final AtomicInteger numberOfExecutions = new AtomicInteger(0); | ||
|
||
public PrevAnnotationBlockReconciler() {} | ||
|
||
@Override | ||
public UpdateControl<PrevAnnotationBlockCustomResource> reconcile( | ||
PrevAnnotationBlockCustomResource resource, | ||
Context<PrevAnnotationBlockCustomResource> context) { | ||
numberOfExecutions.getAndIncrement(); | ||
|
||
return UpdateControl.noUpdate(); | ||
} | ||
|
||
public int getNumberOfExecutions() { | ||
return numberOfExecutions.get(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../io/javaoperatorsdk/operator/dependent/prevblocklist/PrevAnnotationBlockReconcilerIT.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,50 @@ | ||
package io.javaoperatorsdk.operator.dependent.prevblocklist; | ||
|
||
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.Deployment; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
class PrevAnnotationBlockReconcilerIT { | ||
|
||
public static final String TEST_1 = "test1"; | ||
|
||
@RegisterExtension | ||
LocallyRunOperatorExtension extension = | ||
LocallyRunOperatorExtension.builder() | ||
// Removing resource from blocklist List would result in test failure | ||
// .withConfigurationService( | ||
// o -> o.previousAnnotationUsageBlocklist(Collections.emptyList())) | ||
.withReconciler(PrevAnnotationBlockReconciler.class) | ||
.build(); | ||
|
||
@Test | ||
void doNotUsePrevAnnotationForDeploymentDependent() { | ||
extension.create(testResource(TEST_1)); | ||
|
||
var reconciler = extension.getReconcilerOfType(PrevAnnotationBlockReconciler.class); | ||
await() | ||
.pollDelay(Duration.ofMillis(200)) | ||
.untilAsserted( | ||
() -> { | ||
var deployment = extension.get(Deployment.class, TEST_1); | ||
assertThat(deployment).isNotNull(); | ||
assertThat(reconciler.getNumberOfExecutions()).isGreaterThan(0).isLessThan(10); | ||
}); | ||
} | ||
|
||
PrevAnnotationBlockCustomResource testResource(String name) { | ||
var res = new PrevAnnotationBlockCustomResource(); | ||
res.setMetadata(new ObjectMetaBuilder().withName(name).build()); | ||
res.setSpec(new PrevAnnotationBlockSpec()); | ||
res.getSpec().setValue("value"); | ||
return res; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...est/java/io/javaoperatorsdk/operator/dependent/prevblocklist/PrevAnnotationBlockSpec.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.dependent.prevblocklist; | ||
|
||
public class PrevAnnotationBlockSpec { | ||
|
||
private String value; | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public PrevAnnotationBlockSpec setValue(String value) { | ||
this.value = value; | ||
return this; | ||
} | ||
} |
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.
I would suggest also documenting the default implementation in the javadoc.
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.
Not sure that is needed / that helpful since users can just open the code and see the defaults, also we don't do it for other configs.