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

concord-agent-operator: use informers API #1072

Merged
merged 1 commit into from
Jan 28, 2025
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 @@ -25,67 +25,74 @@
import com.walmartlabs.concord.agentoperator.crd.AgentPool;
import com.walmartlabs.concord.agentoperator.crd.AgentPoolList;
import com.walmartlabs.concord.agentoperator.scheduler.AutoScalerFactory;
import com.walmartlabs.concord.agentoperator.scheduler.Event;
import com.walmartlabs.concord.agentoperator.scheduler.Scheduler;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.concurrent.Executors;

import static com.walmartlabs.concord.agentoperator.scheduler.Event.Type.DELETED;
import static com.walmartlabs.concord.agentoperator.scheduler.Event.Type.MODIFIED;

public class Operator {

private static final Logger log = LoggerFactory.getLogger(Operator.class);

private static final long RESYNC_PERIOD = Duration.ofSeconds(10).toMillis();

public static void main(String[] args) {
// TODO support overloading the CRD with an external file?

var namespace = System.getenv("WATCH_NAMESPACE");
if (namespace == null) {
namespace = "default";
}

var client = new DefaultKubernetesClient() // NOSONAR
.inNamespace(namespace);

var namespace = getEnv("WATCH_NAMESPACE", "default");
var baseUrl = getEnv("CONCORD_BASE_URL", "http://192.168.99.1:8001"); // use minikube/vbox host's default address
var apiToken = getEnv("CONCORD_API_TOKEN", null);
var useMaintenanceMode = Boolean.parseBoolean(getEnv("USE_AGENT_MAINTENANCE_MODE", "false"));

// TODO use secrets for the token?
var cfg = new Scheduler.Configuration(baseUrl, apiToken);
var client = new DefaultKubernetesClient().inNamespace(namespace);
var autoScalerFactory = new AutoScalerFactory(cfg, client);
var agentClientFactory = new AgentClientFactory(useMaintenanceMode);
var executor = Executors.newCachedThreadPool();

var scheduler = new Scheduler(autoScalerFactory, client, agentClientFactory);
scheduler.start();
var handler = new ResourceEventHandler<AgentPool>() {
@Override
public void onAdd(AgentPool resource) {
executor.submit(() -> scheduler.onEvent(MODIFIED, resource));
}

@Override
public void onUpdate(AgentPool oldResource, AgentPool newResource) {
if (oldResource == newResource) {
return;
}
executor.submit(() -> scheduler.onEvent(MODIFIED, newResource));
}

log.info("main -> my watch begins... (namespace={})", namespace);
@Override
public void onDelete(AgentPool resource, boolean deletedFinalStateUnknown) {
executor.submit(() -> scheduler.onEvent(DELETED, resource));
}
};

var dummyClient = client.resources(AgentPool.class, AgentPoolList.class);
try {
dummyClient.watch(new Watcher<>() {
@Override
public void eventReceived(Action action, AgentPool resource) {
scheduler.onEvent(actionToEvent(action), resource);
}
var informer = client.resources(AgentPool.class, AgentPoolList.class)
.inAnyNamespace()
.inform(handler, RESYNC_PERIOD);

@Override
public void onClose(WatcherException we) {
log.error("Watcher exception {}", we.getMessage(), we);
}
});
scheduler.start();

try {
informer.run();
} catch (Exception e) {
log.error("Error while watching for CRs (namespace={})", namespace, e);
System.exit(2);
}
}

private static Event.Type actionToEvent(Watcher.Action action) {
return switch (action) {
case ADDED, MODIFIED -> Event.Type.MODIFIED;
case DELETED -> Event.Type.DELETED;
default -> throw new IllegalArgumentException("Unknown action type: " + action);
};
log.info("main -> and so my watch begins... (namespace={})", namespace);
}

private static String getEnv(String key, String defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public Scheduler(AutoScalerFactory autoScalerFactory, KubernetesClient k8sClient
}

public void onEvent(Event.Type type, AgentPool resource) {
log.info("onEvent -> handling {} for {}/{}", type, resource.getMetadata().getNamespace(), resource.getMetadata().getName());
synchronized (events) {
events.add(new Event(type, resource));
}
Expand Down Expand Up @@ -161,13 +162,13 @@ private AgentPoolInstance updateTargetSize(AgentPoolInstance i) throws IOExcepti
}

private void processActive(AgentPoolInstance i) throws IOException {
log.info("processActive ['{}']", i);
log.info("processActive ['{}']", i.getName());
List<Change> changes = planner.plan(i);
apply(changes);
}

private void processDeleted(AgentPoolInstance i) throws IOException {
log.info("processDeleted ['{}']", i);
log.info("processDeleted ['{}']", i.getName());
String resourceName = i.getName();

// remove all pool's pods
Expand Down