Skip to content

Commit 999af70

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

File tree

11 files changed

+1016
-8
lines changed

11 files changed

+1016
-8
lines changed

.cirrus.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fedora_packaging_task:
6262
image: "${PRIOR_FEDORA_CONTAINER_FQIN}"
6363

6464
script:
65-
- dnf install -y rpm-build libseccomp-devel
65+
- dnf install -y rpm-build libseccomp-devel json-c-devel
6666
- cd $CIRRUS_WORKING_DIR
6767
- make
6868
- make -f .rpmbuild/Makefile
@@ -83,7 +83,7 @@ build_task:
8383
memory: 4
8484

8585
script:
86-
- dnf install -y make glib2-devel git gcc pkg-config systemd-devel libseccomp-devel
86+
- dnf install -y make glib2-devel git gcc pkg-config systemd-devel libseccomp-devel json-c-devel
8787
- cd $CIRRUS_WORKING_DIR
8888
- make
8989
- make test

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 $(shell $(PKG_CONFIG) --libs json-c)
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 json-c) -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.

meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_project_arguments('-Os', '-Wall', '-Werror',
3434
language : 'c')
3535

3636
glib = dependency('glib-2.0')
37+
json_c = dependency('json-c')
3738
seccomp = dependency('libseccomp', version : '>= 2.5.2')
3839
if seccomp.found()
3940
add_project_arguments('-DUSE_SECCOMP=1', language : 'c')
@@ -87,7 +88,7 @@ executable('conmon',
8788
'src/utils.h',
8889
'src/seccomp_notify.c',
8990
'src/seccomp_notify.h'],
90-
dependencies : [glib, libdl, sd_journal, seccomp],
91+
dependencies : [glib, json_c, libdl, sd_journal, seccomp],
9192
install : true,
9293
install_dir : get_option('bindir'),
9394
)

nix/derivation.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ with pkgs; stdenv.mkDerivation rec {
1919
] ++ [
2020
pkgsStatic.glib
2121
libseccomp
22+
json_c
2223
] ++ lib.optionals enableSystemd [
2324
# Only include systemd for dynamic builds, not static builds
2425
# Static builds will use PKG_CONFIG_PATH approach instead

rpm/conmon.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Source0: %{url}/archive/v%{version}.tar.gz
2929
%if %{with docs}
3030
BuildRequires: go-md2man
3131
%endif
32+
BuildRequires: json-c-devel
3233
BuildRequires: gcc
3334
BuildRequires: git-core
3435
BuildRequires: glib2-devel

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: 41 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,38 @@ 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+
} else {
440+
nwarnf("Failed to discover healthcheck config from OCI bundle");
441+
}
442+
443+
/* Always free the config, regardless of success or failure */
444+
healthcheck_config_free(&config);
445+
}
446+
411447
#ifdef __linux__
412448
setup_oom_handling(container_pid);
413449
#endif
@@ -495,6 +531,9 @@ int main(int argc, char *argv[])
495531
g_source_remove(signal_fd_tag);
496532
close(signal_fd);
497533

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

0 commit comments

Comments
 (0)