From 6d3bd661215f3f2c575eca4247bbd4d5a3a30c24 Mon Sep 17 00:00:00 2001 From: DanyT Date: Mon, 1 Jul 2024 18:55:51 +0300 Subject: [PATCH] add support to load kubernetes configuration for memory buffer - existing api is not changed - load_kube_config works as before - extend kubeconfig_t to hold in memory buffer - make a common flow for file/buffer code - set fileName/buffer members accordingly - provide new api: load_kube_config_buffer --- kubernetes/config/kube_config.c | 51 ++++++++++++++++++++++----- kubernetes/config/kube_config.h | 38 ++++++++++++++++++++ kubernetes/config/kube_config_model.c | 4 +++ kubernetes/config/kube_config_model.h | 1 + kubernetes/config/kube_config_yaml.c | 14 ++++++-- kubernetes/config/kube_config_yaml.h | 4 ++- 6 files changed, 99 insertions(+), 13 deletions(-) diff --git a/kubernetes/config/kube_config.c b/kubernetes/config/kube_config.c index 115f2088..5383c650 100644 --- a/kubernetes/config/kube_config.c +++ b/kubernetes/config/kube_config.c @@ -304,21 +304,14 @@ static int kuberconfig_auth_provider(kubeconfig_property_t * current_user, kubec return rc; } -int load_kube_config(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, const char *configFileName) +int load_kube_config_common(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, kubeconfig_t *kubeconfig) { - static char fname[] = "load_kube_config()"; + static char fname[] = "load_kube_config_common()"; int rc = 0; const kubeconfig_property_t *current_context = NULL; const kubeconfig_property_t *current_cluster = NULL; kubeconfig_property_t *current_user = NULL; - kubeconfig_t *kubeconfig = kubeconfig_create(); - if (!kubeconfig) { - fprintf(stderr, "%s: Cannot create kubeconfig.[%s]\n", fname, strerror(errno)); - return -1; - } - - kubeconfig->fileName = getWorkingConfigFile(configFileName); rc = kubeyaml_load_kubeconfig(kubeconfig); if (0 != rc) { fprintf(stderr, "%s: Cannot load the kubeconfig %s\n", fname, kubeconfig->fileName); @@ -393,8 +386,48 @@ int load_kube_config(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApi } end: + return rc; +} + +int load_kube_config(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, const char *configFileName) +{ + static char fname[] = "load_kube_config()"; + int rc = 0; + + kubeconfig_t *kubeconfig = kubeconfig_create(); + if (!kubeconfig) { + fprintf(stderr, "%s: Cannot create kubeconfig.[%s]\n", fname, strerror(errno)); + return -1; + } + + kubeconfig->fileName = getWorkingConfigFile(configFileName); + + rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, kubeconfig); + + kubeconfig_free(kubeconfig); + kubeconfig = NULL; + + return rc; +} + +int load_kube_config_buffer(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, const char *buffer) +{ + static char fname[] = "load_kube_config_buffer()"; + int rc = 0; + + kubeconfig_t *kubeconfig = kubeconfig_create(); + if (!kubeconfig) { + fprintf(stderr, "%s: Cannot create kubeconfig.[%s]\n", fname, strerror(errno)); + return -1; + } + + kubeconfig->buffer = strdup(buffer); + + rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, kubeconfig); + kubeconfig_free(kubeconfig); kubeconfig = NULL; + return rc; } diff --git a/kubernetes/config/kube_config.h b/kubernetes/config/kube_config.h index 9d29f729..31d06054 100644 --- a/kubernetes/config/kube_config.h +++ b/kubernetes/config/kube_config.h @@ -46,6 +46,44 @@ extern "C" { int load_kube_config(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, const char *configFileName); +/* + * load_kube_config_buffer + * + * + * Description: + * + * + * Load kubernetes cluster configuration from specfied buffer + * + * + * Return: + * + * 0 Success + * -1 Failed + * + * + * Parameter: + * + * + * IN: + + * buffer : kubernetes cluster configuration data + * + * + * OUT: + * + * pBasePath: The pointer to API server address + * pSslConfig: The pointer to SSL configuration for client + * pApiKeys: The pointer to API tokens for client + * + * The memory will be allocated inside this function. User + * should call free_client_config to free the memory after + * these parameters are not used. + * + */ + + int load_kube_config_buffer(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, const char *buffer); + /* * free_client_config * diff --git a/kubernetes/config/kube_config_model.c b/kubernetes/config/kube_config_model.c index 8076c596..e162c09b 100644 --- a/kubernetes/config/kube_config_model.c +++ b/kubernetes/config/kube_config_model.c @@ -224,6 +224,10 @@ void kubeconfig_free(kubeconfig_t * kubeconfig) free(kubeconfig->fileName); kubeconfig->fileName = NULL; } + if (kubeconfig->buffer) { + free(kubeconfig->buffer); + kubeconfig->buffer = NULL; + } if (kubeconfig->apiVersion) { free(kubeconfig->apiVersion); kubeconfig->apiVersion = NULL; diff --git a/kubernetes/config/kube_config_model.h b/kubernetes/config/kube_config_model.h index 45833a21..6bae4ada 100644 --- a/kubernetes/config/kube_config_model.h +++ b/kubernetes/config/kube_config_model.h @@ -88,6 +88,7 @@ extern "C" { typedef struct kubeconfig_t { char *fileName; + char *buffer; char *apiVersion; char *preferences; char *kind; diff --git a/kubernetes/config/kube_config_yaml.c b/kubernetes/config/kube_config_yaml.c index 3007f6eb..21ffe293 100644 --- a/kubernetes/config/kube_config_yaml.c +++ b/kubernetes/config/kube_config_yaml.c @@ -430,7 +430,7 @@ int kubeyaml_load_kubeconfig(kubeconfig_t * kubeconfig) { static char fname[] = "kubeyaml_load_kubeconfig()"; - /* Set a file input. */ + /* Set a file input or use the provided buffer. */ FILE *input = NULL; if (kubeconfig->fileName) { input = fopen(kubeconfig->fileName, "rb"); @@ -438,17 +438,25 @@ int kubeyaml_load_kubeconfig(kubeconfig_t * kubeconfig) fprintf(stderr, "%s: Cannot open the file %s.[%s]\n", fname, kubeconfig->fileName, strerror(errno)); return -1; } + else if (kubeconfig->buffer) { + // Nothing to do here for now. + } } else { fprintf(stderr, "%s: The kubeconf file name needs be set by kubeconfig->fileName .\n", fname); return -1; } + /* Create the Parser object. */ yaml_parser_t parser; yaml_document_t document; - /* Create the Parser object. */ yaml_parser_initialize(&parser); - yaml_parser_set_input_file(&parser, input); + if (input) { + yaml_parser_set_input_file(&parser, input); + } + else { + yaml_parser_set_input_string(&parser, (const unsigned char*)kubeconfig->buffer, strlen(kubeconfig->buffer)); + } int done = 0; while (!done) { diff --git a/kubernetes/config/kube_config_yaml.h b/kubernetes/config/kube_config_yaml.h index b162b5cf..eb093556 100644 --- a/kubernetes/config/kube_config_yaml.h +++ b/kubernetes/config/kube_config_yaml.h @@ -24,13 +24,15 @@ extern "C" { * * IN: * kubeconfig->fileName: kubernetes cluster configuration file name - * + * kubeconfig->buffer: kubernetes cluster configuration data; this is considered only if kubeconfig->fileName is set to NULL + * * OUT: * kubeconfig: kubernetes cluster configuration * */ int kubeyaml_load_kubeconfig(kubeconfig_t * kubeconfig); + /* * kubeyaml_parse_exec_crendential *