Skip to content

Commit d9d7828

Browse files
author
Samuel Archambault
committed
healthcheck feature
Signed-off-by: Samuel Archambault <samuel.archambault@getmaintainx.com>
1 parent e25af44 commit d9d7828

File tree

8 files changed

+972
-5
lines changed

8 files changed

+972
-5
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LIBEXECDIR ?= ${PREFIX}/libexec
55
PKG_CONFIG ?= pkg-config
66
HEADERS := $(wildcard src/*.h)
77

8-
OBJS := src/conmon.o src/cmsg.o src/ctr_logging.o src/utils.o src/cli.o src/globals.o src/cgroup.o src/conn_sock.o src/oom.o src/ctrl.o src/ctr_stdio.o src/parent_pipe_fd.o src/ctr_exit.o src/runtime_args.o src/close_fds.o src/seccomp_notify.o
8+
OBJS := src/conmon.o src/cmsg.o src/ctr_logging.o src/utils.o src/cli.o src/globals.o src/cgroup.o src/conn_sock.o src/oom.o src/ctrl.o src/ctr_stdio.o src/parent_pipe_fd.o src/ctr_exit.o src/runtime_args.o src/close_fds.o src/seccomp_notify.o src/healthcheck.o
99

1010
MAKEFILE_PATH := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
1111

@@ -25,10 +25,10 @@ else
2525
$(eval GIT_BRANCH_CLEAN := unknown)
2626
endif
2727

28-
override LIBS += $(shell $(PKG_CONFIG) --libs glib-2.0)
28+
override LIBS += $(shell $(PKG_CONFIG) --libs glib-2.0) -lpthread -lcjson
2929

3030
CFLAGS ?= -std=c99 -Os -Wall -Wextra -Werror
31-
override CFLAGS += $(shell $(PKG_CONFIG) --cflags glib-2.0) -DVERSION=\"$(VERSION)\" -DGIT_COMMIT=\"$(GIT_COMMIT)\"
31+
override CFLAGS += $(shell $(PKG_CONFIG) --cflags glib-2.0) $(shell $(PKG_CONFIG) --cflags libcjson) -DVERSION=\"$(VERSION)\" -DGIT_COMMIT=\"$(GIT_COMMIT)\"
3232

3333
# Conditionally compile journald logging code if the libraries can be found
3434
# if they can be found, set USE_JOURNALD macro for use in conmon code.

src/cli.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ char *opt_sdnotify_socket = NULL;
5757
gboolean opt_full_attach_path = FALSE;
5858
char *opt_seccomp_notify_socket = NULL;
5959
char *opt_seccomp_notify_plugins = NULL;
60+
gboolean opt_enable_healthcheck = FALSE;
6061
GOptionEntry opt_entries[] = {
6162
{"api-version", 0, 0, G_OPTION_ARG_NONE, &opt_api_version, "Conmon API version to use", NULL},
6263
{"bundle", 'b', 0, G_OPTION_ARG_STRING, &opt_bundle_path, "Location of the OCI Bundle path", NULL},
@@ -117,6 +118,8 @@ GOptionEntry opt_entries[] = {
117118
"Path to the socket where the seccomp notification fd is received", NULL},
118119
{"seccomp-notify-plugins", 0, 0, G_OPTION_ARG_STRING, &opt_seccomp_notify_plugins,
119120
"Plugins to use for managing the seccomp notifications", NULL},
121+
{"enable-healthcheck", 0, 0, G_OPTION_ARG_NONE, &opt_enable_healthcheck,
122+
"Enable healthcheck functionality (for non-systemd environments)", NULL},
120123
{NULL, 0, 0, 0, NULL, NULL, NULL}};
121124

122125

src/cli.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern gboolean opt_sync;
4747
extern char *opt_sdnotify_socket;
4848
extern char *opt_seccomp_notify_socket;
4949
extern char *opt_seccomp_notify_plugins;
50+
extern gboolean opt_enable_healthcheck;
5051
extern GOptionEntry opt_entries[];
5152
extern gboolean opt_full_attach_path;
5253

src/conmon.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "close_fds.h"
2121
#include "seccomp_notify.h"
2222
#include "runtime_args.h"
23+
#include "healthcheck.h"
2324

2425
#include <sys/stat.h>
2526
#include <locale.h>
@@ -46,14 +47,18 @@ int main(int argc, char *argv[])
4647
_cleanup_close_ int dev_null_r_cleanup = -1;
4748
_cleanup_close_ int dev_null_w_cleanup = -1;
4849
_cleanup_close_ int dummyfd = -1;
49-
5050
int initialize_ec = initialize_cli(argc, argv);
5151
if (initialize_ec >= 0) {
5252
exit(initialize_ec);
5353
}
5454

5555
process_cli();
5656

57+
/* Initialize healthcheck subsystem - always initialize for automatic discovery */
58+
if (!healthcheck_init()) {
59+
pexit("Failed to initialize healthcheck subsystem");
60+
}
61+
5762
attempt_oom_adjust(-1000);
5863

5964
/* ignoring SIGPIPE prevents conmon from being spuriously killed */
@@ -396,7 +401,6 @@ int main(int argc, char *argv[])
396401
}
397402

398403
container_pid = atoi(contents);
399-
ndebugf("container PID: %d", container_pid);
400404

401405
g_hash_table_insert(pid_to_handler, (pid_t *)&container_pid, container_exit_cb);
402406

@@ -408,6 +412,36 @@ int main(int argc, char *argv[])
408412
if ((opt_api_version >= 1 || !opt_exec) && sync_pipe_fd >= 0)
409413
write_or_close_sync_fd(&sync_pipe_fd, container_pid, NULL);
410414

415+
/* Configure healthcheck - automatic discovery from OCI config.json */
416+
/* Only start healthcheck timers if explicitly enabled via CLI flag */
417+
if (opt_bundle_path != NULL && opt_enable_healthcheck) {
418+
healthcheck_config_t config;
419+
memset(&config, 0, sizeof(config));
420+
421+
if (healthcheck_discover_from_oci_config(opt_bundle_path, &config)) {
422+
healthcheck_timer_t *timer = healthcheck_timer_new(opt_cid, &config);
423+
if (timer != NULL) {
424+
if (healthcheck_timer_start(timer)) {
425+
if (active_healthcheck_timers != NULL) {
426+
hash_table_put(active_healthcheck_timers, opt_cid, timer);
427+
ninfof("Started healthcheck for container %s", opt_cid);
428+
} else {
429+
nwarnf("Active healthcheck timers table is NULL");
430+
healthcheck_timer_free(timer);
431+
}
432+
} else {
433+
nwarnf("Failed to start healthcheck for container %s", opt_cid);
434+
healthcheck_timer_free(timer);
435+
}
436+
} else {
437+
nwarnf("Failed to create healthcheck timer for container %s", opt_cid);
438+
}
439+
healthcheck_config_free(&config);
440+
} else {
441+
nwarnf("Failed to discover healthcheck config from OCI bundle");
442+
}
443+
}
444+
411445
#ifdef __linux__
412446
setup_oom_handling(container_pid);
413447
#endif
@@ -495,6 +529,9 @@ int main(int argc, char *argv[])
495529
g_source_remove(signal_fd_tag);
496530
close(signal_fd);
497531

532+
/* Cleanup healthcheck timers */
533+
healthcheck_cleanup();
534+
498535
/*
499536
* Podman injects some fd's into the conmon process so that exposed ports are kept busy while
500537
* the container runs. Close them before we notify the container exited, so that they can be

0 commit comments

Comments
 (0)