-
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
base: main
Are you sure you want to change the base?
Changes from all commits
a0548b2
72fb838
c46bc69
53081c4
2910c6a
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package io.javaoperatorsdk.operator.api.config; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutorService; | ||
|
@@ -13,6 +14,8 @@ | |
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
import io.fabric8.kubernetes.api.model.apps.StatefulSet; | ||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.ConfigBuilder; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
|
@@ -448,6 +451,25 @@ default boolean previousAnnotationForDependentResourcesEventFiltering() { | |
return true; | ||
} | ||
|
||
/** | ||
* For dependent resources framework can add an annotation to filter our events that are results | ||
* of changes made by the framework. There are, however, few resources that do not follow the K8S | ||
* API convention that changes in metadata do not increase the "metadata.generation". For these | ||
* resources, the generation is increased by adding the annotation and their controller increases | ||
* the observedGeneration in the status. This results in a new event, that if not handled | ||
* correctly with the resource matcher yet again results in an update and a previous version | ||
* annotation change, thus results in an infinite loop. | ||
* | ||
* <p>As a workaround, we automatically skip adding previous annotation for those well-known | ||
* resources. Note that if you are sure that the matcher works (most of the cases does) for your | ||
* case, you can remove the resource from the blocklist. | ||
* | ||
* @return blocklist of resource classes where the previous version annotation won't be used. | ||
*/ | ||
default List<Class<? extends HasMetadata>> previousAnnotationUsageBlocklist() { | ||
return List.of(Deployment.class, StatefulSet.class); | ||
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. Another aspect is whether or not these 2 classes should always be added to the block list regardless of what the user might set this list to (i.e. what is returned by this method should be added to the default list, not replace it completely). |
||
} | ||
|
||
/** | ||
* If the event logic should parse the resourceVersion to determine the ordering of dependent | ||
* resource events. This is typically not needed. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package io.javaoperatorsdk.operator.api.config; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutorService; | ||
|
@@ -40,6 +41,7 @@ public class ConfigurationServiceOverrider { | |
private Boolean parseResourceVersions; | ||
private Boolean useSSAToPatchPrimaryResource; | ||
private Boolean cloneSecondaryResourcesWhenGettingFromCache; | ||
private List<Class<? extends HasMetadata>> previousAnnotationUsageBlocklist; | ||
|
||
@SuppressWarnings("rawtypes") | ||
private DependentResourceFactory dependentResourceFactory; | ||
|
@@ -188,6 +190,12 @@ public ConfigurationServiceOverrider withCloneSecondaryResourcesWhenGettingFromC | |
return this; | ||
} | ||
|
||
public ConfigurationServiceOverrider previousAnnotationUsageBlocklist( | ||
List<Class<? extends HasMetadata>> previousAnnotationUsageBlacklist) { | ||
this.previousAnnotationUsageBlocklist = previousAnnotationUsageBlacklist; | ||
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. See the previous comment on how this should probably be adding to the default set rather than completely replace it. |
||
return this; | ||
} | ||
|
||
public ConfigurationService build() { | ||
return new BaseConfigurationService(original.getVersion(), cloner, client) { | ||
@Override | ||
|
@@ -328,6 +336,13 @@ public boolean cloneSecondaryResourcesWhenGettingFromCache() { | |
cloneSecondaryResourcesWhenGettingFromCache, | ||
ConfigurationService::cloneSecondaryResourcesWhenGettingFromCache); | ||
} | ||
|
||
@Override | ||
public List<Class<? extends HasMetadata>> previousAnnotationUsageBlocklist() { | ||
return overriddenValueOrDefault( | ||
previousAnnotationUsageBlocklist, | ||
ConfigurationService::previousAnnotationUsageBlocklist); | ||
} | ||
}; | ||
} | ||
} |
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 | ||
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. This should use the configuration option from #2760 instead. |
||
@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())) | ||
.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.
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.
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.
What would make more sense, though, is to explain what is the consequence for a resource type to be in the block list because this isn't clear at all and people might be hesitant adding a resource to the block list without more details of what happens when they do.
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.
Also, this should probably be a set rather than a list as the order is meaningless whereas we only want one instance of a given resource type in this collection.