Skip to content

Commit

Permalink
add support to load kubernetes configuration for memory buffer
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
DanyT committed Jul 1, 2024
1 parent dc7d670 commit 6d3bd66
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 13 deletions.
51 changes: 42 additions & 9 deletions kubernetes/config/kube_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
38 changes: 38 additions & 0 deletions kubernetes/config/kube_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
4 changes: 4 additions & 0 deletions kubernetes/config/kube_config_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions kubernetes/config/kube_config_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ extern "C" {

typedef struct kubeconfig_t {
char *fileName;
char *buffer;
char *apiVersion;
char *preferences;
char *kind;
Expand Down
14 changes: 11 additions & 3 deletions kubernetes/config/kube_config_yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,25 +430,33 @@ 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");
if (!input) {
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) {
Expand Down
4 changes: 3 additions & 1 deletion kubernetes/config/kube_config_yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 6d3bd66

Please sign in to comment.