From d175b792dcacdc5eed8a8fc02845a59cc52545ef Mon Sep 17 00:00:00 2001 From: Ryanne Dolan Date: Thu, 20 Jun 2024 20:17:49 -0500 Subject: [PATCH] Add optional filter to subscription reconciler --- .../operator/HoptimatorOperatorApp.java | 14 +++++++++----- .../subscription/SubscriptionReconciler.java | 17 +++++++++++++---- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/HoptimatorOperatorApp.java b/hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/HoptimatorOperatorApp.java index b64d857..bb0734b 100644 --- a/hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/HoptimatorOperatorApp.java +++ b/hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/HoptimatorOperatorApp.java @@ -28,6 +28,7 @@ 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); @@ -35,14 +36,17 @@ public class HoptimatorOperatorApp { final String modelPath; final String namespace; final ApiClient apiClient; + final Predicate 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 subscriptionFilter, Properties properties) { this.modelPath = modelPath; this.namespace = namespace; this.apiClient = apiClient; + this.subscriptionFilter = subscriptionFilter; this.properties = properties; this.environment = new Resource.SimpleEnvironment(properties); } @@ -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; @@ -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 { @@ -96,7 +100,7 @@ public void run() throws Exception { List 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])); diff --git a/hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/subscription/SubscriptionReconciler.java b/hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/subscription/SubscriptionReconciler.java index 9adbe22..1131fc1 100644 --- a/hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/subscription/SubscriptionReconciler.java +++ b/hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/subscription/SubscriptionReconciler.java @@ -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; @@ -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 filter; private SubscriptionReconciler(Operator operator, HoptimatorPlanner.Factory plannerFactory, - Resource.Environment environment) { + Resource.Environment environment, Predicate filter) { this.operator = operator; this.plannerFactory = plannerFactory; this.environment = environment; + this.filter = filter; } @Override @@ -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); } @@ -368,8 +376,9 @@ private static Map 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 filter) { + Reconciler reconciler = new SubscriptionReconciler(operator, plannerFactory, environment, filter); return ControllerBuilder.defaultBuilder(operator.informerFactory()) .withReconciler(reconciler) .withName("subscription-controller")