Skip to content
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

Add optional filter to subscription reconciler #65

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,25 @@
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

public class HoptimatorOperatorApp {
private static final Logger log = LoggerFactory.getLogger(HoptimatorOperatorApp.class);

final String modelPath;
final String namespace;
final ApiClient apiClient;
final Predicate<V1alpha1Subscription> subscriptionFilter;
final Properties properties;
final Resource.Environment environment;

/** This constructor is likely to evolve and break. */
public HoptimatorOperatorApp(String modelPath, String namespace, ApiClient apiClient, Properties properties) {
public HoptimatorOperatorApp(String modelPath, String namespace, ApiClient apiClient,
Predicate<V1alpha1Subscription> subscriptionFilter, Properties properties) {
this.modelPath = modelPath;
this.namespace = namespace;
this.apiClient = apiClient;
this.subscriptionFilter = subscriptionFilter;
this.properties = properties;
this.environment = new Resource.SimpleEnvironment(properties);
}
Expand All @@ -66,7 +70,7 @@ public static void main(String[] args) throws Exception {
cmd = parser.parse(options, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp("utility-name", options);
formatter.printHelp("hoptimator-operator", options);

System.exit(1);
return;
Expand All @@ -75,8 +79,8 @@ public static void main(String[] args) throws Exception {
String modelFileInput = cmd.getArgs()[0];
String namespaceInput = cmd.getOptionValue("namespace", "default");

new HoptimatorOperatorApp(modelFileInput, namespaceInput, Config.defaultClient(),
new Properties()).run();
new HoptimatorOperatorApp(modelFileInput, namespaceInput, Config.defaultClient(), null,
new Properties()).run();
}

public void run() throws Exception {
Expand All @@ -96,7 +100,7 @@ public void run() throws Exception {

List<Controller> controllers = new ArrayList<>();
controllers.addAll(ControllerService.controllers(operator));
controllers.add(SubscriptionReconciler.controller(operator, plannerFactory, environment));
controllers.add(SubscriptionReconciler.controller(operator, plannerFactory, environment, subscriptionFilter));

ControllerManager controllerManager = new ControllerManager(operator.informerFactory(),
controllers.toArray(new Controller[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.CountDownLatch;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -47,12 +48,14 @@ public class SubscriptionReconciler implements Reconciler {
private final Operator operator;
private final HoptimatorPlanner.Factory plannerFactory;
private final Resource.Environment environment;
private final Predicate<V1alpha1Subscription> filter;

private SubscriptionReconciler(Operator operator, HoptimatorPlanner.Factory plannerFactory,
Resource.Environment environment) {
Resource.Environment environment, Predicate<V1alpha1Subscription> filter) {
this.operator = operator;
this.plannerFactory = plannerFactory;
this.environment = environment;
this.filter = filter;
}

@Override
Expand All @@ -67,7 +70,12 @@ public Result reconcile(Request request) {
name);

if (object == null) {
log.info("Object {}/{} deleted, skipping.", namespace, name);
log.info("Object {}/{} deleted. Skipping.", namespace, name);
return new Result(false);
}

if (filter != null && !filter.test(object)) {
log.info("Object {}/{} filtered. Skipping.", namespace, name);
return new Result(false);
}

Expand Down Expand Up @@ -368,8 +376,9 @@ private static Map<String, String> guessAttributes(DynamicKubernetesObject obj)
return Collections.emptyMap();
}

public static Controller controller(Operator operator, HoptimatorPlanner.Factory plannerFactory, Resource.Environment environment) {
Reconciler reconciler = new SubscriptionReconciler(operator, plannerFactory, environment);
public static Controller controller(Operator operator, HoptimatorPlanner.Factory plannerFactory,
Resource.Environment environment, Predicate<V1alpha1Subscription> filter) {
Reconciler reconciler = new SubscriptionReconciler(operator, plannerFactory, environment, filter);
return ControllerBuilder.defaultBuilder(operator.informerFactory())
.withReconciler(reconciler)
.withName("subscription-controller")
Expand Down
Loading