-
Notifications
You must be signed in to change notification settings - Fork 219
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
Changes from all commits
5e80d98
f7ba067
3b147fe
0aefb56
74c13ff
7a1902b
e31793c
19ce185
7639330
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) {} | ||
} | ||
} |
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 {} |
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(); | ||
} | ||
} |
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())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be a new test then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be an additional test, I did not add that since I did not find it important to show that something is not working. |
||
.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; | ||
} | ||
} |
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; | ||
} | ||
} |
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.
This should use the configuration option from #2760 instead.
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.
If that is merged, can change it (might be a new PR). thx!