|
| 1 | +package io.javaoperatorsdk.operator.api.config; |
| 2 | + |
| 3 | +import io.fabric8.kubernetes.client.CustomResource; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +public class ControllerConfigurationOverrider<R extends CustomResource> { |
| 9 | + |
| 10 | + private String finalizer; |
| 11 | + private boolean generationAware; |
| 12 | + private Set<String> namespaces; |
| 13 | + private RetryConfiguration retry; |
| 14 | + private final ControllerConfiguration<R> original; |
| 15 | + |
| 16 | + private ControllerConfigurationOverrider(ControllerConfiguration<R> original) { |
| 17 | + finalizer = original.getFinalizer(); |
| 18 | + generationAware = original.isGenerationAware(); |
| 19 | + namespaces = new HashSet<>(original.getNamespaces()); |
| 20 | + retry = original.getRetryConfiguration(); |
| 21 | + this.original = original; |
| 22 | + } |
| 23 | + |
| 24 | + public ControllerConfigurationOverrider<R> withFinalizer(String finalizer) { |
| 25 | + this.finalizer = finalizer; |
| 26 | + return this; |
| 27 | + } |
| 28 | + |
| 29 | + public ControllerConfigurationOverrider<R> withGenerationAware(boolean generationAware) { |
| 30 | + this.generationAware = generationAware; |
| 31 | + return this; |
| 32 | + } |
| 33 | + |
| 34 | + public ControllerConfigurationOverrider<R> withCurrentNamespace() { |
| 35 | + namespaces.clear(); |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + public ControllerConfigurationOverrider<R> addingNamespaces(String... namespaces) { |
| 40 | + this.namespaces.addAll(List.of(namespaces)); |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public ControllerConfigurationOverrider<R> removingNamespaces(String... namespaces) { |
| 45 | + this.namespaces.removeAll(List.of(namespaces)); |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public ControllerConfigurationOverrider<R> settingNamespace(String namespace) { |
| 50 | + this.namespaces.clear(); |
| 51 | + this.namespaces.add(namespace); |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + public ControllerConfigurationOverrider<R> withRetry(RetryConfiguration retry) { |
| 56 | + this.retry = retry; |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + public ControllerConfiguration<R> build() { |
| 61 | + return new AbstractControllerConfiguration<R>( |
| 62 | + original.getAssociatedControllerClassName(), |
| 63 | + original.getName(), |
| 64 | + original.getCRDName(), |
| 65 | + finalizer, |
| 66 | + generationAware, |
| 67 | + namespaces, |
| 68 | + retry) { |
| 69 | + @Override |
| 70 | + public Class<R> getCustomResourceClass() { |
| 71 | + return original.getCustomResourceClass(); |
| 72 | + } |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + public static <R extends CustomResource> ControllerConfigurationOverrider<R> override( |
| 77 | + ControllerConfiguration<R> original) { |
| 78 | + return new ControllerConfigurationOverrider<>(original); |
| 79 | + } |
| 80 | +} |
0 commit comments