1818
1919#ifdef _POSIX_VERSION
2020#include <sys/utsname.h>
21+ #include <unistd.h>
2122#endif
2223
2324#ifdef _WIN32
@@ -356,39 +357,33 @@ _get_system_info (mongoc_handshake_t *handshake)
356357 handshake -> os_architecture = _get_os_architecture ();
357358}
358359
359- static void
360- _free_system_info (mongoc_handshake_t * handshake )
361- {
362- bson_free (handshake -> os_type );
363- bson_free (handshake -> os_name );
364- bson_free (handshake -> os_version );
365- bson_free (handshake -> os_architecture );
366- }
367-
368360static void
369361_get_driver_info (mongoc_handshake_t * handshake )
370362{
371363 handshake -> driver_name = bson_strndup ("mongoc" , HANDSHAKE_DRIVER_NAME_MAX );
372364 handshake -> driver_version = bson_strndup (MONGOC_VERSION_S , HANDSHAKE_DRIVER_VERSION_MAX );
373365}
374366
375- static void
376- _free_driver_info (mongoc_handshake_t * handshake )
377- {
378- bson_free (handshake -> driver_name );
379- bson_free (handshake -> driver_version );
380- }
381-
382367static void
383368_set_platform_string (mongoc_handshake_t * handshake )
384369{
385370 handshake -> platform = bson_strdup ("" );
386371}
387372
388373static void
389- _free_env_info (mongoc_handshake_t * handshake )
374+ _get_container_info (mongoc_handshake_t * handshake )
390375{
391- bson_free (handshake -> env_region );
376+ char * kubernetes_env = _mongoc_getenv ("KUBERNETES_SERVICE_HOST" );
377+ handshake -> kubernetes = kubernetes_env ;
378+
379+ handshake -> docker = false;
380+ #ifdef _WIN32
381+ handshake -> docker = (_access_s ("C:\\.dockerenv" , 0 ) == 0 );
382+ #else
383+ handshake -> docker = (access ("/.dockerenv" , F_OK ) == 0 );
384+ #endif
385+
386+ bson_free (kubernetes_env );
392387}
393388
394389static void
@@ -514,21 +509,14 @@ _set_flags (mongoc_handshake_t *handshake)
514509 handshake -> flags = mcommon_string_from_append_destroy_with_steal (& append );
515510}
516511
517- static void
518- _free_platform_string (mongoc_handshake_t * handshake )
519- {
520- bson_free (handshake -> platform );
521- bson_free (handshake -> compiler_info );
522- bson_free (handshake -> flags );
523- }
524-
525512void
526513_mongoc_handshake_init (void )
527514{
528515 _get_system_info (_mongoc_handshake_get ());
529516 _get_driver_info (_mongoc_handshake_get ());
530517 _set_platform_string (_mongoc_handshake_get ());
531518 _get_env_info (_mongoc_handshake_get ());
519+ _get_container_info (_mongoc_handshake_get ());
532520 _set_compiler_info (_mongoc_handshake_get ());
533521 _set_flags (_mongoc_handshake_get ());
534522
@@ -540,10 +528,16 @@ void
540528_mongoc_handshake_cleanup (void )
541529{
542530 mongoc_handshake_t * h = _mongoc_handshake_get ();
543- _free_system_info (h );
544- _free_driver_info (h );
545- _free_platform_string (h );
546- _free_env_info (h );
531+ bson_free (h -> os_type );
532+ bson_free (h -> os_name );
533+ bson_free (h -> os_version );
534+ bson_free (h -> os_architecture );
535+ bson_free (h -> driver_name );
536+ bson_free (h -> driver_version );
537+ bson_free (h -> platform );
538+ bson_free (h -> compiler_info );
539+ bson_free (h -> flags );
540+ bson_free (h -> env_region );
547541 * h = (mongoc_handshake_t ) {0 };
548542
549543 bson_mutex_destroy (& gHandshakeLock );
@@ -701,7 +695,11 @@ _mongoc_handshake_build_doc_with_application (const char *appname)
701695 doc (kv ("name" , cstr (env_name )),
702696 if (md -> env_timeout_sec .set , then (kv ("timeout_sec" , int32 (md -> env_timeout_sec .value )))),
703697 if (md -> env_memory_mb .set , then (kv ("memory_mb" , int32 (md -> env_memory_mb .value )))),
704- if (md -> env_region , then (kv ("region" , cstr (md -> env_region )))))))));
698+ if (md -> env_region , then (kv ("region" , cstr (md -> env_region )))))))),
699+ if (md -> kubernetes || md -> docker ,
700+ then (kv ("container" ,
701+ doc (if (md -> docker , then (kv ("runtime" , cstr ("docker" )))),
702+ if (md -> kubernetes , then (kv ("orchestrator" , cstr ("kubernetes" )))))))));
705703
706704 if (md -> platform ) {
707705 _append_platform_field (doc , md -> platform , false);
0 commit comments