Skip to content

Commit 6d3bd66

Browse files
committed
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
1 parent dc7d670 commit 6d3bd66

File tree

6 files changed

+99
-13
lines changed

6 files changed

+99
-13
lines changed

kubernetes/config/kube_config.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,21 +304,14 @@ static int kuberconfig_auth_provider(kubeconfig_property_t * current_user, kubec
304304
return rc;
305305
}
306306

307-
int load_kube_config(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, const char *configFileName)
307+
int load_kube_config_common(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, kubeconfig_t *kubeconfig)
308308
{
309-
static char fname[] = "load_kube_config()";
309+
static char fname[] = "load_kube_config_common()";
310310
int rc = 0;
311311
const kubeconfig_property_t *current_context = NULL;
312312
const kubeconfig_property_t *current_cluster = NULL;
313313
kubeconfig_property_t *current_user = NULL;
314314

315-
kubeconfig_t *kubeconfig = kubeconfig_create();
316-
if (!kubeconfig) {
317-
fprintf(stderr, "%s: Cannot create kubeconfig.[%s]\n", fname, strerror(errno));
318-
return -1;
319-
}
320-
321-
kubeconfig->fileName = getWorkingConfigFile(configFileName);
322315
rc = kubeyaml_load_kubeconfig(kubeconfig);
323316
if (0 != rc) {
324317
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
393386
}
394387

395388
end:
389+
return rc;
390+
}
391+
392+
int load_kube_config(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, const char *configFileName)
393+
{
394+
static char fname[] = "load_kube_config()";
395+
int rc = 0;
396+
397+
kubeconfig_t *kubeconfig = kubeconfig_create();
398+
if (!kubeconfig) {
399+
fprintf(stderr, "%s: Cannot create kubeconfig.[%s]\n", fname, strerror(errno));
400+
return -1;
401+
}
402+
403+
kubeconfig->fileName = getWorkingConfigFile(configFileName);
404+
405+
rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, kubeconfig);
406+
407+
kubeconfig_free(kubeconfig);
408+
kubeconfig = NULL;
409+
410+
return rc;
411+
}
412+
413+
int load_kube_config_buffer(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, const char *buffer)
414+
{
415+
static char fname[] = "load_kube_config_buffer()";
416+
int rc = 0;
417+
418+
kubeconfig_t *kubeconfig = kubeconfig_create();
419+
if (!kubeconfig) {
420+
fprintf(stderr, "%s: Cannot create kubeconfig.[%s]\n", fname, strerror(errno));
421+
return -1;
422+
}
423+
424+
kubeconfig->buffer = strdup(buffer);
425+
426+
rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, kubeconfig);
427+
396428
kubeconfig_free(kubeconfig);
397429
kubeconfig = NULL;
430+
398431
return rc;
399432
}
400433

kubernetes/config/kube_config.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,44 @@ extern "C" {
4646

4747
int load_kube_config(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, const char *configFileName);
4848

49+
/*
50+
* load_kube_config_buffer
51+
*
52+
*
53+
* Description:
54+
*
55+
*
56+
* Load kubernetes cluster configuration from specfied buffer
57+
*
58+
*
59+
* Return:
60+
*
61+
* 0 Success
62+
* -1 Failed
63+
*
64+
*
65+
* Parameter:
66+
*
67+
*
68+
* IN:
69+
70+
* buffer : kubernetes cluster configuration data
71+
*
72+
*
73+
* OUT:
74+
*
75+
* pBasePath: The pointer to API server address
76+
* pSslConfig: The pointer to SSL configuration for client
77+
* pApiKeys: The pointer to API tokens for client
78+
*
79+
* The memory will be allocated inside this function. User
80+
* should call free_client_config to free the memory after
81+
* these parameters are not used.
82+
*
83+
*/
84+
85+
int load_kube_config_buffer(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApiKeys, const char *buffer);
86+
4987
/*
5088
* free_client_config
5189
*

kubernetes/config/kube_config_model.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ void kubeconfig_free(kubeconfig_t * kubeconfig)
224224
free(kubeconfig->fileName);
225225
kubeconfig->fileName = NULL;
226226
}
227+
if (kubeconfig->buffer) {
228+
free(kubeconfig->buffer);
229+
kubeconfig->buffer = NULL;
230+
}
227231
if (kubeconfig->apiVersion) {
228232
free(kubeconfig->apiVersion);
229233
kubeconfig->apiVersion = NULL;

kubernetes/config/kube_config_model.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ extern "C" {
8888

8989
typedef struct kubeconfig_t {
9090
char *fileName;
91+
char *buffer;
9192
char *apiVersion;
9293
char *preferences;
9394
char *kind;

kubernetes/config/kube_config_yaml.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,25 +430,33 @@ int kubeyaml_load_kubeconfig(kubeconfig_t * kubeconfig)
430430
{
431431
static char fname[] = "kubeyaml_load_kubeconfig()";
432432

433-
/* Set a file input. */
433+
/* Set a file input or use the provided buffer. */
434434
FILE *input = NULL;
435435
if (kubeconfig->fileName) {
436436
input = fopen(kubeconfig->fileName, "rb");
437437
if (!input) {
438438
fprintf(stderr, "%s: Cannot open the file %s.[%s]\n", fname, kubeconfig->fileName, strerror(errno));
439439
return -1;
440440
}
441+
else if (kubeconfig->buffer) {
442+
// Nothing to do here for now.
443+
}
441444
} else {
442445
fprintf(stderr, "%s: The kubeconf file name needs be set by kubeconfig->fileName .\n", fname);
443446
return -1;
444447
}
445448

449+
/* Create the Parser object. */
446450
yaml_parser_t parser;
447451
yaml_document_t document;
448452

449-
/* Create the Parser object. */
450453
yaml_parser_initialize(&parser);
451-
yaml_parser_set_input_file(&parser, input);
454+
if (input) {
455+
yaml_parser_set_input_file(&parser, input);
456+
}
457+
else {
458+
yaml_parser_set_input_string(&parser, (const unsigned char*)kubeconfig->buffer, strlen(kubeconfig->buffer));
459+
}
452460

453461
int done = 0;
454462
while (!done) {

kubernetes/config/kube_config_yaml.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ extern "C" {
2424
*
2525
* IN:
2626
* kubeconfig->fileName: kubernetes cluster configuration file name
27-
*
27+
* kubeconfig->buffer: kubernetes cluster configuration data; this is considered only if kubeconfig->fileName is set to NULL
28+
*
2829
* OUT:
2930
* kubeconfig: kubernetes cluster configuration
3031
*
3132
*/
3233
int kubeyaml_load_kubeconfig(kubeconfig_t * kubeconfig);
3334

35+
3436
/*
3537
* kubeyaml_parse_exec_crendential
3638
*

0 commit comments

Comments
 (0)